This is an PPT of Data Structure. It include the following topic "BFS & DFS in Data Structure".
Size: 222.75 KB
Language: en
Added: Apr 13, 2020
Slides: 37 pages
Slide Content
BFS AND DFS Presented By: Muskaan MCA/25020/18
CONTENTS Introduction Types of graph Traversal Depth-First Search Breadth-First Search Application of BFS and DFS
INTRODUCTION Graph traversal is also known as graph search refers to the process of visiting each vertex in a graph . Such traversals are classified by the order in which the vertices are visited. Tree traversal is a special case of graph traversal .
DEPTH FIRST SEARCH Depth-first search ( DFS ) is an algorithm for traversing or searching tree or graph data structures. DFS uses stack.
Depth-First Search // recursive, preorder 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
E xa m p le 7 1 5 4 3 2 6 Policy: Visit adjacent nodes in increasing index order
DFS: Start with Node 5 7 1 5 4 3 2 6 D on e 5 1 0 3 7 6 4 2
BREADTH FIRST SEARCH Breadth-first search ( BFS ) is an algorithm for traversing or searching tree or graph data structures. It uses queue.
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
Ap p l ic a t i ons of B FS • • • Computing Distances Checking for cycles in a graph Reachability : Given a graph G and vertices x and y , determine if there exists a path from x to y .
A pp l i c a t i ons of 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 .