UNIT 2 - CS3401-Algorithms
UNIT 2 - CS3401-Algorithms
Graph traversal means visiting every vertex and edge exactly once in
a well-defined order in the graph. The traversing will start from the Source Node 1. Push Node 1
There are two types of Graph traversal. to the top of the stack. Node 1 will be marked as 'visited'.
1. Depth First Search (DFS)
2. Breadth First Search (BFS)
DFS is implemented using Stack (LIFO: Last-In-First-Out). BFS is
implemented using Queue (FIFO: First-In-First-Out).
The stack is empty and it comes out of the loop. All the nodes
have been traversed by using DFS.
BFS (G, s): //Where G is the graph and s is the source node
let Q be queue.
Q.enqueue( s ) //Inserting s in queue
mark s as visited.
while ( Q is not empty)
v = Q.dequeue( ) //Removing that vertex from queue Dequeue Node 1 and visit the unvisited neighbours and
enqueue them. Node 3 will be marked as 'visited'.
//processing all the neighbours of v
for all neighbours w of v in Graph G
if w is not visited
Q.enqueue( w ) //Stores w in Q
mark w as visited.
Dequeue Node 5.
The queue is empty and it comes out of the loop. All the nodes
have been traversed by using BFS.
CS3401-ALGORITHMS (Sem-IV-R2021) - UNIT-II: 4
Shortest Path Algorithm Choose a starting vertex and assign infinity path values to all
The shortest path algorithms calculate the minimum other vertices.
travelling cost from source node to destination node of a graph
in optimal time and space complexities.
The shortest path problem is about finding a path between
vertices in a graph such that the total sum of the edges weights
is minimum.
Example:
Time Complexity
Best Case Average Case Worst Case
O(E) O(VE) O(VE)
Space Complexity
The space complexity is O(V).
BellmanFord(G, S)
for each vertex V in G
distance[V] <- infinite
previous[V] <- NULL
distance[S] <- 0
Unvisited set : {S , a , b , c , d , e}
Visited set :{}
Time Complexity
Vertex ‘e’ is chosen. Best Case Average Case Worst Case
There are no outgoing edges for vertex ‘e’. O((V + E) log V) O((V + E) log V) O((V2) log V)
Unvisited set : { }
Visited set : {S , a , d , b , c , e}
Space Complexity
All vertices of the graph are processed. The space complexity is O(V)
dijkstra(G, S)
for each vertex V in G
distance[V] <- infinite
previous[V] <- NULL
If V != S, add V to Priority Queue Q
distance[S] <- 0
The resultant matrix gives the shortest path between each pair
of vertices.
MST_Kruskal(Edges, V, E):
e=0, i=0
sum=0
Sort(Edges) From the list of sorted edge costs, select the least cost edge
while(e<V-1): B→D=5
u=Edges[i].u Minimum cost = 5
v=Edges[i].v Visited array, v = {B, D}
if(Adding edge {u, v} do not form cycle):
Print(Adding edge {u, v} to MST)
sum=sum + Edges[i].weight
e=e+1
i=i+1
The next least cost edge is C → F = 9 The next least cost edge is G → F = 12
Minimum Cost = 5 + 6 + 9 = 20 Minimum cost = 5 + 6 + 9 + 10 + 11 + 12 = 53
Visited array, v = {B, D, A, C, F} Visited array, v = {B, D, A, C, F, E, G}
The obtained result is the minimum spanning tree of the given graph
The next least cost edge is F → E = 10
with cost = 53
Minimum Cost = 5 + 6 + 9 + 10 = 30
Visited array, v = {B, D, A, C, F, E}
Complexity Analysis of Kruskal’s Algorithm
MST_Prim(G):
T = ∅;
U = { 1 }; Create a visited array to store all the visited vertices
while (U ≠ V) V={}
let (u, v) be the lowest cost edge such that u ∈ U and v ∈ V - U; The arbitrary root is S.
Find the least cost edge that are connected to S.
T = T ∪ {(u, v)}
S→B=8
U = U ∪ {v}
Minimum cost = 8
Prim's algorithm shows how we create two sets of vertices
V = {S, B}
U and V-U.
U contains the list of vertices that have been visited and
V-U the list of vertices that haven't.
One by one, we move vertices from set V-U to set U by
connecting the least weight edge.
Since B is the last visited, check for the least cost edge that is
connected to the vertex B.
B→A=9
Minimum cost = 8 + 9 = 17
V = {S, B, A}
Since E is the last visited, check for the least cost edge that is
connected to the vertex E.
The minimum spanning tree is obtained with the minimum cost = 46
E→D=3
Minimum cost = 8 + 9 + 11 + 3 = 31
Complexity Analysis of Prim’s Algorithm
V = {S, B, A, E, D}
Adjacency Matrix
Graph is represented in the form of the 2D matrix where rows
and columns denote vertices. Each entry in the matrix
represents the weight of the edge between those vertices.
A graph is said to be bi-connected if: It is possible to travel from one vertex to another vertex. Here,
It is connected. (It is possible to reach every vertex from we can traverse from vertex B to H using the path B -> A -> D
every other vertex by a simple path) -> F -> E -> H. Hence it is a connected graph.
There are two disjoint paths between any two vertices.
There exists simple cycle between two vertices.
There should not be any cut vertex (Cut vertex is a 7) What is flow networks?
vertex which if we remove then the graph becomes A flow network is a directed graph G = (V,E) with vertices S
disconnected.) (source) and T (sink), in which each edge (u,v) ∈ E has a non-
negative capacity c(u,v).
5) What is strong connectivity? (strong connected graph)
A directed graph is strongly connected if there is directed path
from any vertex to every other vertex.
A bipartite graph is a graph whose vertex set is partitioned into 11) What is graph traversal?
two disjoint sets L, R such that each edge has one endpoint in Graph traversal means visiting every vertex and edge exactly
L and the other endpoint in R. once in a well-defined order in the graph.
There are two types of Graph traversal.
1. Depth First Search (DFS)
2. Breadth First Search (BFS)
DFS is implemented using Stack (LIFO: Last-In-First-Out).
BFS is implemented using Queue (FIFO: First-In-First-Out).
Types of shortest path algorithms: single-source and all- for each edge (U,V) in G
pairs. If distance[U] + edge_weight(U, V) < distance[V}
Error: Negative Cycle Exists
Bellman-Ford Algorithm Single-Source vertex to all other
vertices return distance[], previous[]
Dijkstra’s Algorithm Single-Source vertex to all other
vertices
Floyd-Warshall Algorithm Between All pairs of vertices