The document discusses graph traversal algorithms depth-first search (DFS) and breadth-first search (BFS). DFS uses a stack and visits nodes by traversing as deep as possible before backtracking. BFS uses a queue and visits all nodes at each level from the starting node before moving to the next level. Examples are given applying DFS and BFS to a sample graph. Applications of DFS and BFS are also listed such as computing distances, checking for cycles/bipartiteness, and topological sorting.
Introduces Depth-First Search (DFS) and Breadth-First Search (BFS) methods, their goals and characteristics. Highlights tree structure and traversal types.
Details recursive DFS method with code and examples, showing node visitation order starting from Node 5 using stack operations.
Explains BFS concept, likening it to ripples in a pond, along with the implementation of BFS in code.
Illustrates step-by-step execution of BFS starting at Node 5, showing how adjacent nodes are visited systematically.
Discusses various applications of BFS, including computing distances, cycle detection, bipartiteness, and reachability.
Explores applications of DFS, such as computing strongly connected components, checking for biconnected graphs, and topological ordering.
Overview • Goal – Tosystematically visit the nodes of a graph • A tree is a directed, acyclic, graph (DAG) • If the graph is a tree, – DFS is exhibited by preorder, postorder, and (for binary trees) inorder traversals – BFS is exhibited by level-order traversal
3.
Depth-First Search // recursive,preorder, depth-first search void dfs (Node v) { if (v == null) return; if (v not yet visited) visit&mark(v); // visit node before adjacent nodes for (each w adjacent to v) if (w has not yet been visited) dfs(w); } // dfs
Breadth-first Search • Ripplesin a pond • Visit designated node • Then visited unvisited nodes a distance i away, where i = 1, 2, 3, etc. • For nodes the same distance away, visit nodes in systematic manner (eg. increasing index order)
22.
Breadth-First Search // non-recursive,preorder, breadth-first search void bfs (Node v) { if (v == null) return; enqueue(v); while (queue is not empty) { dequeue(v); if (v has not yet been visited) mark&visit(v); for (each w adjacent to v) if (w has not yet been visited && has not been queued) enqueue(w); } // while } // bfs
Applications of BFS •Computing Distances: Given a source vertex x, compute the distance of all vertices from x. • Checking for cycles in a graph: Given an undirected graph G, report whether there exists a cycle in the graph or not. (Note: won’t work for directed graphs) • Checking for bipartite graph: Given a graph, check whether it is bipartite or not? A graph is said to be bipartite if there is a partition of the vertex set V into two sets V1 and V2 such that if two vertices are adjacent, either both are in V1 or both are in V2. • Reachability: Given a graph G and vertices x and y, determine if there exists a path from x to y.
35.
Applications of DFS •Computing Strongly Connected Components: A directed graph is strongly connected if there exists a path from every vertex to every other vertex. Trivial Algo: Perform DFS n times. Efficient Algo: Single DFS. • Checking for Biconnected Graph: A graph is biconnected if removal of any vertex does not affect connectivity of the other vertices. Used to test if network is robust to failure of certain nodes. A single DFS traversal algorithm. • Topological Ordering: Topological sorting for Directed Acyclic Graph (DAG) is a linear ordering of vertices such that for every directed edge (x, y), vertex x comes before y in the ordering