Graph
Graph
Graph Representation
The following two are the most commonly used representations of a graph.
1. Adjacency Matrix
2. Adjacency List
Points to Remember-
In case of directed Graph, the sum of matrix is always equal to number of edges|E| or sum of in-degree of each vertex.
In above diagram
Number of edges= 7
Sum of matrix = 7
Number of edges = Sum of Matrix
Example (Undirected Graph)
Graph Adjacency matrix Representation
Points to Remember-
In case of undirected Graph, the sum of matrix is always equal to twice the number of edges i.e. 2|E| or sum of degree of
each vertex.
• All possible spanning trees of graph G, have the same number of edges and vertices.
• Removing one edge from the spanning tree will make the graph disconnected, i.e. the
spanning tree is minimally connected.
Minimum Spanning Tree (MST)
In a weighted graph, a minimum spanning tree is a spanning tree that has minimum
weight than all other spanning trees of the same graph. In real-world situations,
this weight can be measured as distance, congestion, traffic load or any arbitrary
value denoted to the edges.
We shall learn about two most important spanning tree algorithms here −
Kruskal's Algorithm
Prim's Algorithm
Total cost= 37
0
5
2
2
1 2
6 7
3
25 10
14
12 5
4
11 15
6
0
0
5 2
2
2
2 1 2
1 2
6
6 7
3
3
10
25 10
14
12 5
12 4
4 5
11
11 15 Total cost:43
6
6
Kruskal’s algorithm
Step1: set minimum spanning tree (MST) initially empty.
A=Ø
Step2: For each vertex V Ɛ V(G)
create (V) no. of tree where tree contain all vertex no edge.
Step3: Set the edges into ascending order according to their weights
and select the edge which has minimum weight.
Step4: if that forms cycles rejected it otherwise add it.
Step5: repeat until the single tree is formed.
Step 6: return MST.
Prims Algorithm
For a Graph G={V,E}
Assume S= Set of vertices in MST
A= Set of edges in MST
Step1: select any vertex ‘r’ and set S={r} and also set A= {Ø}
Step2: Find the smallest weight edge such that one end point in S and other in
{ V-S}.
Step3: Now add this edge into the set A and set another point into set S
Step 4:IF {V-S} =Ø then
Stop
and output is return i.e. MST
otherwise goto Step2.
0 Set of Vertices Edges Selected edges 0
5 in MST (less weight)
2 2
2 { 0} (0,1)(0,2)
1 2 2
6 7
3
25 10
14
12 5
4
11 15
6
0 Set of Vertices Edges Selected 0
5 in MST edges
2 (less 2
weight)
2 2
1 2 1 2
{ 0} (0,1)(0,2) (0,2)
6 7
3 {0,2} (0,1)(2,1)(2,3)(2,5) (2,1)
{0,2,1}
25 10
14
12 5
4
11 15
6
0 Set of Vertices Edges Selected 0
5 in MST edges
2 (less 2
weight)
2 2
1 2 1 2
{ 0} (0,1)(0,2) (0,2)
6 7 6
3 {0,2} (0,1)(2,1)(2,3)(2,5) (2,1)
3
14
12 5
4
11 15
6
0 Set of Vertices Edges Selected 0
5 in MST edges
2 (less 2
weight)
2 2
1 2 1 2
{ 0} (0,1)(0,2) (0,2)
6 7 6
3 {0,2} (0,1)(2,1)(2,3)(2,5) (2,1) 3
11 15 {0,1,2,3,4,5,6} - - 11
6 6
Total cost=2+2+6=10+12+11= 43
Single Source Shortest path
For a given Graph G= (V, E), we want to find a shortest path from a give
source vetex S Ɛ V o every vertex v Ɛ V . A single source shortest path
may be computed with the following algorithms.
1. Dijkstra's Algorithm
Dijkstra's Algorithm
Dijakstra’s algo solves the single source shortest path problem on a
weighted , directed graph G=(V,E) for the case in which all edge
weights are non negative.
The Dijaksta’s algorithm uses a greedy strategies i.e. it always select
the lightest or closest vertex.
Find the shortest path from source
vertex to all other Source
vertexvertex S=0
S=Finally contains vertex of final shortest path
Q= Priority queue used to store vertices based on
1 v0 their distance from source vertex
u0
10
o0 2
3
4 6
5 7
x2 y0
2
Find the shortest path from source
vertex to all other Source
vertexvertex S=0
S=Finally contains vertex of final shortest path
Q= Priority queue used to store vertices based on
1 v0 their distance from source vertex
u0
10
o0 3 Initially
2 4 S= { Ø}
6
Q= o u v x y
5 7 0 ∞ ∞ ∞ ∞
x2 y0
∞ 1 v0 ∞
Initially u0
10
S= { Ø} 0
0 3
2 4 6
Q= o u v x y
0 ∞ ∞ ∞ ∞ 5 7
x2 y0
Q= extract minQueue {Q} ∞
S={o} ∞ 2
∞ 1 v0 ∞
Initially u0
10
S= { Ø} 0
0 3
2 4 6
Q= o u v x y
0 ∞ ∞ ∞ ∞ 5 7
x2 y0
Q= extract minQueue {Q} ∞
S={o} ∞ 2
∞ 1 v0 ∞
u0
10
0
Q= u v x y
0 3
10 ∞ 5 ∞ 2 4 6
Q= extract minQueue {Q} 5 7
S={o, x}
2x y0
∞
5 2
S={o, x} ∞ 1 v0 ∞
u0
10
Q= u v y 0
8 ∞ 7 0 3
2 4 6
Q= extract minQueue {Q}
S={o,x,y} 5 7
2x y0
7
5 2
S={o, x} ∞ 1 v0 ∞
u0
10
Q= u v y 0
8 ∞ 7 0 3
2 4 6
Q= extract minQueue {Q}
S={o,x,y} 5 7
2x y0
7
5 2
Q= u v 8 ∞
1 v0
8 13 u0
Q= extract minQueue {Q} 10
0
S={o, x, y , u }
0 3
2 4 6
5 7
2x y0
7
5 2
8 9
S={o, x, y , u } 1 v0
u0
10
Q= v 0
9 0 3
2 4 6
Q= extract minQueue {Q}
5 7
S={o,x,y, u, v}
2x y0
7
5 2
1 v0
u0
0 3
5
2x y0
2
problem
solution
problem
solution
DIJKSTRA’S ALGO
Step1: Initialize single source vertex by zero.
Step2: Initialize S= {Ø} where S, finally contains vertices of final shortest
path from source vertex.
Step3: Initialize priority queue Q such that Q V(G)
Step 4: while priority queue (Q) is not empty do step 5 to 7
Step5: select vertex u of minimum value from Queue such that
u extract of minQueue
Step6: new S SU {u}
Step7: Insert all the adjacent nodes into Queue,assign their weights as
sum of weight of parent and edge weight.
Step8: End;
Transitive closure of a graph
• Given a directed graph, find out if a vertex j is reachable from another
vertex i for all vertex pairs (i, j) in the given graph. Here reachable
mean that there is a path from vertex i to j. The reach-ability matrix is
called the transitive closure of a graph.
Transitive closure of
graphs is
1 1 1 1
1 1 1 1
1 1 1 1
0 0 0 1
Floyd Warshall Algorithm, find the
shortest path distance between
every pair of vertices
Floyd Warshall Algorithm
Step1: We initialize the solution matrix same as the input graph matrix .
Step2: Then we update the solution matrix by considering all vertices as an
intermediate vertex. The idea is to one by one pick all vertices and updates
all shortest paths which include the picked vertex as an intermediate vertex
in the shortest path.
When we pick vertex number k as an intermediate vertex, we already have
considered vertices {0, 1, 2, .. k-1} as intermediate vertices. For every pair (i,
j) of the source and destination vertices respectively.
There are two possible cases:
Cae1:k is not an intermediate vertex in shortest path from i to j. We keep the
value of dist[i][j] as it is.
Case2: k is an intermediate vertex in shortest path from i to j. We update the
value of dist[i][j] as dist[i][k] + dist[k][j] if dist[i][j] > dist[i][k] + dist[k][j]