Docsity

Graph Theory: Breadth-First Search (BFS) and Depth-First Search (DFS), Lecture notes of Data Structures and Algorithms

Partial preview of the text

Download Graph Theory: Breadth-First Search (BFS) and Depth-First Search (DFS) and more Lecture notes Data Structures and Algorithms in PDF only on Docsity!

Graph Theory- SC322:

BFS & DFS

Graph searching

Graph seacrhing is the systematic traversal of the vertices and edges of a graph in order to learn useful structural properties of a graph.

Graph searching

Graph seacrhing is the systematic traversal of the vertices and edges of a graph in order to learn useful structural properties of a graph. Graph searching techniques are central to several important graph algorithms. Many advanced algorithms begin by a graph searching routine before proceeding with other computations. The two most common graph searching algorithms are Breadth-First Search (BFS) and Depth-First Search (DFS).

BFS explores the edges and vertices of a graph starting from a specified vertex by looking at vertices closest first and then moving to remote vertices. It performs explorations at all vertices at a distance k from the source before looking at any vertex before doing so for any vertex at distance k + 1.

BFS explores the edges and vertices of a graph starting from a specified vertex by looking at vertices closest first and then moving to remote vertices. It performs explorations at all vertices at a distance k from the source before looking at any vertex before doing so for any vertex at distance k + 1.

DFS explores further and deeper along a path until no further progress is possible and then retraces and branches from an earlier point.

During their course both these algorithms get useful information about the graph. They are used either directly or as prototypes to develop subroutines for many graph algorithms.

BFS (G,s)

(^1) for each vertex u ∈ V [G ] \ {s}

BFS (G,s)

(^1) for each vertex u ∈ V [G ] \ {s} (^2) do colour [u] ← white (^3) d[u] ← āˆž

BFS (G,s)

(^1) for each vertex u ∈ V [G ] \ {s} (^2) do colour [u] ← white (^3) d[u] ← āˆž (^4) Ļ€[u] ← nil

BFS (G,s)

(^1) for each vertex u ∈ V [G ] \ {s} (^2) do colour [u] ← white (^3) d[u] ← āˆž (^4) Ļ€[u] ← nil (^5) colour [s] ← grey (^6) d[s] ← s

BFS (G,s)

(^1) for each vertex u ∈ V [G ] \ {s} (^2) do colour [u] ← white (^3) d[u] ← āˆž (^4) Ļ€[u] ← nil (^5) colour [s] ← grey (^6) d[s] ← s (^7) Ļ€[s] ← nil

BFS (G,s)

(^1) for each vertex u ∈ V [G ] \ {s} (^2) do colour [u] ← white (^3) d[u] ← āˆž (^4) Ļ€[u] ← nil (^5) colour [s] ← grey (^6) d[s] ← s (^7) Ļ€[s] ← nil (^8) Q ← āˆ… (^9) ENQUEUE (Q, s)

(^10) while (Q 6 = āˆ…)

(^10) while (Q 6 = āˆ…)

(^11) do u ← DEQUEUE (Q)

(^12) for each v ∈ Adj[u]

(^10) while (Q 6 = āˆ…)

(^11) do u ← DEQUEUE (Q)

(^12) for each v ∈ Adj[u]

(^13) do if colour [v ] = white