Professional Documents
Culture Documents
DATA STRUCTURES
Module 6
Graph
Dr Anant K Jayswal
MTech(CSE), PhD(CSE)-JNU GATE(CS), UGC-NET(CS)
1
Amity School of Engineering & Technology
Contents:
❑ Basic Concepts of Graph
❑Different types of Graph
❑Representation of Graph
2
Amity School of Engineering & Technology
Objectives
3
Amity School of Engineering & Technology
Graphs…
Graph is a non linear Data Structure. Example:
It is a collection of nodes(vertices)
and edges(arcs) that relate nodes to
each other in the graph.
Generally, A Graph is represented as
G = (V, E)
Where,
V is the set of vertices(nodes or
points) and
E is the set of edges(links or arcs). 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)}.
4
Amity School of Engineering & Technology
Graph…
❖ Vertex: An individual data element of a graph is called as
vertex. Vertex is also known as node.
❖ Edge: An edge is a connecting link between two vertices.
Graph Terminology
Adjacent Nodes: Any two nodes which are connected by an
edge are said to be adjacent nodes.
Adjacent edges- Any two edges which are having one common
vertex
Loop or Self Loop: An edge that starts and end at the same
vertex
Parallel Edges- If more than one edge exists between the nodes
then the edges are called the parallel ledges.
6
Amity School of Engineering & Technology
Types of Graphs
Null Graph: A graph with no
edges is called a Null Graph E(G)=φ
V(G)≠ φ
Types of Graphs
Types of Graphs
9
Amity School of Engineering & Technology
Graph Terminology
Degree: Total number of edges connected to a vertex.
Types of Graphs
11
Amity School of Engineering & Technology
1. Sequential Representation
2. Linked Representation
12
Amity School of Engineering & Technology
Sequential Representation
13
Amity School of Engineering & Technology
14
Amity School of Engineering & Technology
15
Amity School of Engineering & Technology
16
Contents:
17
Graph Representations in Memory
1. Sequential Representation
2. Linked Representation
18
Sequential Representation
❖ Adjacency Matrix Representation
❖ Incidence Matrix Representation
19
Adjacency Matrix Representation
In this representation, a graph is ▪ Undirected Graph
represented with the help of matrix of size
NxN
where, N is the number of vertices.
It is given by A=[aij]
aij =1if there is an edge from i to j.
▪ Directed Graph
aij =0 if there is no edge from i to j.
20
Incidence Matrix Representation
In this representation, a graph is • Undirected Graph
22
Amity School of Engineering Technology
Graph Traversal
Graph traversal is technique used for searching a vertex in a graph. The
graph traversal is also used to decide the order of vertices to be visit in
the search process.
DFS (Depth First Search)
BFS (Breadth First Search)
23
Amity School of Engineering Technology
24
Amity School of Engineering Technology
Example
Amity School of Engineering Technology
Example
Amity School of Engineering Technology
Example
Amity School of Engineering Technology
Practice Question
Consider the graph G along with its adjacency list, given in the figure below. Calculate
the order to print all the nodes of the graph starting from node H, by using Breadth
first search (DFS) algorithm.
Amity School of Engineering Technology
Example
Amity School of Engineering Technology
Example
Amity School of Engineering Technology
Example
Amity School of Engineering Technology
Practice Question
Consider the graph G along with its adjacency list, given in the figure below. Calculate
the order to print all the nodes of the graph starting from node H, by using depth first
search (DFS) algorithm.
PATH Matrix / Reachability Matrix
If G is the simple graph with n-vertices & e –edges then the Path Matrix
of G is the n-square matrix P=(pij) defined as
1, 𝑖𝑓 𝑡ℎ𝑒𝑟𝑒 𝑖𝑠 𝑎 𝑝𝑎𝑡ℎ 𝑓𝑟𝑜𝑚 𝑣𝑖 𝑎𝑛𝑑 𝑣𝑗
𝑃𝑖𝑗 = ቊ
0, 𝑜𝑡ℎ𝑒𝑟𝑤𝑖𝑠𝑒
Example:
35
Amity School of Engineering Technology
36
Amity School of Engineering Technology
37
Amity School of Engineering Technology
𝑅(1) =
Warshall’s Algorithm cont’d 1
1 2
1
3 4
0
5
0 1 0
2 0 0 0 1 1
3 0 0 0 1 0
4 1 1 1 1 1
5 0 0 0 0 0
𝑅(2) = 1 2 3 4 5
1 0 1 1 1 1
Find the path matrix for the above graph
2 0 0 0 1 1
p[i, j] = p[i, j] or (p[i,k] and p[k, j]) 3 0 0 0 1 0
Or 4 1 1 1 1 1
𝑅 (1) 𝑖, 𝑗 = 𝑅 0 𝑖, 𝑗 𝑜𝑟(𝑅 0 𝑖, 𝑘 𝑎𝑛𝑑𝑅 0 𝑘, 𝑗
5 0 0 0 0 0
(0)
𝑅 = A/1 B/2 C/3 D/4 E/5 𝑅(3) = 1 2 3 4 5
A/1 0 1 1 0 0 1 0 1 1 1 1
B/2 0 0 0 1 1 2 0 0 0 1 1
C/3 0 0 0 1 0 3 0 1
0 0 0
D/4 1 0 0 1 1 4 1 1 1 1 1
E/5 0 0 0 0 0 5 38
0 0 0 0 0
Amity School of Engineering Technology
Exercise F C
A
B D
H
G E
44
Spanning Tree
• Spanning Tree of a connected undirected graph is a connected subgraph which
has all the vertices covered with minimum possible number of edges.
• If G is a complete graph with n vertices, then total number of spanning trees will
be n(n-2).
45
General properties of spanning tree
• A connected graph G can have more than one spanning tree.
• All possible spanning trees of graph G, have same number of edges and vertices.
• Removing one edge from spanning tree will make the graph disconnected i.e.
spanning tree is minimally connected.
• Adding one edge to a spanning tree will create a cycle or loop i.e. spanning tree
is maximally acyclic.
46
Example: Number of Spanning trees for a complete graph
48
Minimum Spanning trees
There are two important algorithms for finding minimum cost spanning trees.
• Kruskal's Algorithm
• Prim's Algorithm
Both are greedy algorithms.
49
Kruskal’s Algorithm
50
Kruskal’s Algorithm
Example:
51
Kruskal’s Algorithm
Step 3: Select the shortest edge connected to any vertex already connected
53
Prim’s Algorithm
Example:
54
Prim’s Algorithm
55
Practice Questions:
Find minimum Spanning tree for the following using Kruskal’s and Prim’s Algorithm
1. 2.
3. 44.
56
Dijkstra’s Algorithm(Single Source Shortest Path Algorithm)
57
58
Practice Question
59
60