SlideShare a Scribd company logo
1 of 57
Unit - 5
Graph and Hashing
Prepared By:
Ms. Purvi Tandel, Chandni Naik
Topics of Unit 5
• Graph-Definitions and Concepts
• Matrix and list representation of graphs
• Elementary graph operations - Breadth first search
• Elementary graph operations - Depth first search
• Hashing - Hashing functions
• Collision-Resolution techniques.
Introduction of Graph
• Graph is a non-linear data structure.
• It contains a set of points known as nodes (or vertices) and a set of links
known as edges (or Arcs).
• Here edges are used to connect the vertices.
• Generally, a graph G is represented as G = ( V , E ), where V is set of
vertices and E is set of edges.
Example:
• Graph with 5 vertices and 6 edges.
This graph G can be defined as G = ( V , E )
Where V = {A,B,C,D,E} and
E = {(A,B),(A,C)(A,D),(B,D),(C,D),(B,E),(E,D)}.
Graph is a collection of nodes and edges in which nodes are connected with edges.
Graph Definitions
1) Graph: A graph G is defined as an ordered set (V, E), where V(G) represents
the set of vertices and E(G) represents the edges that connect these
vertices.
2) Vertex: Individual data element of a graph is called as Vertex. Vertex is also
known as node.
3) Edge: An edge is a connecting link between two vertices. Edge is also known
as Arc.
Graph Definitions
4) Adjacent Node: For every edge, e =
(u, v) that connects nodes u and v, the
nodes u and v are the end-points and
are said to be the adjacent nodes or
neighbors.
In example A and B are adjacent
nodes.
5) Directed graph: In a directed
graph, edges form an ordered pair. If
there is an edge from A to B, then
there is a path from A to B but not
from B to A.
The edge (A, B) is said to initiate
from node A (also known as initial
node) and terminate at node B
(terminal node).
Graph Definitions
6) Undirected Graph: In an
undirected graph, edges do not have
any direction associated with them.
That is, if an edge is drawn between
nodes A and B, then the nodes can be
traversed from A to B as well as from
B to A.
7) Loop: An edge that has identical
end-points is called a loop.
That is, e = (u, u).
Graph Definitions
8) Parallel Edge: In some directed as
well as undirected graphs, we may
have certain pairs of nodes joined by
more than one edges, such edges are
called Parallel edges.
In example edges between node 1
and 2 are parallel edges.
9) Distinct Edge: In case of directed
edges, two possible edges between
any pair of nodes which are opposite
in direction are considered Distinct.
In example edges between node 2
and 3 are distinct edges.
2
1
4
3
2
2
1
1
1
2
2
1
4
3
2
2
1
1
1
Graph Definitions
10) Weighted Graph: In a weighted
graph, the edges of the graph are
assigned some weight or length. The
weight of an edge denoted by w(e) is
a positive value which indicates the
cost of traversing the edge.
11) Isolated Graph: In a graph a
node which is not adjacent to any
other node is called isolated node.
Graph Definitions
12) Null Graph: A graph having no edges is called a Null Graph.
A graph containing only isolated nodes are called null graph.
13) Degree of node: The degree of a node, written as deg(u), is equal to the
sum of in-degree and out-degree of that node.
degree(u) = indegree(u) + outdegree(u)
Indegree : The no of edges which have V as their terminal node is call as indegree of node V.
Outdegree : The no of edges which have V as their initial node is call as outdegree of node V.
Degree of A = 2, Degree of B = 3
Degree of C = 2, Degree of D = 3
Degree of E = 2
Matrix and list representation of graphs
Graph data structure is represented using following representations.
• Adjacency Matrix
• Adjacency List (Linked List)
Matrix and list representation of graphs
Graph data structure is represented using following representations.
1) Adjacency Matrix: In this representation, the graph is represented using a matrix of
size total number of vertices by a total number of vertices. That means a graph with 4
vertices is represented using a matrix of size 4X4. In this matrix, both rows and columns
represent vertices. This matrix is filled with either 1 or 0. Here, 1 represents that there is
an edge from row vertex to column vertex and 0 represents that there is no edge from
row vertex to column vertex.
Undirected
graph
representation
Matrix and list representation of graphs
Graph data structure is represented using following representations.
1) Adjacency Matrix: In this representation, the graph is represented using a matrix of
size total number of vertices by a total number of vertices. That means a graph with 4
vertices is represented using a matrix of size 4X4. In this matrix, both rows and columns
represent vertices. This matrix is filled with either 1 or 0. Here, 1 represents that there is
an edge from row vertex to column vertex and 0 represents that there is no edge from
row vertex to column vertex.
Directed graph
representation
Matrix and list representation of graphs
Graph data structure is represented using following representations.
1) Adjacency Matrix: One more example in directed graph representation.
For a given graph G =m (V, E), an adjacency matrix depends upon the ordering of the
elements of V.
V1
V2 V3
V4
0 1 0 1
1 0 0 0
1 1 0 1
0 1 0 0
V1
V1
V2
V2
V3
V3
V4
V4
A =
Matrix and list representation of graphs
Graph data structure is represented using following representations.
2) Adjacency List: In this representation, every vertex of graph contains list of its adjacent
vertices.
For example, consider the following directed graph representation implemented using
linked list.
Matrix and list representation of graphs
Graph data structure is represented using following representations.
2) Adjacency List: One more example of undirected graph representation.
0
1
3
2
4
0
1
2
3
4
1 2 3 4
0 3
0 3 4
0 1 2 4
0 2 3
Elementary graph operations – DFS & BFS
• One of the elementary operation on graph is graph traversal.
• Graph traversal is technique used for searching a vertex in a graph.
• The graph traversal is also used to decide the order of vertices to be visit in the
search process.
• A graph traversal finds the edges to be used in the search process without
creating loops that means using graph traversal we visit all vertices of graph
without getting into looping path.
There are two graph traversal techniques:
• DFS (Depth First Search)
• BFS (Breadth First Search)
DFS – Depth First Search
Depth First Search (DFS) algorithm traverses a graph in a depth-ward motion and
uses a stack to remember to get the next vertex to start a search, when a dead
end occurs in any iteration.
A
B C
D E F G
H
A B D H E C F G
✓
✓
✓
✓
✓
✓
✓ ✓
DFS – Depth First Search
We use the following steps to implement DFS traversal.
1) Define a Stack of size total number of vertices in the graph.
2) Select any vertex as starting point for traversal. Visit that vertex and push it on
to the Stack.
3) Visit any one of the adjacent vertex of the vertex which is at top of the stack
which is not visited and push it on to the stack.
4) Repeat step 3 until there are no new vertex to be visit from the vertex on top
of the stack.
5) When there is no new vertex to be visit then use back tracking and pop one
vertex from the stack.
6) Repeat steps 3, 4 and 5 until stack becomes Empty.
7) When stack becomes Empty, then produce final spanning tree by removing
unused edges from the graph.
DFS – Depth First Search – Example 1
1 Initialize the stack.
2
Mark S as visited and put it onto the stack. Explore any
unvisited adjacent node from S. We have three nodes and
we can pick any of them. For this example, we shall take
the node in an alphabetical order.
3
Mark A as visited and put it onto the stack. Explore any
unvisited adjacent node from A. Both S and D are adjacent
to A but we are concerned for unvisited nodes only.
DFS – Depth First Search – Example 1
4 Visit D and mark it as visited and put onto the
stack. Here, we have B and C nodes, which are
adjacent to D and both are unvisited. However,
we shall again choose in an alphabetical order.
5
We choose B, mark it as visited and put onto the stack.
Here B does not have any unvisited adjacent node. So, we
pop B from the stack.
6
We check the stack top for return to the previous
node and check if it has any unvisited nodes. Here,
we find D to be on the top of the stack.
DFS – Depth First Search – Example 1
7
Only unvisited adjacent node is from D is C now.
So we visit C, mark it as visited and put it onto
the stack.
For DFS sequence, write the vertex in order of
their visited sequence.
DFS sequence (visited sequence): S A D B C
Resulted spanning tree
Spanning tree:
A spanning tree of a connected, undirected graph G
is a sub-graph of G which is a tree that connects all
the vertices together.
Answer:
Introduction to Minimum spanning tree
Spanning Tree:
• A spanning tree of a connected, undirected graph G is a sub-graph of G which is a tree that
connects all the vertices together.
• A graph G can have many different spanning trees. We can assign weights to each edge
(which is a number that represents how unfavorable the edge is), and use it to assign a
weight to a spanning tree by calculating the sum of the weights of the edges in that
spanning tree.
Introduction to Minimum spanning tree
Minimum spanning tree:
• A minimum spanning tree (MST) is defined as a spanning tree with weight less than or
equal to the weight of every other spanning tree.
• In other words, a minimum spanning tree is a spanning tree that has weights associated
with its edges, and the total weight of the tree (the sum of the weights of its edges) is at a
minimum.
DFS – Depth First Search – Example 2
DFS sequence
(visited sequence):
A
Answer:
DFS – Depth First Search – Example 2
DFS sequence
(visited sequence):
A B C
Answer:
DFS – Depth First Search – Example 2
DFS sequence
(visited sequence):
A B C E D
Answer:
DFS – Depth First Search – Example 2
DFS sequence
(visited sequence):
A B C E D F
Answer:
DFS – Depth First Search – Example 2
DFS sequence
(visited sequence):
A B C E D F G
Answer:
DFS – Depth First Search – Example 2
DFS sequence
(visited sequence):
A B C E D F G
Answer:
DFS – Depth First Search – Example 2
DFS sequence
(visited sequence):
A B C E D F G
Answer:
DFS – Depth First Search – Example 2
DFS sequence
(visited sequence):
A B C E D F G
Answer:
DFS – Depth First Search – Exercise Question
Starting Vertex A:
ABEFCGIHD
Starting Vertex H:
HEFCBGI (A and D are
unreachable)
Answer:
Find the sequence of traverse using DFS.
a. Starting point is A
b. Starting point is H
BFS – Breadth First Search
Breadth First Search (BFS) algorithm traverses a graph in a breadth-ward motion
and uses a queue to remember to get the next vertex to start a search, when a
dead end occurs in any iteration.
A
B C
D E F G
H
A| B C| D E F G| H
✓ ✓
✓ ✓ ✓
✓
✓
✓
We use the following steps to implement BFS traversal.
1) Define a Queue of size total number of vertices in the graph.
2) Select any vertex as starting point for traversal. Visit that vertex and insert it
into the Queue.
3) Visit all the adjacent vertices of the vertex which is at front of the Queue
which is not visited and insert them into the Queue.
4) When there is no new vertex to be visit from the vertex at front of the Queue
then delete that vertex from the Queue.
5) Repeat step 3 and 4 until queue becomes empty.
6) When queue becomes Empty, then produce final spanning tree by removing
unused edges from the graph.
BFS – Breadth First Search
BFS – Breadth First Search – Example 1
1 Initialize the queue.
2
We start from visiting S (starting node), and mark
it as visited.
3
We then see an unvisited adjacent node from S.
In this example, we have three nodes but
alphabetically we choose A, mark it as visited
and enqueue it.
BFS – Breadth First Search – Example 1
4 Next, the unvisited adjacent node
from S is B. We mark it as visited and
enqueue it.
5
Next, the unvisited adjacent node from S is C.
We mark it as visited and enqueue it.
6
Now, S is left with no unvisited adjacent
nodes. So, we dequeue and find A.
BFS – Breadth First Search – Example 1
7
From A we have D as unvisited adjacent
node. We mark it as visited and
enqueue it.
For BFS sequence, write the vertex in
order of their visited sequence.
BFS sequence (visited sequence): S A B C D
Resulted spanning tree
Spanning tree:
A spanning tree of a connected, undirected graph G
is a sub-graph of G which is a tree that connects all
the vertices together.
Answer:
BFS – Breadth First Search – Example 2
BFS sequence
(visited sequence):
A
Answer:
BFS – Breadth First Search – Example 2
BFS sequence
(visited sequence):
A D E B
Answer:
BFS – Breadth First Search – Example 2
BFS sequence
(visited sequence):
A D E B C F
Answer:
BFS – Breadth First Search – Example 2
BFS sequence
(visited sequence):
A D E B C F G
Answer:
BFS – Breadth First Search – Example 2
BFS sequence
(visited sequence):
A D E B C F G
Answer:
BFS – Breadth First Search – Exercise Question
Starting Vertex A:
ABCDEGFIH
Starting Vertex H:
HEICFBG (A and D are
unreachable)
Answer:
Find the sequence of traverse using BFS.
a. Starting point is A
b. Starting point is H
Differentiate DFS and BFS
DFS
• DFS stands for Depth First Search.
• DFS starts the traversal from the
root node and explore the search as
far as possible from the root node.
i.e. depthwise
• DFS can be done using stack (LIFO
implementation)
BFS
• BFS stands for Breadth First Search.
• BFS starts traversal from the root
node and then explore the search
level by level. i.e. as close as
possible from root node
• BFS can be done using queue (FIFO
implementation)
Differentiate DFS and BFS
DFS
Example:
BFS
Example:
Minimum Spanning Tree (MST)
• Let 𝐺 = 𝑁, 𝐴 be a connected, undirected graph where,
1. 𝑁 is the set of nodes and
2. 𝐴 is the set of edges.
• Each edge has a given positive length or weight.
• A spanning tree of a graph 𝐺 is a sub-graph which is basically a tree and it
contains all the vertices of 𝐺 but does not contain cycle.
• A minimum spanning tree (MST) of a weighted connected graph 𝐺 is a
spanning tree with minimum or smallest weight of edges.
• Two Algorithms for constructing minimum spanning tree are,
1. Kruskal’s Algorithm
2. Prim’s Algorithm
Minimum Spanning Tree (MST) – Prim’s algorithm
A
B
C D
E
4
2
1
7
5
3
6 6
5
A – B | 4
A – E | 5
A – C | 6
A – D | 6
B – E | 3
B – C | 2
C – E | 5
C – D | 1
D – E | 7
Let X be the set of nodes
explored, initially X = { A }
A
Step 1: Taking minimum Weight
edge of all Adjacent edges of X={A}
A
B
4
X = { A , B }
Step 2: Taking minimum weight
edge of all Adjacent edges of
X = { A , B }
A
B
4
2
C
X = {A ,B,C}
Step 3: Taking minimum
weight edge of all Adjacent
edges of X = { A , B , C }
A
B
4
2
C D
1
X = {A,B,C,D}
A
B
4
2
C D
1
X = {A,B,C,D,E}
Step 4: Taking minimum
weight edge of all Adjacent
edges of X = {A ,B ,C ,D }
E
3
We obtained minimum
spanning tree of cost:
4 + 2 + 1 + 3 = 10
Minimum Spanning Tree (MST) – Kruskal’s algorithm
A
B
C D
E
4
2 7
5
3
6 6
5
Step 1: Taking min
edge (C,D)
1
C D
1
Step 2: Taking next
min edge (B,C)
B
C D
2
1
Step 3: Taking next
min edge (B,E)
B
C D
E
2
3
1
Step 4: Taking next
min edge (A,B)
B
C D
E
2
3
1
A
4
so we obtained minimum
spanning tree of cost:
4 + 2 + 1 + 3 = 10
Minimum Spanning Tree (MST) – Exercise questions
0
1 3
4 5
2
3 6
5 5
3 6
6
4
1
2
0
1 3
4 5
2
3
3 4
1
2
3
1
6
4
2
7
5
1
4
1
2 3
2
2
3
3
3
1
4
2
5
1
1
2
2
2
3
7
6
4
Shortest Path Algorithm – Dijkstra’s algorithm
• Let G = (V,E) be a simple diagraph with n vertices
• The problem is to find out shortest distance from a vertex to all other
vertices of a graph
• Dijkstra Algorithm – it is also called Single Source Shortest Path Algorithm
Shortest Path Algorithm – Dijkstra’s algorithm
A
B
C
D
E
F
1
1
5
2
4
5
7
6
2
A B C D E F
0 ∞ ∞ ∞ ∞ ∞
0 0 0 0 0 0
Distance
Visited
A
B
C
D
E
F
1
1
5
2
4
5
7
6
2
A B C D E F
0 ∞ ∞ ∞
0 0 0
Distance
Visited
0
1st Iteration: Select Vertex A with minimum distance
0
∞
1
1 0 0
∞
5
1 5
∞
∞
∞
Example
Shortest Path Algorithm – Dijkstra’s algorithm Example
A
B
C
D
E
F
1
1
5
2
4 7
6
2
0
2nd Iteration: Select Vertex B with minimum distance
1
2
Cost of going to C via B = dist[B] + cost[B][C] = 1 + 1 = 2
Cost of going to D via B = dist[B] + cost[B][D] = 1 + 2 = 3
Cost of going to E via B = dist[B] + cost[B][E] = 1 + 4 = 5
Cost of going to F via B = dist[B] + cost[B][F] = 1 + ∞ = ∞
A B C D E F
0 1 5 ∞ ∞ ∞
1 0 0 0 0 0
Distance
Visited
3
5
∞
A B C D E F
0 1 2 3 5 ∞
0 0 0
Distance
Visited 1 1 0
5
Shortest Path Algorithm – Dijkstra’s algorithm Example
3rd Iteration: Select Vertex C via B with minimum distance
A B C D E F
0 1 2 3 5 ∞
1 1 0 0 0 0
Distance
Visited
A
B
C
D
E
F
1
1
5
2
4 7
6
2
0
1 3
2 5
∞
Cost of going to D via C = dist[C] + cost[C][D] = 2 + ∞ = ∞
Cost of going to E via C = dist[C] + cost[C][E] = 2 + 5 = 7
Cost of going to F via C = dist[C] + cost[C][F] = 2 + ∞ = ∞
A B C D E F
0 1 2 3 5 ∞
0 0 0
Distance
Visited 1 1 1
5
Shortest Path Algorithm – Dijkstra’s algorithm Example
4th Iteration: Select Vertex D via path A - B with minimum distance
A B C D E F
0 1 2 3 5 ∞
1 1 1 0 0 0
Distance
Visited
A
B
C
D
E
F
1
1
5
2
4 7
6
2
1 3
2 5
9
Cost of going to E via D = dist[D] + cost[D][E] = 3 + 7 = 10
Cost of going to F via D = dist[D] + cost[D][F] = 3 + 6 = 9
A B C D E F
0 1 2 3 5 9
1 0 0
Distance
Visited 1 1 1
5
Shortest Path Algorithm – Dijkstra’s algorithm Example
4th Iteration: Select Vertex E via path A – B – E with minimum distance
A B C D E F
0 1 2 3 5 9
1 1 1 1 0 0
Distance
Visited
A
B
C
D
E
F
1
1
5
2
4 7
6
2
1 3
2 5
7
Cost of going to F via E = dist[E] + cost[E][F] = 5 + 2 = 7
A B C D E F
0 1 2 3 5 7
1 1 0
Distance
Visited 1 1 1
Shortest Path from A to F is
A  B  E  F = 7
Shortest Path Algorithm – Dijkstra’s algorithm Exercise
Find out shortest path from node 0 to all other nodes using Dijkstra Algorithm
0
1
2 3
4
10
100
30
60
50 10
20
0
1
2 3
4
10
100
30
60
50 10
20
0
10
50
60
30
Thank you

More Related Content

Similar to Unit-6 Graph.ppsx ppt

Lecture 2.3.1 Graph.pptx
Lecture 2.3.1 Graph.pptxLecture 2.3.1 Graph.pptx
Lecture 2.3.1 Graph.pptxking779879
 
Graphical reprsentation dsa (DATA STRUCTURE ALGORITHM)
Graphical reprsentation dsa (DATA STRUCTURE ALGORITHM)Graphical reprsentation dsa (DATA STRUCTURE ALGORITHM)
Graphical reprsentation dsa (DATA STRUCTURE ALGORITHM)Abdul Naqashbandi
 
Graph Basic In Data structure
Graph Basic In Data structureGraph Basic In Data structure
Graph Basic In Data structureIkhlas Rahman
 
Graph terminology and algorithm and tree.pptx
Graph terminology and algorithm and tree.pptxGraph terminology and algorithm and tree.pptx
Graph terminology and algorithm and tree.pptxasimshahzad8611
 
Depth first traversal(data structure algorithms)
Depth first traversal(data structure algorithms)Depth first traversal(data structure algorithms)
Depth first traversal(data structure algorithms)bhuvaneshwariA5
 
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
 
Data structure computer graphs
Data structure computer graphsData structure computer graphs
Data structure computer graphsKumar
 
Lecture 5b graphs and hashing
Lecture 5b graphs and hashingLecture 5b graphs and hashing
Lecture 5b graphs and hashingVictor Palmar
 
NON-LINEAR DATA STRUCTURE-Graphs.pptx
NON-LINEAR DATA STRUCTURE-Graphs.pptxNON-LINEAR DATA STRUCTURE-Graphs.pptx
NON-LINEAR DATA STRUCTURE-Graphs.pptxRajitha Reddy Alugati
 
Unit II_Graph.pptxkgjrekjgiojtoiejhgnltegjte
Unit II_Graph.pptxkgjrekjgiojtoiejhgnltegjteUnit II_Graph.pptxkgjrekjgiojtoiejhgnltegjte
Unit II_Graph.pptxkgjrekjgiojtoiejhgnltegjtepournima055
 
Graphs in data structures
Graphs in data structuresGraphs in data structures
Graphs in data structuresSavit Chandra
 

Similar to Unit-6 Graph.ppsx ppt (20)

Graph Theory
Graph TheoryGraph Theory
Graph Theory
 
Lecture 2.3.1 Graph.pptx
Lecture 2.3.1 Graph.pptxLecture 2.3.1 Graph.pptx
Lecture 2.3.1 Graph.pptx
 
Spanningtreesppt
SpanningtreespptSpanningtreesppt
Spanningtreesppt
 
U1 L5 DAA.pdf
U1 L5 DAA.pdfU1 L5 DAA.pdf
U1 L5 DAA.pdf
 
logic.pptx
logic.pptxlogic.pptx
logic.pptx
 
DATA STRUCTURES.pptx
DATA STRUCTURES.pptxDATA STRUCTURES.pptx
DATA STRUCTURES.pptx
 
Graphical reprsentation dsa (DATA STRUCTURE ALGORITHM)
Graphical reprsentation dsa (DATA STRUCTURE ALGORITHM)Graphical reprsentation dsa (DATA STRUCTURE ALGORITHM)
Graphical reprsentation dsa (DATA STRUCTURE ALGORITHM)
 
Graph Basic In Data structure
Graph Basic In Data structureGraph Basic In Data structure
Graph Basic In Data structure
 
Graph terminology and algorithm and tree.pptx
Graph terminology and algorithm and tree.pptxGraph terminology and algorithm and tree.pptx
Graph terminology and algorithm and tree.pptx
 
Depth first traversal(data structure algorithms)
Depth first traversal(data structure algorithms)Depth first traversal(data structure algorithms)
Depth first traversal(data structure algorithms)
 
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
 
Data structure computer graphs
Data structure computer graphsData structure computer graphs
Data structure computer graphs
 
Graphs.pptx
Graphs.pptxGraphs.pptx
Graphs.pptx
 
Unit 9 graph
Unit   9 graphUnit   9 graph
Unit 9 graph
 
Lecture 5b graphs and hashing
Lecture 5b graphs and hashingLecture 5b graphs and hashing
Lecture 5b graphs and hashing
 
Unit ix graph
Unit   ix    graph Unit   ix    graph
Unit ix graph
 
NON-LINEAR DATA STRUCTURE-Graphs.pptx
NON-LINEAR DATA STRUCTURE-Graphs.pptxNON-LINEAR DATA STRUCTURE-Graphs.pptx
NON-LINEAR DATA STRUCTURE-Graphs.pptx
 
Unit II_Graph.pptxkgjrekjgiojtoiejhgnltegjte
Unit II_Graph.pptxkgjrekjgiojtoiejhgnltegjteUnit II_Graph.pptxkgjrekjgiojtoiejhgnltegjte
Unit II_Graph.pptxkgjrekjgiojtoiejhgnltegjte
 
Graph Data Structure
Graph Data StructureGraph Data Structure
Graph Data Structure
 
Graphs in data structures
Graphs in data structuresGraphs in data structures
Graphs in data structures
 

Recently uploaded

VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130Suhani Kapoor
 
Electronically Controlled suspensions system .pdf
Electronically Controlled suspensions system .pdfElectronically Controlled suspensions system .pdf
Electronically Controlled suspensions system .pdfme23b1001
 
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
 
CCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdf
CCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdfCCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdf
CCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdfAsst.prof M.Gokilavani
 
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
 
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
 
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...Soham Mondal
 
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
 
Architect Hassan Khalil Portfolio for 2024
Architect Hassan Khalil Portfolio for 2024Architect Hassan Khalil Portfolio for 2024
Architect Hassan Khalil Portfolio for 2024hassan khalil
 
Artificial-Intelligence-in-Electronics (K).pptx
Artificial-Intelligence-in-Electronics (K).pptxArtificial-Intelligence-in-Electronics (K).pptx
Artificial-Intelligence-in-Electronics (K).pptxbritheesh05
 
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
 
SPICE PARK APR2024 ( 6,793 SPICE Models )
SPICE PARK APR2024 ( 6,793 SPICE Models )SPICE PARK APR2024 ( 6,793 SPICE Models )
SPICE PARK APR2024 ( 6,793 SPICE Models )Tsuyoshi Horigome
 
power system scada applications and uses
power system scada applications and usespower system scada applications and uses
power system scada applications and usesDevarapalliHaritha
 
Biology for Computer Engineers Course Handout.pptx
Biology for Computer Engineers Course Handout.pptxBiology for Computer Engineers Course Handout.pptx
Biology for Computer Engineers Course Handout.pptxDeepakSakkari2
 
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptxDecoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptxJoão Esperancinha
 
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
 

Recently uploaded (20)

VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
 
Electronically Controlled suspensions system .pdf
Electronically Controlled suspensions system .pdfElectronically Controlled suspensions system .pdf
Electronically Controlled suspensions system .pdf
 
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
 
Design and analysis of solar grass cutter.pdf
Design and analysis of solar grass cutter.pdfDesign and analysis of solar grass cutter.pdf
Design and analysis of solar grass cutter.pdf
 
CCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdf
CCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdfCCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdf
CCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdf
 
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
 
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
 
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
 
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
 
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
 
Architect Hassan Khalil Portfolio for 2024
Architect Hassan Khalil Portfolio for 2024Architect Hassan Khalil Portfolio for 2024
Architect Hassan Khalil Portfolio for 2024
 
Artificial-Intelligence-in-Electronics (K).pptx
Artificial-Intelligence-in-Electronics (K).pptxArtificial-Intelligence-in-Electronics (K).pptx
Artificial-Intelligence-in-Electronics (K).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
 
SPICE PARK APR2024 ( 6,793 SPICE Models )
SPICE PARK APR2024 ( 6,793 SPICE Models )SPICE PARK APR2024 ( 6,793 SPICE Models )
SPICE PARK APR2024 ( 6,793 SPICE Models )
 
young call girls in Green Park🔝 9953056974 🔝 escort Service
young call girls in Green Park🔝 9953056974 🔝 escort Serviceyoung call girls in Green Park🔝 9953056974 🔝 escort Service
young call girls in Green Park🔝 9953056974 🔝 escort Service
 
power system scada applications and uses
power system scada applications and usespower system scada applications and uses
power system scada applications and uses
 
Biology for Computer Engineers Course Handout.pptx
Biology for Computer Engineers Course Handout.pptxBiology for Computer Engineers Course Handout.pptx
Biology for Computer Engineers Course Handout.pptx
 
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptxDecoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
 
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
 
🔝9953056974🔝!!-YOUNG call girls in Rajendra Nagar Escort rvice Shot 2000 nigh...
🔝9953056974🔝!!-YOUNG call girls in Rajendra Nagar Escort rvice Shot 2000 nigh...🔝9953056974🔝!!-YOUNG call girls in Rajendra Nagar Escort rvice Shot 2000 nigh...
🔝9953056974🔝!!-YOUNG call girls in Rajendra Nagar Escort rvice Shot 2000 nigh...
 

Unit-6 Graph.ppsx ppt

  • 1. Unit - 5 Graph and Hashing Prepared By: Ms. Purvi Tandel, Chandni Naik
  • 2. Topics of Unit 5 • Graph-Definitions and Concepts • Matrix and list representation of graphs • Elementary graph operations - Breadth first search • Elementary graph operations - Depth first search • Hashing - Hashing functions • Collision-Resolution techniques.
  • 3. Introduction of Graph • Graph is a non-linear data structure. • It contains a set of points known as nodes (or vertices) and a set of links known as edges (or Arcs). • Here edges are used to connect the vertices. • Generally, a graph G is represented as G = ( V , E ), where V is set of vertices and E is set of edges. Example: • Graph with 5 vertices and 6 edges. This graph G can be defined as G = ( V , E ) Where V = {A,B,C,D,E} and E = {(A,B),(A,C)(A,D),(B,D),(C,D),(B,E),(E,D)}. Graph is a collection of nodes and edges in which nodes are connected with edges.
  • 4. Graph Definitions 1) Graph: A graph G is defined as an ordered set (V, E), where V(G) represents the set of vertices and E(G) represents the edges that connect these vertices. 2) Vertex: Individual data element of a graph is called as Vertex. Vertex is also known as node. 3) Edge: An edge is a connecting link between two vertices. Edge is also known as Arc.
  • 5. Graph Definitions 4) Adjacent Node: For every edge, e = (u, v) that connects nodes u and v, the nodes u and v are the end-points and are said to be the adjacent nodes or neighbors. In example A and B are adjacent nodes. 5) Directed graph: In a directed graph, edges form an ordered pair. If there is an edge from A to B, then there is a path from A to B but not from B to A. The edge (A, B) is said to initiate from node A (also known as initial node) and terminate at node B (terminal node).
  • 6. Graph Definitions 6) Undirected Graph: In an undirected graph, edges do not have any direction associated with them. That is, if an edge is drawn between nodes A and B, then the nodes can be traversed from A to B as well as from B to A. 7) Loop: An edge that has identical end-points is called a loop. That is, e = (u, u).
  • 7. Graph Definitions 8) Parallel Edge: In some directed as well as undirected graphs, we may have certain pairs of nodes joined by more than one edges, such edges are called Parallel edges. In example edges between node 1 and 2 are parallel edges. 9) Distinct Edge: In case of directed edges, two possible edges between any pair of nodes which are opposite in direction are considered Distinct. In example edges between node 2 and 3 are distinct edges. 2 1 4 3 2 2 1 1 1 2 2 1 4 3 2 2 1 1 1
  • 8. Graph Definitions 10) Weighted Graph: In a weighted graph, the edges of the graph are assigned some weight or length. The weight of an edge denoted by w(e) is a positive value which indicates the cost of traversing the edge. 11) Isolated Graph: In a graph a node which is not adjacent to any other node is called isolated node.
  • 9. Graph Definitions 12) Null Graph: A graph having no edges is called a Null Graph. A graph containing only isolated nodes are called null graph. 13) Degree of node: The degree of a node, written as deg(u), is equal to the sum of in-degree and out-degree of that node. degree(u) = indegree(u) + outdegree(u) Indegree : The no of edges which have V as their terminal node is call as indegree of node V. Outdegree : The no of edges which have V as their initial node is call as outdegree of node V. Degree of A = 2, Degree of B = 3 Degree of C = 2, Degree of D = 3 Degree of E = 2
  • 10. Matrix and list representation of graphs Graph data structure is represented using following representations. • Adjacency Matrix • Adjacency List (Linked List)
  • 11. Matrix and list representation of graphs Graph data structure is represented using following representations. 1) Adjacency Matrix: In this representation, the graph is represented using a matrix of size total number of vertices by a total number of vertices. That means a graph with 4 vertices is represented using a matrix of size 4X4. In this matrix, both rows and columns represent vertices. This matrix is filled with either 1 or 0. Here, 1 represents that there is an edge from row vertex to column vertex and 0 represents that there is no edge from row vertex to column vertex. Undirected graph representation
  • 12. Matrix and list representation of graphs Graph data structure is represented using following representations. 1) Adjacency Matrix: In this representation, the graph is represented using a matrix of size total number of vertices by a total number of vertices. That means a graph with 4 vertices is represented using a matrix of size 4X4. In this matrix, both rows and columns represent vertices. This matrix is filled with either 1 or 0. Here, 1 represents that there is an edge from row vertex to column vertex and 0 represents that there is no edge from row vertex to column vertex. Directed graph representation
  • 13. Matrix and list representation of graphs Graph data structure is represented using following representations. 1) Adjacency Matrix: One more example in directed graph representation. For a given graph G =m (V, E), an adjacency matrix depends upon the ordering of the elements of V. V1 V2 V3 V4 0 1 0 1 1 0 0 0 1 1 0 1 0 1 0 0 V1 V1 V2 V2 V3 V3 V4 V4 A =
  • 14. Matrix and list representation of graphs Graph data structure is represented using following representations. 2) Adjacency List: In this representation, every vertex of graph contains list of its adjacent vertices. For example, consider the following directed graph representation implemented using linked list.
  • 15. Matrix and list representation of graphs Graph data structure is represented using following representations. 2) Adjacency List: One more example of undirected graph representation. 0 1 3 2 4 0 1 2 3 4 1 2 3 4 0 3 0 3 4 0 1 2 4 0 2 3
  • 16. Elementary graph operations – DFS & BFS • One of the elementary operation on graph is graph traversal. • Graph traversal is technique used for searching a vertex in a graph. • The graph traversal is also used to decide the order of vertices to be visit in the search process. • A graph traversal finds the edges to be used in the search process without creating loops that means using graph traversal we visit all vertices of graph without getting into looping path. There are two graph traversal techniques: • DFS (Depth First Search) • BFS (Breadth First Search)
  • 17. DFS – Depth First Search Depth First Search (DFS) algorithm traverses a graph in a depth-ward motion and uses a stack to remember to get the next vertex to start a search, when a dead end occurs in any iteration. A B C D E F G H A B D H E C F G ✓ ✓ ✓ ✓ ✓ ✓ ✓ ✓
  • 18. DFS – Depth First Search We use the following steps to implement DFS traversal. 1) Define a Stack of size total number of vertices in the graph. 2) Select any vertex as starting point for traversal. Visit that vertex and push it on to the Stack. 3) Visit any one of the adjacent vertex of the vertex which is at top of the stack which is not visited and push it on to the stack. 4) Repeat step 3 until there are no new vertex to be visit from the vertex on top of the stack. 5) When there is no new vertex to be visit then use back tracking and pop one vertex from the stack. 6) Repeat steps 3, 4 and 5 until stack becomes Empty. 7) When stack becomes Empty, then produce final spanning tree by removing unused edges from the graph.
  • 19. DFS – Depth First Search – Example 1 1 Initialize the stack. 2 Mark S as visited and put it onto the stack. Explore any unvisited adjacent node from S. We have three nodes and we can pick any of them. For this example, we shall take the node in an alphabetical order. 3 Mark A as visited and put it onto the stack. Explore any unvisited adjacent node from A. Both S and D are adjacent to A but we are concerned for unvisited nodes only.
  • 20. DFS – Depth First Search – Example 1 4 Visit D and mark it as visited and put onto the stack. Here, we have B and C nodes, which are adjacent to D and both are unvisited. However, we shall again choose in an alphabetical order. 5 We choose B, mark it as visited and put onto the stack. Here B does not have any unvisited adjacent node. So, we pop B from the stack. 6 We check the stack top for return to the previous node and check if it has any unvisited nodes. Here, we find D to be on the top of the stack.
  • 21. DFS – Depth First Search – Example 1 7 Only unvisited adjacent node is from D is C now. So we visit C, mark it as visited and put it onto the stack. For DFS sequence, write the vertex in order of their visited sequence. DFS sequence (visited sequence): S A D B C Resulted spanning tree Spanning tree: A spanning tree of a connected, undirected graph G is a sub-graph of G which is a tree that connects all the vertices together. Answer:
  • 22. Introduction to Minimum spanning tree Spanning Tree: • A spanning tree of a connected, undirected graph G is a sub-graph of G which is a tree that connects all the vertices together. • A graph G can have many different spanning trees. We can assign weights to each edge (which is a number that represents how unfavorable the edge is), and use it to assign a weight to a spanning tree by calculating the sum of the weights of the edges in that spanning tree.
  • 23. Introduction to Minimum spanning tree Minimum spanning tree: • A minimum spanning tree (MST) is defined as a spanning tree with weight less than or equal to the weight of every other spanning tree. • In other words, a minimum spanning tree is a spanning tree that has weights associated with its edges, and the total weight of the tree (the sum of the weights of its edges) is at a minimum.
  • 24. DFS – Depth First Search – Example 2 DFS sequence (visited sequence): A Answer:
  • 25. DFS – Depth First Search – Example 2 DFS sequence (visited sequence): A B C Answer:
  • 26. DFS – Depth First Search – Example 2 DFS sequence (visited sequence): A B C E D Answer:
  • 27. DFS – Depth First Search – Example 2 DFS sequence (visited sequence): A B C E D F Answer:
  • 28. DFS – Depth First Search – Example 2 DFS sequence (visited sequence): A B C E D F G Answer:
  • 29. DFS – Depth First Search – Example 2 DFS sequence (visited sequence): A B C E D F G Answer:
  • 30. DFS – Depth First Search – Example 2 DFS sequence (visited sequence): A B C E D F G Answer:
  • 31. DFS – Depth First Search – Example 2 DFS sequence (visited sequence): A B C E D F G Answer:
  • 32. DFS – Depth First Search – Exercise Question Starting Vertex A: ABEFCGIHD Starting Vertex H: HEFCBGI (A and D are unreachable) Answer: Find the sequence of traverse using DFS. a. Starting point is A b. Starting point is H
  • 33. BFS – Breadth First Search Breadth First Search (BFS) algorithm traverses a graph in a breadth-ward motion and uses a queue to remember to get the next vertex to start a search, when a dead end occurs in any iteration. A B C D E F G H A| B C| D E F G| H ✓ ✓ ✓ ✓ ✓ ✓ ✓ ✓
  • 34. We use the following steps to implement BFS traversal. 1) Define a Queue of size total number of vertices in the graph. 2) Select any vertex as starting point for traversal. Visit that vertex and insert it into the Queue. 3) Visit all the adjacent vertices of the vertex which is at front of the Queue which is not visited and insert them into the Queue. 4) When there is no new vertex to be visit from the vertex at front of the Queue then delete that vertex from the Queue. 5) Repeat step 3 and 4 until queue becomes empty. 6) When queue becomes Empty, then produce final spanning tree by removing unused edges from the graph. BFS – Breadth First Search
  • 35. BFS – Breadth First Search – Example 1 1 Initialize the queue. 2 We start from visiting S (starting node), and mark it as visited. 3 We then see an unvisited adjacent node from S. In this example, we have three nodes but alphabetically we choose A, mark it as visited and enqueue it.
  • 36. BFS – Breadth First Search – Example 1 4 Next, the unvisited adjacent node from S is B. We mark it as visited and enqueue it. 5 Next, the unvisited adjacent node from S is C. We mark it as visited and enqueue it. 6 Now, S is left with no unvisited adjacent nodes. So, we dequeue and find A.
  • 37. BFS – Breadth First Search – Example 1 7 From A we have D as unvisited adjacent node. We mark it as visited and enqueue it. For BFS sequence, write the vertex in order of their visited sequence. BFS sequence (visited sequence): S A B C D Resulted spanning tree Spanning tree: A spanning tree of a connected, undirected graph G is a sub-graph of G which is a tree that connects all the vertices together. Answer:
  • 38. BFS – Breadth First Search – Example 2 BFS sequence (visited sequence): A Answer:
  • 39. BFS – Breadth First Search – Example 2 BFS sequence (visited sequence): A D E B Answer:
  • 40. BFS – Breadth First Search – Example 2 BFS sequence (visited sequence): A D E B C F Answer:
  • 41. BFS – Breadth First Search – Example 2 BFS sequence (visited sequence): A D E B C F G Answer:
  • 42. BFS – Breadth First Search – Example 2 BFS sequence (visited sequence): A D E B C F G Answer:
  • 43. BFS – Breadth First Search – Exercise Question Starting Vertex A: ABCDEGFIH Starting Vertex H: HEICFBG (A and D are unreachable) Answer: Find the sequence of traverse using BFS. a. Starting point is A b. Starting point is H
  • 44. Differentiate DFS and BFS DFS • DFS stands for Depth First Search. • DFS starts the traversal from the root node and explore the search as far as possible from the root node. i.e. depthwise • DFS can be done using stack (LIFO implementation) BFS • BFS stands for Breadth First Search. • BFS starts traversal from the root node and then explore the search level by level. i.e. as close as possible from root node • BFS can be done using queue (FIFO implementation)
  • 45. Differentiate DFS and BFS DFS Example: BFS Example:
  • 46. Minimum Spanning Tree (MST) • Let 𝐺 = 𝑁, 𝐴 be a connected, undirected graph where, 1. 𝑁 is the set of nodes and 2. 𝐴 is the set of edges. • Each edge has a given positive length or weight. • A spanning tree of a graph 𝐺 is a sub-graph which is basically a tree and it contains all the vertices of 𝐺 but does not contain cycle. • A minimum spanning tree (MST) of a weighted connected graph 𝐺 is a spanning tree with minimum or smallest weight of edges. • Two Algorithms for constructing minimum spanning tree are, 1. Kruskal’s Algorithm 2. Prim’s Algorithm
  • 47. Minimum Spanning Tree (MST) – Prim’s algorithm A B C D E 4 2 1 7 5 3 6 6 5 A – B | 4 A – E | 5 A – C | 6 A – D | 6 B – E | 3 B – C | 2 C – E | 5 C – D | 1 D – E | 7 Let X be the set of nodes explored, initially X = { A } A Step 1: Taking minimum Weight edge of all Adjacent edges of X={A} A B 4 X = { A , B } Step 2: Taking minimum weight edge of all Adjacent edges of X = { A , B } A B 4 2 C X = {A ,B,C} Step 3: Taking minimum weight edge of all Adjacent edges of X = { A , B , C } A B 4 2 C D 1 X = {A,B,C,D} A B 4 2 C D 1 X = {A,B,C,D,E} Step 4: Taking minimum weight edge of all Adjacent edges of X = {A ,B ,C ,D } E 3 We obtained minimum spanning tree of cost: 4 + 2 + 1 + 3 = 10
  • 48. Minimum Spanning Tree (MST) – Kruskal’s algorithm A B C D E 4 2 7 5 3 6 6 5 Step 1: Taking min edge (C,D) 1 C D 1 Step 2: Taking next min edge (B,C) B C D 2 1 Step 3: Taking next min edge (B,E) B C D E 2 3 1 Step 4: Taking next min edge (A,B) B C D E 2 3 1 A 4 so we obtained minimum spanning tree of cost: 4 + 2 + 1 + 3 = 10
  • 49. Minimum Spanning Tree (MST) – Exercise questions 0 1 3 4 5 2 3 6 5 5 3 6 6 4 1 2 0 1 3 4 5 2 3 3 4 1 2 3 1 6 4 2 7 5 1 4 1 2 3 2 2 3 3 3 1 4 2 5 1 1 2 2 2 3 7 6 4
  • 50. Shortest Path Algorithm – Dijkstra’s algorithm • Let G = (V,E) be a simple diagraph with n vertices • The problem is to find out shortest distance from a vertex to all other vertices of a graph • Dijkstra Algorithm – it is also called Single Source Shortest Path Algorithm
  • 51. Shortest Path Algorithm – Dijkstra’s algorithm A B C D E F 1 1 5 2 4 5 7 6 2 A B C D E F 0 ∞ ∞ ∞ ∞ ∞ 0 0 0 0 0 0 Distance Visited A B C D E F 1 1 5 2 4 5 7 6 2 A B C D E F 0 ∞ ∞ ∞ 0 0 0 Distance Visited 0 1st Iteration: Select Vertex A with minimum distance 0 ∞ 1 1 0 0 ∞ 5 1 5 ∞ ∞ ∞ Example
  • 52. Shortest Path Algorithm – Dijkstra’s algorithm Example A B C D E F 1 1 5 2 4 7 6 2 0 2nd Iteration: Select Vertex B with minimum distance 1 2 Cost of going to C via B = dist[B] + cost[B][C] = 1 + 1 = 2 Cost of going to D via B = dist[B] + cost[B][D] = 1 + 2 = 3 Cost of going to E via B = dist[B] + cost[B][E] = 1 + 4 = 5 Cost of going to F via B = dist[B] + cost[B][F] = 1 + ∞ = ∞ A B C D E F 0 1 5 ∞ ∞ ∞ 1 0 0 0 0 0 Distance Visited 3 5 ∞ A B C D E F 0 1 2 3 5 ∞ 0 0 0 Distance Visited 1 1 0 5
  • 53. Shortest Path Algorithm – Dijkstra’s algorithm Example 3rd Iteration: Select Vertex C via B with minimum distance A B C D E F 0 1 2 3 5 ∞ 1 1 0 0 0 0 Distance Visited A B C D E F 1 1 5 2 4 7 6 2 0 1 3 2 5 ∞ Cost of going to D via C = dist[C] + cost[C][D] = 2 + ∞ = ∞ Cost of going to E via C = dist[C] + cost[C][E] = 2 + 5 = 7 Cost of going to F via C = dist[C] + cost[C][F] = 2 + ∞ = ∞ A B C D E F 0 1 2 3 5 ∞ 0 0 0 Distance Visited 1 1 1 5
  • 54. Shortest Path Algorithm – Dijkstra’s algorithm Example 4th Iteration: Select Vertex D via path A - B with minimum distance A B C D E F 0 1 2 3 5 ∞ 1 1 1 0 0 0 Distance Visited A B C D E F 1 1 5 2 4 7 6 2 1 3 2 5 9 Cost of going to E via D = dist[D] + cost[D][E] = 3 + 7 = 10 Cost of going to F via D = dist[D] + cost[D][F] = 3 + 6 = 9 A B C D E F 0 1 2 3 5 9 1 0 0 Distance Visited 1 1 1 5
  • 55. Shortest Path Algorithm – Dijkstra’s algorithm Example 4th Iteration: Select Vertex E via path A – B – E with minimum distance A B C D E F 0 1 2 3 5 9 1 1 1 1 0 0 Distance Visited A B C D E F 1 1 5 2 4 7 6 2 1 3 2 5 7 Cost of going to F via E = dist[E] + cost[E][F] = 5 + 2 = 7 A B C D E F 0 1 2 3 5 7 1 1 0 Distance Visited 1 1 1 Shortest Path from A to F is A  B  E  F = 7
  • 56. Shortest Path Algorithm – Dijkstra’s algorithm Exercise Find out shortest path from node 0 to all other nodes using Dijkstra Algorithm 0 1 2 3 4 10 100 30 60 50 10 20 0 1 2 3 4 10 100 30 60 50 10 20 0 10 50 60 30

Editor's Notes

  1. You can safely remove this slide. This slide design was provided by SlideModel.com – You can download more templates, shapes and elements for PowerPoint from http://slidemodel.com