Analysis and Design of Algorithm
Lab Exercise 7
Graph Algorithm
A graph is simply a set of points together with a set of lines connecting various points. Myriad realworld application problems can be reduced to problems on graphs.
A graph G = (V, E)
V = set of vertices, E = set of edges
Dense graph:
|E| |V|2; Sparse graph: |E| |V|
Undirected graph:
Edge (u,v) = edge (v,u)
No self-loops
Directed graph:
Edge (u,v) goes from vertex u to vertex v, notated uv
A weighted graph associates weights with either the edges or the vertices
Graph Searching:
Given: a graph G = (V, E), directed or undirected
Goal: methodically explore every vertex and every edge
Ultimately: build a tree on the graph
Pick a vertex as the root
Choose certain edges to produce a tree
Note: might also build a forest if graph is not connected
Breadth First Search
Search a graph (directed or not) in breadth first; this is done by using a queue where the vertices
found are stored.
Here is a brief description of the BFS algorithm:
bfs (Graph G)
{
all vertices of G are first painted white
the graph root is painted gray and put in a queue
while the queue is not empty
{
a vertex u is removed from the queue
for all white successors v of u
{
Page 1 of 6
Hamdard University – Faculty of Engineering Sciences and Technology
Analysis and Design of Algorithm
Lab Exercise 7
v is painted gray
v is added to the queue
}
u is painted black
}
}
The total running time for breadth-first search traversal is O(V + E).
Example: The following figure illustrates the progress of breadth-first search on the undirected
sample graph.
After initialization (paint every vertex white, set d[u] to infinity for each vertex u, and set the
parent of every vertex to be NIL), Initialize Q to contain just the source vertex s.
The algorithm discovers all vertices 1 edge from s i.e., discovered all vertices (w and r) at
level 1.
Now the step is:
The algorithm discovers all vertices 2 edges from s i.e., discovered all vertices (t, x, and v) at
level 2.
Page 2 of 6
Hamdard University – Faculty of Engineering Sciences and Technology
Analysis and Design of Algorithm
Lab Exercise 7
Now the step is:
Now the step is:
The algorithm discovers all vertices 3 edges from s i.e., discovered all vertices (u and y) at
level 3.
Now the step is:
The algorithm terminates when every vertex has been fully explored.
Page 3 of 6
Hamdard University – Faculty of Engineering Sciences and Technology
Analysis and Design of Algorithm
Lab Exercise 7
Analysis
The while-loop in breadth-first search is executed at most |V| times. The reason is that every
vertex enqueued at most once. So, we have O(V).
The for-loop inside the while-loop is executed at most |E| times if G is a directed graph or
2|E| times if G is undirected. The reason is that every vertex dequeued at most once and we
examine (u, v) only when u is dequeued. Therefore, every edge examined at most once if
directed, at most twice if undirected. So, we have O(E).
Therefore, the total running time for breadth-first search traversal is O(V + E).
Depth First Search
Depth-first search is a systematic way to find all the vertices reachable from a source vertex, s.
Historically, depth-first was first stated formally hundreds of years ago as a method for traversing
mazes. Like breadth-first search, DFS traverse a connected component of a given graph and defines
a spanning tree. The basic idea of depth-first search is this: It methodically explore every edge. We
start over from different vertices as necessary. As soon as we discover a vertex, DFS starts exploring
from it (unlike BFS, which puts a vertex on a queue so that it explores from it later).
Here is a brief description of the DFS algorithm:
dfs-visit (Graph G, Vertex u)
{
the vertex u is painted gray
for all white successors v of u
{
dfs-visit(G, v)
}
u is painted black
}
dfs (Graph G)
{
all vertices of G are first painted white
dfs-visit(G, root of G)
Page 4 of 6
}
Hamdard University – Faculty of Engineering Sciences and Technology
Analysis and Design of Algorithm
Lab Exercise 7
Example (CLRS):
In the following figure, the solid edge represents discovery or tree
edge and the dashed edge shows the back edge. Furthermore, each vertex has two time stamps: the
first time-stamp records when vertex is first discovered and second time-stamp records when the
search finishes examining adjacency list of vertex.
Analysis
The analysis is similar to that of BFS analysis. The DFS-Visit is called (from DFS or from itself)
once for each vertex in V[G] since each vertex is changed from white to gray once. The for-loop in
DFS-Visit is executed a total of |E| times for a directed graph or 2|E| times for an undirected graph
since each edge is explored once. Moreover, initialization takes Θ(|V|) time. Therefore, the running
time of DFS is Θ(V + E).
Page 5 of 6
Hamdard University – Faculty of Engineering Sciences and Technology
Analysis and Design of Algorithm
Lab Exercise 7
Tasks:
Question 1.
Question 2.
Page 6 of 6
Write a program using the algorithm discussed in lab that perform Breadth
first search.
Write a program using the algorithm discussed in lab that perform Depth first
search.
Hamdard University – Faculty of Engineering Sciences and Technology
© Copyright 2026 Paperzz