Download presentation
Presentation is loading. Please wait.
Published byIris Sanders Modified over 9 years ago
1
Design and Analysis of Algorithms BFS, DFS, and topological sort Haidong Xue Summer 2012, at GSU
2
What are BFS and DFS? Two ambiguous terms: search, traversal Visit each of vertices once –E–E.g.: tree walks of a binary search tree –T–Traversal –S–Search Start from a vertex, visit all the reachable vertices –S–Search
3
To eliminate the ambiguity, in my class Search indicates –S–Start from a vertex, visit all the reachable vertices Traversal indicates –V–Visit each of vertices once However, in other materials, you may see some time “search” is considered as “traversal”
4
BFS – Start from a vertex, visit all the reachable vertices in a breadth first manner DFS – Start from a vertex, visit all the reachable vertices in a depth first manner BFS or DFS based traversal – Repeat BFS or DFS for unreachable vertices
5
BFS, BF tree and shortest path Breadth-first search – From a source vertex s – Breadth-firstly explores the edges to discover every vertex that is reachable from s BFS(s) visit(s); queue.insert(s); while( queue is not empty ){ u = queue.extractHead(); for each edge { if(d has not been visited) visit(d); queue.insert(d); }
6
BFS, BF tree and shortest path 123 456 Queue: 1 1 42 Visit order: 42 5 5 BFS(1) BFS(s) visit(s); queue.insert(s); while( queue is not empty ){ u = queue.extractHead(); for each edge { if(d has not been visited) visit(d); queue.insert(d); }
7
BFS, BF tree and shortest path 123 456 Queue: 2 2 4 Visit order: 45 5 BFS(2) BFS(s) visit(s); queue.insert(s); while( queue is not empty ){ u = queue.extractHead(); for each edge { if(d has not been visited) visit(d); queue.insert(d); }
8
BFS, BF tree and shortest path 123 456 Queue: 3 3 Visit order: 654 65 4 2 2 Note that: no matter visit 5 first or visit 6 first, they are BFS BFS(3) BFS(s) visit(s); queue.insert(s); while( queue is not empty ){ u = queue.extractHead(); for each edge { if(d has not been visited) visit(d); queue.insert(d); }
9
BFS, BF tree and shortest path Byproducts of BFS(s) – Breadth first tree The tree constructed when a BFS is done – Shortest path A path with minimum number of edges from one vertex to another BFS(s) find out all the shortest paths from s to all its reachable vertices
10
BFS, BF tree and shortest path 123 456 1 BFS( ): 42 5 1 1 24 5 BF Tree: All shortest paths started from vertex 1 are found e.g. 1 to 5
11
BFS, BF tree and shortest path 123 456 BFS( ): 2 2 5 4 BF Tree: 2 45 Shortest 2 to 5 Shortest 2 to 4
12
BFS, BF tree and shortest path 123 456 BFS( ): 3 3 6 BF Tree: Shortest 3 to 6 Shortest 3 to 5 36542 5 4 2 Shortest 3 to 4 Shortest 3 to 2
13
BFS, BF tree and shortest path BFS Traversal BFS_Traversal(G) for each v in G{ if (v has not been visited) BFS(v); }
14
BFS, BF tree and shortest path 123 456 Queue: 1 1 42 Visit order: 42 5 5 BFS_Traversal(G) for each v in G{ if (v has not been visited) BFS(v); } 3 3 6 6
15
DFS, DF tree Depth-first search – From a source vertex s – Depth-firstly search explores the edges to discover every vertex that is reachable from s DFS(s): s.underDFS = true; // grey for each edge { if(! d.underDFS and d has not been visited) DFS(d); } Visit(s); // black From the deepest one to the current one
16
DFS, DF tree 123 456 Visit order: 5 DFS(s): s.underDFS = true; for each edge { if((! d.underDFS and d has not been visited) DFS(d); } Visit(s); DFS(1) DFS(4) DFS(2) DFS(5) 241 DFS(1)
17
DFS, DF tree 123 456 Visit order: 4 DFS(s): s.underDFS = true; for each edge { if((! d.underDFS and d has not been visited) DFS(d); } Visit(s); 52 DFS(2) DFS(5)DFS(4) DFS(2)
18
DFS, DF tree 123 456 Visit order: 6 DFS(s): s.underDFS = true; for each edge { if((!d.underDFS and d has not been visited) DFS(d); } Visit(s); 24 DFS(2) DFS(5)DFS(4) DFS(3) DFS(6) 53 DFS(3) The reachable vertices are exactly the same with BFS, but with a different order
19
DFS, DF tree Depth first tree – The tree constructed when a DFS is done
20
DFS, DF tree 123 456 Visit order: 5241 DF Tree of DFS(1) 12 45 DF Tree
21
DFS, DF tree 123 456 Visit order: 452 DF Tree of DFS(2) 2 45 DF Tree
22
DFS, DF tree 123 456 Visit order: 5241 DF Tree of DFS(3) DF Tree 62453 2 3 45 6
23
DFS, DF tree DFS Traversal // (The DFS in the textbook) DFS_Traversal(G) for each v in G{ if (v has not been visited) DFS(v); }
24
DFS, DF tree 123 456 Visit order: 5 DFS(1) DFS(4) DFS(2) DFS(5) 241 DFS_Traversal(G) for each v in G{ if (v has not been visited) DFS(v); } DFS(3) DFS(6) 63
25
Topological sort DAG: directed acyclic graph – A graph without cycles 123 456 Dag?No
26
Topological sort DAG: directed acyclic graph – A graph without cycles 123 456 Dag?No
27
Topological sort Ordering in DAGs – If there is an edge, then u appears before v in the ordering 123 456 Dag?Yes
28
Topological sort Example 123 456 1 23 4 5 6 Put all the topological sorted vertices in a line, all edges go from left to right
29
Topological sort How to topological sort a dag? Just use DFS_Traversal The reverse order of DFS_Traversal is a topological sorted order
30
Topological sort 123 456 DFS_Traversal(G): 421365 123 4 5 6
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.