Define Basic Graph Terminologies: Lecture Notes
Define Basic Graph Terminologies: Lecture Notes
Define Basic Graph Terminologies: Lecture Notes
asia
Lecture Notes
UNIT V - GRAPHS
Representation of Graphs – Breadth-first search – Depth-first search – Topological sort –
Minimum Spanning Trees – Kruskal and Prim algorithm – Shortest path algorithm –
Dijkstra’s algorithm – Bellman-Ford algorithm – Floyd - Warshall algorithm.
o In adjacency list, for each vertex, keep a list of all adjacent vertices.
o The space requirement is O(|E| + |V|).
o If the edges have weights, then it is also stored in the adjacency lists.
jntuworldupdates.org Specworld.in
Smartzworld.com Smartworld.asia
Lecture Notes
jntuworldupdates.org Specworld.in
Smartzworld.com Smartworld.asia
Lecture Notes
jntuworldupdates.org Specworld.in
Smartzworld.com Smartworld.asia
Lecture Notes
Example
jntuworldupdates.org Specworld.in
Smartzworld.com Smartworld.asia
Lecture Notes
Legal orderings
1) SSLCÆHSSÆScienceÆMaster
2) SSLCÆHSSÆArtsÆMaster
3) SSLCÆHSSÆEnggÆMaster
4) SSLCÆDiplomaÆ Engg ÆMaster
Improved Algorithm
o After initial scanning, only those vertices whose updated indegrees have become zero are
scanned.
1. Store each vertex’s In-Degree in an array
2. Initialize a queue with all in-degree zero vertices
3. While there are vertices remaining in the queue:
a. Dequeue and output a vertex
b. Reduce In-Degree of all vertices adjacent to it by 1
c. Enqueue any of these vertices whose In-Degree became zero
4. The topological ordering then is the order in which the vertices dequeue.
jntuworldupdates.org Specworld.in
Smartzworld.com Smartworld.asia
Lecture Notes
jntuworldupdates.org Specworld.in
Smartzworld.com Smartworld.asia
Lecture Notes
Graph MST
jntuworldupdates.org Specworld.in
Smartzworld.com Smartworld.asia
Lecture Notes
jntuworldupdates.org Specworld.in
Smartzworld.com Smartworld.asia
Lecture Notes
o The edges in the spanning tree is read from the table: (v2, v1), (v3, v4), (v4, v1), (v5,
v7), (v6, v7), (v7, v4). The total cost is 16.
o The running time is O(|E| log |V|)
jntuworldupdates.org Specworld.in
Smartzworld.com Smartworld.asia
Lecture Notes
o The resultant minimum spanning tree decision pertaining to each edge is shown above.
o The running time of this algorithm is O(|E| log |V|).
Pseudocode
vector<Edge> kruskal(vector<Edge> edges, int numVertices)
{
DisjSets ds{ numVertices };
priority_queue pq{ edges };
vector<Edge> mst;
while( mst.size() != numVertices - 1 )
{
Edge e = pq.pop( ); // Edge e = (u, v)
SetType uset = ds.find( e.getu());
SetType vset = ds.find( e.getv());
if( uset != vset )
{
mst.push_back(e); // Accept the edge
ds.union(uset, vset);
}
}
return mst;
}
Using Dijkstra’s algorithm, find the shortest path from the source to all other nodes of
the graph 'G’
o Dijkstra’s algorithm is widely used to solve the single-source shortest-path problem.
o It is a greedy algorithm that attempts to solve a problem in stages by doing what is the
jntuworldupdates.org Specworld.in
Smartzworld.com Smartworld.asia
Lecture Notes
Example
o For each vertex, three pieces of information is tracked.
o Distance from source vertex s to vertex v is kept in dv.
o Entry in pv is the bookkeeping variable, that enables to print the actual paths.
o Entry known is set to true and the vertex is processed, i.e., dv and pv values for
adjacent vertices are updated.
o The graph to be considered and the initial configuration with start node as v1.
o Initially all vertices are unreachable except for s, whose path length is 0.
o The first vertex selected is v1, with path length 0 and is marked as known.
o Vertices adjacent to v1 are v2 and v4.
o pv value for these vertices is set to v1.
o dv value for v2 and v4 are updated to 2 and 4 respectively
o Next, v4 is selected, since it has minimum dv amongst unknown vertices and is marked
as known. Entries for adjacent vertices v3, v5, v6, and v7 are updated.
jntuworldupdates.org Specworld.in
Smartzworld.com Smartworld.asia
Lecture Notes
o Next, v2 is selected.
o v4 is adjacent but already known, hence no changes are made.
o v5 is adjacent but not adjusted, since the cost of going through v2 is 2 + 10 = 12
against the existing path of length 3 is already known.
o Next vertex selected is v5 at cost 3. v7 is the only adjacent vertex, but it is not updated,
because 3 + 6 > 5.
o Next v3 is selected, and the distance for v6 is adjusted to 3 + 5 = 8
jntuworldupdates.org Specworld.in
Smartzworld.com Smartworld.asia
Lecture Notes
o Finally, v6 is selected and the algorithm terminates as all vertices are known.
jntuworldupdates.org Specworld.in
Smartzworld.com Smartworld.asia
Lecture Notes
o To check for cycles, after completing n-1 iterations, simply scan all edges one more time
to see if there is a vertex that could still be improved.
Example
jntuworldupdates.org Specworld.in
Smartzworld.com Smartworld.asia
Lecture Notes
o Iteration 1:
S A B C T
d[v] 0 7 6 4 2
p[v] S S A B
o Iteration 2:
S A B C T
d[v] 0 7 2 4 -2
p[v] S C A B
o Iteration 3:
S A B C T
d[v] 0 7 2 4 -2
p[v] S C A B
o Since, there is no change in distance after iteration 2, the algorithm terminates.
o Since the algorithm stabilizes after few iteration, there is no negative cycle in the graph.
o Shortest path from vertex S to T is S Æ A Æ C Æ B Æ T
Pseudocode
Bellman-Ford (G,w,s)
{
For every vertex v
d[v] = INFINITY
d[s] = 0
For i=1 to |V|-1 do
{
For every edge (u,v) in E do // Relaxation
{
If d[v]>d[u]+w(u,v) then
{
d[v]=d[u]+w(u,v)
parent[v] = u
}
}
}
For every edge (u,v) in E do
If d[v]>d[u]+w(u,v) then
Return NEGATIVE CYCLE
Return d[], parent[]
}
jntuworldupdates.org Specworld.in
Smartzworld.com Smartworld.asia
Lecture Notes
Algorithm
o Initialize distance matrix D as adjacency matrix of the given weighted graph
o Initialize path matrix P as p[i][j] = j if vertex j is adjacent to vertex i.
o At each step, find out whether or not using vertex k will improve an estimate between the
distances between vertex i and vertex j. If it improves
o record the new shortest path weight between i and j
o record the fact that the shortest path between i and j goes through k
o Final D matrix contains all the shortest paths and final P matrix is used to reconstruct the
shortest path between any pair of vertices.
Example
0 4 7 Nil 2 3
1 0 2 1 Nil 3
6 ’0 1 Nil Nil
jntuworldupdates.org Specworld.in
Smartzworld.com Smartworld.asia
Lecture Notes
0 4 7 0 4 7 Nil 2 3
1 0 2 1 0 2 1 Nil 3
6 ’0 6 10 0 1 1 Nil
0 4 7 0 4 6 Nil 2 2
1 0 2 1 0 2 1 Nil 3
6 10 0 6 10 0 1 1 Nil
0 4 6 0 4 6 Nil 2 2
1 0 2 1 0 2 1 Nil 3
6 10 0 6 10 0 1 1 Nil
o For example path[1][3] = 2. This implies 2 is the vertex last visited before vertex 3. Thus the
path from vertex 1 to 3 is 1 Æ 2 Æ 3. The shortest distance from vertex 1 to 3 is 6.
Pseudocode
For k=1 to n
{
For i=1 to n
{
For j=1 to n
{
if (D[i][k]+D[k][j] < D[i][j])
{
D[i][j] = D[i][k]+D[k][j];
CS6301 Programming & Data Structures II Unit V Page 17
jntuworldupdates.org Specworld.in
Smartzworld.com Smartworld.asia
Lecture Notes
path[i][j] = path[k][j];
}
}
}
}
jntuworldupdates.org Specworld.in