Breadth-First Search • Completely explore the vertices CSE 421: Intro to in order of their distance from v Algorithms • Naturally implemented using a queue Summer 2004 • Works on general graphs, not just trees Graph Algorithms: BFS, DFS, Articulation Points Larry Ruzzo 1 2 BFS(v) BFS(v) 1 Global initialization: mark all vertices "undiscovered" 2 BFS(v) 3 mark v "discovered" 4 queue = v 6 while queue not empty 7 u = remove_first(queue) 9 5 for each edge {u,x} 11 if (x is undiscovered) Exercise: modify 12 mark x discovered 10 code to number vertices & compute append x on queue 8 level numbers mark u completed 3 13 4 BFS analysis Properties of (Undirected) BFS(v) • Each edge is explored once from each • BFS(v) visits x if and only if there is a path in G from v to x. end-point • Edges into then-undiscovered vertices define a tree – the "breadth first spanning tree" of G • Each vertex is discovered by following a • Level i in this tree are exactly those vertices u different edge such that the shortest path (in G, not just the tree) from the root v is of length i. • All non-tree edges join vertices on the same or • Total cost O(m) where m=# of edges adjacent levels • Disconnected? Restart @ undiscovered vertices: O(m+n) 5 6 1
BFS Application: Shortest Paths Depth-First Search 0 1 • Follow the first path you find as far as you Tree gives shortest paths from start vertex can go 2 3 1 • Back up to last unexplored edge when you 4 reach a dead end, then go as far you can 6 7 2 9 5 • Naturally implemented using recursive 3 11 calls or a stack 12 10 • Works on general graphs, not just trees 8 can label by distances from start 4 7 8 13 DFS(v) – Recursive version DFS(v) 1 Global Initialization: mark all vertices v "undiscovered” via v.dfs# = -1 2 10 dfscounter = 0 3 DFS(v) 11 12 v.dfs# = dfscounter++ // mark v “discovered” 7 for each edge (v,x) 8 if (x.dfs# = -1) // tree edge (x previously undiscovered) 13 DFS(x) 6 9 else … // code for back-, fwd-, parent, 4 // edges, if needed // mark v “completed,” if needed 9 5 10 DFS(v) Properties of (Undirected) DFS(v) 1 • Like BFS(v): – DFS(v) visits x ⇔ there is a path in G from v to x 2 (through previously unvisited vertices) 10 – Edges into then-undiscovered vertices define a tree – 3 the "depth first spanning tree" of G • Unlike the BFS tree: 11 12 – the DF spanning tree isn't minimum depth 7 8 – its levels don't reflect min distance from the root 13 – non-tree edges never join vertices on the same or adjacent levels 6 9 • BUT… 4 11 12 5 2
Non-tree edges Application: Articulation Points • A node in an undirected graph is an • All non-tree edges join a vertex and one of articulation point iff removing it its descendents/ancestors in the DFS tree disconnects the graph • Called back/forward edges (depending on end) • articulation points represent vulnerabilities in a network – single points whose failure would split the network into 2 or more • No cross edges! disconnected components 13 14 Articulation Points Exercise 1 • draw a graph, ~ 10 nodes, A-J • redraw as via DFS 2 10 • add dsf#s & tree/back edges (solid/dashed) • find cycles 3 • give alg to find cycles via dfs; does G have any? 11 12 7 8 • find articulation points 13 • what do cycles have to do with articulation 6 9 points? 4 • alg to find articulation points via DFS??? 15 16 5 Articulation Points: Articulation Points from DFS the "LOW" function trivial If removal of u • Definition: LOW(v) is the lowest dfs# of • Root node is an does NOT any vertex that is either in the dfs subtree articulation point separate x, iff it has more rooted at v (including v itself) or connected there must be an exit from than one child to a vertex in that subtree by a back edge. x's subtree. u • Leaf is never an How? Via back x articulation • Key idea 1: if some child x of v has LOW(x) ≥ edge. point dfs#(v) then v is an articulation point. • Key idea 2: LOW(v) = • non-leaf, non-root no non-tree edge goes ⇔ min ( {LOW(w) | w a child of v } ∪ node u is an above u from a sub-tree { dfs#(x) | {v,x} is a back edge from v } ) articulation point below some child of u 17 18 3
DFS(v) for Properties of DFS Vertex Numbering Finding Articulation Points • If u is an ancestor of v in the DFS tree, Global initialization: v.dfs# = -1 ∀ v; DFS(v) ∀ unvisited v. Except for root. Why? DFS(v) then v.dfs# = dfscounter++ ? v.low = v.dfs# // initialization for each edge {v,x} dfs#(u) dfs#(v). if (x.dfs# == -1) // x is undiscovered DFS(x) v.low = min(v.low, x.low) if (x.low >= v.dfs#) print “v is art. pt., separating x” Equiv: “if( {v,x} else if (x is not v’s parent) is a back edge)” 19 20 v.low = min(v.low, x.dfs#) Why? Articulation Points Articulation Points A A 1 2 Vertex DFS # Low Vertex DFS # Low B B A A 1 1 B B 2 1 3 C C C C 3 1 D D 4 3 4 E E 8 1 8 D E D E F F 5 3 G G 9 9 9 5 H H 10 1 10 F G H F G H I 11 I 6 3 J J J J 11 10 6 12 I L K I L K 7 3 L L 12 10 M M 13 13 7 13 K M K M 21 22 4
Recommend
More recommend