BFS/DFS Applications BFS and DFS applications Tyler Moore Shortest path between two nodes in a graph CSE 3353, SMU, Dallas, TX Topological sorting Lecture 7 Finding connected components Some slides created by or adapted from Dr. Kevin Wayne. For more information see http://www.cs.princeton.edu/~wayne/kleinberg-tardos 2 / 25 ������������ ������������������� s-t connectivity problem. Given two node � and � , is there a path between � BFS intuition. Explore outward from s in all possible directions, adding and �� ? nodes one "layer" at a time. s-t shortest path problem. Given two node � and � , what is the length of the s L 1 L 2 L n–1 shortest path between � and �� ? BFS algorithm. � � � ����� � �� . Applications. � � � �� all neighbors of � � . � Friendster. � � � �� all nodes that do not belong to � � or � � , and that have an edge to a � Maze traversal. node in � � . � Kevin Bacon number. � � � �� �� all nodes that do not belong to an earlier layer, and that have an � Fewest number of hops in a communication network. edge to a node in � � . Theorem. For each � , � � consists of all nodes at distance exactly � from � . There is a path from � to � iff � appears in some layer. 17 18 3 / 25 4 / 25
������������������� ������������������������������ Property. Let � be a BFS tree of � ���� � �� � � , and let � � �� � � be an edge of � . Theorem. The above implementation of BFS runs in � � � ��� � � time if the Then, the level of � and � differ by at most 1. graph is given by its adjacency representation. Pf. � Easy to prove �� � � � running time: � at most � lists � � � � � each node occurs on at most one list; for loop runs ≤ � � times � when we consider node � , there are � ≤ � � incident edges � � �� � � , and we spend � ��� processing each edge L 0 � Actually runs in � � � ��� � � time: � when we consider node � , there are ������ � � � incident edges � � �� � � L 1 � total time processing edges is Σ � ∈ � �� ������ � � ������� � . ▪ L 2 each edge (u, v) is counted exactly twice in sum: once in degree(u) and once in degree(v) L 3 19 20 5 / 25 6 / 25 Application of topological sorting ����������������������� Def. A DAG is a directed graph that contains no directed cycles. Def. A topological order of a directed graph � ���� � �� � � is an ordering of its nodes as � � �� � � ����� � � so that for every edge � � � �� � � � we have � ��� � . v 2 v 3 v 6 v 5 v 4 v 1 v 2 v 3 v 4 v 5 v 6 v 7 Figure : Directed acyclic graph for clothing dependencies v 7 v 1 ����� ���������������������� Figure : Topological sort of clothes 45 7 / 25 8 / 25
���������������������� ����������������������� Precedence constraints. Edge � � � �� � � � means task � � must occur before � � . Lemma. If � has a topological order, then � is a DAG. Pf. [by contradiction] Applications. � Suppose that � has a topological order � � �� � � ����� � � and that � also has a � Course prerequisite graph: course � � must be taken before � � . � Compilation: module � � must be compiled before � � . Pipeline of directed cycle � . Let's see what happens. computing jobs: output of job � � needed to determine input of job � � . � Let � � be the lowest-indexed node in � , and let � � be the node just before � � ; thus � � � �� � � � is an edge. � By our choice of � , we have � ��� � . � On the other hand, since � � � �� � � � is an edge and � � �� � � ����� � � is a topological order, we must have � ��� � , a contradiction. ▪ the directed cycle C v 1 v i v j v n ���������������������������������� � ������ � 46 47 9 / 25 10 / 25 ����������������������� ����������������������� Lemma. If � has a topological order, then � is a DAG. Lemma. If � is a DAG, then � has a node with no entering edges. Q. Does every DAG have a topological ordering? Pf. [by contradiction] � Suppose that � is a DAG and every node has at least one entering edge. Q. If so, how do we compute one? Let's see what happens. � Pick any node � , and begin following edges backward from � . Since � has at least one entering edge � � �� � � we can walk backward to � . � Then, since � has at least one entering edge � � �� � � , we can walk backward to � . � Repeat until we visit a node, say � , twice. � Let � denote the sequence of nodes encountered between successive visits to � . � is a cycle. ▪ w x u v 48 49 11 / 25 12 / 25
Examples of Induction-Based Topological Sorting ����������������������� Lemma. If � is a DAG, then � has a topological ordering. Pf. [by induction on � ] � Base case: true if � ���� . � Given DAG on �� ���� nodes, find a node � with no entering edges. � � ����� � �� is a DAG, since deleting � cannot create cycles. � By inductive hypothesis, � ����� � �� has a topological ordering. � Place � first in topological ordering; then append nodes of � ����� � �� � in topological order. This is valid since � has no entering edges. ▪ ��� v 50 13 / 25 14 / 25 Python code for induction-based topsort Topological sorting on DAGs def t o p s o r t (G) : Directed Acyclic Graph Depth-First Search Tree count = d i c t (( u , 0) for u in G) #The in − degree f o r each node g for u in G: v in G[ u ] : 1 for 4 count [ v ] += 1 #Count every in − edge a b b b d d d f Q = [ u for u in G i f count [ u ] == 0] # Valid i n i t i a l nodes 8 S = [ ] #The r e s u l t 2 6 5 while Q: #While we have s t a r t nodes . . . a a a c c c e e e e c b 7 10 u = Q. pop () #Pick one S . append (u) #Use i t as f i r s t of the r e s t 3 1 1 v in G[ u ] : for g g g f f f d count [ v ] − = 1 #”Uncount” i t s out − edges count [ v ] == 0: #New v a l i d i f s t a r t nodes ? g e a c f d b Discovered: Q. append ( v ) #Deal with them next g e c a Topological sort: g a b c f e d Processed: d f b return S 15 / 25 16 / 25
DFS Trees: all descendants of a node u are processed after How can we tell if one node is a descendant of another? u is discovered but before u is processed Undirected Graph Depth-First Search Tree a 1 Answer: with depth- fi rst timestamps! 1 0 After we create a graph in a depth- fi rst traversal, it would be nice to b b b b f be able to verify if node A is encountered before node B , etc. c c c We add one timestamp for when a node is discovered (during preorder 2 processing) and another timestamp for when a node is processed a a a c 6 (during postorder processing) d d d 5 3 e e e f f f d 4 a c e Discovered: b d f e c a e Processed: d b f 17 / 25 18 / 25 Code for depth- fi rst timestamps Using depth- fi rst timestamps for topological sorting d fs (G, s , d , f , S=None , t =0): def i f S i s None : S = s e t () # I n i t i a l i z e the h i s t o r y d [ s ] = t ; t += 1 # Set d i s c o v e r time > f = {} > > S . add ( s ) # We ’ ve v i s i t e d s > d= {} > > for u in G[ s ] : # Explore neighbors > dfs (DAG, ’ g ’ ,d , f ) > > i f u in S : continue # Already v i s i t e d . Skip 14 t = d fs (G, u , d , f , S , t ) # Recurse ; update > t o p s o r t = [ k for k , v in timestamp so r t ed ( f . i t e r i t e m s ( ) , > > f [ s ] = t ; t += 1 # Set f i n i s h time key= lambda (k , v ) : v ) ] return t # Return timestamp > t o p s o r t . r e v e r s e () > > > t o p s o r t > > > > > f= {} [ ’ g ’ , ’ a ’ , ’b ’ , ’ c ’ , ’ f ’ , ’ e ’ , ’ d ’ ] > d= {} > > > d f s (N, ’ a ’ ,d , f ) > > 12 > d > > { ’ a ’ : 0 , ’ c ’ : 2 , ’ b ’ : 1 , ’ e ’ : 4 , ’ d ’ : 3 , ’ f ’ : 9 } > f > > { ’ a ’ : 11 , ’ c ’ : 7 , ’ b ’ : 8 , ’ e ’ : 5 , ’ d ’ : 6 , ’ f ’ : 10 } 19 / 25 20 / 25
Recommend
More recommend