SlideShare a Scribd company logo
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]
Ad

Recommended

Need of SIEM when You have SOAR
Need of SIEM when You have SOAR
Siemplify
 
Deep Learning A-Z™: Regression - Multiple Linear Regression Intuition
Deep Learning A-Z™: Regression - Multiple Linear Regression Intuition
Kirill Eremenko
 
Lego serious play
Lego serious play
Charo Povedano
 
Ciberataques
Ciberataques
Ernesto Burgueño Cervantes
 
Tenable_One_Sales_Presentation_for_Customers.pptx
Tenable_One_Sales_Presentation_for_Customers.pptx
alex hincapie
 
SolarWInds-Incident-ppt.pptx
SolarWInds-Incident-ppt.pptx
TusharPuri20
 
Data Structures-Non Linear DataStructures-Graphs
Data Structures-Non Linear DataStructures-Graphs
sailaja156145
 
Unit ix graph
Unit ix graph
Tribhuvan University
 
Unit 4 dsuc
Unit 4 dsuc
Sweta Singh
 
Graph Algorithms
Graph Algorithms
Ashwin Shiv
 
Unit 9 graph
Unit 9 graph
Dabbal Singh Mahara
 
UNIT II - Graph Algorithms techniques.pptx
UNIT II - Graph Algorithms techniques.pptx
mayakishore55
 
130210107039 2130702
130210107039 2130702
Ketaki_Pattani
 
Graphs
Graphs
LavanyaJ28
 
Day 5 application of graph ,biconnectivity fdp on ds
Day 5 application of graph ,biconnectivity fdp on ds
GUNASUNDARISAPIIICSE
 
14. GRAPH in data structures and algorithm.ppt
14. GRAPH in data structures and algorithm.ppt
saurabhthege
 
Unit V - ppt.pptx
Unit V - ppt.pptx
Kongunadu College of Engineering and Technology
 
UNIT III.pptx
UNIT III.pptx
SwarndeviKm
 
Graph terminology and algorithm and tree.pptx
Graph terminology and algorithm and tree.pptx
asimshahzad8611
 
TREE ADT, TREE TRAVERSALS, BINARY TREE ADT
TREE ADT, TREE TRAVERSALS, BINARY TREE ADT
mohanrajm63
 
Graphs
Graphs
KomalPaliwal3
 
lecture 23 algorithm design and analysis
lecture 23 algorithm design and analysis
bluebirdrish666
 
Bfs dfs mst
Bfs dfs mst
AvichalVishnoi
 
Graph.pptx
Graph.pptx
Bharati Vidyapeeth COE, Navi Mumbai
 
Algorithms and data Chapter 3 V Graph.pptx
Algorithms and data Chapter 3 V Graph.pptx
zerihunnana
 
Graph data structures for ppt for understanding.pptx
Graph data structures for ppt for understanding.pptx
ramkumar649780
 
Unit-6 Graph.ppsx ppt
Unit-6 Graph.ppsx ppt
DhruvilSTATUS
 
Design and analysis of Algorithms Lecture 1 (BFS, DFS).ppsx
Design and analysis of Algorithms Lecture 1 (BFS, DFS).ppsx
SababAshfakFahim
 
IntroSlides-June-GDG-Cloud-Munich community [email protected]
IntroSlides-June-GDG-Cloud-Munich community [email protected]
Luiz Carneiro
 
System design handwritten notes guidance
System design handwritten notes guidance
Shabista Imam
 

More Related Content

Similar to GRAPH - DISCRETE STRUCTURE AND ALGORITHM (20)

Unit 4 dsuc
Unit 4 dsuc
Sweta Singh
 
Graph Algorithms
Graph Algorithms
Ashwin Shiv
 
Unit 9 graph
Unit 9 graph
Dabbal Singh Mahara
 
UNIT II - Graph Algorithms techniques.pptx
UNIT II - Graph Algorithms techniques.pptx
mayakishore55
 
130210107039 2130702
130210107039 2130702
Ketaki_Pattani
 
Graphs
Graphs
LavanyaJ28
 
Day 5 application of graph ,biconnectivity fdp on ds
Day 5 application of graph ,biconnectivity fdp on ds
GUNASUNDARISAPIIICSE
 
14. GRAPH in data structures and algorithm.ppt
14. GRAPH in data structures and algorithm.ppt
saurabhthege
 
Unit V - ppt.pptx
Unit V - ppt.pptx
Kongunadu College of Engineering and Technology
 
UNIT III.pptx
UNIT III.pptx
SwarndeviKm
 
Graph terminology and algorithm and tree.pptx
Graph terminology and algorithm and tree.pptx
asimshahzad8611
 
TREE ADT, TREE TRAVERSALS, BINARY TREE ADT
TREE ADT, TREE TRAVERSALS, BINARY TREE ADT
mohanrajm63
 
Graphs
Graphs
KomalPaliwal3
 
lecture 23 algorithm design and analysis
lecture 23 algorithm design and analysis
bluebirdrish666
 
Bfs dfs mst
Bfs dfs mst
AvichalVishnoi
 
Graph.pptx
Graph.pptx
Bharati Vidyapeeth COE, Navi Mumbai
 
Algorithms and data Chapter 3 V Graph.pptx
Algorithms and data Chapter 3 V Graph.pptx
zerihunnana
 
Graph data structures for ppt for understanding.pptx
Graph data structures for ppt for understanding.pptx
ramkumar649780
 
Unit-6 Graph.ppsx ppt
Unit-6 Graph.ppsx ppt
DhruvilSTATUS
 
Design and analysis of Algorithms Lecture 1 (BFS, DFS).ppsx
Design and analysis of Algorithms Lecture 1 (BFS, DFS).ppsx
SababAshfakFahim
 
Graph Algorithms
Graph Algorithms
Ashwin Shiv
 
UNIT II - Graph Algorithms techniques.pptx
UNIT II - Graph Algorithms techniques.pptx
mayakishore55
 
Day 5 application of graph ,biconnectivity fdp on ds
Day 5 application of graph ,biconnectivity fdp on ds
GUNASUNDARISAPIIICSE
 
14. GRAPH in data structures and algorithm.ppt
14. GRAPH in data structures and algorithm.ppt
saurabhthege
 
Graph terminology and algorithm and tree.pptx
Graph terminology and algorithm and tree.pptx
asimshahzad8611
 
TREE ADT, TREE TRAVERSALS, BINARY TREE ADT
TREE ADT, TREE TRAVERSALS, BINARY TREE ADT
mohanrajm63
 
lecture 23 algorithm design and analysis
lecture 23 algorithm design and analysis
bluebirdrish666
 
Algorithms and data Chapter 3 V Graph.pptx
Algorithms and data Chapter 3 V Graph.pptx
zerihunnana
 
Graph data structures for ppt for understanding.pptx
Graph data structures for ppt for understanding.pptx
ramkumar649780
 
Unit-6 Graph.ppsx ppt
Unit-6 Graph.ppsx ppt
DhruvilSTATUS
 
Design and analysis of Algorithms Lecture 1 (BFS, DFS).ppsx
Design and analysis of Algorithms Lecture 1 (BFS, DFS).ppsx
SababAshfakFahim
 

Recently uploaded (20)

IntroSlides-June-GDG-Cloud-Munich community [email protected]
IntroSlides-June-GDG-Cloud-Munich community [email protected]
Luiz Carneiro
 
System design handwritten notes guidance
System design handwritten notes guidance
Shabista Imam
 
Complete guidance book of Asp.Net Web API
Complete guidance book of Asp.Net Web API
Shabista Imam
 
DESIGN OF REINFORCED CONCRETE ELEMENTS S
DESIGN OF REINFORCED CONCRETE ELEMENTS S
prabhusp8
 
NEW Strengthened Senior High School Gen Math.pptx
NEW Strengthened Senior High School Gen Math.pptx
DaryllWhere
 
Machine Learning - Classification Algorithms
Machine Learning - Classification Algorithms
resming1
 
Fundamentals of Digital Design_Class_12th April.pptx
Fundamentals of Digital Design_Class_12th April.pptx
drdebarshi1993
 
Proposal for folders structure division in projects.pdf
Proposal for folders structure division in projects.pdf
Mohamed Ahmed
 
Unit III_One Dimensional Consolidation theory
Unit III_One Dimensional Consolidation theory
saravananr808639
 
Modern multi-proposer consensus implementations
Modern multi-proposer consensus implementations
François Garillot
 
special_edition_using_visual_foxpro_6.pdf
special_edition_using_visual_foxpro_6.pdf
Shabista Imam
 
Tally.ERP 9 at a Glance.book - Tally Solutions .pdf
Tally.ERP 9 at a Glance.book - Tally Solutions .pdf
Shabista Imam
 
Stay Safe Women Security Android App Project Report.pdf
Stay Safe Women Security Android App Project Report.pdf
Kamal Acharya
 
Cadastral Maps
Cadastral Maps
Google
 
Center Enamel can Provide Aluminum Dome Roofs for diesel tank.docx
Center Enamel can Provide Aluminum Dome Roofs for diesel tank.docx
CenterEnamel
 
Tesla-Stock-Analysis-and-Forecast.pptx (1).pptx
Tesla-Stock-Analysis-and-Forecast.pptx (1).pptx
moonsony54
 
FUNDAMENTALS OF COMPUTER ORGANIZATION AND ARCHITECTURE
FUNDAMENTALS OF COMPUTER ORGANIZATION AND ARCHITECTURE
Shabista Imam
 
David Boutry - Mentors Junior Developers
David Boutry - Mentors Junior Developers
David Boutry
 
IPL_Logic_Flow.pdf Mainframe IPLMainframe IPL
IPL_Logic_Flow.pdf Mainframe IPLMainframe IPL
KhadijaKhadijaAouadi
 
Introduction to sensing and Week-1.pptx
Introduction to sensing and Week-1.pptx
KNaveenKumarECE
 
System design handwritten notes guidance
System design handwritten notes guidance
Shabista Imam
 
Complete guidance book of Asp.Net Web API
Complete guidance book of Asp.Net Web API
Shabista Imam
 
DESIGN OF REINFORCED CONCRETE ELEMENTS S
DESIGN OF REINFORCED CONCRETE ELEMENTS S
prabhusp8
 
NEW Strengthened Senior High School Gen Math.pptx
NEW Strengthened Senior High School Gen Math.pptx
DaryllWhere
 
Machine Learning - Classification Algorithms
Machine Learning - Classification Algorithms
resming1
 
Fundamentals of Digital Design_Class_12th April.pptx
Fundamentals of Digital Design_Class_12th April.pptx
drdebarshi1993
 
Proposal for folders structure division in projects.pdf
Proposal for folders structure division in projects.pdf
Mohamed Ahmed
 
Unit III_One Dimensional Consolidation theory
Unit III_One Dimensional Consolidation theory
saravananr808639
 
Modern multi-proposer consensus implementations
Modern multi-proposer consensus implementations
François Garillot
 
special_edition_using_visual_foxpro_6.pdf
special_edition_using_visual_foxpro_6.pdf
Shabista Imam
 
Tally.ERP 9 at a Glance.book - Tally Solutions .pdf
Tally.ERP 9 at a Glance.book - Tally Solutions .pdf
Shabista Imam
 
Stay Safe Women Security Android App Project Report.pdf
Stay Safe Women Security Android App Project Report.pdf
Kamal Acharya
 
Cadastral Maps
Cadastral Maps
Google
 
Center Enamel can Provide Aluminum Dome Roofs for diesel tank.docx
Center Enamel can Provide Aluminum Dome Roofs for diesel tank.docx
CenterEnamel
 
Tesla-Stock-Analysis-and-Forecast.pptx (1).pptx
Tesla-Stock-Analysis-and-Forecast.pptx (1).pptx
moonsony54
 
FUNDAMENTALS OF COMPUTER ORGANIZATION AND ARCHITECTURE
FUNDAMENTALS OF COMPUTER ORGANIZATION AND ARCHITECTURE
Shabista Imam
 
David Boutry - Mentors Junior Developers
David Boutry - Mentors Junior Developers
David Boutry
 
IPL_Logic_Flow.pdf Mainframe IPLMainframe IPL
IPL_Logic_Flow.pdf Mainframe IPLMainframe IPL
KhadijaKhadijaAouadi
 
Introduction to sensing and Week-1.pptx
Introduction to sensing and Week-1.pptx
KNaveenKumarECE
 
Ad

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]