0% found this document useful (0 votes)
11 views17 pages

BFS Master

The document discusses Breadth First Search (BFS) as a method for graph traversal, particularly in the context of an airline network. It outlines the implementation of BFS, its applications including shortest path calculation, reachability, cycle detection in undirected graphs, checking bipartiteness, and determining strong connectivity in directed graphs. The document also explains the concepts of tree edges, cross edges, and back edges within BFS.

Uploaded by

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

BFS Master

The document discusses Breadth First Search (BFS) as a method for graph traversal, particularly in the context of an airline network. It outlines the implementation of BFS, its applications including shortest path calculation, reachability, cycle detection in undirected graphs, checking bipartiteness, and determining strong connectivity in directed graphs. The document also explains the concepts of tree edges, cross edges, and back edges within BFS.

Uploaded by

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

CSCI_0310: Basic Algorithms

Neelima Gupta
[email protected]
Graph Traversals
Breadth First Search

● Suppose we have an airlines network stored as a graph


with the profile of a city at a node.
● The airline maintains the weather conditions of each city
and updates it everyday in the morning.
● We need a methodical way to traverse all the nodes(cities)
in the graph and update.
○ Breadth First Search/Traversal
○ Depth First Search/Traversal
Breadth First Search
(Undirected Graph)

u1

u2 u3

u4 u6
u5

i
Implementation of BFS

BFS(G, s)
For j=1..n { color[j] = white; level[j] = ∞; parent[j] = null; } //Initialization
Enqueue(Q, s)
color[s] = grey; // Vertex added in the queue
level[s] = 0; white : not yet reached
Q = ∅;
grey: waiting in the queue
While Q ≠ ∅ to be visited
i = Dequeue(Q);
process(i); //for eg. update the weather profile of cityblack:
i visited

color[i] = black; //i has been visited/processed


for each neighbour j of i //explore each neighbor of i
if color[j] == white //If not added to queue earlier
Enqueue(Q, j)
color[j] = grey; // Vertex added in the queue
level[j] = level[i] + 1;
parent[j] = i;
Breadth First Search
(Undirected Graph) - Demo

u1

u2 u3

u4 u6
u5
Tree Edge: (u,v) is a tree edge if ‘v’ is visited first time from ‘u’. ‘u’ is called
the parent of ‘v’ and ‘v’ is a child of ‘u’.
Red Edges: Tree Edges

i
Cross Edges
• Ancestor(u): Ancestor of a vertex ‘u’ is a vertex that lies
on a path from the root to ‘u’ (including ‘u’ and the root).
• Descendant(u): Descendant of a vertex ‘u’ is a vertex that
lies on a path from ‘u’ to a leaf (including ‘u’ and the
leaf).
• Cross Edge: (u,v) is a cross edge if neither ‘v’ is a
descendant of ‘u’ nor ‘u’ is a descendant of ‘v’.
Breadth First Search
(Undirected Graph)

u1

u2 u3

u4 u6
u5
Green Edges: Cross Edges
Can we have a (u, v) edge with ‘u’ as an ancestor/descendant of ‘v’ other
than the tree edge?

i
Breadth First Search
(Directed Graph)
u1

u2 u3

u4 u6
u5

Red Edges are Tree edges.

i
Breadth First Search
(Directed Graph)
u1

u2 u3

u4 u6
u5

Red Edges are Tree edges.


Green Edges are Cross Edges.
Back Edges: (u,v) is a Back Edge if ‘v’ is an ancestor of ‘u’.
Orange Edges are Back Edges.
Can we have a (u, v) edge with ‘u’ as an ancestor of ‘v’?
i
Application of BFS: Shortest Path

u1

u2 u3

u4 u6
u5

Level[uj] is the shortest path of j from u1 if j is


reachable from u1 else it is ∞
i
Application of BFS: Reachability

u1

u2 u3

u4 u6
u5

A vertex u is reachable from u1 if level[u] is finite i.e. <



i
Application of BFS: Detecting a Cycle
in an undirected graph

while exploring v from u, if v is not parent(u) return


true.
u1

u2 u3

u4 u6
u5

i
Applications of BFS: Checking Bipartiteness

Some graph problems become


• easier if the graph is bipartite (matching)
• tractable if the graph is bipartite
(independent set )

Thus, it is very useful to know if the given


graph is bipartite.
BFS can be used to check if the given graph is
bipartite
Applications of BFS: Checking Bipartiteness
In an undirected graph, if (x, y) is an edge in the graph then
level(x) and level(y) differ by at most 1.
u1

u2 u3

u4 u6
u5

i
Applications of BFS: Checking Bipartiteness

• A graph is bipartite iff it does not contain an odd length


cycle
• A graph does not contain an odd length cycle iff no edge
in G joins two vertices at the same level in a BFS tree.
• Thus a graph is bipartite iff no edge in G joins two
vertices at the same level in a BFS tree.

Algo (BFS): while exploring v from u,


if level(v) = level(u) return true.
Application of BFS: Checking if the given directed
graph is strongly connected

• Pick any node s in G


• Run BFS on s in G
• Run BFS on s in GT
• Return true if all the nodes are reachable in both the
runs
Summarize applications of BFS
• BFS(u) tree computes the shortest path of every node
reachable from u.

• BFS can be used to determine


– set of vertices reachable from a given vertex
– if the graph contains a cycle in an undirected graph.
– (can do in directed graph as well, but we have a simpler
algorithm(DFS) for that)
– if a graph is bipartite/2 - colorable.
– if a given directed graph is strongly connected.

You might also like