SlideShare a Scribd company logo
1 of 81
Graph
.
1
Introduction
• Graph is a collection of two finite sets (V and E)
where V contains the finite number of elements
called vertices(nodes) and E contains the finite
number of elements called the edges.
• A graph G = (V,E) consists of a finite set of
vertices, V, and a finite set of edges E.
• Each edge is a pair (v,w) where v, w  V.
• Node
–has a unique label for identification
2
Contd.
• Edge
– connects two nodes
– can be a
• directed edge
• undirected edge
3
Contd.
• “Graphs” are the mathematical and computer
science abstraction that capture many shared
and common concepts of real-life objects such
as
–networks, e.g.
• electricity
• Internet
–road maps
Etc…
4
Example
.
5
A B
C
D
E F
Terminologies
1. Adjacent vertices: Two vertices are said to be
adjacent if they are connected by an edge.
For example , Band C are adjacent to vertex A
because separate edges connect B and C to A.
2. Adjacent edges: Two edges are said to be
adjacent if they are incident on the common
vertex. For example, edges CD and CE are
adjacent because they are incident on the
common vertex C.
6
Contd.
3. Path
–connected sequence of edges:
–e.g. A to B to D to C to E
–if the edges are directed then the path has
to follow the directions of the edges.
4. A cyclic path is a path such that
– There are at least two vertices on the path
– w1 = wn (path starts and ends at same vertex)
i.e. a cycle is a path that goes in a loop from a node
back to itself, and without using the same node
twice 7
Contd.
• E.g. A-B-E-A in
B
C
D
E
F
G
A
5. LOOP: It is a special cycle that starts and ends
with the same vertex without visiting any other
vertex.
8
contd.
• An acyclic path is a path where each vertex is
unique
• An “acyclic graph” has no cycles.
5. Degree of a vertex: The degree of a vertex is
the number of lines incident on it.
6. Connected vertices: Two vertices are said to
be connected if there is a path between them.
7. Out-degree: The out-degree of a directed
graph is the number of arcs leaving the vertex.
8. In-degree: The in-degree of a directed graph
is the number of arcs entering the vertex.
9
Test Your Knowledge
Cyclic or Acyclic?
10
11
Contd.
Example: What are the in-degrees and out-degrees of the
vertices a, b, c, d in this graph:
a
b
c
d
deg-(a) = 1
deg+(a) = 2
deg-(b) = 4
deg+(b) = 2
deg-(d) = 2
deg+(d) = 1
deg-(c) = 0
deg+(c) = 2
Types of Graph
1. Simple graph: A graph that does not contain
any loop or multiple edges between two
vertices is called a simple graph.
A B
C
D
E F
12
Contd.
2. Multi-graph: A graph that contains more than
one edge between two vertices is called a
multi-graph.
Example: B
C
D
E F
13
Contd.
3. Directed graph: In a directed graph, the edges
are arrows.
• Directed graphs show the flow from one node
to another and not vise versa.
14
Contd.
4. Undirected Graphs: In an undirected graph,
the edges are lines.
• Undirected graphs show a relationship
between two nodes.
15
Contd.
5. Weighted graph: A graph is known as a
weighted graph if a weight or metric is
associated with each edge.
16
Contd.
6. Complete graph: An undirected graph that
has an edge between every pair of vertices is
called a complete graph.
• Note: A directed graph can also be a complete
graph; in that case, there must be an edge
from every vertex to every other vertex.
17
Representation of Graph
• Graphs can be represented by their adjacency
matrix or an edge (or vertex) list. i.e.
1. Sequential representation using adjacency
matrices.
2. Linked representation using adjacent list or
linked list of neighbors.
Adjacency matrices have a value ai,j = 1 if
nodes i and j share an edge; 0 otherwise.
18
19
Representing Graphs
a
b
c
d
a, d
b
a, d
c
a, b, c
d
b, c, d
a
Adjacent Vertices
Vertex
Sequential Representation using
Adjacency Matrix
An undirected graph and its adjacency matrix representation.
A directed graph and its adjacency matrix representation.
A
B
C
D
E
A
A B C D E
A 0 1 1 0 0
B 0 0 0 1 0
C 0 1 0 0 1
D 0 0 0 0 1
E 0 0 0 0 0
20
Traversal of Graph
• Some application of the graph require
systematically visiting all the nodes.
• In a graph ,we can reach to the vertex from
multiple paths existing in the graph, so we can
say that graph traversal is not as simple as
traversal in other data structure.
• There are two standard traversal methods
generally used with graph:
1. Breadth first search(BFS)
2. Depth first search(DFS)
21
Breadth first search(BFS)
• BFS is that it starts traversing the graph from the
starting node (any selected node) and then traverses all
the nodes which are directly connected to the starting
node in a particular order.
• First, the starting node is examined and all the adjacent
nodes are placed into the queue.
• The node traversed is marked as visited.
• The next node is taken from the front of the queue and
examined;
• This node also marked as visited and all the neighbors
of this node are placed into the rear of the queue.
• In BFS all the nodes at a particular level are processed
first, only then can we move to the next level.
22
BFS algorithm
1. Start traversing the given graph with the given
number of vertices and edges
2. Insert the starting source vertex S to the queue
and mark it as visited
3. Repeat
a. Remove the front node K of the queue and
examine it.
b. Insert all the adjacent nodes of K (which are not
visited so far)to the queue and mark them as
visited
4. Until queue is empty
5. Exit
23
Example
• Traverse the following graph using the BFS
starting from E.
24
F
E
A
B
C
D
Solution
Step 1: start traversing the graph given.
Step 2: Insert the starting source vertex E to the
queue and mark it as visited.
Step 3: Take the front node E. Examine it.
Step 4: insert all the adjacent nodes of E to queue
named as Q and mark them as visited.
25
E
0 1 2 3 4 5
F=0,R=0
B F
0 1 2 3 4 5
F=0,R=1
Contd.
Step 5: take the front node B. examine it.
Step 6: insert all the adjacent nodes of B to Q
and mark them as visited.
26
F
0 1 2 3 4 5
F=1,R=1
F A C D
0 1 2 3 4 5
F=1,R=4
Contd.
Step 7: take the front node F. examine it.
Step 8: insert all the adjacent nodes of F to Q
and mark them as visited.
Step 9: take the front node A. examine it.
27
A C D
0 1 2 3 4 5
F=2,R=4
A C D
0 1 2 3 4 5
F=2,R=4
C D
0 1 2 3 4 5
F=3,R=4
Contd.
Step 10: insert all the adjacent nodes of A to Q
and mark them as visited.
Step 11: take the front node C. examine it.
Step 12: insert all the adjacent nodes of C to Q
and mark them as visited.
28
C D
0 1 2 3 4 5
F=3,R=4
D
0 1 2 3 4 5
F=4,R=4
Contd.
.
Step 13: take the front node D. examine it.
Step 14: insert all the adjacent nodes of D to Q
Step 15: stop now because Q is empty
The BFS order is EBFACD
29
D
0 1 2 3 4 5
F=4,R=4
0 1 2 3 4 5
F=5,R=4
Depth First search
• The traversal starts at the starting source vertex
and traverses all the nodes along the path
which begins at the source vertex.
• The implementation of the DFS is similar to the
BFS except that stack is used in place of queue.
30
DFS algorithm
1.Start traversing the given graph with the given
number of vertices and edges.
2. Push the starting source vertex S to the stack and
mark it as visited.
3. Repeat
a. Remove the top node K of the stack and
examine it.
b. Push all adjacent nodes of K(which are not
visited so far)to the stack and mark them as
visited
4. Until stack is empty
5. Exit
31
Example
• Traverse the following graph using the DFS
starting from A.
32
F
E
A
B
C
D
solution
Step 1: Start traversing the given graph with
number of vertices and edges.
Step 2: Push the starting source vertex A into
the vertex and mark it as visited.
33
Top A
Contd.
Step 3: take away(pop) the top node A. examine it.
Step 4: Push all the adjacent node of A to the stack
in any order and mark them as visited.
34
B
C
Top
Contd.
Step 5: take away(pop) the top node B. examine it.
Step 6: Insert all the adjacent node of B to the stack
in any order and mark them as visited.
35
D
E
C
Top
Contd.
Step 7: take away(pop) the top node D. examine it.
Step 8: Insert all the adjacent node of D to the
stack. B and C are already marked as visited
36
F
E
C
Top
Contd.
Step 9: take away(pop) the top node F. examine it.
Step 10: Insert all the adjacent node of F to the
stack. D, C and E are already marked as visited
37
E
C
Top
Contd.
Step 11: take away(pop) the top node E. examine it.
Step 12: Insert all the adjacent node of F to the
stack. B and F are already marked as visited
38
C
Top
Contd.
Step 13: take away(pop) the top node C. examine it.
Step 14: Insert all the adjacent node of F to the
stack. A,B ,D and F are already marked as visited
39
Top=NULL
Contd.
Step 15: Stop now because stack is empty
Therefore, The DFS order is ABDFEC
40
The Minimum Spanning Tree
• Any tree consisting of all the vertices of a graph is
called a spanning tree.
• A spanning tree of an undirected graph G is a subgraph
of G that is a tree containing all the vertices of G.
• In a weighted graph, the weight of a subgraph is the
sum of the weights of the edges in the subgraph.
• A minimum spanning tree (MST) for a weighted
undirected graph is a spanning tree with minimum
weight.
• A minimum spanning tree is a subgraph of an
undirected weighted graph G, such that
 it is a tree (i.e., it is acyclic)
 it covers all the vertices V
41
Applications of MST
• Any time you want to visit all vertices in a
graph at minimum cost (e.g., wire routing on
printed circuit boards, sewer pipe layout, road
planning…)
• There are two different algorithms to find out
the MST:
1. Prim’s algorithm
2. Kruskal algorithm
42
Minimum Spanning Tree: Prim's Algorithm
• Prim's algorithm for finding an MST is a greedy algorithm.i.e.
Constructs a solution to an optimization problem piece by
piece through a sequence of choices that are:
• locally optimal (with respect to some neighborhood
definition)
• greedy (in terms of some measure), and irrevocable
• Start by selecting an arbitrary vertex, include it into the
current MST.
• Grow the current MST by inserting into it the vertex
closest(incident) to one of the vertices already in current MST.
43
Example
• Let us take the graph shown in figure below.
And find out the MST using Prim’s algorithm.
44
B
C
D
E
F
A
2
2
2
3
3
4
3
7
6
Solution
Step 1: Select the vertex A. Now V={A}
Step 2: The edge incident on A are AB and AC. So
add the minimum of AB and AC.
Min{AB,AC}=Min{3,4}=3=AB
Now V={A,B} 45
A
B
A
3
Contd.
Step 3: Find the edge with minimum cost incident on
either A or B. AB and AC are the edges incident on
A and BC ,BD and BE are incident on B. since AB is
already included so ignore it. There fore
,Min{AC,BD,BE,BC}=Min{4,2,7,3}=2=BD. Add BD.
Now V={A,B,D}
46
B
A
3
D
2
Contd.
Step 4: find the edge with minimum cost incident
on A,B or D. AB and AC are the edges incident on
A; BC,BD and BE are incident on B .And BD, CD
and DF are incident on D. Since AB and BD are
already included so ignore them. Therefore,
Min{AC,BE,BC,CD,DF}=Min{4,7,3,6,2}=2=DF. Add
DF.
Now V={A,B,D,F} 47
B
A
3
D
2
F
2
Contd.
Step 5: Find the edge with minimum cost incident
on A,B,D or F. The edges incident on these
vertices and which are not included in the tree
are AC,BC,BE,DC,EF. Therefore,
Min{AC,BC,BE,DC,EF}=Min{4,3,7,6,2}=2=EF. So add
this edge.
48
B
A
3
D
2
F
E
2
2
Now V={A,B,D,F,E}
Contd.
Step 6: Find the edge with minimum cost incident on A,B,D ,F
or E. The edge incident on these vertices and which are
not included in the tree are AC,BC,BE,DC,EC. Therefore,
Min{AC,BC,BE,DC,EC}=Min{4,3,7,6,3}=3={BC,EC}
Select any one(BC) from this set.
Now V={A,B,C,D,E,F}.
step 7: Stop, since all 5 edges are included. 49
B
A
3
D
2
F
E
2
2
C
3
Exercise
• Find the MST for the following graph using
Prim's Algorithm.(select vertex b first)
50
Minimum Spanning Tree: Kruskal
algorithm
This algorithm was developed by Joseph Kruskal.
The kruskal algorithm creates the MST T by adding
the edge one at a time to T.
A minimum cost spanning tree T is built edge by
edge.
We start with the edge of minimum cost.
If there are several edges with the same minimum
cost, then we select any one of them and add it to
the spanning tree T, provided its addition does not
form a cycle.
We then add an edge with next lowest cost and so
on. 51
Example
• Let us take the graph shown in figure below to
find out the MST using Kruskal algorithm.
52
B
C
D
E
F
A
2
2
2
3
3
4
3
7
6
Solution
Step 1: The edges with minimum cost are BD,DF
and EF. Select any one.
Step 2: The edges with the next minimum cost
are BD and DF. Select any one.
53
E
F
2
E
F
2
D
2
Contd.
Step 3: The edges with the next minimum cost is
BD . So add it if it does not form a cycle.
54
E
F
2
D
2
B 2
Contd.
Step 4: The edges with the next minimum cost are
AB,BC and CE . So add any one that does not
form a cycle.
55
E
F
2
D
2
B 2
A
3
Contd.
Step 5: The edges with the next minimum cost
are BC and CE . So add any one that does not
form a cycle.
Step 6: Stop, since 5 edges are included.
56
E
F
2
D
2
B
2
A
3
C
3
Single-Source Shortest Path Problem
Single-Source Shortest Path Problem - The
problem of finding shortest paths from a source
vertex v to all other vertices in the graph.
There are various algorithms to find out the
shortest path.
57
Dijkstra's algorithm
Dijkstra's algorithm - is a solution to the single-
source shortest path problem in graph theory.
Works on both directed and undirected graphs.
However, all edges must have nonnegative
weights.
Input: Weighted graph G={E,V} and source vertex
v∈V, such that all edge weights are nonnegative
Output: Lengths of shortest paths (or the shortest
paths themselves) from a given source vertex
v∈V to all other vertices
58
Algorithm: Dijkstra’s algorithm
1. Set p={starting vertex S} and T={V}
2. Assign the index value 0 to the starting vertex S=0
and ∞ to all other vertices.
3. Repeat
a. Take the vertex v1 with minimum index value,
delete it from T and mark it as visited.
b. Find the new index value of adjacent vertex v2
with respect to v1 as follows:
Index(v2)=Min{Index(v2),Index(v1)+w(v1,v2)}
4. Until T is empty
59
Dijkstra's algorithm - Pseudocode
dist[s] ←0 (distance to source vertex is zero)
for all v ∈ V–{s}
do dist[v] ←∞ (set all other distances to infinity)
S←∅ (S, the set of visited vertices is initially empty)
Q←V (Q, the queue initially contains all
vertices)
while Q ≠∅ (while the queue is not empty)
do u ← mindistance(Q,dist) (select the element of Q with the min.
distance)
S←S∪{u} (add u to list of visited vertices)
for all v ∈ neighbors[u]
do if dist[v] > dist[u] + w(u, v) (if new shortest path found)
then d[v] ←d[u] + w(u, v) (set new value of shortest path)
(if desired, add traceback code)
return dist
60
Example
• Find the shortest path from the vertex A.
61
A B
C D
E
F
2
8
5
6 3
6
5
3
Contd.
Step 1: The index value of the source vertex A is
assign to 0 and index value of other vertices to
infinity T={A,B,C,D,E,F}
62
Vertex A B C D E F
Index
value
0 ∞ ∞ ∞ ∞ ∞
T A B C D E F
Contd.
Step 2: Select the vertex A with minimum value and
delete A from T. So T={B,C,D,E,F}
Find the new index value of all adjacent vertices of
A. Therefore
Newindex(B)=Min{Index(B),Index(A)+w(A,B)={∞,0+2}=2
Newindex(C)=Min{Index(C),Index(A)+w(A,C)={∞,0+5}=5
Newindex(E)=Min{Index(E),Index(A)+w(A,E)={∞,0+3}=3
63
Vertex A B C D E F
Index
value
0 2 5 ∞ 3 ∞
T B C D E F
Contd.
Step 3: Select the vertex B with minimum index
value and delete B from T. So T={C,D,E,F}
Find the new index value of all adjacent vertices of
B,that is A,E,D. However, A is not the member of
T so
Newindex(D)=Min{Index(D),Index(B)+w(B,D)={∞,2+8}=10
Newindex(E)=Min{Index(E),Index(B)+w(B,E)={3,2+5}=3
64
Vertex A B C D E F
Index
value
0 2 5 10 3 ∞
T C D E F
Contd.
Step 4: Select the vertex E with minimum index
value and delete Efrom T. So T={C,D,F}
Find the new index value of all adjacent vertices of
E, that is A,B,F. However, A and B are not the
member of T so
Newindex(F)=Min{Index(F),Index(E)+w(E,F)={∞,3+6}=9
65
Vertex A B C D E F
Index
value
0 2 5 10 3 9
T C D F
Contd.
Step 5: Select the vertex C with minimum index
value and delete C from T. So T={D,F}
Find the new index value of all adjacent vertices of
C, that is A,F. However, A is not the member of T
so
Newindex(F)=Min{Index(F),Index(C)+w(C,F)={9,5+6}=9
It remains unchanged.
66
Vertex A B C D E F
Index
value
0 2 5 10 3 9
T D F
Contd.
Step 6: Select the vertex F with minimum index
value and delete F from T. So T={D}
Find the new index value of all adjacent vertices of
F, that is C,E,D. but C and E are not the member
of T so
Newindex(D)=Min{Index(D),Index(F)+w(F,D)={10,9+3}=10
It remains unchanged.
67
Vertex A B C D E F
Index
value
0 2 5 10 3 9
T D
Contd.
Step 7: Select the vertex D with minimum index value and
delete D from T. So T={}. Since this is the last vertex so stop.
68
Vertex A B C D E F
Index
value
0 2 5 10 3 9
T
Therefore, now we can see that the shortest path between
A and B = AB of cost 2
A and C = AC of cost 5
A and D = ABD of cost 10
A and E =AE of cost 3
A and F = AEF of cost 9
Dijkstra Animated Example: start at A
69
Dijkstra Animated Example
70
Dijkstra Animated Example
71
Dijkstra Animated Example
72
Dijkstra Animated Example
73
Dijkstra Animated Example
74
Dijkstra Animated Example
75
Dijkstra Animated Example
76
Dijkstra Animated Example
77
Dijkstra Animated Example
78
Dijkstra Animated Example
79
80
Exercise
Find shortest path from s to t.
s
3
t
2
6
7
4
5
24
18
2
9
14
15
5
30
20
44
16
11
6
19
6
81
Dijkstra's Shortest Path Algorithm
s
3
t
2
6
7
4
5
24
18
2
9
14
15
5
30
20
44
16
11
6
19
6







0
distance label
S = { }
PQ = { s, 2, 3, 4, 5, 6, 7, t }

More Related Content

Similar to Algorithms and data Chapter 3 V Graph.pptx

Similar to Algorithms and data Chapter 3 V Graph.pptx (20)

Graph therory
Graph theroryGraph therory
Graph therory
 
Unit-6 Graph.ppsx ppt
Unit-6 Graph.ppsx                                       pptUnit-6 Graph.ppsx                                       ppt
Unit-6 Graph.ppsx ppt
 
Graph Theory
Graph TheoryGraph Theory
Graph Theory
 
DATA STRUCTURES.pptx
DATA STRUCTURES.pptxDATA STRUCTURES.pptx
DATA STRUCTURES.pptx
 
Graphs in datastructures
Graphs in datastructuresGraphs in datastructures
Graphs in datastructures
 
Graph
GraphGraph
Graph
 
logic.pptx
logic.pptxlogic.pptx
logic.pptx
 
LEC 12-DSALGO-GRAPHS(final12).pdf
LEC 12-DSALGO-GRAPHS(final12).pdfLEC 12-DSALGO-GRAPHS(final12).pdf
LEC 12-DSALGO-GRAPHS(final12).pdf
 
Graphs
GraphsGraphs
Graphs
 
Unit V - ppt.pptx
Unit V - ppt.pptxUnit V - ppt.pptx
Unit V - ppt.pptx
 
Unit II_Graph.pptxkgjrekjgiojtoiejhgnltegjte
Unit II_Graph.pptxkgjrekjgiojtoiejhgnltegjteUnit II_Graph.pptxkgjrekjgiojtoiejhgnltegjte
Unit II_Graph.pptxkgjrekjgiojtoiejhgnltegjte
 
Elements of Graph Theory for IS.pptx
Elements of Graph Theory for IS.pptxElements of Graph Theory for IS.pptx
Elements of Graph Theory for IS.pptx
 
UNIT III.pptx
UNIT III.pptxUNIT III.pptx
UNIT III.pptx
 
09_Graphs_handout.pdf
09_Graphs_handout.pdf09_Graphs_handout.pdf
09_Graphs_handout.pdf
 
Graph_data_structure_information_engineering.pptx
Graph_data_structure_information_engineering.pptxGraph_data_structure_information_engineering.pptx
Graph_data_structure_information_engineering.pptx
 
Graph theory1234
Graph theory1234Graph theory1234
Graph theory1234
 
Data structure computer graphs
Data structure computer graphsData structure computer graphs
Data structure computer graphs
 
Graph 1
Graph 1Graph 1
Graph 1
 
Graph ds
Graph dsGraph ds
Graph ds
 
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
 

Recently uploaded

The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024Rafal Los
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdfhans926745
 
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024BookNet Canada
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking MenDelhi Call girls
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsEnterprise Knowledge
 
Pigging Solutions Piggable Sweeping Elbows
Pigging Solutions Piggable Sweeping ElbowsPigging Solutions Piggable Sweeping Elbows
Pigging Solutions Piggable Sweeping ElbowsPigging Solutions
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationMichael W. Hawkins
 
The Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxThe Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxMalak Abu Hammad
 
Azure Monitor & Application Insight to monitor Infrastructure & Application
Azure Monitor & Application Insight to monitor Infrastructure & ApplicationAzure Monitor & Application Insight to monitor Infrastructure & Application
Azure Monitor & Application Insight to monitor Infrastructure & ApplicationAndikSusilo4
 
Install Stable Diffusion in windows machine
Install Stable Diffusion in windows machineInstall Stable Diffusion in windows machine
Install Stable Diffusion in windows machinePadma Pradeep
 
How to Remove Document Management Hurdles with X-Docs?
How to Remove Document Management Hurdles with X-Docs?How to Remove Document Management Hurdles with X-Docs?
How to Remove Document Management Hurdles with X-Docs?XfilesPro
 
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking MenDelhi Call girls
 
Benefits Of Flutter Compared To Other Frameworks
Benefits Of Flutter Compared To Other FrameworksBenefits Of Flutter Compared To Other Frameworks
Benefits Of Flutter Compared To Other FrameworksSoftradix Technologies
 
Key Features Of Token Development (1).pptx
Key  Features Of Token  Development (1).pptxKey  Features Of Token  Development (1).pptx
Key Features Of Token Development (1).pptxLBM Solutions
 
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure serviceWhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure servicePooja Nehwal
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)Gabriella Davis
 
08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking MenDelhi Call girls
 
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...Neo4j
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreternaman860154
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonetsnaman860154
 

Recently uploaded (20)

The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf
 
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI Solutions
 
Pigging Solutions Piggable Sweeping Elbows
Pigging Solutions Piggable Sweeping ElbowsPigging Solutions Piggable Sweeping Elbows
Pigging Solutions Piggable Sweeping Elbows
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day Presentation
 
The Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxThe Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptx
 
Azure Monitor & Application Insight to monitor Infrastructure & Application
Azure Monitor & Application Insight to monitor Infrastructure & ApplicationAzure Monitor & Application Insight to monitor Infrastructure & Application
Azure Monitor & Application Insight to monitor Infrastructure & Application
 
Install Stable Diffusion in windows machine
Install Stable Diffusion in windows machineInstall Stable Diffusion in windows machine
Install Stable Diffusion in windows machine
 
How to Remove Document Management Hurdles with X-Docs?
How to Remove Document Management Hurdles with X-Docs?How to Remove Document Management Hurdles with X-Docs?
How to Remove Document Management Hurdles with X-Docs?
 
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
 
Benefits Of Flutter Compared To Other Frameworks
Benefits Of Flutter Compared To Other FrameworksBenefits Of Flutter Compared To Other Frameworks
Benefits Of Flutter Compared To Other Frameworks
 
Key Features Of Token Development (1).pptx
Key  Features Of Token  Development (1).pptxKey  Features Of Token  Development (1).pptx
Key Features Of Token Development (1).pptx
 
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure serviceWhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)
 
08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men
 
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreter
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonets
 

Algorithms and data Chapter 3 V Graph.pptx

  • 2. Introduction • Graph is a collection of two finite sets (V and E) where V contains the finite number of elements called vertices(nodes) and E contains the finite number of elements called the edges. • A graph G = (V,E) consists of a finite set of vertices, V, and a finite set of edges E. • Each edge is a pair (v,w) where v, w  V. • Node –has a unique label for identification 2
  • 3. Contd. • Edge – connects two nodes – can be a • directed edge • undirected edge 3
  • 4. Contd. • “Graphs” are the mathematical and computer science abstraction that capture many shared and common concepts of real-life objects such as –networks, e.g. • electricity • Internet –road maps Etc… 4
  • 6. Terminologies 1. Adjacent vertices: Two vertices are said to be adjacent if they are connected by an edge. For example , Band C are adjacent to vertex A because separate edges connect B and C to A. 2. Adjacent edges: Two edges are said to be adjacent if they are incident on the common vertex. For example, edges CD and CE are adjacent because they are incident on the common vertex C. 6
  • 7. Contd. 3. Path –connected sequence of edges: –e.g. A to B to D to C to E –if the edges are directed then the path has to follow the directions of the edges. 4. A cyclic path is a path such that – There are at least two vertices on the path – w1 = wn (path starts and ends at same vertex) i.e. a cycle is a path that goes in a loop from a node back to itself, and without using the same node twice 7
  • 8. Contd. • E.g. A-B-E-A in B C D E F G A 5. LOOP: It is a special cycle that starts and ends with the same vertex without visiting any other vertex. 8
  • 9. contd. • An acyclic path is a path where each vertex is unique • An “acyclic graph” has no cycles. 5. Degree of a vertex: The degree of a vertex is the number of lines incident on it. 6. Connected vertices: Two vertices are said to be connected if there is a path between them. 7. Out-degree: The out-degree of a directed graph is the number of arcs leaving the vertex. 8. In-degree: The in-degree of a directed graph is the number of arcs entering the vertex. 9
  • 10. Test Your Knowledge Cyclic or Acyclic? 10
  • 11. 11 Contd. Example: What are the in-degrees and out-degrees of the vertices a, b, c, d in this graph: a b c d deg-(a) = 1 deg+(a) = 2 deg-(b) = 4 deg+(b) = 2 deg-(d) = 2 deg+(d) = 1 deg-(c) = 0 deg+(c) = 2
  • 12. Types of Graph 1. Simple graph: A graph that does not contain any loop or multiple edges between two vertices is called a simple graph. A B C D E F 12
  • 13. Contd. 2. Multi-graph: A graph that contains more than one edge between two vertices is called a multi-graph. Example: B C D E F 13
  • 14. Contd. 3. Directed graph: In a directed graph, the edges are arrows. • Directed graphs show the flow from one node to another and not vise versa. 14
  • 15. Contd. 4. Undirected Graphs: In an undirected graph, the edges are lines. • Undirected graphs show a relationship between two nodes. 15
  • 16. Contd. 5. Weighted graph: A graph is known as a weighted graph if a weight or metric is associated with each edge. 16
  • 17. Contd. 6. Complete graph: An undirected graph that has an edge between every pair of vertices is called a complete graph. • Note: A directed graph can also be a complete graph; in that case, there must be an edge from every vertex to every other vertex. 17
  • 18. Representation of Graph • Graphs can be represented by their adjacency matrix or an edge (or vertex) list. i.e. 1. Sequential representation using adjacency matrices. 2. Linked representation using adjacent list or linked list of neighbors. Adjacency matrices have a value ai,j = 1 if nodes i and j share an edge; 0 otherwise. 18
  • 19. 19 Representing Graphs a b c d a, d b a, d c a, b, c d b, c, d a Adjacent Vertices Vertex
  • 20. Sequential Representation using Adjacency Matrix An undirected graph and its adjacency matrix representation. A directed graph and its adjacency matrix representation. A B C D E A A B C D E A 0 1 1 0 0 B 0 0 0 1 0 C 0 1 0 0 1 D 0 0 0 0 1 E 0 0 0 0 0 20
  • 21. Traversal of Graph • Some application of the graph require systematically visiting all the nodes. • In a graph ,we can reach to the vertex from multiple paths existing in the graph, so we can say that graph traversal is not as simple as traversal in other data structure. • There are two standard traversal methods generally used with graph: 1. Breadth first search(BFS) 2. Depth first search(DFS) 21
  • 22. Breadth first search(BFS) • BFS is that it starts traversing the graph from the starting node (any selected node) and then traverses all the nodes which are directly connected to the starting node in a particular order. • First, the starting node is examined and all the adjacent nodes are placed into the queue. • The node traversed is marked as visited. • The next node is taken from the front of the queue and examined; • This node also marked as visited and all the neighbors of this node are placed into the rear of the queue. • In BFS all the nodes at a particular level are processed first, only then can we move to the next level. 22
  • 23. BFS algorithm 1. Start traversing the given graph with the given number of vertices and edges 2. Insert the starting source vertex S to the queue and mark it as visited 3. Repeat a. Remove the front node K of the queue and examine it. b. Insert all the adjacent nodes of K (which are not visited so far)to the queue and mark them as visited 4. Until queue is empty 5. Exit 23
  • 24. Example • Traverse the following graph using the BFS starting from E. 24 F E A B C D
  • 25. Solution Step 1: start traversing the graph given. Step 2: Insert the starting source vertex E to the queue and mark it as visited. Step 3: Take the front node E. Examine it. Step 4: insert all the adjacent nodes of E to queue named as Q and mark them as visited. 25 E 0 1 2 3 4 5 F=0,R=0 B F 0 1 2 3 4 5 F=0,R=1
  • 26. Contd. Step 5: take the front node B. examine it. Step 6: insert all the adjacent nodes of B to Q and mark them as visited. 26 F 0 1 2 3 4 5 F=1,R=1 F A C D 0 1 2 3 4 5 F=1,R=4
  • 27. Contd. Step 7: take the front node F. examine it. Step 8: insert all the adjacent nodes of F to Q and mark them as visited. Step 9: take the front node A. examine it. 27 A C D 0 1 2 3 4 5 F=2,R=4 A C D 0 1 2 3 4 5 F=2,R=4 C D 0 1 2 3 4 5 F=3,R=4
  • 28. Contd. Step 10: insert all the adjacent nodes of A to Q and mark them as visited. Step 11: take the front node C. examine it. Step 12: insert all the adjacent nodes of C to Q and mark them as visited. 28 C D 0 1 2 3 4 5 F=3,R=4 D 0 1 2 3 4 5 F=4,R=4
  • 29. Contd. . Step 13: take the front node D. examine it. Step 14: insert all the adjacent nodes of D to Q Step 15: stop now because Q is empty The BFS order is EBFACD 29 D 0 1 2 3 4 5 F=4,R=4 0 1 2 3 4 5 F=5,R=4
  • 30. Depth First search • The traversal starts at the starting source vertex and traverses all the nodes along the path which begins at the source vertex. • The implementation of the DFS is similar to the BFS except that stack is used in place of queue. 30
  • 31. DFS algorithm 1.Start traversing the given graph with the given number of vertices and edges. 2. Push the starting source vertex S to the stack and mark it as visited. 3. Repeat a. Remove the top node K of the stack and examine it. b. Push all adjacent nodes of K(which are not visited so far)to the stack and mark them as visited 4. Until stack is empty 5. Exit 31
  • 32. Example • Traverse the following graph using the DFS starting from A. 32 F E A B C D
  • 33. solution Step 1: Start traversing the given graph with number of vertices and edges. Step 2: Push the starting source vertex A into the vertex and mark it as visited. 33 Top A
  • 34. Contd. Step 3: take away(pop) the top node A. examine it. Step 4: Push all the adjacent node of A to the stack in any order and mark them as visited. 34 B C Top
  • 35. Contd. Step 5: take away(pop) the top node B. examine it. Step 6: Insert all the adjacent node of B to the stack in any order and mark them as visited. 35 D E C Top
  • 36. Contd. Step 7: take away(pop) the top node D. examine it. Step 8: Insert all the adjacent node of D to the stack. B and C are already marked as visited 36 F E C Top
  • 37. Contd. Step 9: take away(pop) the top node F. examine it. Step 10: Insert all the adjacent node of F to the stack. D, C and E are already marked as visited 37 E C Top
  • 38. Contd. Step 11: take away(pop) the top node E. examine it. Step 12: Insert all the adjacent node of F to the stack. B and F are already marked as visited 38 C Top
  • 39. Contd. Step 13: take away(pop) the top node C. examine it. Step 14: Insert all the adjacent node of F to the stack. A,B ,D and F are already marked as visited 39 Top=NULL
  • 40. Contd. Step 15: Stop now because stack is empty Therefore, The DFS order is ABDFEC 40
  • 41. The Minimum Spanning Tree • Any tree consisting of all the vertices of a graph is called a spanning tree. • A spanning tree of an undirected graph G is a subgraph of G that is a tree containing all the vertices of G. • In a weighted graph, the weight of a subgraph is the sum of the weights of the edges in the subgraph. • A minimum spanning tree (MST) for a weighted undirected graph is a spanning tree with minimum weight. • A minimum spanning tree is a subgraph of an undirected weighted graph G, such that  it is a tree (i.e., it is acyclic)  it covers all the vertices V 41
  • 42. Applications of MST • Any time you want to visit all vertices in a graph at minimum cost (e.g., wire routing on printed circuit boards, sewer pipe layout, road planning…) • There are two different algorithms to find out the MST: 1. Prim’s algorithm 2. Kruskal algorithm 42
  • 43. Minimum Spanning Tree: Prim's Algorithm • Prim's algorithm for finding an MST is a greedy algorithm.i.e. Constructs a solution to an optimization problem piece by piece through a sequence of choices that are: • locally optimal (with respect to some neighborhood definition) • greedy (in terms of some measure), and irrevocable • Start by selecting an arbitrary vertex, include it into the current MST. • Grow the current MST by inserting into it the vertex closest(incident) to one of the vertices already in current MST. 43
  • 44. Example • Let us take the graph shown in figure below. And find out the MST using Prim’s algorithm. 44 B C D E F A 2 2 2 3 3 4 3 7 6
  • 45. Solution Step 1: Select the vertex A. Now V={A} Step 2: The edge incident on A are AB and AC. So add the minimum of AB and AC. Min{AB,AC}=Min{3,4}=3=AB Now V={A,B} 45 A B A 3
  • 46. Contd. Step 3: Find the edge with minimum cost incident on either A or B. AB and AC are the edges incident on A and BC ,BD and BE are incident on B. since AB is already included so ignore it. There fore ,Min{AC,BD,BE,BC}=Min{4,2,7,3}=2=BD. Add BD. Now V={A,B,D} 46 B A 3 D 2
  • 47. Contd. Step 4: find the edge with minimum cost incident on A,B or D. AB and AC are the edges incident on A; BC,BD and BE are incident on B .And BD, CD and DF are incident on D. Since AB and BD are already included so ignore them. Therefore, Min{AC,BE,BC,CD,DF}=Min{4,7,3,6,2}=2=DF. Add DF. Now V={A,B,D,F} 47 B A 3 D 2 F 2
  • 48. Contd. Step 5: Find the edge with minimum cost incident on A,B,D or F. The edges incident on these vertices and which are not included in the tree are AC,BC,BE,DC,EF. Therefore, Min{AC,BC,BE,DC,EF}=Min{4,3,7,6,2}=2=EF. So add this edge. 48 B A 3 D 2 F E 2 2 Now V={A,B,D,F,E}
  • 49. Contd. Step 6: Find the edge with minimum cost incident on A,B,D ,F or E. The edge incident on these vertices and which are not included in the tree are AC,BC,BE,DC,EC. Therefore, Min{AC,BC,BE,DC,EC}=Min{4,3,7,6,3}=3={BC,EC} Select any one(BC) from this set. Now V={A,B,C,D,E,F}. step 7: Stop, since all 5 edges are included. 49 B A 3 D 2 F E 2 2 C 3
  • 50. Exercise • Find the MST for the following graph using Prim's Algorithm.(select vertex b first) 50
  • 51. Minimum Spanning Tree: Kruskal algorithm This algorithm was developed by Joseph Kruskal. The kruskal algorithm creates the MST T by adding the edge one at a time to T. A minimum cost spanning tree T is built edge by edge. We start with the edge of minimum cost. If there are several edges with the same minimum cost, then we select any one of them and add it to the spanning tree T, provided its addition does not form a cycle. We then add an edge with next lowest cost and so on. 51
  • 52. Example • Let us take the graph shown in figure below to find out the MST using Kruskal algorithm. 52 B C D E F A 2 2 2 3 3 4 3 7 6
  • 53. Solution Step 1: The edges with minimum cost are BD,DF and EF. Select any one. Step 2: The edges with the next minimum cost are BD and DF. Select any one. 53 E F 2 E F 2 D 2
  • 54. Contd. Step 3: The edges with the next minimum cost is BD . So add it if it does not form a cycle. 54 E F 2 D 2 B 2
  • 55. Contd. Step 4: The edges with the next minimum cost are AB,BC and CE . So add any one that does not form a cycle. 55 E F 2 D 2 B 2 A 3
  • 56. Contd. Step 5: The edges with the next minimum cost are BC and CE . So add any one that does not form a cycle. Step 6: Stop, since 5 edges are included. 56 E F 2 D 2 B 2 A 3 C 3
  • 57. Single-Source Shortest Path Problem Single-Source Shortest Path Problem - The problem of finding shortest paths from a source vertex v to all other vertices in the graph. There are various algorithms to find out the shortest path. 57
  • 58. Dijkstra's algorithm Dijkstra's algorithm - is a solution to the single- source shortest path problem in graph theory. Works on both directed and undirected graphs. However, all edges must have nonnegative weights. Input: Weighted graph G={E,V} and source vertex v∈V, such that all edge weights are nonnegative Output: Lengths of shortest paths (or the shortest paths themselves) from a given source vertex v∈V to all other vertices 58
  • 59. Algorithm: Dijkstra’s algorithm 1. Set p={starting vertex S} and T={V} 2. Assign the index value 0 to the starting vertex S=0 and ∞ to all other vertices. 3. Repeat a. Take the vertex v1 with minimum index value, delete it from T and mark it as visited. b. Find the new index value of adjacent vertex v2 with respect to v1 as follows: Index(v2)=Min{Index(v2),Index(v1)+w(v1,v2)} 4. Until T is empty 59
  • 60. Dijkstra's algorithm - Pseudocode dist[s] ←0 (distance to source vertex is zero) for all v ∈ V–{s} do dist[v] ←∞ (set all other distances to infinity) S←∅ (S, the set of visited vertices is initially empty) Q←V (Q, the queue initially contains all vertices) while Q ≠∅ (while the queue is not empty) do u ← mindistance(Q,dist) (select the element of Q with the min. distance) S←S∪{u} (add u to list of visited vertices) for all v ∈ neighbors[u] do if dist[v] > dist[u] + w(u, v) (if new shortest path found) then d[v] ←d[u] + w(u, v) (set new value of shortest path) (if desired, add traceback code) return dist 60
  • 61. Example • Find the shortest path from the vertex A. 61 A B C D E F 2 8 5 6 3 6 5 3
  • 62. Contd. Step 1: The index value of the source vertex A is assign to 0 and index value of other vertices to infinity T={A,B,C,D,E,F} 62 Vertex A B C D E F Index value 0 ∞ ∞ ∞ ∞ ∞ T A B C D E F
  • 63. Contd. Step 2: Select the vertex A with minimum value and delete A from T. So T={B,C,D,E,F} Find the new index value of all adjacent vertices of A. Therefore Newindex(B)=Min{Index(B),Index(A)+w(A,B)={∞,0+2}=2 Newindex(C)=Min{Index(C),Index(A)+w(A,C)={∞,0+5}=5 Newindex(E)=Min{Index(E),Index(A)+w(A,E)={∞,0+3}=3 63 Vertex A B C D E F Index value 0 2 5 ∞ 3 ∞ T B C D E F
  • 64. Contd. Step 3: Select the vertex B with minimum index value and delete B from T. So T={C,D,E,F} Find the new index value of all adjacent vertices of B,that is A,E,D. However, A is not the member of T so Newindex(D)=Min{Index(D),Index(B)+w(B,D)={∞,2+8}=10 Newindex(E)=Min{Index(E),Index(B)+w(B,E)={3,2+5}=3 64 Vertex A B C D E F Index value 0 2 5 10 3 ∞ T C D E F
  • 65. Contd. Step 4: Select the vertex E with minimum index value and delete Efrom T. So T={C,D,F} Find the new index value of all adjacent vertices of E, that is A,B,F. However, A and B are not the member of T so Newindex(F)=Min{Index(F),Index(E)+w(E,F)={∞,3+6}=9 65 Vertex A B C D E F Index value 0 2 5 10 3 9 T C D F
  • 66. Contd. Step 5: Select the vertex C with minimum index value and delete C from T. So T={D,F} Find the new index value of all adjacent vertices of C, that is A,F. However, A is not the member of T so Newindex(F)=Min{Index(F),Index(C)+w(C,F)={9,5+6}=9 It remains unchanged. 66 Vertex A B C D E F Index value 0 2 5 10 3 9 T D F
  • 67. Contd. Step 6: Select the vertex F with minimum index value and delete F from T. So T={D} Find the new index value of all adjacent vertices of F, that is C,E,D. but C and E are not the member of T so Newindex(D)=Min{Index(D),Index(F)+w(F,D)={10,9+3}=10 It remains unchanged. 67 Vertex A B C D E F Index value 0 2 5 10 3 9 T D
  • 68. Contd. Step 7: Select the vertex D with minimum index value and delete D from T. So T={}. Since this is the last vertex so stop. 68 Vertex A B C D E F Index value 0 2 5 10 3 9 T Therefore, now we can see that the shortest path between A and B = AB of cost 2 A and C = AC of cost 5 A and D = ABD of cost 10 A and E =AE of cost 3 A and F = AEF of cost 9
  • 69. Dijkstra Animated Example: start at A 69
  • 80. 80 Exercise Find shortest path from s to t. s 3 t 2 6 7 4 5 24 18 2 9 14 15 5 30 20 44 16 11 6 19 6
  • 81. 81 Dijkstra's Shortest Path Algorithm s 3 t 2 6 7 4 5 24 18 2 9 14 15 5 30 20 44 16 11 6 19 6        0 distance label S = { } PQ = { s, 2, 3, 4, 5, 6, 7, t }