Upload
krutarth-patel
View
220
Download
1
Embed Size (px)
Citation preview
5/10/2014 BFS - NOCOW
http://www.nocow.cn/index.php/%E5%AE%BD%E5%BA%A6%E4%BC%98%E5%85%88%E6%90%9C%E7%B4%A2 1/3
Breadthfirst searchFrom NOCOW(Jump from the breadthfirst search )
Directory
1 Introduction2 algorithm instance:3 Attachment: bidirectional breadthfirst search4 algorithm introduced
Introduction
Breadthfirst search (BFS)
In depth-first search algorithm, is the greater depth of more first node expanded. If you put in a search algorithm to hierarchical node search,The layer node does not search processed, it can not be processed on the lower nodes, ie the smaller the depth of the more nodes expanded first, that first produced The nodes can be extended at first, this search algorithm called breadth-first search method. English using Breadth-First-Search said, so weAlso the breadth-first search method referred to as BFS.
Breadthfirst search of the basic algorithm:
1) starting from a start vertex visit, be accessed vertices labeled accordingly, and output access vertex number;
2) starting from the vertex to be accessed sequentially search the edges associated with the vertex adjacency point is not accessible to all, and make the appropriate tag.
3) in turn based on 2) all the neighbors being accessed, the access point associated with the adjacent neighbors are not accessible to all, until all vertices are accessed so far.
[Process] algorithm
program BFS; var Head , tail , i : LongInt ; a : array [ 0 .. Max ] of nodes; begin initial node assignment a [ 0 ] Head : = - 1 ; tail : = 1 ; REPEAT inc ( Head ) ; / / extract a not visited nodes. if ( current node has a child node ) Then for i : = 1 to the maximum number of nodes can be extended do / / expand each child node of the current node if ( the child node does not appear too ) Then begin inc ( tail ) ; a [ tail ] : = new node End ; until Head = tail; / / All nodes have been generated. End .
(ABOVE FROM http://noip.tanghu.net/aosai/shuanfa/024.htm )
Algorithm instance:
Problem description: A, B, C three bucket capacity were 3L, 7L, 10L. There are now C 10L bucket of water. Required only in the context of water transfer between thebarrel, making the C and B buckets equally 10L bucket of water. Find the most simple operation.
program ; BFS const V : array [ 1 .. 3 ] of Integer = ( 3 , 7 , 10 ) ; / / three barrel capacity. type node = record L : array [ 1 .. 3 ] of LongInt ; / / three buckets. P : LongInt ; / / parent node of each node. End ; var i , J , Head , tail : LongInt ; t : boolean ; / / find the target sign. a : array [ 0 .. 100 ] of node; procedure init; var i , J : LongInt ; begin FillChar ( a , sizeof ( a ) , 0 ) ; t : = false ; a [ 0 ] . L [ 3 ] : = 10 ; Head : = - 1 ; tail : = 0 ; End ; procedure pour ( x , y : LongInt ) ; var i , J : LongInt ; begin
5/10/2014 BFS - NOCOW
http://www.nocow.cn/index.php/%E5%AE%BD%E5%BA%A6%E4%BC%98%E5%85%88%E6%90%9C%E7%B4%A2 2/3
if ( a [ Head ] . L [ x ] = 0 ) or t Then Exit; inc ( tail ) ; a [ tail ] : = a [ Head ] ; a [ tail ] . P : = Head; if a [ tail ] . L [ x ] > V [ y ] - a [ tail ] . L [ y ] Then begin Dec ( a [ tail ] . L [ x ] , V [ y ] - a [ tail ] . L [ y ] ) ; a [ tail ] . L [ y ] : = V [ y ] ; End else begin inc ( a [ tail ] . L [ y ] , a [ tail ] . L [ x ] ) ; a [ tail ] . L [ x ] : = 0 ; End ; for i : = 0 to tail - 1 do / / check the status of whether there had been, it is, then delete it. begin if a [ i ] = a [ tail ] Then begin Dec ( tail ) ; exit; End ; End ; if a [ tail ] . L [ 3 ] = 5 Then t : = true ; End ; procedure main; var i , J : LongInt ; begin REPEAT inc ( Head ) ; pour ( 1 , 2 ) ; / / action function is to try to pour a bucket of water into the x y barrel, see if I can produce the new state. pour ( 2 , 1 ) ; pour ( 1 , 3 ) ; pour ( 3 , 1 ) ; pour ( 2 , 3 ) ; pour ( 3 , 2 ) ; until ( a [ tail ] . L [ 3 ] = 5 ) or ( tail = 100 ) ; / / When you find the target or has exceeded a predetermined search range when to quit. End ; procedure Print; var c : array [ 1 .. 100 ] of LongInt ; i , J : LongInt ; begin i : = 0 ; while a [ tail ] . P <> 0 do begin inc ( i ) ; c [ i ] : = tail; tail : = a [ tail ] . P ; End ; for J : = i downto 1 do writeln ( a [ c [ J ] ] . L [ 1 ] , '' , a [ c [ J ] ] . L [ 2 ] , '' , a [ c [ J ] ] . L [ 3 ] ) ; End ; begin init; main; print;End .
Attachment: Bidirectional breadthfirst search
Bidirectional breadthfirst search (BIBFS) refers to the search in two directions simultaneously:
Forward search: search from the initial node to the destination node direction;Reverse Search: Search from the target node to the initial node direction;
Bidirectional breadthfirst search data structure than the oneway BFS complicated. Since the formation of two trees bidirectional breadthfirst search in the search processdirection opposite answer tree, so will
Shall be provided with four tables:
OPEN0 table, CLOSE0 table - storage to be expanded and produced in the forward search has been expanded nodeOPEN1 table, CLOSE1 table - to be expanded and produced the reverse search has been extended storage node
Which set the CL [0,0], OP [0,0], CL [1,0], OP [1,0] is a pointer.
Algorithm introduced
procedure Print ( st , k : Integer ) ; begin if k <> 1 Then begin if st = 1 Then the output list [ st , k ] . State / / reverse search, press f pointer output list [1, k] ... list [1,1] path Print ( st , list [ st , k ] . f ) ; if st = 0 Then Output list [ st , k ] . State / / forward search, the output list [0, k] .. . list [0,1] End ; End ; procedure Check ( st : 0 .. 1 ) ; var i : Integer ; begin for i : = 1 to op [ 1 . st ] - 1 do / / check st extend in opposite directions to each node. if list [ st , op [ 1 . st ] - 1 Then if list [ st , op [ st ] ] . State intersects the list [ 1 - st , i ] . State Then begin if st = 0 Then begin
最短路:Dijkstra BellmanFord(SPFA) FloydWarshall
5/10/2014 BFS - NOCOW
http://www.nocow.cn/index.php/%E5%AE%BD%E5%BA%A6%E4%BC%98%E5%85%88%E6%90%9C%E7%B4%A2 3/3
[Edit] (http://www.nocow.cn/index.php?title=%E6%A8%A1%E6%9D%BF:%E5%9B%BE%E8%AE%BA&action=edit)
Print ( O , op [ st ] ) ; / / this is a positive direction, the first output list [0 .. 1] .. list [0, op [st]], then outputs the list [1, i] .. list [1,1 ] Print ( 1 , i ) ; End else begin Print [ 0 , i ] ; / / this is the reverse direction. Print [ 1 , op [ st ] ) ; End ; halt; End ; End ; procedure expand ( st : 0 .. 1 ) ; begin q : = list ( st , cl [ st ] ) ; while ( q nodes can be expanded ) and ( op [ st ] <MAXN ) do begin along the direction of extension of the q st The child node qt. State ; list [ st , op [ st ] ] . State : = qt. State ; list [ st , op [ st ] ] . Father : = cl [ st ] ; Check ( st ) ; / / both directions Search intersects qt output. op [ st ] : = sp [ st ] + 1 ; End ; cl [ st ] : = cl [ st ] + 1 End ; begin list [ 0 , 1 ] : = start state; list [ 0 , 1 ] . Father : = 0 ; op [ 0 ] = 2 ; cl [ 0 ] : = 1 ; list [ 1 , 1 ] : = target state; list [ 1 , 1 ] . Father : = 0 ; op [ 1 ] = 2 ; cl [ 1 ] : = 1 ; while ( ( op [ 0 ] < = MAXN ) and ( cl [ 0 ] <op [ 0 ] ) ) or ( ( op [ 1 ] < = MAXN ) and ( cl [ 1 ] <op [ 1 ] ) ) do if op [ 0 ] <op [ 1 else expand ( 1 ) ; End .
Graph theory and graph theoryalgorithms
Figure directed graph undirected graph connected graph strongly connected graph complete graph sparse graphs zero figure tree Network
The basic traversal algorithms: breadthfirst search depthfirst search A * and check collection requirements connected components Flood Fill
Minimum Spanning Tree : Prim Kruskal
Strongly connected components : Kosaraju Gabow Tarjan
Network Flow : augmenting path method ( FordFulkerson , EdmondsKarp , Dinic ) preflow push RelabeltoFront
Figure match bipartite graph matching : Hungarian algorithm KuhnMunkres Edmonds' BlossomContraction
From " http://www.nocow.cn/index.php?title =% E5% B9% BF% E5% BA% A6% E4% BC% 98% E5% 85% 88% E6% 90% 9C% E7 % B4% A2 & oldid = 23679 "A Category : Graph Theory
This page consists NOCOW user Cotton in November 2010月20日(星期六) 20:12 to make the final changes. In ymf , NOCOW anonymous users 222.212.103.64, 212.117.187.10 and 95.169.190.71 and other work basis.All Content is available under GNU Free Documentation License 1.2 license.Shaanxi ICP No. 09005692
Shortest : Dijkstra BellmanFord ( SPFA ) FloydWarshall Johnson algorithm
Johnson算法
Contribute a better translation