Graphs Notes
Graphs Notes
Graphs Notes
UNIT-4
Minimum Cost spanning tree, Prims and Kruskal Algorithm, BFS, DFS,
11/19/2023 1
Objective of Unit
11/19/2023 2
Prerequisite and Recap
Introduction to graphs
BFS
DFS
Prims
Kruskal
11/19/2023 3
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/19/2023 4
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/19/2023 5
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/19/2023 6
Graphs
11/19/2023 7
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.
End vertices or Endpoints
The two vertices joined by edge are called end vertices (or endpoints)
of that edge.
11/19/2023 8
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/19/2023 9
Graphs
Incident
Edge is said to be incident on a vertex if the vertex is one of the
endpoints of that edge.
Outgoing Edge
A directed edge is said to be outgoing edge on its origin vertex.
Incoming Edge
A directed edge is said to be incoming edge on its destination
vertex.
Degree
Total number of edges connected to a vertex is said to be degree of
that vertex.
11/19/2023 10
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/19/2023 11
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/19/2023 12
Graphs
Graph Representations
1.Adjacency Matrix
2.Incidence Matrix
3.Adjacency List
11/19/2023 13
Graphs
Adjacency Matrix
In this representation, the graph is represented using a matrix of size
total number of vertices by a total number of vertices.
11/19/2023 14
Graphs
11/19/2023 15
Graphs
Incidence Matrix
11/19/2023 16
Graphs
Incidence Matrix
11/19/2023 17
Graphs
Adjacency List
In this representation, every vertex of a graph contains list of its
adjacent vertices.
11/19/2023 18
Graphs
There are two graph traversal techniques and they are as follows...
1.DFS (Depth First Search)
2.BFS (Breadth First Search)
11/19/2023 19
Graphs
11/19/2023 20
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/19/2023 21
Graphs
11/19/2023 22
Graphs
11/19/2023 23
Graphs
11/19/2023 24
Graphs
11/19/2023 25
Graphs
11/19/2023 26
Graphs
11/19/2023 27
Graphs
11/19/2023 28
Graphs
11/19/2023 29
Graphs
11/19/2023 30
Graphs
11/19/2023 31
Graphs
11/19/2023 32
Graphs
11/19/2023 33
Graphs
11/19/2023 34
Graphs
11/19/2023 35
Graphs
Spanning Tree
11/19/2023 36
Graphs
Spanning Tree
11/19/2023 37
Graphs
11/19/2023 38
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/19/2023 39
Graphs
Prim's Algorithm
11/19/2023 40
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/19/2023 41
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/19/2023 42
Graphs
11/19/2023 43
Graphs
Kruskal's Algorithm
11/19/2023 44
Graphs
Algorithm
o Step 1: Create a forest in such a way that each graph is a
separate tree.
11/19/2023 45
Graphs
ELSE
Discard the edge
Step 6: END
11/19/2023 46
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/19/2023 47
Graphs
Edge AB DE BC CD AE AC AD
Weight 1 2 3 4 5 7 10
11/19/2023 48
Graphs
11/19/2023 49
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/19/2023 50
Shortest Path Problems
B 210 B
A A
450
60 190
C unweighted C weighted
graph graph
200 130
D D
E E
11/19/2023 51
Shortest Path Problems
How can we find the shortest route between two points on a map?
Model the problem as a graph problem:
11/19/2023 52
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/19/2023 53
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/19/2023 54
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/19/2023 55
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/19/2023 56
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/19/2023 57
Summary
Graphs can be used to solve some very complex problems, such as least
cost routing, mapping, program analysis, and so on.
11/19/2023 58
11/19/2023 59