Graphs
Graphs
Introduction to Graphs
Graph is a non-linear data structure. It contains a set of points
known as nodes (or vertices) and a set of links known as edges (or
Arcs). Here edges are used to connect the vertices. A graph is
defined as follows...
11/24/2024 1
Graphs
Example
The following is a graph with 5 vertices and 6 edges.
This graph G can be defined as G = ( V , E )
Where V = {A,B,C,D,E} and E = {(A,B),(A,C)(A,D),(B,D),(C,D),
(B,E), (E,D)}.
11/24/2024 2
Graphs
Graph Terminology
We use the following terms in graph data structure...
Vertex
Individual data element of a graph is called as Vertex. Vertex is
also known as node. In above example graph, A, B, C, D & E are
known as vertices.
Edge
An edge is a connecting link between two vertices. Edge is also
known as Arc. An edge is represented as (startingVertex,
endingVertex). For example, in above graph the link between
vertices A and B is represented as (A,B). In above example graph,
there are 7 edges (i.e., (A,B), (A,C), (A,D), (B,D), (B,E), (C,D),
(D,E)).
11/24/2024 3
Graphs
11/24/2024 4
Graphs
Undirected Graph
A graph with only undirected edges is said to be undirected graph.
Directed Graph
A graph with only directed edges is said to be directed graph.
Mixed Graph
A graph with both undirected and directed edges is said to be mixed
graph.
11/24/2024 5
Graphs
Origin
If a edge is directed, its first endpoint is said to be the origin of it.
Destination
If a edge is directed, its first endpoint is said to be the origin of it
and the other endpoint is said to be the destination of that edge.
Adjacent
If there is an edge between vertices A and B then both A and B are
said to be adjacent. In other words, vertices A and B are said to be
adjacent if there is an edge between them.
11/24/2024 6
Graphs
Indegree
Total number of incoming edges connected to a vertex is said to be
indegree of that vertex.
Outdegree
Total number of outgoing edges connected to a vertex is said to be
outdegree of that vertex.
Parallel edges or Multiple edges
If there are two undirected edges with same end vertices and two
directed edges with same origin and destination, such edges are
called parallel edges or multiple edges.
11/24/2024 7
Graphs
Self-loop
Edge (undirected or directed) is a self-loop if its two endpoints
coincide with each other.
Simple Graph
A graph is said to be simple if there are no parallel and self-loop
edges.
Path
A path is a sequence of alternate vertices and edges that starts at a
vertex and ends at other vertex such that each edge is incident to
its predecessor and successor vertex.
11/24/2024 8
Graphs
Graph Representations
1.Adjacency Matrix
11/24/2024 9
Graphs
Adjacency Matrix
In this representation, the graph a square matrix that represents a
graph in graph theory.
That means a graph with 4 vertices is represented using a matrix of
size 4X4.
11/24/2024 10
Graphs
11/24/2024 11
Graphs
Graph Traversal – BFS
11/24/2024 12
Graphs
11/24/2024 13
Graphs
DFS (Depth First Search)
We use the following steps to implement DFS traversal...
Step 1 - Define a Stack of size total number of vertices in the
graph.
11/24/2024 14
Graphs
11/24/2024 15
Graphs
Example
11/24/2024 16
Graphs
11/24/2024 17
Graphs
11/24/2024 18
Graphs
11/24/2024 19
Graphs
11/24/2024 20
Graphs
11/24/2024 21
Graphs
11/24/2024 22
Graphs
11/24/2024 23
Graphs
11/24/2024 24
Graphs
11/24/2024 25
Graphs
11/24/2024 26
Graphs
11/24/2024 27
Graphs
11/24/2024 28
Graphs
11/24/2024 29
Graphs
Spanning Tree
11/24/2024 30
Graphs
Spanning Tree
11/24/2024 31
Graphs
11/24/2024 32
Graphs
Prim's Algorithm
Prim's algorithm starts with the single node and explore all the adjacent
nodes with all the connecting edges at every step. The edges with the
minimal weights causing no cycles in the graph got selected.
11/24/2024 33
Graphs
Prim's Algorithm
The algorithm is given as follows.
Algorithm
11/24/2024 34
Graphs
Example :
Construct a minimum spanning tree of the graph given in the
following figure by using prim's algorithm.
Solution
o Step 1 : Choose a starting vertex B.
o Step 2: Add the vertices that are adjacent to A. the edges that
connecting the vertices are shown by dotted lines.
11/24/2024 35
Graphs
o Step 3: Choose the edge with the minimum weight among all. i.e.
BD and add it to MST. Add the adjacent vertices of D i.e. C and E.
o Step 3: Choose the edge with the minimum weight among all. In this
case, the edges DE and CD are such edges. Add them to MST and
explore the adjacent of C i.e. E and A.
o Step 4: Choose the edge with the minimum weight i.e. CA. We
can't choose CE as it would cause cycle in the graph.
The graph produces in the step 4 is the minimum spanning tree of the
graph shown in the above figure.
The cost of MST will be calculated as;
cost(MST) = 4 + 2 + 1 + 3 = 10 units.
11/24/2024 36
Graphs
11/24/2024 37
Graphs
Kruskal's Algorithm
11/24/2024 38
Graphs
Algorithm
o Step 1: Create a forest in such a way that each graph is a
separate tree.
11/24/2024 39
Graphs
ELSE
Discard the edge
Step 6: END
11/24/2024 40
Graphs
Example :
Apply the Kruskal's algorithm on the graph given as follows.
Solution:
the weight of the edges given as:
Edge AE AD AC AB BC CD DE
Weight 5 10 7 1 3 4 2
11/24/2024 41
Graphs
Edge AB DE BC CD AE AC AD
Weight 1 2 3 4 5 7 10
11/24/2024 42
Graphs
11/24/2024 43
Graphs
The next step is to add AE, but we can't add that as it will cause a
cycle.
The next edge to be added is AC, but it can't be added as it will
cause a cycle.
The next edge to be added is AD, but it can't be added as it
will contain a cycle.
Hence, the final MST is the one which is shown in
the step 4. the cost of MST = 1 + 2 + 3 + 4 = 10.
11/24/2024 44
Shortest Path Problems
B 210 B
A A
450
60 190
C unweighted C weighted
graph graph
200 130
D D
E E
11/24/2024 45
Shortest Path Problems
• How can we find the shortest route between two points on a map?
• Model the problem as a graph problem:
– Road map is a weighted graph:
vertices = cities
edges = road segments between cities
edge weights = road distances
– Goal: find a shortest path between two vertices (cities)
11/24/2024 46
11/24/2024 47
Variants of Shortest Paths
• Single-source shortest path
– G = (V, E) find a shortest path from a given source vertex s to
each vertex v V
• Single-destination shortest path
– Find a shortest path to a given destination vertex t from each
vertex v
– Reverse the direction of each edge single-source
• Single-pair shortest path
– Find a shortest path from u to v for given vertices u and v
– Solve the single-source problem
• All-pairs shortest-paths
– Find a shortest path from u to v for every pair of vertices u and
v
11/24/2024 48
Shortest Path Algorithm
Find the single source shortest path from the vertex A.
Priority queue A B C D E F
0∞ ∞ ∞ ∞∞
Solution set (S)= ϕ
Extract min(Queue) = A
S = S U {A}
= {A}
11/24/2024 49
Shortest Path Algorithm
Priority queue B C D E F
2 ∞ 8 ∞∞
Solution set (S)= {A}
Extract min(Queue) = B
S = S U {B}
= {A,B}
Priority queue C D E F
5 7 ∞∞
Solution set (S)= {A,B}
Extract min(Queue) = C
S = S U {C}
= {A,B,C}
11/24/2024 50
Shortest Path Algorithm
Priority queue D E F
6 12 11
Solution set (S)= {A,B,C}
Extract min(Queue) = D
S = S U {D}
= {A,B,C,D}
Priority queue E F
12 11
Solution set (S)= {A,B,C,D}
Extract min(Queue) = F
S = S U {F}
= {A,B,C,D,F}
11/24/2024 51
Shortest Path Algorithm
Priority queue E
12
Solution set (S)= {A,B,C,D,F}
Extract min(Queue) = E
S = S U {E}
= {A,B,C,D,F,E}
11/24/2024 52
Dijkstra’s Algorithm
t 1 x
Find the single source shortest path from the 10 9
vertex S. s 0
2 3 4 6
5 7
2
y z
11/24/2024 53
Dijkstra’s Algorithm
24
2 3
9
s
18
14
2 6
6
30 4 19
11
15 5
5
6
20 16
t
7 44
11/24/2024 54
Dijkstra’s Pseudo Code
function dijkstra(G, S)
for each vertex V in G
distance[V] <- infinite
previous[V] <- NULL
If V != S, add V to Priority Queue Q
distance[S] <- 0
11/24/2024 55
Applications of Dijkstra's Algorithm
11/24/2024 56
Applications of Dijkstra's Algorithm
11/24/2024 57
Applications of Dijkstra's Algorithm
11/24/2024 58
Floyd Warshall Algorithm-
•It computes the shortest path between every pair of vertices of the
given graph.
11/24/2024 59
Floyd Warshall Algorithm-
Advantages-
Floyd Warshall Algorithm has the following main advantages-
11/24/2024 60
Floyd Warshall Algorithm-
11/24/2024 61
Floyd Warshall Algorithm-
if M[ i ][ j ] > M[ i ][ k ] + M[ k ][ j ]
M[ i ][ j ] = M[ i ][ k ] + M[ k ][ j ]
11/24/2024 62
Floyd Warshall Algorithm-
Time Complexity-
•Floyd Warshall Algorithm consists of three loops over all the nodes.
11/24/2024 63
Floyd Warshall Algorithm-
11/24/2024 64
Floyd Warshall Problem
Problem-
Using Floyd Warshall Algorithm, find the shortest path distance between
every pair of vertices.
11/24/2024 65
Floyd Warshall Algorithm-
Solution-
Step-01:
•Remove all the self loops and parallel edges (keeping the lowest weight
edge) from the graph.
•In the given graph, there are neither self edges nor parallel edges.
Step-02:
11/24/2024 66
Floyd Warshall Algorithm-
Step-02: (Continue)
11/24/2024 67
Floyd Warshall Algorithm-
Step-03:
11/24/2024 68
Floyd Warshall Algorithm-
Step-03:
11/24/2024 69
Floyd Warshall Algorithm-
11/24/2024 70
Floyd Warshall Algorithm-
11/24/2024 71
Floyd Warshall Algorithm-
11/24/2024 72
Floyd Warshall Algorithm-
Remember-
•In the above problem, there are 4 vertices in the given graph.
•So, there will be total 4 matrices of order 4 x 4 in the solution
excluding the initial distance matrix.
•Diagonal elements of each matrix will always be 0.
11/24/2024 73
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.
1111
1111
1111
0001
11/24/2024 74
Faculty Video Links, Youtube & NPTEL
Video Links and Online Courses Details
https://www.youtube.com/watch?v=eujc7_0FhCI
https://www.youtube.com/watch?v=gXgEDyodOJU
11/24/2024 75
Daily Quiz
11/24/2024 76
Weekly Assignment
Q.1 Draw the complete undirected graphs on one, two, three, four and five
vertices. Prove that the number of edges in an n vertex complete graph is
n ( n -1)/2.
Q.2 What are the different ways of representing a graph? Represent the
following
graph using those ways.
Q.3 What are the different ways of representing a graph? Represent the
following
graph using those ways.
11/24/2024 77
Weekly Assignment
Q.4 Write an algorithm which does depth first search through an un-
weighted
connected graph. In an un-weighted graph, would breadth first search
or depth
first search or neither find a shortest path tree from some node? Why?
Q.5 Which are the two standard ways of traversing a graph? Explain
them with an example of each.
11/24/2024 78
MCQ s
1. Which of the following algorithms can be used to most efficiently
determine the presence of a cycle in a given graph ?
a. Depth First Search
b. Breadth First Search
c. Prim's Minimum Spanning Tree Algorithm
d. Kruskal' Minimum Spanning Tree Algorithm
2. Traversal of a graph is different from tree because
a. There can be a loop in graph so we must maintain a visited
flag for every vertex
b. DFS of a graph uses stack, but in-order traversal of a tree is
recursive
c. BFS of a graph uses queue, but a time efficient BFS of a tree
is recursive.
d. All of above
11/24/2024 79
MCQ s
3. What are the appropriate data structures for following algorithms?
a. Breadth First Search
b. Depth First Search
c. Prim's Minimum Spanning Tree
d. Kruskal' Minimum Spanning Tree
4. The Breadth First Search algorithm has been implemented using the
queue data structure. One possible order of visiting the nodes of the
following graph is
11/24/2024 80
MCQ s
5. Consider the following graph,
Among the following sequences:
(I) a b e g h f
(II) a b f e h g
(III) a b f h g e
(IV) a f g h b e
Which are depth first traversals of the above graph?
a) Only BFS
b) Only DFS
c) Both BFS and DFS
d) Neither BFS nor DFS
11/24/2024 81
MCQ s
7. Which of the following is the most commonly used data structure for
implementing Dijkstra's Algorithm?
a. Max priority queue
b. Stack
c. Circular queue
d. Min priority queue
11/24/2024 82
MCQ s
11/24/2024 84
Expected Questions for University Exam
7. Define Floyd Warshall Algorithm for all pair shortest path and apply the
same on following graph:
11/24/2024 85
Old Question Papers
https://drive.google.com/open?id=15fAkaWQ5c
cZRZPzwlP4PBh1LxcPp4VAd
11/24/2024 86
Summary
Graphs can be used to solve some very complex problems, such as least
cost routing, mapping, program analysis, and so on.
11/24/2024 87
References
11/24/2024 88
Thank you
11/24/2024 89