Strongly connected components Finding strongly-connected components A strongly connected component is the maximal subset of a graph with a directed path between any two vertices B Tyler Moore CSE 3353, SMU, Dallas, TX g e Lecture 9 f a b h Some slides created by or adapted from Dr. Kevin Wayne. For more information see c d i http://www.cs.princeton.edu/~wayne/kleinberg-tardos . Some code reused or adapted from Python Algorithms by Magnus Lie Hetland. A C 2 / 15 ������������������� ������������������������������ Def. Nodes � and � are mutually reachable if there is a both path from � to � Theorem. Can determine if � is strongly connected in � � � ��� � � time. and also a path from � to � . Pf. � Pick any node � . reverse orientation of every edge in G Def. A graph is strongly connected if every pair of nodes is mutually � Run BFS from � in � . reachable. � Run BFS from � in � ������� . � Return true iff all nodes reached in both BFS executions. � Correctness follows immediately from previous lemma. ▪ Lemma. Let � be any node. � is strongly connected iff every node is reachable from � , and � is reachable from every node. Pf. ⇒ Follows from definition. Pf. ⇐ Path from � to � : concatenate � ↝ � path with � ↝ � path. Path from � to � : concatenate � ↝ � path with � ↝ � path. ▪ ok if paths overlap s ������������������ ���������������������� u v 41 42 3 / 15 4 / 15
Is Wikipedia a strongly connected graph? ����������������� Def. A strong component is a maximal subset of mutually reachable nodes. Theorem. [Tarjan 1972] Can find all strong components in � � � ��� � � time. 43 5 / 15 6 / 15 Strongly connected components Strongly connected components A strongly connected component is the maximal subset of a graph with a directed path between any two vertices B B g e B g e f a b h A C f a b h Are supernodes in a DAG? c d i c d i A C A C 7 / 15 8 / 15
Strongly connected components Kosaraju’s algorithm for fi nding SCCs What if we transpose all edges? B g e B 1 Get a topological sort of all vertices 2 Transpose the graph (reverse all edges) f 3 Traverse the graph in topologically sorted order, adding an SCC each a b h A C time a dead end is reached. Supernodes still in DAG c d i A C SCCs don’t change 8 / 15 9 / 15 Kosaraju’s Algorithm for Finding Strongly Connected Kosaraju’s Algorithm for Finding Strongly Connected Components Components 1. Get a topological sort of all vertices 2. Reverse all edges g g e e topsort: [a, b, e, f, topsort: [a, b, e, f, g, c, d, h, i] g, c, d, h, i] seen: seen: f f a a b h b h {} {} sccs: sccs: [] [] c c d i d i 10 / 15 10 / 15
Kosaraju’s Algorithm for Finding Strongly Connected Kosaraju’s Algorithm for Finding Strongly Connected Components Components 3. Traverse the graph in topologically sorted order, adding an SCC each 3. Traverse the graph in topologically sorted order, adding an SCC each time a dead end is reached. time a dead end is reached. 2nd SCC g g e e topsort: [a, b, e, f, topsort: [a, b, e, f, g, c, d, h, i] g, c, d, h, i] seen: seen: f f a a b h b h { a,b,c,d } { a,b,c,d,e,g,f } sccs: sccs: [ { a,b,c,d } ] [ { a,b,c,d } , { e,g,f } ] c c d i d i 1st SCC 1st SCC 10 / 15 10 / 15 Kosaraju’s Algorithm for Finding Strongly Connected Code for Kosaraju’s SCC Algorithm Components def t r (G) : # Transpose ( rev . edges of ) G GT = {} 3. Traverse the graph in topologically sorted order, adding an SCC each for u in G: GT[ u ] = set () # Get a l l the nodes in there time a dead end is reached. for u in G: for v in G[ u ] : 2nd SCC GT[ v ] . add (u) # Add a l l r e v e r s e edges return GT g e topsort: [a, b, e, f, g, c, d, h, i] def scc (G) : seen: GT = t r (G) # Get the transposed graph f a b h { a,b,c,d,e,g,f,h,i } sccs , seen = [ ] , set () for u in i t e r d f s t o p s o r t (G) : # DFS s t a r t i n g p o i n t s sccs: [ { a,b,c,d } , { e,g,f } , { h,i } ] i f u in seen : continue # Ignore covered nodes c d i C = walk (GT, u , seen ) # Don ’ t go ”backward” ( seen ) seen . update (C) # We ’ ve now seen C sccs . append (C) # Another SCC found 1st SCC 3rd SCC print sccs 10 / 15 11 / 15
Code for Kosaruju’s SCC Algorithm (topological sort) Code for Kosaruju’s SCC Algorithm (graph traversal) def i t e r d f s t o p s o r t (G) : def walk (G, s , S= set ( ) ) : #Walk the graph from node s S , Q, r e s = set ( ) , [ ] , [ ] # V i s i t ed − s e t and queue P, Q = dict ( ) , set () #Predecessor s + ” to do” queue for u in G: #Cover e n t i r e graph P[ s ] = None #s has no p r e d e c e s s o r Q. append (u) Q. add ( s ) # We plan on s t a r t i n g with s while Q: # Planned nodes l e f t ? while Q: #S t i l l nodes to v i s i t v = Q. pop () # Get one u = Q. pop () #Pick one , a r b i t r a r i l y i f u in S : continue for v in G[ u ] . d i f f e r e n c e (P, S ) : # New nodes ? S . add ( v ) # We ’ ve v i s i t e d i t now Q. add ( v ) # We plan to v i s i t them ! Q. extend (G[ v ] ) P[ v ] = u #Remember where we came from r e s . append (u) # Postorder process : add node when f i n i s h e d return P #The t r a v e r s a l t r e e r e s . r e v e r s e () return r e s 12 / 15 13 / 15 Exercise 1: Apply Kosaraju’s SCC Algorithm Exercise 2: Apply Kosaraju’s SCC Algorithm Graph G Transpose( G ) Graph G Transpose( G ) g g a a d d a c e a c e e e b h b h b d f b d f c c f i f i What is the topological sort of G ? Let’s make the DFS tree starting from a What is the topological sort of G ? Let’s make the DFS tree starting from a What are the strongly connected components? What are the strongly connected components? 14 / 15 15 / 15
Recommend
More recommend