GRAPH
Graph Representation
The following two are the most commonly used representations of a
graph.
1. Adjacency Matrix
2. Adjacency List
Adjacency matrix Representation
Adjacency matrix, also called bit matrix or Boolean matrix. If an edge exists
between two nodes it stores true (represented by 1) otherwise false (represented
by 0).
True or 1 if edge exist between I to j
v[i][j]
False or 0 otherwise
Example (Directed Graph)
Graph Adjacency matrix Representation
Graph Adjacency matrix Representation
In above diagram A to B edge exist so in the matrix A to B is 1. A to C edge does not exist so in
the matrix A to C is 0.
Points to Remember-
In case of directed Graph, the sum of matrix is always equal to number of edges|E| or sum of in-degree of each vertex.
In above diagram
Number of edges= 7
Sum of matrix = 7
Number of edges = Sum of Matrix
1
Example (Undirected Graph)
Graph Adjacency matrix Representation
Points to Remember-
In case of undirected Graph, the sum of matrix is always equal to twice the number of edges i.e. 2|E| or sum of degree of
each vertex.
In above diagram , Number of edges= 9,
Sum of matrix= 18
Sum of matrix=2|E|
Example (Weighted Directed Graph)
Graph Weight matrix Representation
In above diagram A to B edge exist so in the matrix A to B is 2. A to C edge does not exist so in the matrix A to C is 0.
Example (Weighted Undirected Graph)
Example (Weighted Undirected Graph)
Disadvantage of Adjacency matrix
No. of entries for directed graph= e
No. of entries for undirected graph= 2e
where e= edges of graph
total entries in matrix= n X n
where n=no. of vertex
Memory wasted for directed graph=n2 -e
Memory wasted for undirected graph= n2 -2e
Advantage of adjacency matrix
• Used efficiently for more entries
• Traversal or accessing of any node is easy
• Adjacency List:
An array of lists is used. The size of the array is equal to the number of
vertices. Let the array be an array[]. An entry array[i] represents the
list of vertices adjacent to the ith vertex.
• This representation can also be used to represent a weighted graph.
The weights of edges can be represented as lists of pairs.
GRAPH - DISCRETE STRUCTURE AND ALGORITHM
GRAPH - DISCRETE STRUCTURE AND ALGORITHM
Disadvantage of adjacency list
• Checking the existence of an edge between two vertices i.e. i and e ,
is time consuming
• Calculations of the degree of the vertex is also time consuming
traversal algorithms
The graph has two types of traversal algorithms. These are called
• Breadth First Search (BFS)
• Depth First Search (DFS)
Traversal algorithms: Breadth First Search (BFS)
During the execution of our algorithms, each node N of G will be one of
three states, called the status of N, as follows:
STATUS=1: (Ready state) The initial state of the node N
STATUS =2: (Waiting state) The node N is on the Queue ,waiting to be processed
STATUS=3: (Processed state) The Node n has been processed
This algorithm executes a BFS on a Graph G beginning at a starting node A
Step1: Initialize all nodes to the ready state (STATUS=1).
Step 2 : Put the starting node A in QUEUE and change its status to the
waiting state (STATUS=2).
Step3: Repeat steps 4 & 5 until QUEUE is empty:
Step4: Remove the front node N of QUEUE . Process N and change
the status of N to the processed state (STATUS=3).
Step5: Add to the rear of QUEUE all the neighbors of N
that are in the steady state (STATUS=1) and change their
status to the waiting state (STATUS=2).
[End of Step3 loop]
Step6: Exit
GRAPH - DISCRETE STRUCTURE AND ALGORITHM
Traversal algorithms: Depth First Search (DFS)
During the execution of our algorithms, each node N of G will be one of
three states, called the status of N, as follows:
STATUS=1: (Ready state) The initial state of the node N
STATUS =2: (Waiting state) The node N is on the Stack ,waiting to be processed
STATUS=3: (Processed state) The Node n has been processed
This algorithm executes a BFS on a Graph G beginning at a starting node A
Step1: Initialize all nodes to the ready state (STATUS=1).
Step 2 : Put the starting node A in STACK and change its status to the
waiting state (STATUS=2).
Step3: Repeat steps 4 & 5 until STACK is empty:
Step4: Pop the top node N of STACK . Process N and change the
status of N to the processed state (STATUS=3).
Step5: Push on to STACK all the neighbors of N that are in the
steady state (STATUS=1) and change their status to the
waiting state (STATUS=2).
[End of Step3 loop]
Step6: Exit
Spanning Tree
A spanning tree is a subset of Graph G, which has all the vertices
covered with minimum possible number of edges. Hence, a spanning
tree does not have cycles and it cannot be disconnected.
GRAPH - DISCRETE STRUCTURE AND ALGORITHM
We found three spanning trees off one complete graph. A complete
undirected graph can have maximum n(n-2) number of spanning trees,
where n is the number of nodes. In the above addressed example, n is
3, hence 3(3−2) = 3 spanning trees are possible.
General Properties of Spanning Tree
We now understand that one graph can have more than one spanning tree.
Following are a few properties of the spanning tree connected to graph G −
• A connected graph G can have more than one spanning tree.
• All possible spanning trees of graph G, have the same number of edges and
vertices.
• The spanning tree does not have any cycle (loops).
• Removing one edge from the spanning tree will make the graph disconnected, i.e.
the spanning tree is minimally connected.
Minimum Spanning Tree (MST)
In a weighted graph, a minimum spanning tree is a spanning tree that has
minimum weight than all other spanning trees of the same graph. In real-
world situations, this weight can be measured as distance, congestion, traffic
load or any arbitrary value denoted to the edges.
We shall learn about two most important spanning tree algorithms here −
Kruskal's Algorithm
Prim's Algorithm
Both are greedy algorithms.
Prim’s Minimum Spanning Tree and Kruskal’s
algorithm fails for directed graphs
• Prim’s algorithm assumes that all vertices are connected. But in a
directed graph, every node is not reachable from every other node.
So, Prim’s algorithm fails due to this reason.
As it is visible in the graph, no node is reachable from node 4. Directed
graphs fail the requirement that all vertices are connected.
Why Kruskal’s Algorithm fails for directed graph ?
• In Kruskal’s algorithm, In each step, it is checked that if the edges form a cycle
with the spanning-tree formed so far. But Kruskal’s algorithm fails to detect the
cycles in a directed graph as there are cases when there is no cycle between
the vertices but Kruskal’s Algorithm assumes it to cycle and don’t take consider
some edges due to which Kruskal’s Algorithm fails for directed graph.
For example:
Kruskal's Algorithm
Finding MST?
Arrange all the edges in ascending
order to their weight.
GRAPH - DISCRETE STRUCTURE AND ALGORITHM
Kruskal's Algorithm
Finding MST?
Arrange all the edges in ascending
order to their weight.
edge weight
(7,6) 1
(5,6) 2
Finding MST?
Arrange all the edges in ascending
order to their weight.
edge weight
(6,7) 1
(6,5) (2,8) 2
(2,5) 4
(8,6) 6
(2,3)(7,8) 7
(1,2) (0,7) 8
(3,4) 9
(5,4) 10
(1,7) 11
(3,5) 14
Total cost= 41
0
2
5
2
10
25
6 7
14
11 15
12
2
0
2
1
3
4 5
6
0
2
5
2
10
25
6 7
14
11 15
12
2
0
2
1
3
4 5
6
0
2
2
10
6
11
12
2
0
2
1
3
4 5
6
Total cost:43
GRAPH - DISCRETE STRUCTURE AND ALGORITHM
Kruskal’s algorithm
Step1: set minimum spanning tree (MST) initially empty.
A=Ø
Step2: For each vertex V Ɛ V(G)
create (V) no. of tree where tree contain all vertex no edge.
Step3: Set the edges into ascending order according to their weights
and select the edge which has minimum weight.
Step4: if that forms cycles rejected it otherwise add it.
Step5: repeat until the single tree is formed.
Step 6: return MST.
Prims Algorithm
For a Graph G={V,E}
Assume S= Set of vertices in MST
A= Set of edges in MST
Step1: select any vertex ‘r’ and set S={r} and also set A= {Ø}
Step2: Find the smallest weight edge such that one end point in S and other in
{ V-S}.
Step3: Now add this edge into the set A and set another point into set S
Step 4:IF {V-S} =Ø then
Stop
and output is return i.e. MST
otherwise goto Step2.
0
2
5
2
10
25
6 7
14
11 15
12
2
0
2
1
3
4 5
6
Set of Vertices
in MST
Edges Selected edges
(less weight)
{ 0} (0,1)(0,2)
0
2
2
0
2
0
2
5
2
10
25
6 7
14
11 15
12
2
0
2
1
3
4 5
6
Set of Vertices
in MST
Edges Selected
edges
(less
weight)
{ 0} (0,1)(0,2) (0,2)
{0,2} (0,1)(2,1)(2,3)(2,5) (2,1)
{0,2,1}
0
2
2
0
2
2
1
0
2
5
2
10
25
6 7
14
11 15
12
2
0
2
1
3
4 5
6
Set of Vertices
in MST
Edges Selected
edges
(less
weight)
{ 0} (0,1)(0,2) (0,2)
{0,2} (0,1)(2,1)(2,3)(2,5) (2,1)
{0,2,1} (2,3)(2,5)(1,3)(1,4) (1,3)
0
2
2
0
2
2
1
6
3
0
2
5
2
10
25
6 7
14
11 15
12
2
0
2
1
3
4 5
6
Set of Vertices
in MST
Edges Selected
edges
(less
weight)
{ 0} (0,1)(0,2) (0,2)
{0,2} (0,1)(2,1)(2,3)(2,5) (2,1)
{0,2,1} (2,3)(2,5)(1,3)(1,4) (1,3)
{0,2,1,3} (2,5)(1,4)(3,6) (2,5)
{0,2,1,3,5} (1,4)(3,6)(5,4)(5,6) (5,4)
{0,1,2,3,4,5} (3,6)(5,6)(4,6) (4,6)
{0,1,2,3,4,5,6} - -
0
2
2
10
6
11
12
2
0
2
1
3
4 5
6
Total cost=2+2+6=10+12+11= 43
Single Source Shortest path
For a given Graph G= (V, E), we want to find a shortest path from a give
source vetex S Ɛ V o every vertex v Ɛ V . A single source shortest path
may be computed with the following algorithms.
1. Dijkstra's Algorithm
Dijkstra's Algorithm
Dijakstra’s algo solves the single source shortest path problem on a
weighted , directed graph G=(V,E) for the case in which all edge
weights are non negative.
The Dijaksta’s algorithm uses a greedy strategies i.e. it always select
the lightest or closest vertex.
Find the shortest path from source vertex to
all other vertex
0
2
o
x
0
u 0
v
0
y
10
5 7
2
3
1
4 6
Source vertex S=0
S=Finally contains vertex of final shortest path
Q= Priority queue used to store vertices based on
their distance from source vertex
2
Find the shortest path from source vertex to
all other vertex
0
2
o
x
0
u 0
v
0
y
10
5 7
2
3
1
4 6
Source vertex S=0
S=Finally contains vertex of final shortest path
Q= Priority queue used to store vertices based on
their distance from source vertex
Initially
S= { Ø}
Q= o u v x y
0 ∞ ∞ ∞ ∞
0
2
x
0
u 0
v
0
y
10
5 7
2
3
1
4 6
Initially
S= { Ø}
Q= o u v x y
0 ∞ ∞ ∞ ∞
Q= extract minQueue {Q}
S={o}
0
∞
∞
∞
∞
2
0
2
x
0
u 0
v
0
y
10
5 7
2
3
1
4 6
Initially
S= { Ø}
Q= o u v x y
0 ∞ ∞ ∞ ∞
Q= extract minQueue {Q}
S={o}
Q= u v x y
10 ∞ 5 ∞
Q= extract minQueue {Q}
S={o, x}
0
∞
∞
∞
∞
0
2
x
0
u 0
v
0
y
10
5 7
2
3
1
4 6
0
∞
5
∞
∞
2
2
S={o, x}
Q= u v y
8 ∞ 7
Q= extract minQueue {Q}
S={o,x,y}
0
2
x
0
u 0
v
0
y
10
5 7
2
3
1
4 6
0
∞
5
∞
7
2
S={o, x}
Q= u v y
8 ∞ 7
Q= extract minQueue {Q}
S={o,x,y}
Q= u v
8 13
Q= extract minQueue {Q}
S={o, x, y , u }
0
2
x
0
u 0
v
0
y
10
5 7
2
3
1
4 6
0
∞
5
∞
7
2
0
2
x
0
u 0
v
0
y
10
5 7
2
3
1
4 6
0
8
5
∞
7
2
S={o, x, y , u }
Q= v
9
Q= extract minQueue {Q}
S={o,x,y, u, v}
0
2
x
0
u 0
v
0
y
10
5 7
2
3
1
4 6
0
8
5
9
7
2
0
2
x
0
u 0
v
0
y
5
3
1
2
problem
problem
solution
DIJKSTRA’S ALGO
Step1: Initialize single source vertex by zero.
Step2: Initialize S= {Ø} where S, finally contains vertices of final shortest
path from source vertex.
Step3: Initialize priority queue Q such that Q V(G)
Step 4: while priority queue (Q) is not empty do step 5 to 7
Step5: select vertex u of minimum value from Queue such that
u extract of minQueue
Step6: new S SU {u}
Step7: Insert all the adjacent nodes into Queue,assign their weights as
sum of weight of parent and edge weight.
Step8: End;
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. The reach-ability matrix is
called the transitive closure of a graph.
Transitive closure of graphs
is
1 1 1 1
1 1 1 1
1 1 1 1
0 0 0 1
Floyd Warshall Algorithm, find the shortest
path distance between every pair of vertices
GRAPH - DISCRETE STRUCTURE AND ALGORITHM
GRAPH - DISCRETE STRUCTURE AND ALGORITHM
GRAPH - DISCRETE STRUCTURE AND ALGORITHM
Floyd Warshall Algorithm
Step1: We initialize the solution matrix same as the input graph matrix .
Step2: Then we update the solution matrix by considering all vertices as an
intermediate vertex. The idea is to one by one pick all vertices and updates
all shortest paths which include the picked vertex as an intermediate vertex
in the shortest path.
When we pick vertex number k as an intermediate vertex, we already have
considered vertices {0, 1, 2, .. k-1} as intermediate vertices. For every pair (i,
j) of the source and destination vertices respectively.
There are two possible cases:
Cae1:k is not an intermediate vertex in shortest path from i to j. We keep
the value of dist[i][j] as it is.
Case2: k is an intermediate vertex in shortest path from i to j. We update the
value of dist[i][j] as dist[i][k] + dist[k][j] if dist[i][j] > dist[i][k] + dist[k][j]

More Related Content

PDF
Clustering:k-means, expect-maximization and gaussian mixture model
PPT
PDF
Lecture 4 Decision Trees (2): Entropy, Information Gain, Gain Ratio
PPTX
Random forest and decision tree
PPT
5.3 mining sequential patterns
PPTX
Random Forest Tutorial | Random Forest in R | Machine Learning | Data Science...
PPTX
Multi ways trees
Clustering:k-means, expect-maximization and gaussian mixture model
Lecture 4 Decision Trees (2): Entropy, Information Gain, Gain Ratio
Random forest and decision tree
5.3 mining sequential patterns
Random Forest Tutorial | Random Forest in R | Machine Learning | Data Science...
Multi ways trees

What's hot (20)

PPTX
The propositional calculus
PPTX
Lect 02 second portion
PPTX
Discrete Mathematics - Trees
PPT
morphological image processing
PPT
2.2 decision tree
PDF
Data Structures and Algorithms
PDF
Lung Cancer Detection using Image Processing Techniques
PDF
Lecture11
DOCX
Naive bayes classifier
PDF
Euler and hamilton paths
ODP
NAIVE BAYES CLASSIFIER
PDF
IRJET-Performance Analysis of Liver Disease Prediction using Machine Learning...
PPT
graphics notes
PPT
Bayesian Networks Model in Step By Steps
PPTX
PPTX
1_unit-1.1_Introduction to DIP.pptx
PPT
B trees and_b__trees
PPTX
Decision trees
PPTX
Bfs and Dfs
PPT
Binary search trees
The propositional calculus
Lect 02 second portion
Discrete Mathematics - Trees
morphological image processing
2.2 decision tree
Data Structures and Algorithms
Lung Cancer Detection using Image Processing Techniques
Lecture11
Naive bayes classifier
Euler and hamilton paths
NAIVE BAYES CLASSIFIER
IRJET-Performance Analysis of Liver Disease Prediction using Machine Learning...
graphics notes
Bayesian Networks Model in Step By Steps
1_unit-1.1_Introduction to DIP.pptx
B trees and_b__trees
Decision trees
Bfs and Dfs
Binary search trees
Ad

Similar to GRAPH - DISCRETE STRUCTURE AND ALGORITHM (20)

PPT
Data Structures-Non Linear DataStructures-Graphs
PPTX
Unit ix graph
PPTX
Unit 4 dsuc
PPTX
Graph Algorithms
PPTX
Unit 9 graph
PPTX
UNIT II - Graph Algorithms techniques.pptx
PPTX
130210107039 2130702
PDF
Unit-10 Graphs .pdf
PPT
Graphs
PDF
Day 5 application of graph ,biconnectivity fdp on ds
PPT
14. GRAPH in data structures and algorithm.ppt
PPTX
UNIT III.pptx
PPTX
Graph terminology and algorithm and tree.pptx
PPTX
TREE ADT, TREE TRAVERSALS, BINARY TREE ADT
PPTX
PDF
lecture 23 algorithm design and analysis
PPTX
Bfs dfs mst
PPTX
Algorithms and data Chapter 3 V Graph.pptx
Data Structures-Non Linear DataStructures-Graphs
Unit ix graph
Unit 4 dsuc
Graph Algorithms
Unit 9 graph
UNIT II - Graph Algorithms techniques.pptx
130210107039 2130702
Unit-10 Graphs .pdf
Graphs
Day 5 application of graph ,biconnectivity fdp on ds
14. GRAPH in data structures and algorithm.ppt
UNIT III.pptx
Graph terminology and algorithm and tree.pptx
TREE ADT, TREE TRAVERSALS, BINARY TREE ADT
lecture 23 algorithm design and analysis
Bfs dfs mst
Algorithms and data Chapter 3 V Graph.pptx
Ad

Recently uploaded (20)

PDF
Abrasive, erosive and cavitation wear.pdf
PPTX
"Array and Linked List in Data Structures with Types, Operations, Implementat...
PDF
Implantable Drug Delivery System_NDDS_BPHARMACY__SEM VII_PCI .pdf
PPTX
Software Engineering and software moduleing
PDF
distributed database system" (DDBS) is often used to refer to both the distri...
PPTX
Chemical Technological Processes, Feasibility Study and Chemical Process Indu...
PDF
Accra-Kumasi Expressway - Prefeasibility Report Volume 1 of 7.11.2018.pdf
PPTX
Management Information system : MIS-e-Business Systems.pptx
PPTX
Amdahl’s law is explained in the above power point presentations
PDF
August 2025 - Top 10 Read Articles in Network Security & Its Applications
PPTX
Module 8- Technological and Communication Skills.pptx
PPT
Chapter 1 - Introduction to Manufacturing Technology_2.ppt
PDF
Computer System Architecture 3rd Edition-M Morris Mano.pdf
PPTX
AUTOMOTIVE ENGINE MANAGEMENT (MECHATRONICS).pptx
PDF
First part_B-Image Processing - 1 of 2).pdf
PPTX
tack Data Structure with Array and Linked List Implementation, Push and Pop O...
PDF
UEFA_Carbon_Footprint_Calculator_Methology_2.0.pdf
PDF
Exploratory_Data_Analysis_Fundamentals.pdf
PPTX
Building constraction Conveyance of water.pptx
PDF
Java Basics-Introduction and program control
Abrasive, erosive and cavitation wear.pdf
"Array and Linked List in Data Structures with Types, Operations, Implementat...
Implantable Drug Delivery System_NDDS_BPHARMACY__SEM VII_PCI .pdf
Software Engineering and software moduleing
distributed database system" (DDBS) is often used to refer to both the distri...
Chemical Technological Processes, Feasibility Study and Chemical Process Indu...
Accra-Kumasi Expressway - Prefeasibility Report Volume 1 of 7.11.2018.pdf
Management Information system : MIS-e-Business Systems.pptx
Amdahl’s law is explained in the above power point presentations
August 2025 - Top 10 Read Articles in Network Security & Its Applications
Module 8- Technological and Communication Skills.pptx
Chapter 1 - Introduction to Manufacturing Technology_2.ppt
Computer System Architecture 3rd Edition-M Morris Mano.pdf
AUTOMOTIVE ENGINE MANAGEMENT (MECHATRONICS).pptx
First part_B-Image Processing - 1 of 2).pdf
tack Data Structure with Array and Linked List Implementation, Push and Pop O...
UEFA_Carbon_Footprint_Calculator_Methology_2.0.pdf
Exploratory_Data_Analysis_Fundamentals.pdf
Building constraction Conveyance of water.pptx
Java Basics-Introduction and program control

GRAPH - DISCRETE STRUCTURE AND ALGORITHM

  • 2. Graph Representation The following two are the most commonly used representations of a graph. 1. Adjacency Matrix 2. Adjacency List Adjacency matrix Representation Adjacency matrix, also called bit matrix or Boolean matrix. If an edge exists between two nodes it stores true (represented by 1) otherwise false (represented by 0). True or 1 if edge exist between I to j v[i][j] False or 0 otherwise
  • 3. Example (Directed Graph) Graph Adjacency matrix Representation
  • 4. Graph Adjacency matrix Representation In above diagram A to B edge exist so in the matrix A to B is 1. A to C edge does not exist so in the matrix A to C is 0. Points to Remember- In case of directed Graph, the sum of matrix is always equal to number of edges|E| or sum of in-degree of each vertex. In above diagram Number of edges= 7 Sum of matrix = 7 Number of edges = Sum of Matrix 1
  • 6. Graph Adjacency matrix Representation Points to Remember- In case of undirected Graph, the sum of matrix is always equal to twice the number of edges i.e. 2|E| or sum of degree of each vertex. In above diagram , Number of edges= 9, Sum of matrix= 18 Sum of matrix=2|E|
  • 7. Example (Weighted Directed Graph) Graph Weight matrix Representation In above diagram A to B edge exist so in the matrix A to B is 2. A to C edge does not exist so in the matrix A to C is 0.
  • 10. Disadvantage of Adjacency matrix No. of entries for directed graph= e No. of entries for undirected graph= 2e where e= edges of graph total entries in matrix= n X n where n=no. of vertex Memory wasted for directed graph=n2 -e Memory wasted for undirected graph= n2 -2e
  • 11. Advantage of adjacency matrix • Used efficiently for more entries • Traversal or accessing of any node is easy
  • 12. • Adjacency List: An array of lists is used. The size of the array is equal to the number of vertices. Let the array be an array[]. An entry array[i] represents the list of vertices adjacent to the ith vertex. • This representation can also be used to represent a weighted graph. The weights of edges can be represented as lists of pairs.
  • 15. Disadvantage of adjacency list • Checking the existence of an edge between two vertices i.e. i and e , is time consuming • Calculations of the degree of the vertex is also time consuming
  • 16. traversal algorithms The graph has two types of traversal algorithms. These are called • Breadth First Search (BFS) • Depth First Search (DFS)
  • 17. Traversal algorithms: Breadth First Search (BFS) During the execution of our algorithms, each node N of G will be one of three states, called the status of N, as follows: STATUS=1: (Ready state) The initial state of the node N STATUS =2: (Waiting state) The node N is on the Queue ,waiting to be processed STATUS=3: (Processed state) The Node n has been processed
  • 18. This algorithm executes a BFS on a Graph G beginning at a starting node A Step1: Initialize all nodes to the ready state (STATUS=1). Step 2 : Put the starting node A in QUEUE and change its status to the waiting state (STATUS=2). Step3: Repeat steps 4 & 5 until QUEUE is empty: Step4: Remove the front node N of QUEUE . Process N and change the status of N to the processed state (STATUS=3). Step5: Add to the rear of QUEUE all the neighbors of N that are in the steady state (STATUS=1) and change their status to the waiting state (STATUS=2). [End of Step3 loop] Step6: Exit
  • 20. Traversal algorithms: Depth First Search (DFS) During the execution of our algorithms, each node N of G will be one of three states, called the status of N, as follows: STATUS=1: (Ready state) The initial state of the node N STATUS =2: (Waiting state) The node N is on the Stack ,waiting to be processed STATUS=3: (Processed state) The Node n has been processed
  • 21. This algorithm executes a BFS on a Graph G beginning at a starting node A Step1: Initialize all nodes to the ready state (STATUS=1). Step 2 : Put the starting node A in STACK and change its status to the waiting state (STATUS=2). Step3: Repeat steps 4 & 5 until STACK is empty: Step4: Pop the top node N of STACK . Process N and change the status of N to the processed state (STATUS=3). Step5: Push on to STACK all the neighbors of N that are in the steady state (STATUS=1) and change their status to the waiting state (STATUS=2). [End of Step3 loop] Step6: Exit
  • 22. Spanning Tree A spanning tree is a subset of Graph G, which has all the vertices covered with minimum possible number of edges. Hence, a spanning tree does not have cycles and it cannot be disconnected.
  • 24. We found three spanning trees off one complete graph. A complete undirected graph can have maximum n(n-2) number of spanning trees, where n is the number of nodes. In the above addressed example, n is 3, hence 3(3−2) = 3 spanning trees are possible.
  • 25. General Properties of Spanning Tree We now understand that one graph can have more than one spanning tree. Following are a few properties of the spanning tree connected to graph G − • A connected graph G can have more than one spanning tree. • All possible spanning trees of graph G, have the same number of edges and vertices. • The spanning tree does not have any cycle (loops). • Removing one edge from the spanning tree will make the graph disconnected, i.e. the spanning tree is minimally connected.
  • 26. Minimum Spanning Tree (MST) In a weighted graph, a minimum spanning tree is a spanning tree that has minimum weight than all other spanning trees of the same graph. In real- world situations, this weight can be measured as distance, congestion, traffic load or any arbitrary value denoted to the edges. We shall learn about two most important spanning tree algorithms here − Kruskal's Algorithm Prim's Algorithm Both are greedy algorithms.
  • 27. Prim’s Minimum Spanning Tree and Kruskal’s algorithm fails for directed graphs • Prim’s algorithm assumes that all vertices are connected. But in a directed graph, every node is not reachable from every other node. So, Prim’s algorithm fails due to this reason. As it is visible in the graph, no node is reachable from node 4. Directed graphs fail the requirement that all vertices are connected.
  • 28. Why Kruskal’s Algorithm fails for directed graph ? • In Kruskal’s algorithm, In each step, it is checked that if the edges form a cycle with the spanning-tree formed so far. But Kruskal’s algorithm fails to detect the cycles in a directed graph as there are cases when there is no cycle between the vertices but Kruskal’s Algorithm assumes it to cycle and don’t take consider some edges due to which Kruskal’s Algorithm fails for directed graph. For example:
  • 29. Kruskal's Algorithm Finding MST? Arrange all the edges in ascending order to their weight.
  • 31. Kruskal's Algorithm Finding MST? Arrange all the edges in ascending order to their weight. edge weight (7,6) 1 (5,6) 2
  • 32. Finding MST? Arrange all the edges in ascending order to their weight. edge weight (6,7) 1 (6,5) (2,8) 2 (2,5) 4 (8,6) 6 (2,3)(7,8) 7 (1,2) (0,7) 8 (3,4) 9 (5,4) 10 (1,7) 11 (3,5) 14 Total cost= 41
  • 34. 0 2 5 2 10 25 6 7 14 11 15 12 2 0 2 1 3 4 5 6 0 2 2 10 6 11 12 2 0 2 1 3 4 5 6 Total cost:43
  • 36. Kruskal’s algorithm Step1: set minimum spanning tree (MST) initially empty. A=Ø Step2: For each vertex V Ɛ V(G) create (V) no. of tree where tree contain all vertex no edge. Step3: Set the edges into ascending order according to their weights and select the edge which has minimum weight. Step4: if that forms cycles rejected it otherwise add it. Step5: repeat until the single tree is formed. Step 6: return MST.
  • 37. Prims Algorithm For a Graph G={V,E} Assume S= Set of vertices in MST A= Set of edges in MST Step1: select any vertex ‘r’ and set S={r} and also set A= {Ø} Step2: Find the smallest weight edge such that one end point in S and other in { V-S}. Step3: Now add this edge into the set A and set another point into set S Step 4:IF {V-S} =Ø then Stop and output is return i.e. MST otherwise goto Step2.
  • 38. 0 2 5 2 10 25 6 7 14 11 15 12 2 0 2 1 3 4 5 6 Set of Vertices in MST Edges Selected edges (less weight) { 0} (0,1)(0,2) 0 2 2 0 2
  • 39. 0 2 5 2 10 25 6 7 14 11 15 12 2 0 2 1 3 4 5 6 Set of Vertices in MST Edges Selected edges (less weight) { 0} (0,1)(0,2) (0,2) {0,2} (0,1)(2,1)(2,3)(2,5) (2,1) {0,2,1} 0 2 2 0 2 2 1
  • 40. 0 2 5 2 10 25 6 7 14 11 15 12 2 0 2 1 3 4 5 6 Set of Vertices in MST Edges Selected edges (less weight) { 0} (0,1)(0,2) (0,2) {0,2} (0,1)(2,1)(2,3)(2,5) (2,1) {0,2,1} (2,3)(2,5)(1,3)(1,4) (1,3) 0 2 2 0 2 2 1 6 3
  • 41. 0 2 5 2 10 25 6 7 14 11 15 12 2 0 2 1 3 4 5 6 Set of Vertices in MST Edges Selected edges (less weight) { 0} (0,1)(0,2) (0,2) {0,2} (0,1)(2,1)(2,3)(2,5) (2,1) {0,2,1} (2,3)(2,5)(1,3)(1,4) (1,3) {0,2,1,3} (2,5)(1,4)(3,6) (2,5) {0,2,1,3,5} (1,4)(3,6)(5,4)(5,6) (5,4) {0,1,2,3,4,5} (3,6)(5,6)(4,6) (4,6) {0,1,2,3,4,5,6} - - 0 2 2 10 6 11 12 2 0 2 1 3 4 5 6 Total cost=2+2+6=10+12+11= 43
  • 42. Single Source Shortest path For a given Graph G= (V, E), we want to find a shortest path from a give source vetex S Ɛ V o every vertex v Ɛ V . A single source shortest path may be computed with the following algorithms. 1. Dijkstra's Algorithm
  • 43. Dijkstra's Algorithm Dijakstra’s algo solves the single source shortest path problem on a weighted , directed graph G=(V,E) for the case in which all edge weights are non negative. The Dijaksta’s algorithm uses a greedy strategies i.e. it always select the lightest or closest vertex.
  • 44. Find the shortest path from source vertex to all other vertex 0 2 o x 0 u 0 v 0 y 10 5 7 2 3 1 4 6 Source vertex S=0 S=Finally contains vertex of final shortest path Q= Priority queue used to store vertices based on their distance from source vertex 2
  • 45. Find the shortest path from source vertex to all other vertex 0 2 o x 0 u 0 v 0 y 10 5 7 2 3 1 4 6 Source vertex S=0 S=Finally contains vertex of final shortest path Q= Priority queue used to store vertices based on their distance from source vertex Initially S= { Ø} Q= o u v x y 0 ∞ ∞ ∞ ∞
  • 46. 0 2 x 0 u 0 v 0 y 10 5 7 2 3 1 4 6 Initially S= { Ø} Q= o u v x y 0 ∞ ∞ ∞ ∞ Q= extract minQueue {Q} S={o} 0 ∞ ∞ ∞ ∞ 2
  • 47. 0 2 x 0 u 0 v 0 y 10 5 7 2 3 1 4 6 Initially S= { Ø} Q= o u v x y 0 ∞ ∞ ∞ ∞ Q= extract minQueue {Q} S={o} Q= u v x y 10 ∞ 5 ∞ Q= extract minQueue {Q} S={o, x} 0 ∞ ∞ ∞ ∞ 0 2 x 0 u 0 v 0 y 10 5 7 2 3 1 4 6 0 ∞ 5 ∞ ∞ 2 2
  • 48. S={o, x} Q= u v y 8 ∞ 7 Q= extract minQueue {Q} S={o,x,y} 0 2 x 0 u 0 v 0 y 10 5 7 2 3 1 4 6 0 ∞ 5 ∞ 7 2
  • 49. S={o, x} Q= u v y 8 ∞ 7 Q= extract minQueue {Q} S={o,x,y} Q= u v 8 13 Q= extract minQueue {Q} S={o, x, y , u } 0 2 x 0 u 0 v 0 y 10 5 7 2 3 1 4 6 0 ∞ 5 ∞ 7 2 0 2 x 0 u 0 v 0 y 10 5 7 2 3 1 4 6 0 8 5 ∞ 7 2
  • 50. S={o, x, y , u } Q= v 9 Q= extract minQueue {Q} S={o,x,y, u, v} 0 2 x 0 u 0 v 0 y 10 5 7 2 3 1 4 6 0 8 5 9 7 2 0 2 x 0 u 0 v 0 y 5 3 1 2
  • 54. DIJKSTRA’S ALGO Step1: Initialize single source vertex by zero. Step2: Initialize S= {Ø} where S, finally contains vertices of final shortest path from source vertex. Step3: Initialize priority queue Q such that Q V(G) Step 4: while priority queue (Q) is not empty do step 5 to 7 Step5: select vertex u of minimum value from Queue such that u extract of minQueue Step6: new S SU {u} Step7: Insert all the adjacent nodes into Queue,assign their weights as sum of weight of parent and edge weight. Step8: End;
  • 55. 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. The reach-ability matrix is called the transitive closure of a graph.
  • 56. Transitive closure of graphs is 1 1 1 1 1 1 1 1 1 1 1 1 0 0 0 1
  • 57. Floyd Warshall Algorithm, find the shortest path distance between every pair of vertices
  • 61. Floyd Warshall Algorithm Step1: We initialize the solution matrix same as the input graph matrix . Step2: Then we update the solution matrix by considering all vertices as an intermediate vertex. The idea is to one by one pick all vertices and updates all shortest paths which include the picked vertex as an intermediate vertex in the shortest path. When we pick vertex number k as an intermediate vertex, we already have considered vertices {0, 1, 2, .. k-1} as intermediate vertices. For every pair (i, j) of the source and destination vertices respectively. There are two possible cases: Cae1:k is not an intermediate vertex in shortest path from i to j. We keep the value of dist[i][j] as it is. Case2: k is an intermediate vertex in shortest path from i to j. We update the value of dist[i][j] as dist[i][k] + dist[k][j] if dist[i][j] > dist[i][k] + dist[k][j]