design and analysis of algorithms bfs, dfs, and topological sort

Download Design and Analysis of Algorithms BFS, DFS,  and topological  sort

Post on 09-Feb-2016

51 views

Category:

Documents

0 download

Embed Size (px)

DESCRIPTION

Design and Analysis of Algorithms BFS, DFS, and topological sort. Haidong Xue Summer 2012, at GSU. What are BFS and DFS?. Two ambiguous terms: search, traversal Visit each of vertices once E.g.: tree walks of a binary search tree Traversal Search - PowerPoint PPT Presentation

TRANSCRIPT

PowerPoint Presentation

Design and Analysis of AlgorithmsBFS, DFS, and topological sortHaidong XueSummer 2012, at GSUWhat are BFS and DFS?Two ambiguous terms: search, traversalVisit each of vertices onceE.g.: tree walks of a binary search treeTraversalSearchStart from a vertex, visit all the reachable verticesSearch

What are BFS and DFS?To eliminate the ambiguity, in my classSearch indicatesStart from a vertex, visit all the reachable verticesTraversal indicatesVisit each of vertices onceHowever, in other materials, you may see some time search is considered as traversal

What are BFS and DFS?BFSStart from a vertex, visit all the reachable vertices in a breadth first mannerDFSStart from a vertex, visit all the reachable vertices in a depth first mannerBFS or DFS based traversalRepeat BFS or DFS for unreachable vertices

BFS, BF tree and shortest pathBreadth-first searchFrom a source vertex sBreadth-firstly explores the edges to discover every vertex that is reachable from sBFS(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);}}

BFS, BF tree and shortest path123456Queue: 1142Visit order: 4255BFS(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);}}BFS, BF tree and shortest path123456Queue: 224Visit order: 455BFS(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);}}BFS, BF tree and shortest path123456Queue: 33Visit order: 65465422Note that: no matter visit 5 first or visit 6 first, they are BFSBFS(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);}}BFS, BF tree and shortest pathByproducts of BFS(s)Breadth first treeThe tree constructed when a BFS is doneShortest pathA path with minimum number of edges from one vertex to anotherBFS(s) find out all the shortest paths from s to all its reachable vertices

BFS, BF tree and shortest path1234561BFS( ): 42511245BF Tree:All shortest paths started from vertex 1 are founde.g. 1 to 5 BFS, BF tree and shortest path123456BFS( ): 2254BF Tree:245Shortest 2 to 5Shortest 2 to 4BFS, BF tree and shortest path123456BFS( ): 336BF Tree:Shortest 3 to 6Shortest 3 to 536542542Shortest 3 to 4Shortest 3 to 2BFS, BF tree and shortest pathBFS TraversalBFS_Traversal(G)for each v in G{if (v has not been visited)BFS(v);}BFS, BF tree and shortest path123456Queue: 1142Visit order: 4255BFS_Traversal(G)for each v in G{if (v has not been visited)BFS(v);}3366DFS, DF treeDepth-first searchFrom a source vertex sDepth-firstly search explores the edges to discover every vertex that is reachable from sDFS(s): s.underDFS = true; // greyfor each edge {if(! d.underDFS and d has not been visited)DFS(d);}Visit(s); // black

From the deepest one to the current one DFS, DF tree123456Visit order: 5DFS(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)241DFS(1)DFS, DF tree123456Visit order: 4DFS(s):s.underDFS = true;for each edge {if((! d.underDFS and d has not been visited)DFS(d);}Visit(s);52DFS(2)DFS(5)DFS(4)DFS(2)DFS, DF tree123456Visit order: 6DFS(s):s.underDFS = true;for each edge {if((!d.underDFS and d has not been visited)DFS(d);}Visit(s);24DFS(2)DFS(5)DFS(4)DFS(3)DFS(6)53DFS(3)The reachable vertices are exactly the same with BFS, but with a different orderDFS, DF treeDepth first treeThe tree constructed when a DFS is done

DFS, DF tree123456Visit order: 5241DF Tree of DFS(1)1245DF Tree DFS, DF tree123456Visit order: 452DF Tree of DFS(2)245DF Tree DFS, DF tree123456Visit order: 5241DF Tree of DFS(3)DF Tree 6245323456DFS, DF treeDFS Traversal // (The DFS in the textbook)DFS_Traversal(G)for each v in G{if (v has not been visited)DFS(v);}DFS, DF tree123456Visit order: 5DFS(1)DFS(4)DFS(2)DFS(5)241DFS_Traversal(G)DFS_Traversal(G)for each v in G{if (v has not been visited)DFS(v);}DFS(3)DFS(6)63Topological sortDAG: directed acyclic graph A graph without cycles123456Dag? NoTopological sortDAG: directed acyclic graph A graph without cycles123456Dag? NoTopological sortOrdering in DAGs If there is an edge , then u appears before v in the ordering123456Dag? YesTopological sortExample123456123456Put all the topological sorted vertices in a line, all edges go from left to right Topological sortHow to topological sort a dag?Just use DFS_TraversalThe reverse order of DFS_Traversal is a topological sorted orderTopological sort123456DFS_Traversal(G):421365123456

Recommended

View more >