Professional Documents
Culture Documents
Class work:
1) What is degree of vertex of a graph?
Homework:
1) -------------- is a vertex having in-degree of 1 and out-degree of 0.
Previous JNTUK Questions:
1) What is path, cycle, loop and Adjacency vertex of a graph? (JNTUK-
Regular- April/May – 2016 -SET-2- [4M])
Day 34
Topics to be covered: Representation of Graphs
Q1. What are the various ways to represent the graphs? Explain the
adjacency matrix representation of graphs.
A graph can be represented most commonly by 2 ways
i) Adjacency matrix representation
ii) Adjacency list representation (linked representation)
Representation of graph using adjacency matrix: An adjacency matrix is a
matrix in which the entries or elements of the matrix are defined as follows
aij = 1 , if there is an edge from vi to vj. = 0, otherwise.
Adjacency matrix is also termed as Boolean or bit matrix (because it uses the
bits 0 or 1 in matrix)
Example: An adjacency matrix is used to represent the graph in the following
way for an undirected graph
7
For example consider the weighted graph given below, the adjacency list
representation is given as
Class work:
1) Write the node representation of a weighted graph.
Homework:
1) If there is an edge from vi to vj in the graph then the value that edge in
adjacency matrix is --------------
Previous JNTUK Questions:
1) How to represent a graph using adjacency matrix and adjacency
list? Explain. (JNTUK-Regular- December – 2013 – SET -4 [8M])
Day 35
Topics to be covered: Graph Traversal algorithms
Q1. What is a graph traversal? Name the graph traversal algorithms.
A graph traversal means visiting all the vertices in the graph exactly only once.
Graph traversal algorithms are used are
i) BFS (Breadth First Search) ii) DFS (Depth First Search)
Q2. Explain DFS algorithm with example.
9
DFS (Depth First Search) algorithm is a graph traversal algorithm where we
will start searching or finding the path from a starting or source vertex to the
ending or destination vertex deeply or vertically from top to bottom.
In DFS algorithm we will use stack data structure to search the path because
we search the path vertically from top to bottom.
Algorithm: Suppose a graph of vertices v1, v2, v3, v4........vn is given as below.
Step 1: Start the algorithm from vertex v1 and mark this vertex V1 as visited in
the array as ‘1’, if not visited mark as ‘0’ and push the vertex V1 into stack.
Step 2: Find adjacent vertices of V1are V2,V8,V3 .Now pop the vertex V1 from the
stack and place it in output and push any one of the adjacent vertex to V1 into
the stack. Repeat this procedure of steps for the remaining vertices.
Step 3: Find adjacent vertices of V2are V1, V4, V5 and V8 .Vertex V1 is already
visited so don’t consider the adjacent vertex V1 and take any adjacent vertex
for the next step and say V8.Now pop the vertex V2 from the stack and place it
in output and push adjacent vertex V8 into the stack and mark V8 as visited .
10
Step 4: Find adjacent vertices of V8 are V1,V2,V3 and V8 .Vertex V1 and V2 are
already visited so don’t consider them and take any adjacent vertex for the
next step and say V3.Now pop the vertex V8 from the stack and place it in
output and push adjacent vertex V3 into the stack and mark V3 as visited .
Step 5: Find adjacent vertices of V3 are V1,V4,V6 and V8 .Vertex V1 and V8 are
already visited so don’t consider them and take any adjacent vertex for the
next step and say V6.Now pop the vertex V3 from the stack and place it in
output and push adjacent vertex V6 into the stack and mark V3 as visited .
Step 8: Find adjacent vertices of V5 are V2,V4 and V7 .Vertex V2 and V7 are already
visited so don’t consider it and take adjacent vertex V4 for the next step. Now
pop the vertex V5 from the stack and place it in output and push adjacent
vertex V4 into the stack and mark V4 as visited.
Step 9: As all the vertices in the graph are visited, now pop the remaining
vertex V4 from the stack and place it in output
12
The traversal path for the given graph by using DFS is
V1 – V2 – V8 - V3 – V6 – V7 – V5 – V4
Q3. Describe BFS algorithm with example.
BFS (Breadth First Search)algorithm is a graph traversal algorithm where we
will start searching or finding the path from a starting or source vertex to the
ending or destination vertex breadth wise or horizontally from top to bottom.
In BFS algorithm we will use Queue data structure to search the path because
we search the path horizontally from top to bottom. We insert the elements
at rear of the queue and remove it from front of the queue.
The algorithm works by taking a source vertex and finding its adjacent
vertices, inserting them into the marking them as visited or known. This
procedure is repeated until all the vertices are visited in the graph.
Algorithm:
Suppose a graph of vertices v1, v2, v3, v4........vn is given as below.
Step 1: Start the algorithm from vertex v1 and mark this vertex V1 as visited in
the array as ‘1’, if not visited mark as ‘0’ and push the vertex V1 into rear of
the queue.
Step 2: Find adjacent vertices of V1 are V2,V3,V4 and V8 . and mark them as
visited in the array. Now remove the vertex V1 from the front of the queue
and place it in output. Insert V2,V3,V4 and V8 into rear of the queue.
13
Step 3: Consider vertex V2, Find adjacent vertices of V2 are V8,V3,V4, V1 and V5 .
and mark them as visited in the array. Now remove the vertex V2 from the
front of the queue and place it in output. Insert V5 into rear of the queue.
Step 4: Consider vertex V8, Find adjacent vertices of V8 are V8,V3,V4, V1 and V5
and mark them as visited in the array. Now remove the vertex V8 from the
front of the queue and place it in output.
Step 1: The initial configuration of the distance matrix for the given graph is
Step 2: Choose vertex V1 as root of the spanning tree. Find the adjacent
vertices of V1 are V2,V3 and V4. Mark V1 as Known in the table and write Pv
values for V2,V3 and V4 as V1 in the table. Write the dv values for V2(V2 - V1 is 2),
V3(V3 - V1 is 4) and V4 (V4 - V1 is 1) as 2,4 and 1 in the table. Select the vertex
with least dv value for unknown vertices in the next step, So choose V4.
17
Step 3: Choose vertex V4 , find the adjacent vertices of V4 and all vertices are
adjacent to it . So mark V4 as Known in the table and write dv values to all
vertices from V4 in the table such that if new dv value is a smaller value for the
vertex in the table of previous step, then only update dv value otherwise keep
the dv value as it is and write the pv values for the vertex selected. For
example for V2 the edge V4 - V2 is 3,but already we have a smaller value of 2
present for V2 in the previous table so don’t update dv , V3(V3 - V1 is 4) and for
V3 the edge V4 - V3 is 2 whereas in the previous table it is 4,So update dv as 2 for
V3. Select the vertex with least dv value for unknown vertices in the next step,
So choose V2.
Step 4: Choose vertex V2. Find the adjacent vertices of V2 are V1,V4 and V5. So
mark V2 as Known in the table and write or update dv values to all vertices
from V2 in the table. Select the vertex with least dv value for unknown vertices
in the next step, So choose V3.
Step 5: Choose vertex V3 , Find the adjacent vertices of V3 are V1,V4 and V6. So
mark V3 as Known in the table and write or update dv values to all vertices
from V3 in the table. Select the vertex with least dv value for unknown vertices
in the next step, So choose V7.
18
Step 6: Choose vertex V7 , Find the adjacent vertices of V7 are V5,V4 and V6. So
mark V7 as Known in the table and write or update dv values to all vertices
from V7 in the table. Select the vertex with least dv value for unknown vertices
in the next step, So choose V6.
Step 7: Choose vertex V6 , Find the adjacent vertices of V6 are V3,V4 and V7. So
mark V6 as Known in the table and write or update dv values to all vertices
from V6 in the table. Select the vertex with least dv value for unknown vertices
in the next step, So choose V5.
Step 7: Choose vertex V5 , Find the adjacent vertices of V5 are V2,V4 and V7. So
mark V5 as Known in the table and no update of dv values in the table. The only
dv value for unknown vertices is 6 for the edge V5-V7, so add this remaining
edge to the spanning tree.
1) Arrange all the edges with their weights in the ascending order.
2) Choose the edge with minimum weight 1(V1-V4) as the first edge of the
spanning tree. This edge is considered as accepted edge because assigning
this edge in spanning tree does not form a cycle.
3) Choose the edge with next minimum weight 1(V6-V7) as the next edge of
the spanning tree and it is accepted because it does not forms a cycle.
20
4) Choose the edge with next minimum weight 2(V1-V2) as the next edge of
the spanning tree and it is accepted because it does not forms a cycle.
5) Choose the edge with next minimum weight 2(V3-V4) as the next edge of
the spanning tree and it is accepted because it does not forms a cycle.
6) Choose the edge with next minimum weight 3(V2-V4) as the next edge of
the spanning tree, but it is rejected because it forms a cycle.
7) Choose the edge with next minimum weight 4(V1-V3) as the next edge of
the spanning tree, but it is rejected because it forms a cycle.
8) Choose the edge with next minimum weight 2(V4-V7) as the next edge of
the spanning tree and it is accepted because it does not forms a cycle.
9) Choose the edge with next minimum weight 5(V3-V6) as the next edge of
the spanning tree, but it is rejected because it forms a cycle.
10) Choose the edge with next minimum weight 6(V5-V7) as the next edge of
the spanning tree and it is accepted because it does not forms a cycle.
21
11) The edges with next minimum weights 7, 8 and 10 are rejected because
all of them form a cycle in the graph.
So the final minimum spanning tree is given below and its cost is 16
(1+1+2+2+4+6).
Q5. Write the differences between Prim’s algorithm and Kruskal’s algorithm.
The differences between Prim’s algorithm and Kruskal’s algorithm
Prim’s algorithm Kruskal’s algorithm
1.Prim’s algorithm constructs a 1. Kruskal’s algorithm constructs a
minimum spanning tree by adding a minimum spanning tree by adding
vertex at a time one edge at a time.
2.The time complexity of prim’s 2.The time complexity of Kruskal’s
algorithm is O(E+V log V) algorithm is O(E log V)
3.Prim’s algorithm selects the edges 3. Kruskal’s algorithm selects edges
only from the set of edges that are from the set of all possible edges of a
adjacent to the vertices graph in ascending order.
4. Prim’s algorithm works better (or 4. Kruskal’s algorithm works better (or
is chosen) when we have more is chosen) when we have more
number of vertices in a graph. number of edge and less number of
vertices in a graph.
Class work:
1) Define minimum spanning tree.
Homework:
1) -------------- algorithm builds spanning tree by adding one edge at a time.
Previous JNTUK Questions:
1) Explain Prim’s algorithm with an example. Give analysis of prim’s algorithm
(JNTUK-Regular- April/May - 2016– SET -4 [10M])
2) Explain Kruskal’s algorithm with example. (JNTUK-Regular- April/May -
2016– SET -3 [8M])
3) What is a minimum spanning tree? Explain with an example, Kruskal’s
algorithm for constructing a minimum cost spanning tree. (JNTUK-Regular-
May/June - 2015– SET -2 [8M])
Day 37
Topics to be covered: Single-source shortest path algorithm–Dijkstra’s
algorithm.
Q1. What is single source shortest path?
A Single-source shortest path is defined as finding the path from a single
source vertex to all the other vertices in the given graph with lowest cost.
Q2. Describe Dijkstra’s algorithm with an example.
22
Dijkstra’s algorithm is used to solve the single source shortest path problem.
Dijkstra’s algorithm is an example of greedy method.
In this algorithm we assume all the weights attached are positive and a
digraph is taken for this algorithm.
Step 1: Select the source vertex V from which the shortest path has to be
calculated.
Step 2: Find adjacent vertices to the vertex V that has the shortest(smallest)
dv value among all vertices and mark that vertex as known by putting a value
‘1’ in the Known column of the table.
Step 3: Repeat this procedure of selecting the adjacent vertices and finding
the shortest through this vertex until all the vertices are covered in dv of the
table.
Example:
Step 1: Select the source vertex as V1, but the value of dv and pv are taken as 0
to indicate that it is a source vertex. Find adjacent vertices to V1 are V2 and V3
The distances are V1 – V2 = 2
V1 – V4 = 1
Update the dv values for V2 and V4 as 2 and 1 in the table.
From the table choose the least dv value i.e. 1 for vertex V4 and choose V4 as
vertex for the next step.
Step 2: Select the vertex V4, find adjacent vertices to V4 are V3, V6,V5 and V7
The distances (dv) are for V3 = 3 (V1 – V4 – V3 ) (1+2= 3)
for V5 = 3 (V1 – V4 – V5 ) (1+2= 3)
23
for V6 = 9 (V1 – V4 – V6 ) (1+8= 9)
for V7 = 5 (V1 – V4 – V6 ) (1+4= 5)
So update the values of dv for V3, V5, V6, V7 because they have ∞ values for V3,
V5, V6, V7 in the previous table. Mark Vertex V4 as known in the table.
From the table choose the least dv value for unknown vertices i.e. 2 for vertex
V2 and choose V2 as vertex for the next step.
Step 3: Select the vertex V2, find adjacent vertices to V2 are V4 and V5
The distances (dv) are for V4 = 3 (V2 – V4)
for V5 = 10 (V2 –V5 )
But vertex V4 is already known. So do not update the values of dv for V4, and V5
because they have least values for V4, V5 have 1 and 3 in the previous table.
Mark Vertex V2 as known in the table.
From the table choose the least dv value for unknown vertices i.e. 3 for vertex
V5 and choose V5 as vertex for the next step.
Step 4: Select the vertex V5, find adjacent vertices are V7
The distances (dv) are for V7 = 6 (V5 – V7)
So do not update the values of dv for V7, because they have least value of 5 for
V7.
24
From the table choose the least dv value for unknown vertices i.e. 3 for vertex
V3 and choose V3 as vertex for the next step
Step 5: Select the vertex V3, find adjacent vertices are V6
The distances (dv) are for V6 = 8 (V1 – V4 – V3 – V6) = (1+2+5= 8)
So update the values of dv for V6 as 8 in the table.
From the table choose the least dv value for unknown vertices i.e. 5 for vertex
V7 and choose V7 as vertex for the next step.
Step 6: Select the vertex V7, find adjacent vertices are V6
The distances (dv) are for V6 = 6 (V1 – V4 – V7 – V6) = (1+4+1= 6)
So update the values of dv for V6 as 6 in the table.
From the table choose the least dv value for unknown vertices i.e. 6 for vertex
V6 and is the only vertex not visited, choose V6 as vertex for the next step.
Step 7: Select the vertex V6, find adjacent vertices to V6, There are no adjacent
vertices to V6. Mark as known in the table
Hence the shortest path from vertex V1 to all vertices (single source shortest
path) for the given graph is shown below.
25
Vertex Path Shortest distance
V2 V1 – V2 2
V3 V1 – V4 – V3 3
V4 V1 – V4 1
V5 V1 – V4 – V5 3
V6 V1 – V4 – V7 – V6 6
V7 V1 – V4 – V7 5
Class work:
1) What is single-source shortest path?
Home work:
1) -------------- algorithm is used to solve the single-source shortest path
problem.
Previous JNTUK Questions:
1) How to find shortest path between two vertices using Dijkstra’s
algorithm? (JNTUK-Regular- April/May - 2016– SET -2 [8M])
2) Explain single source shortest path problem with an example. (JNTUK-
Regular- May/June - 2015– SET -1 [8M])
3) Develop an algorithm to compute the shortest path using Dijkstra’s
algorithm. Validate the algorithm with a suitable example. (JNTUK-
Regular- May/June - 2015– SET -3 [16M])
Day 38
Topics to be covered: All pairs shortest path algorithms: Warshall’s algorithm
and Floyd’s algorithm
Q1. Define Transitive closure of a graph.
Consider a graph G=(V,E) and has 3 vertices x, y and z, if there are 2 edges x
y , yz , then we can derive a new edge x z. A graph containing all the edges of
this nature is called transitive closure of given graph G.
Q2. Name the algorithms used to find the all pairs shortest path of a graph
All pairs shortest path can be found out by using the following algorithms
i) Warshall’s algorithm ii) Floyd’s algorithm
Q3. Describe an algorithm to find transitive closure of a graph.
Warshall’s algorithm is used to find the transitive closure of a given graph by
constructing a Boolean matrix called Reachability matrix(R) of the given
graph.
In Warshall’s algorithm, we construct a sequence of Boolean matrices
R(0),R(1),R(2) ...............R(n)
Step 1 : For the given graph ‘G’, find the adjacency matrix of graph G and call
this adjacency matrix as R(0).
26
Step 2 : Find R from R and R represents reachability matrix in which only
(1) (0) (1)
intermediate vertex. D(2) represents the distance matrix to find the shortest
path with two intermediate vertices. D(3) represents the distance matrix to
find the shortest path with three intermediate vertices.
Finally D(n) represents the distance matrix that gives the all pairs shortest
(shortest distance or path between every pair of vertices)
‘C’ Code for Floyd’s algorithm:
int Floyd’s algorithm( distance matrix A[n][n] )
{
Input : Distance matrix A[n][n] of given graph.
Output : All pairs shortest path of the given graph.
D(0) = distance matrix A[n][n];
for(i=1; i<=n; i++)
{
for(j=1; j<=n; j++)
{
for(k=1; k<=n; k++)
{
D (k) [i][j]= min {D (k-1) [i][j] , (D(k-1) [i][k] + D(k-1) [k][j] } ;
}
}
}
Return D (k) [i][j] ;
}
Example: Consider graph given below