Graph Traversals DFS and BFS Ingrid Narvaez Fall
Graph Traversals: DFS and BFS Ingrid Narvaez Fall 2013 Dr. Mongi Abidi
Introduction • It is of great interest to explore a graph by visiting its edges and learning as much as possible form the data enclosed in therm. • In this presentation, we cover two basic recursive search algorithms: – Depth First Search (DFS) – Breadth First Search (BFS) Slide 2
DFS • DFS is a classic and versatile algorithm that corresponds to maze exploration. • This algorithm utilizes recursion, and a stack of vertices that have been visited. • A stack follows the LIFO (Last In, First Out) methodology. Slide 3
DFS Explained • DFS search algorithm, tries to visit every node in the graph by doing an exhaustive search. • At every new unvisited node, it looks at its neighbors and visits the unexplored ones. • By using recursion, the algorithm, explores and visits every node in the graph. • The search ends when all the nodes have been visited. Slide 4
DFS Implementation Given a graph of n vertices, define a starting point A. 1. Mark vertex A as visited and add it to the stack, this becomes your current vertex. 2. Get the next unvisited neighbor of the current vertex, mark it as visited and push it to the stack. The top of the stack becomes your current vertex. 3. Repeat 2 for all unvisited vertices of current vertex. 4. Pop the current vertex and repeat step 2 The algorithm finishes when the stack is empty. Slide 5
DFS Example • Slide 6
DFS Example • Get the next unvisited neighbor, of the current vertex mark it as visited, push it to the stack Slide 7
DFS Example • Get the next unvisited neighbor, of the current vertex mark it as visited, push it to the stack Slide 8
DFS Example • Get the next unvisited neighbor, of the current vertex. It does not have any unvisited neighbors, pop the last entry of the stack. Slide 9
DFS Example • Get the next unvisited neighbor, of the current vertex mark it as visited, push it to the stack Slide 10
DFS Example • Get the next unvisited neighbor, of the current vertex. It has none. Pop it from the stack. Slide 11
DFS Example • Get the next unvisited neighbor, of the current vertex. It has none. Pop it from the stack. Slide 12
DFS Example • Get the next unvisited neighbor, of the current vertex. It has none. Pop it from the stack. • The stack is empty. The algorithm stops. Slide 13
BFS • DFS is a classic and versatile algorithm that corresponds to finding the shortest path between two vertices. • This algorithm utilizes recursion, and a queue of vertices that have been visited. • A queue follows the FIFO (First In, First Out) methodology. Slide 14
BFS Explained • BFS search algorithm, is analogous to finding the shortest path between two vertices by exploring all of the neighbors of the current vertex. • If the target node is not in the recently visited neighbors, it chooses a node and repeats the process. • By using recursion, the algorithm, explores and visits every node in between the start and target node. Slide 15
BFS Implementation Given a graph of n vertices, define a starting point A. 1. Mark vertex A as visited and add it to the queue, this becomes your current vertex. 2. Get all the unvisited neighbors of current vertex, and add each vertex to the queue in the order they were visited. Update current vertex to the first vertex in the queue. 3. Repeat 2 until there are no more unvisited vertices. When there are no more unvisited nodes, remove the first vertex from queue, repeat 2. The algorithm finishes when the queue is empty. Slide 16
BFS Example • Slide 17
DFS Example • Get all the neighbors of the current vertex, add them to the queue. Remove the first vertex of the queue and update current vertex. Slide 18
DFS Example • There are no unvisited nodes for the current vertex. Remove it from the queue, and update currents vertex Slide 19
DFS Example • There are no unvisited nodes for the current vertex. Remove it from the queue. Slide 20
DFS Example • The queue is empty, the algorithm finishes. Slide 21
References [1]R. Sedgewick and Graph algorithms, Algorithms in C. Boston, MA: Addison-Wesley, 2001. Slide 22
- Slides: 22