SlideShare a Scribd company logo
1 of 61
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.
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
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.
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.
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
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
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

Similar to GRAPH - DISCRETE STRUCTURE AND ALGORITHM

Data structure computer graphs
Data structure computer graphsData structure computer graphs
Data structure computer graphsKumar
 
smith chart By Engr Mimkhan
smith chart By Engr Mimkhansmith chart By Engr Mimkhan
smith chart By Engr MimkhanEngr Mimkhan
 
Chap10 slides
Chap10 slidesChap10 slides
Chap10 slidesHJ DS
 
Graphs and eularian circuit & path with c++ program
Graphs and eularian circuit & path with c++ programGraphs and eularian circuit & path with c++ program
Graphs and eularian circuit & path with c++ programMuhammad Danish Badar
 
Graph Theory,Graph Terminologies,Planar Graph & Graph Colouring
Graph Theory,Graph Terminologies,Planar Graph & Graph ColouringGraph Theory,Graph Terminologies,Planar Graph & Graph Colouring
Graph Theory,Graph Terminologies,Planar Graph & Graph ColouringSaurabh Kaushik
 
Unit-6 Graph.ppsx ppt
Unit-6 Graph.ppsx                                       pptUnit-6 Graph.ppsx                                       ppt
Unit-6 Graph.ppsx pptDhruvilSTATUS
 
Graph in data structure
Graph in data structureGraph in data structure
Graph in data structurePooja Bhojwani
 
EDGE-NEIGHBOR RUPTURE DEGREE ON GRAPH OPERATIONS
EDGE-NEIGHBOR RUPTURE DEGREE ON GRAPH OPERATIONSEDGE-NEIGHBOR RUPTURE DEGREE ON GRAPH OPERATIONS
EDGE-NEIGHBOR RUPTURE DEGREE ON GRAPH OPERATIONSmathsjournal
 
EDGE-NEIGHBOR RUPTURE DEGREE ON GRAPH OPERATIONS
EDGE-NEIGHBOR RUPTURE DEGREE ON GRAPH OPERATIONSEDGE-NEIGHBOR RUPTURE DEGREE ON GRAPH OPERATIONS
EDGE-NEIGHBOR RUPTURE DEGREE ON GRAPH OPERATIONSmathsjournal
 
EDGE-NEIGHBOR RUPTURE DEGREE ON GRAPH OPERATIONS
EDGE-NEIGHBOR RUPTURE DEGREE ON GRAPH OPERATIONSEDGE-NEIGHBOR RUPTURE DEGREE ON GRAPH OPERATIONS
EDGE-NEIGHBOR RUPTURE DEGREE ON GRAPH OPERATIONSmathsjournal
 
EDGE-NEIGHBOR RUPTURE DEGREE ON GRAPH OPERATIONS
EDGE-NEIGHBOR RUPTURE DEGREE ON GRAPH OPERATIONSEDGE-NEIGHBOR RUPTURE DEGREE ON GRAPH OPERATIONS
EDGE-NEIGHBOR RUPTURE DEGREE ON GRAPH OPERATIONSmathsjournal
 
Comparative Report Ed098
Comparative Report Ed098Comparative Report Ed098
Comparative Report Ed098mikebrowl
 
QR Factorizations and SVDs for Tall-and-skinny Matrices in MapReduce Architec...
QR Factorizations and SVDs for Tall-and-skinny Matrices in MapReduce Architec...QR Factorizations and SVDs for Tall-and-skinny Matrices in MapReduce Architec...
QR Factorizations and SVDs for Tall-and-skinny Matrices in MapReduce Architec...Austin Benson
 

Similar to GRAPH - DISCRETE STRUCTURE AND ALGORITHM (20)

Graphs
GraphsGraphs
Graphs
 
Data structure computer graphs
Data structure computer graphsData structure computer graphs
Data structure computer graphs
 
Optimisation random graph presentation
Optimisation random graph presentationOptimisation random graph presentation
Optimisation random graph presentation
 
smith chart By Engr Mimkhan
smith chart By Engr Mimkhansmith chart By Engr Mimkhan
smith chart By Engr Mimkhan
 
Chap10 slides
Chap10 slidesChap10 slides
Chap10 slides
 
Weighted graphs
Weighted graphsWeighted graphs
Weighted graphs
 
Graphs and eularian circuit & path with c++ program
Graphs and eularian circuit & path with c++ programGraphs and eularian circuit & path with c++ program
Graphs and eularian circuit & path with c++ program
 
Graph Theory,Graph Terminologies,Planar Graph & Graph Colouring
Graph Theory,Graph Terminologies,Planar Graph & Graph ColouringGraph Theory,Graph Terminologies,Planar Graph & Graph Colouring
Graph Theory,Graph Terminologies,Planar Graph & Graph Colouring
 
Unit-6 Graph.ppsx ppt
Unit-6 Graph.ppsx                                       pptUnit-6 Graph.ppsx                                       ppt
Unit-6 Graph.ppsx ppt
 
Graph in data structure
Graph in data structureGraph in data structure
Graph in data structure
 
LEC 12-DSALGO-GRAPHS(final12).pdf
LEC 12-DSALGO-GRAPHS(final12).pdfLEC 12-DSALGO-GRAPHS(final12).pdf
LEC 12-DSALGO-GRAPHS(final12).pdf
 
09_DS_MCA_Graphs.pdf
09_DS_MCA_Graphs.pdf09_DS_MCA_Graphs.pdf
09_DS_MCA_Graphs.pdf
 
EDGE-NEIGHBOR RUPTURE DEGREE ON GRAPH OPERATIONS
EDGE-NEIGHBOR RUPTURE DEGREE ON GRAPH OPERATIONSEDGE-NEIGHBOR RUPTURE DEGREE ON GRAPH OPERATIONS
EDGE-NEIGHBOR RUPTURE DEGREE ON GRAPH OPERATIONS
 
EDGE-NEIGHBOR RUPTURE DEGREE ON GRAPH OPERATIONS
EDGE-NEIGHBOR RUPTURE DEGREE ON GRAPH OPERATIONSEDGE-NEIGHBOR RUPTURE DEGREE ON GRAPH OPERATIONS
EDGE-NEIGHBOR RUPTURE DEGREE ON GRAPH OPERATIONS
 
EDGE-NEIGHBOR RUPTURE DEGREE ON GRAPH OPERATIONS
EDGE-NEIGHBOR RUPTURE DEGREE ON GRAPH OPERATIONSEDGE-NEIGHBOR RUPTURE DEGREE ON GRAPH OPERATIONS
EDGE-NEIGHBOR RUPTURE DEGREE ON GRAPH OPERATIONS
 
EDGE-NEIGHBOR RUPTURE DEGREE ON GRAPH OPERATIONS
EDGE-NEIGHBOR RUPTURE DEGREE ON GRAPH OPERATIONSEDGE-NEIGHBOR RUPTURE DEGREE ON GRAPH OPERATIONS
EDGE-NEIGHBOR RUPTURE DEGREE ON GRAPH OPERATIONS
 
Bfs dfs mst
Bfs dfs mstBfs dfs mst
Bfs dfs mst
 
Comparative Report Ed098
Comparative Report Ed098Comparative Report Ed098
Comparative Report Ed098
 
QR Factorizations and SVDs for Tall-and-skinny Matrices in MapReduce Architec...
QR Factorizations and SVDs for Tall-and-skinny Matrices in MapReduce Architec...QR Factorizations and SVDs for Tall-and-skinny Matrices in MapReduce Architec...
QR Factorizations and SVDs for Tall-and-skinny Matrices in MapReduce Architec...
 
Graphs
GraphsGraphs
Graphs
 

Recently uploaded

DATA ANALYTICS PPT definition usage example
DATA ANALYTICS PPT definition usage exampleDATA ANALYTICS PPT definition usage example
DATA ANALYTICS PPT definition usage examplePragyanshuParadkar1
 
VICTOR MAESTRE RAMIREZ - Planetary Defender on NASA's Double Asteroid Redirec...
VICTOR MAESTRE RAMIREZ - Planetary Defender on NASA's Double Asteroid Redirec...VICTOR MAESTRE RAMIREZ - Planetary Defender on NASA's Double Asteroid Redirec...
VICTOR MAESTRE RAMIREZ - Planetary Defender on NASA's Double Asteroid Redirec...VICTOR MAESTRE RAMIREZ
 
Software and Systems Engineering Standards: Verification and Validation of Sy...
Software and Systems Engineering Standards: Verification and Validation of Sy...Software and Systems Engineering Standards: Verification and Validation of Sy...
Software and Systems Engineering Standards: Verification and Validation of Sy...VICTOR MAESTRE RAMIREZ
 
Artificial-Intelligence-in-Electronics (K).pptx
Artificial-Intelligence-in-Electronics (K).pptxArtificial-Intelligence-in-Electronics (K).pptx
Artificial-Intelligence-in-Electronics (K).pptxbritheesh05
 
main PPT.pptx of girls hostel security using rfid
main PPT.pptx of girls hostel security using rfidmain PPT.pptx of girls hostel security using rfid
main PPT.pptx of girls hostel security using rfidNikhilNagaraju
 
Concrete Mix Design - IS 10262-2019 - .pptx
Concrete Mix Design - IS 10262-2019 - .pptxConcrete Mix Design - IS 10262-2019 - .pptx
Concrete Mix Design - IS 10262-2019 - .pptxKartikeyaDwivedi3
 
What are the advantages and disadvantages of membrane structures.pptx
What are the advantages and disadvantages of membrane structures.pptxWhat are the advantages and disadvantages of membrane structures.pptx
What are the advantages and disadvantages of membrane structures.pptxwendy cai
 
Application of Residue Theorem to evaluate real integrations.pptx
Application of Residue Theorem to evaluate real integrations.pptxApplication of Residue Theorem to evaluate real integrations.pptx
Application of Residue Theorem to evaluate real integrations.pptx959SahilShah
 
HARMONY IN THE NATURE AND EXISTENCE - Unit-IV
HARMONY IN THE NATURE AND EXISTENCE - Unit-IVHARMONY IN THE NATURE AND EXISTENCE - Unit-IV
HARMONY IN THE NATURE AND EXISTENCE - Unit-IVRajaP95
 
pipeline in computer architecture design
pipeline in computer architecture  designpipeline in computer architecture  design
pipeline in computer architecture designssuser87fa0c1
 
UNIT III ANALOG ELECTRONICS (BASIC ELECTRONICS)
UNIT III ANALOG ELECTRONICS (BASIC ELECTRONICS)UNIT III ANALOG ELECTRONICS (BASIC ELECTRONICS)
UNIT III ANALOG ELECTRONICS (BASIC ELECTRONICS)Dr SOUNDIRARAJ N
 
Call Girls Delhi {Jodhpur} 9711199012 high profile service
Call Girls Delhi {Jodhpur} 9711199012 high profile serviceCall Girls Delhi {Jodhpur} 9711199012 high profile service
Call Girls Delhi {Jodhpur} 9711199012 high profile servicerehmti665
 
Risk Assessment For Installation of Drainage Pipes.pdf
Risk Assessment For Installation of Drainage Pipes.pdfRisk Assessment For Installation of Drainage Pipes.pdf
Risk Assessment For Installation of Drainage Pipes.pdfROCENODodongVILLACER
 
CCS355 Neural Networks & Deep Learning Unit 1 PDF notes with Question bank .pdf
CCS355 Neural Networks & Deep Learning Unit 1 PDF notes with Question bank .pdfCCS355 Neural Networks & Deep Learning Unit 1 PDF notes with Question bank .pdf
CCS355 Neural Networks & Deep Learning Unit 1 PDF notes with Question bank .pdfAsst.prof M.Gokilavani
 
INFLUENCE OF NANOSILICA ON THE PROPERTIES OF CONCRETE
INFLUENCE OF NANOSILICA ON THE PROPERTIES OF CONCRETEINFLUENCE OF NANOSILICA ON THE PROPERTIES OF CONCRETE
INFLUENCE OF NANOSILICA ON THE PROPERTIES OF CONCRETEroselinkalist12
 
Call Girls Narol 7397865700 Independent Call Girls
Call Girls Narol 7397865700 Independent Call GirlsCall Girls Narol 7397865700 Independent Call Girls
Call Girls Narol 7397865700 Independent Call Girlsssuser7cb4ff
 
complete construction, environmental and economics information of biomass com...
complete construction, environmental and economics information of biomass com...complete construction, environmental and economics information of biomass com...
complete construction, environmental and economics information of biomass com...asadnawaz62
 

Recently uploaded (20)

DATA ANALYTICS PPT definition usage example
DATA ANALYTICS PPT definition usage exampleDATA ANALYTICS PPT definition usage example
DATA ANALYTICS PPT definition usage example
 
VICTOR MAESTRE RAMIREZ - Planetary Defender on NASA's Double Asteroid Redirec...
VICTOR MAESTRE RAMIREZ - Planetary Defender on NASA's Double Asteroid Redirec...VICTOR MAESTRE RAMIREZ - Planetary Defender on NASA's Double Asteroid Redirec...
VICTOR MAESTRE RAMIREZ - Planetary Defender on NASA's Double Asteroid Redirec...
 
Software and Systems Engineering Standards: Verification and Validation of Sy...
Software and Systems Engineering Standards: Verification and Validation of Sy...Software and Systems Engineering Standards: Verification and Validation of Sy...
Software and Systems Engineering Standards: Verification and Validation of Sy...
 
Artificial-Intelligence-in-Electronics (K).pptx
Artificial-Intelligence-in-Electronics (K).pptxArtificial-Intelligence-in-Electronics (K).pptx
Artificial-Intelligence-in-Electronics (K).pptx
 
main PPT.pptx of girls hostel security using rfid
main PPT.pptx of girls hostel security using rfidmain PPT.pptx of girls hostel security using rfid
main PPT.pptx of girls hostel security using rfid
 
young call girls in Rajiv Chowk🔝 9953056974 🔝 Delhi escort Service
young call girls in Rajiv Chowk🔝 9953056974 🔝 Delhi escort Serviceyoung call girls in Rajiv Chowk🔝 9953056974 🔝 Delhi escort Service
young call girls in Rajiv Chowk🔝 9953056974 🔝 Delhi escort Service
 
Concrete Mix Design - IS 10262-2019 - .pptx
Concrete Mix Design - IS 10262-2019 - .pptxConcrete Mix Design - IS 10262-2019 - .pptx
Concrete Mix Design - IS 10262-2019 - .pptx
 
What are the advantages and disadvantages of membrane structures.pptx
What are the advantages and disadvantages of membrane structures.pptxWhat are the advantages and disadvantages of membrane structures.pptx
What are the advantages and disadvantages of membrane structures.pptx
 
Application of Residue Theorem to evaluate real integrations.pptx
Application of Residue Theorem to evaluate real integrations.pptxApplication of Residue Theorem to evaluate real integrations.pptx
Application of Residue Theorem to evaluate real integrations.pptx
 
HARMONY IN THE NATURE AND EXISTENCE - Unit-IV
HARMONY IN THE NATURE AND EXISTENCE - Unit-IVHARMONY IN THE NATURE AND EXISTENCE - Unit-IV
HARMONY IN THE NATURE AND EXISTENCE - Unit-IV
 
pipeline in computer architecture design
pipeline in computer architecture  designpipeline in computer architecture  design
pipeline in computer architecture design
 
UNIT III ANALOG ELECTRONICS (BASIC ELECTRONICS)
UNIT III ANALOG ELECTRONICS (BASIC ELECTRONICS)UNIT III ANALOG ELECTRONICS (BASIC ELECTRONICS)
UNIT III ANALOG ELECTRONICS (BASIC ELECTRONICS)
 
Call Girls Delhi {Jodhpur} 9711199012 high profile service
Call Girls Delhi {Jodhpur} 9711199012 high profile serviceCall Girls Delhi {Jodhpur} 9711199012 high profile service
Call Girls Delhi {Jodhpur} 9711199012 high profile service
 
Risk Assessment For Installation of Drainage Pipes.pdf
Risk Assessment For Installation of Drainage Pipes.pdfRisk Assessment For Installation of Drainage Pipes.pdf
Risk Assessment For Installation of Drainage Pipes.pdf
 
CCS355 Neural Networks & Deep Learning Unit 1 PDF notes with Question bank .pdf
CCS355 Neural Networks & Deep Learning Unit 1 PDF notes with Question bank .pdfCCS355 Neural Networks & Deep Learning Unit 1 PDF notes with Question bank .pdf
CCS355 Neural Networks & Deep Learning Unit 1 PDF notes with Question bank .pdf
 
INFLUENCE OF NANOSILICA ON THE PROPERTIES OF CONCRETE
INFLUENCE OF NANOSILICA ON THE PROPERTIES OF CONCRETEINFLUENCE OF NANOSILICA ON THE PROPERTIES OF CONCRETE
INFLUENCE OF NANOSILICA ON THE PROPERTIES OF CONCRETE
 
POWER SYSTEMS-1 Complete notes examples
POWER SYSTEMS-1 Complete notes  examplesPOWER SYSTEMS-1 Complete notes  examples
POWER SYSTEMS-1 Complete notes examples
 
9953056974 Call Girls In South Ex, Escorts (Delhi) NCR.pdf
9953056974 Call Girls In South Ex, Escorts (Delhi) NCR.pdf9953056974 Call Girls In South Ex, Escorts (Delhi) NCR.pdf
9953056974 Call Girls In South Ex, Escorts (Delhi) NCR.pdf
 
Call Girls Narol 7397865700 Independent Call Girls
Call Girls Narol 7397865700 Independent Call GirlsCall Girls Narol 7397865700 Independent Call Girls
Call Girls Narol 7397865700 Independent Call Girls
 
complete construction, environmental and economics information of biomass com...
complete construction, environmental and economics information of biomass com...complete construction, environmental and economics information of biomass com...
complete construction, environmental and economics information of biomass com...
 

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.
  • 13.
  • 14.
  • 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
  • 19.
  • 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.
  • 23.
  • 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.
  • 30.
  • 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
  • 35.
  • 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
  • 58.
  • 59.
  • 60.
  • 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]