Unit 5 - DS - AK2 - Graph
Unit 5 - DS - AK2 - Graph
Dr.S.Nagendra Prabhu
Unit - 5
Graph
Introduction to Graph, Graph Traversal, Topological
sorting, Minimum spanning tree – Prims Algorithm,
Kruskal’s Algorithm, Shortest Path Algorithm -
Dijkstra’s Algorithm
Introduction to Graph
• Definition
• A graph can be defined as group of vertices and edges
that are used to connect these vertices.
• A graph G can be defined as an ordered set G(V, E)
where V(G) represents the set of vertices and E(G)
represents the set of edges which are used to connect
these vertices.
• A Graph G(V, E) with
• 5 vertices (A, B, C, D, E) and
• six edges ((A,B), (B,C), (C,E), (E,D), (D,B), (D,A))
shown in the following figure.
• Types of Graph
A A
B C B C
D E D E
A A
A
B C B C
B C
E D E
D D E
F
F F
• Labeled graph or weighted graph: A graph is said to be
labeled if every edge in the graph is assigned some
value (weight).
• In a weighted graph, the edges of the graph are assigned
some weight or length.
• The weight of an edge denoted by w(e) is a positive value
which indicates the cost of traversing the edge.
Example:
• Multiple edges (also called parallel edges or a multi-
edge), are two or more edges that are incident to the
same two vertices.
• A simple graph has no multiple edges.
Strongly connected directed graph: A digraph is said to
be strongly connected if and only if there exists a path
between every pair of nodes in G.
That is, if there is a path from node u to v, then there must
be a path from node v to u.
Graph Representation
• Graph representation, we simply mean the
technique which is to be used in order to store
some graph into the computer's memory.
• There are two ways to store Graph into the
computer's memory
• 1. Sequential Representation
• 2. Linked Representation
Sequential Representation
• In sequential representation, use adjacency matrix to
store the mapping represented by vertices and edges.
• In adjacency matrix, the rows and columns are
represented by the graph vertices. A graph having n
vertices, will have a dimension n x n.
• If there is an edge from row vertex to column vertex it is
given as 1 otherwise 0.
A directed graph and its adjacency matrix
representation is shown in the following figure.
Representation of weighted directed
graph
• It is different. Instead of filling the entry by 1, the Non-
zero entries of the adjacency matrix are represented by
the weight of respective edges.
• Representation of weighted directed graph is different.
Instead of filling the entry by 1, the Non- zero entries of
the adjacency matrix are represented by the weight of
respective edges.
Linked Representation
• In the linked representation, an adjacency list is used to
store the Graph into the computer's memory.
• Consider the undirected graph shown in the following
figure and check the adjacency list representation.
Undirected graph for the adjacency list
representation
• An adjacency list is maintained for each node
present in the graph which stores the node
value and a pointer to the next adjacent node
to the respective node.
• If all the adjacent nodes are traversed then
store the NULL in the pointer field of last node
of the list. The sum of the lengths of adjacency
lists is equal to the twice of the number of
edges present in an undirected graph.
Consider the directed graph and check the
adjacency list representation of the graph.
• In a directed graph, the sum of lengths of all the
adjacency lists is equal to the number of edges present
in the graph.
• In the case of weighted directed graph, each
node contains an extra field that is called the
weight of the node. The adjacency list
representation of a directed graph is shown in
the following figure.
Traversing a Graph
• The graph is one non-linear data structure. That is consists of
some nodes and their connected edges. The edges may be
directed or undirected. This graph can be represented as G(V,
E). The following graph can be represented as G({A, B, C, D,
E}, {(A, B), (B, D), (D, E), (B, C), (C, A)})
Brinleigh 5
Cornwell
3
4
8 6
8
Avonford Fingley Donster
7
5
4
2
Edan
We model the situation as a network, then the problem
is to find the minimum connector for the network
B 5
C
3
4
8 6
8
A F D
7
5
4
2
E
Kruskal’s Algorithm
E
Kruskal’s Algorithm
8
A D
7 F
5
4
2
E
Kruskal’s Algorithm
3 ED 2
4 AB 3
8 6
8
A D
7 F
5
4
2
E
Kruskal’s Algorithm
3
ED 2
4 AB 3
8 6
CD 4 (or AE 4)
8
A D
7 F
5
4
2
E
Kruskal’s Algorithm
3
ED 2
4 AB 3
8 6
CD 4
AE 4
8
A D
7 F
5
4
2
E
Kruskal’s Algorithm
3
ED 2
4 AB 3
8 6
CD 4
AE 4
8
BC 5 – forms a cycle
A D
7 F EF 5
5
4
2
E
Kruskal’s Algorithm
5
4 Total weight of tree: 18
2
E
Kruskal’s Algorithm
5
4 Total weight of tree: 18
2
E
Kruskal Algorithm
Algorithm
• Remove all loops & Parallel Edges from the given graph
• Pick the smallest edge. Check if it forms a cycle with the spanning
tree formed so far. If cycle is not formed, include this edge. Else,
discard it.
• Repeat step#2 until there are (V-1) edges in the spanning tree.
Kruskal Algorithm
Kruskal
Algorithm
Find a MST using Kruskal Algorithm
Kruskal algorithm to create MST
Algorithm
MST-KRUSKAL(G, w)
A←Ø
for each vertex v V[G]
do MAKE-SET(v)
sort the edges of E into nondecreasing order by weight w
for each edge (u, v) E, taken in nondecreasing order by weight
do if FIND-SET(u) ≠ FIND-SET(v)
then A ← A {(u, v)}
UNION(u, v)
return A
Algorithm Analysis
• Time complexity
• O(nlogn) where n is the number of unique characters.
Prim's Spanning Tree Algorithm
• Prim's algorithm to find minimum cost spanning
tree (as Kruskal's algorithm) uses the greedy
approach. Prim's algorithm shares a similarity with
the shortest path first algorithms.
•
0 ∞
2
A B
4 1 3 10
2 2 ∞
∞ C D E
5 8 ∞ 4 6
1
F G
∞ ∞
0 2
2
A B
1
4 3 10
2 2 ∞
∞ C D E
5 8 1 4 6
Distance(B) = 2 1
F G
Distance(D) = 1
∞ ∞
Now, select the node with minimum distance. Weights are 2 and 1 hence 1 is
selected i.e., D is chosen
0 2
2
A B
4 1 3 10
2 2
∞ C D E ∞
5 8 1 4 6
1
F G
∞ ∞
Neighbors of D are selected and their distances are updated.
Distance of C = Distance of D + weight of D to C = 1+ 2=3
Distance of E = Distance of D + weight of D to E = 1+ 2=3
Distance of F = Distance of D + weight of D to F = 1+ 8=9
Distance of G = Distance of D + weight of D to G = 1+ 4=5
0 2
2
A B
4 1 3 10
2
2
3 C D E 3
5 8 1 4 6
1
F G
9 5
Pick vertex in List with minimum distance and update neighbors.
B has the minimum distance. Neighbours of B are D and E.
Distance of D is not updated since D is already visited.
Previous distance of E = 3
New distance of E = 2+10 = 12
Distance of E not updated since it is larger than previously computed.
0 2
2
A B
4 1 3 10
2 2
3 C D E 3
5 8 1 4 6
1
F G
9 5
Pick vertex List with minimum distance and update neighbors. E is selected.
Distance of G not updated since it is larger than previously computed.
0 2
2
A B
4 1 3 10
2 2
3 C D E 3
5 8 1 4 6
1
F G
9 5
Pick vertex List with minimum distance and update neighbors.
C is selected. Neighbour is F.
Distance of F = Distance of C + weight of C to F = 3 + 5 = 8
0 2
2
A B
4 1 3 10
2 2
3 C D E 3
5 8 1 4 6
1
F G
8 5
Pick vertex List with minimum distance and update neighbors
G is selected. F is the neighbour.
Previous distance of F is 8.
Distance of F = Distance of G+weight from G to F = 5+1
Update the distance of F as 6.
0 2
2
A B
4 1 3 10
2 2
3 C D E 3
5 8 1 4 6
1
F G
6 5
F is selected and there are no neighbors to F. This is resultant the shortest path graph
0 2
2
A B
4 1 3 10
2 2
3 C D E 3
5 8 1 4 6
1
F G
6 5