Dr.
PAVITHRA L K, VIT Chennai 1
A graph is an abstract data structure to implement
mathematical concepts of graphs.
Graph is a collection of vertices( also called nodes)
and edges that connect the vertices.
Graph is often viewed as a generalization of tree
structure.
Definition
A graph G is defined as an ordered set of
(V,E), where V represents the set of vertices and E
represents the edges that connect these vertices.
Dr. PAVITHRA L K, VIT Chennai 3
A E
C D
Directed Graph
Dr. PAVITHRA L K, VIT Chennai 4
A E
C DD
Undirected Graph
Dr. PAVITHRA L K, VIT Chennai 5
Graph G = (V, E )
V – Vertices (nodes)
E – Edges (arcs)
Number of vertices → n = |V|
Number of Edges → e = |E|
Dr. PAVITHRA L K, VIT Chennai 6
Terminologies – Contd..
Two types of graphs are
Directed / Digraph - If all the edges are directed
Undirected – If all the edges are undirected
Connected Graph – If there is a simple path between
any two nodes of a graph G.
Adjacent nodes/ Neighbors – For every edge e=(u,v)
that connects u and v, u and v are the endpoints known
as adjacent nodes.
Dr. PAVITHRA L K, VIT Chennai 7
Terminologies – Contd..
Cycle – A path in which the first and last vertices are
same.
Complete Graph – If all the nodes of graph G are
fully connected.
Multi graph – a multigraph is a graph which is
permitted to have multiple edges
Size of a graph – Total number of edges in it.
Dr. PAVITHRA L K, VIT Chennai 8
Minimum spanning tree
› Kruskal’s Algorithm
› Prim’s Algorithm
Shortest Path Problem
› Dijkstra’s Algorithm
Dr. PAVITHRA L K, VIT Chennai 9
Spanning Tree – A connected, undirected graph is a tree
whose vertex set is the same as the vertex set of a
given graph and whose edge set is a subset of the edge
set of the given graph.
› No Cycles
› Connected
Spanning tree represented as G’ = ( V’, E’)
V’ = V & E’ = |V| - 1
Minimum Spanning Tree – Spanning tree whose sum of
weight of all its edges is less than all other possible
spanning tree of graph G
Dr. PAVITHRA L K, VIT Chennai 10
Dr. PAVITHRA L K, VIT Chennai 11
a b
Dr. PAVITHRA L K, VIT Chennai 12
4
7
5 8
10
Dr. PAVITHRA L K, VIT Chennai 13
The minimum spanning tree for the given
weighted graph will be,
Dr. PAVITHRA L K, VIT Chennai 14
It’s a Greedy Algorithm – Follows the problem solving
approach of making the locally optimal choice at each
stage with the hope of finding the global optimum.
Kruskal’s Method
› Take a graph with n vertices.
› Keep on adding shortest edge until (n-1) edges have
been added.
› Insert an edge with the minimum weight.
› Insert edges in increasing order of cost.
› Reject if it forms a cyclic path.
Dr. PAVITHRA L K, VIT Chennai 16
4
A B
7
5 8
10
D C
2
Dr. PAVITHRA L K, VIT Chennai 17
Insert an edge with minimum weight/cost.
Edge DC have the minimum weight 2.
A B
D C
2
Dr. PAVITHRA L K, VIT Chennai 18
Insert edges in increasing order of weight.
Edge AB have the next minimum weight 4.
4
A B
D C
2
Dr. PAVITHRA L K, VIT Chennai 19
Include the next minimum weight node.
Check the cycles in the tree
4
A B
D C
2
Dr. PAVITHRA L K, VIT Chennai 20
Check the condition e = n -1
e → number of edges
n → number of vertices
If the condition satisfied then stop the process.
The Kruskal’s minimum cost spanning tree will be,
4
A B
D C
2
Dr. PAVITHRA L K, VIT Chennai 21
Step 1: Remove all the loops and parallel edges from
the graph.
Step 2: Choose any arbitrary vertex as root node.
Step 3: Select an edge e1 has the minimum weight
among the edges incident to root node.
Step 4: Follow the step 3for the newly added vertex.
Step 5: Repeat till n-1 edges have been added.
Dr. PAVITHRA L K, VIT Chennai 22
A 1
6 D
5
B 3
7
2 E
C
4
Dr. PAVITHRA L K, VIT Chennai 23
A 1
6 D
5
B 3
7
2 E
C
4
Dr. PAVITHRA L K, VIT Chennai 24
A 1
6 D
5
B 3
7
2 E
C
4
Dr. PAVITHRA L K, VIT Chennai 25
A 1
6 D
5
B 3
7
2 E
C
4
Dr. PAVITHRA L K, VIT Chennai 26
A 1
D
6 5
B 3
7
2 E
C
4
Dr. PAVITHRA L K, VIT Chennai 27
A 1
6 D
5
B 3
7
2 E
C
4
Dr. PAVITHRA L K, VIT Chennai 28
y 5 o
4
7
3 q
6 2
x
2
8 z p
3
Dr. PAVITHRA L K, VIT Chennai 29
2
E F
4
1 3 10
7
2
D A G
5 8 4
10
B C
1
Dr. PAVITHRA L K, VIT Chennai 30
Activity selection problem
Huffman Coding
Job sequencing Problem
Fractional Knapsack problem
Dr. PAVITHRA L K, VIT Chennai 31
Network Design
› Telephone
› Electrical
› Hydraulic
› TV cable
› Computer
› Road
Approximation Algorithm
› Travelling Salesman Problem
› Steiner Tree
Dr. PAVITHRA L K, VIT Chennai 32
Widest path problem
LDPC codes for error correction
Image registration with Renyi entropy
Learning salient features of face verification
Reducing data storage in sequencing amino acids in
a protein
Cluster analysis
Dr. PAVITHRA L K, VIT Chennai 33
Finding a path between two vertices ( nodes) in a
graph such that the total number of edge’s weight is
minimum.
This problem can be solved by BFS ( Breadth First
Search) Algorithm.
The various algorithms are,
› Dijkstra’s Algorithm
› Bellman Ford’s Algorithm
› Floyd Warshall’s Algorithm
Dr. PAVITHRA L K, VIT Chennai 34
Single source shortest path algorithm.
It finds the shortest path on a graph, where all the
edges are non-negative.
Given a graph G and the source node S, the
algorithm used to find the shortest path between
the source node S and every other node.
Used to find the length of the optimal path between
two nodes in a graph.
Dr. PAVITHRA L K, VIT Chennai 35
Select the source/ initial node S.
Set all the vertices distances (d) as infinity except
the source vertex S. Set the source vertex S as 0.
Find the adjacent nodes of the source node; update
distance values (d) to the new nodes.
Determine a node t, that has the minimal distance
among other adjacent nodes.
Change that node as visited and explore the
adjacent nodes.
If all the nodes can be visited from the source node
S, then stop.
Dr. PAVITHRA L K, VIT Chennai 36
If d(u)+c(u,v)<d(v)
› d(v)= d(u)+c(u,v)
Worst case time complexity
O(n*n)
Dr. PAVITHRA L K, VIT Chennai 37
60
A B
50
10 20 C
20
D 50
E
10
Dr. PAVITHRA L K, VIT Chennai 38
60
A B
50
10 20 C
20
D 50
E
10
Dr. PAVITHRA L K, VIT Chennai 39
A B C D E
Dr. PAVITHRA L K, VIT Chennai 40
The shortest path to every other vertices from
source vertex A is
› A →D →B
› A→D
› A→D→E
› A→D→E→C
Two lists will be maintained
› Visited vertices
› Non-visited vertices
Dr. PAVITHRA L K, VIT Chennai 41
Vertex C doesn’t have any unvisited
neighbors.
Add the current vertex to the list of
visited vertices.
Dr. PAVITHRA L K, VIT Chennai 42
1
Y Z
10
X 2 3 9
6
W V
2
Dr. PAVITHRA L K, VIT Chennai 43
Let distance of start vertex from start vertex = 0
Let distance of all other vertices from start vertex = ∞
(infinity)
Repeat
› Visit the unvisited node with smallest distance from
start node
› For the current vertex, examine the unvisited neighbors.
Dr. PAVITHRA L K, VIT Chennai 44
Algorithm – Contd..
› For the current vertex calculate the distance of
each neighbor from start node
› If calculated distance is less than the known
distance, update the shortest distance
› Update the previous vertex of for each updated
distances
› Add the current vertex to the list of visited nodes
Until all vertices visited.
Dr. PAVITHRA L K, VIT Chennai 45
For Practice
Dr. PAVITHRA L K, VIT Chennai 46
Problem 1
1
V5
3 4 8
4
6
V4 V7
4 3
7
V3 V6
Dr. PAVITHRA L K, VIT Chennai 47
D
A C
B
D
D
C
A
B
Questions Please..
Dr. PAVITHRA L K, VIT Chennai 50
Dr. PAVITHRA L K, VIT Chennai 51