0% found this document useful (0 votes)
52 views35 pages

Graph Traversal

This document discusses two basic graph algorithms: breadth-first search (BFS) and depth-first search (DFS). BFS uses a queue to traverse all neighboring vertices first before moving to vertices farther away. DFS uses a stack to go deep in the graph before exploring other paths, resulting in a recursive traversal. Pseudocode and examples are provided to illustrate how each algorithm traverses a sample graph.

Uploaded by

Amer Ahmad
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
52 views35 pages

Graph Traversal

This document discusses two basic graph algorithms: breadth-first search (BFS) and depth-first search (DFS). BFS uses a queue to traverse all neighboring vertices first before moving to vertices farther away. DFS uses a stack to go deep in the graph before exploring other paths, resulting in a recursive traversal. Pseudocode and examples are provided to illustrate how each algorithm traverses a sample graph.

Uploaded by

Amer Ahmad
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 35

GRAPH ALGORITHMS

Basic graph algorithms


¨  To search a graph G, we need to visit all vertices in
a systematic order.
¨  There are two basic algorithms for graph traversal

¤  BFS: Breadth-first Search


¤  DFS: Depth-first Search
Breadth-first Search Algorithm
¨  Start from any vertex in the graph
Breadth-first Search Algorithm
¨  Traverse to all its neighbors (its adjacent vertices) first
Breadth-first Search Algorithm
¨  Next traverse to the neighbors of the neighbors
etc. until all the vertices of the graph are visited
Breadth-first Search: data structure
¨  We use a Queue to perform this search
¤  Nodes will be visited in the order imposed by the FIFO
queue
BFS: example
BFS: example
¨  Initial state: start with vertex 0
BFS: example
¨  State after processing 0
BFS: example
¨  State after processing 1
BFS: example
¨  State after processing 3
BFS: example
¨  State after processing 8
BFS: example
¨  State after processing 7
BFS: example
¨  State after processing 2
BFS: example
¨  State after processing 4
BFS: example
¨  State after processing 5
BFS: example
¨  State after processing 6
BFS: example
¨  The queue is empty: we’re done!
Let’s code it!
Depth First Search Algorithm
¨  Go deep before going wide
¤  Start from any vertex in the graph
¤  Traverse deeper and deeper until dead end

¤  Back track and traverse other vertices that are not


visited yet
DFS: example
¨  DFS(0)
DFS: example
¨  DFS(0)àDFS(1)
DFS: example
¨ DFS(0)àDFS(1); because vertex 0 is “visited”;
DFS(1)àDFS(7)
DFS: example
¨  DFS(7)àDFS(2)
DFS: example
¨  DFS(2)àDFS(3)
DFS: example
¨  DFS(3)àDFS(4)
DFS: example
¨  DFS(4)àDFS(8)
DFS: example
¨  DFS(8)àDFS(0), we return to 4
DFS: example
¨  we return to DFS(3)
DFS: example
¨  we return to DFS(2)
DFS: example
¨  DFS(2)àDFS(5)
DFS: example
¨  DFS(5)àDFS(6)
DFS: example
¨  DFS(6)àDFS(5) return to DFS(5)
DFS: example
¨  return to DFS(2), then to DFS(7), then to DFS(1), then
DFS(0)
¨  And we are done!
Let’s code it!s

You might also like