SlideShare a Scribd company logo
Ta Quang Hung
FACULTY OF INFORMATION TECHNOLOGY
Discrete Mathematics
Fall, 2023
Lecture 10: Graph – Part 2
Lecture 10 The University of New South Wales
Discrete Mathematics
Contents
Page 2
2 Dijkstras Algorithm
3 Bellman-Ford Algorithm
1 Shortest Path Algorithms
4 Summary
Lecture 10 The University of New South Wales
Discrete Mathematics Page 3
SHORTEST PATH
ALGORITHMS
Lecture 10 The University of New South Wales
Discrete Mathematics
Introduction
• Perhaps the most intuitive graph-processing
problem is one that you encounter regularly, when
using a map application or a navigation system to
get directions from one place to another. A graph
model is immediate: vertices correspond to
intersections and edges correspond to roads, with
weights on the edges that model the cost, perhaps
distance or travel time. The possibility of one-way
roads means that we will need to consider edge-
weighted digraphs. In this model, the problem is
easy to formulate:
Find the lowest-cost way to get
from one vertex to another
Page 4
Lecture 10 The University of New South Wales
Discrete Mathematics
Example: Navigation System
Page 5
Vertices correspond to
intersections and edges
correspond to roads.
Weights on the edges that
model the cost, perhaps
distance or travel time.
Lecture 10 The University of New South Wales
Discrete Mathematics
Example: Geographic Information System (GIS)
Page 6
Multiple layers are
combined to build GIS
Lecture 10 The University of New South Wales
Discrete Mathematics
Example: Printed Circuit Board (PCB)
Page 7
Autoroute Mode: Find the
shortest path for connections to
minimize resistances.
Lecture 10 The University of New South Wales
Discrete Mathematics
Example: PCB layout for DDR3 RAM interface
Page 8
Lecture 10 The University of New South Wales
Discrete Mathematics
Example: Game
Page 9
Base under attack: Troops must
find the shortest path back home.
Game map:
Look like a
maze.
Lecture 10 The University of New South Wales
Discrete Mathematics
Example: Uber Taxi
Page 10
Fare Estimate: calculate the minimum fee for the trip.
Lecture 10 The University of New South Wales
Discrete Mathematics
Properties of Shortest Path
• Paths are directed. A shortest path must respect the
direction of its edges.
• The weights are not necessarily distances. We use
examples where vertices are points in the plane and weights
are Euclidean distances, such as the digraph on the facing
page. But the weights might represent time or cost or an
entirely different variable and do not need to be proportional
to a distance at all. We are emphasizing this point by using
mixed-metaphor terminology where we refer to a shortest
path of minimal weight or cost.
• Not all vertices need be reachable. If t is not reachable
from s, there is no path at all, and therefore there is no
shortest path from s to t. For simplicity, our small running
example is strongly connected (every vertex is reachable
from every other vertex).
Page 11
Lecture 10 The University of New South Wales
Discrete Mathematics
Properties of Shortest Path (con't)
• Negative weights introduce complications. For the moment, we
assume that edge weights are positive (or zero).
• Shortest paths are normally simple. Algorithms ignore zero-weight
edges that form cycles, so that the shortest paths they find have no
cycles.
• Shortest paths are not necessarily unique. There may be multiple
paths of the lowest weight from one vertex to another; we are content to
find any one of them.
• Parallel edges and self-loops may be present. Only the lowest-weight
among a set of parallel edges will play a role, and no shortest path
contains a self-loop (except possibly one of zero weight, which we
ignore). In the text, we implicitly assume that parallel edges are not
present for convenience in using the notation v->w to refer
unambiguously to the edge from v to w.
Page 12
Lecture 10 The University of New South Wales
Discrete Mathematics Page 13
DIJKSTRAS ALGORITHM
Lecture 10 The University of New South Wales
Discrete Mathematics
Dijkstras Algorithm
Page 14
• This algorithm can’t work with negative edge
weights.
• Dijkstras algorithm runs in O(|n|+|m|log|m|) where n
vertices and m edges.
Lecture 10 The University of New South Wales
Discrete Mathematics
Dijkstras Algorithm: Example
Page 15
S = {A}
Find path from A to all other vertices?
Set L(A) = 0
Step 0
A
B
C D
E
G
F
3
6
2
7
1
2
4 5
6
8
9
3
3
Solved node
Unsolved node
Solving node
Denote:
• w(A,B) = weighting coefficient in the
path from A to B. So, from the graph,
we have w(A,B) = 3.
• L(Y) = Label of Y. Minimum cost of Y
or the length of the shortest path
from orignating vertice to Y. L(Y) is a
sum of all weighting coefficient and
the cost of all vertices on the shortest
path.
• S: A set of all vertices that the
shortest path go through.
Lecture 10 The University of New South Wales
Discrete Mathematics
Dijkstras Algorithm: Example
Page 16
S = {A,G}
Check all paths from A to
adjiacent vertices: F, G, B, C
L(A) + w(A,G) = 1
L(A) + w(A,B) = 3
L(A) + w(A,F) = 2
L(A) + w(A,C) = 7
Next vertice is G, add G to set S
Set L(G) = 1
A
B
C D
E
G
F
Solved node
Unsolved node
3
6
2
7
1
2
4 5
6
8
9
3
3
Solving node
0
Step 1
Lecture 10 The University of New South Wales
Discrete Mathematics
Dijkstras Algorithm: Example
Page 17
S = {A,G,F}
Check all paths from A, G: F, B, E, D,
C.
L(G) + w(G,F) = 5
L(G) + w(G,E) = 10
L(G) + w(G,D) = 9
L(G) + w(G,B) = 3
Next vertice is F, set L(F)=2, add F
to set S:
A
B
C D
E
G
F
Solved node
Unsolved node
3
6
2
7
1
2
4 5
6
8
9
3
3
Solving node
0
1
L(A) + w(A,B) = 3
L(A) + w(A,F) = 2
L(A) + w(A,C) = 7
Step 2
Lecture 10 The University of New South Wales
Discrete Mathematics
Dijkstras Algorithm: Example
Page 18
A
B
C D
E
G
F
Solved node
Unsolved node
3
6
2
7
1
2
4 5
6
8
9
3
3
Solving node
0
1
2
S = {A,G,F,B}
Check all paths from A, G, F to
adjiacent vertices: B, E, C, D.
L(G) + w(G,E) = 10
L(G) + w(G,D) = 9
L(G) + w(G,B) = 3
Next vertice is B, set L(B) = 3, add B
to set S:
L(A) + w(A,B) = 3
L(A) + w(A,C) = 7
L(F) + w(F,E) = 7
L(F) + w(F,D) = 5
Step 3
Lecture 10 The University of New South Wales
Discrete Mathematics
Dijkstras Algorithm: Example
Page 19
S = {A,G,F,B,D}
Check all paths from A, G, F, B to
adjiacent vertices: E, C, D.
L(G) + w(G,E) = 10
L(G) + w(G,D) = 9
Next vertice is D, set L(D) = 5, add D
to set S:
L(A) + w(A,C) = 7
L(F) + w(F,E) = 7
L(F) + w(F,D) = 5
L(B) + w(B,C) = 9
A
B
C D
E
G
F
Solved node
Unsolved node
3
6
2
7
1
2
4 5
6
8
9
3
3
Solving node
0
1
2
3
Step 4
Lecture 10 The University of New South Wales
Discrete Mathematics
Dijkstras Algorithm: Example
Page 20
S = {A,G,F,B,D,C}
Check all paths from A, G, F, B, D to
adjiacent vertices: E, C.
L(G) + w(G,E) = 10
Next vertice is C, set L(C) = 7, add C
to set S:
L(A) + w(A,C) = 7
L(F) + w(F,E) = 7
L(B) + w(B,C) = 9
A
B
C D
E
G
F
Solved node
Unsolved node
3
6
2
7
1
2
4 5
6
8
9
3
3
Solving node
0
1
2
3
5
L(D) + w(D,C) = 8
L(D) + w(D,E) = 11
Step 5
Lecture 10 The University of New South Wales
Discrete Mathematics
Dijkstras Algorithm: Example
Page 21
S = {A,G,F,B,D,C,E}
Check all paths from A, G, F, B, D, C
to adjiacent vertices: E.
L(G) + w(G,E) = 10
Next vertice is E, set L(E) = 7, add E
to set S:
L(F) + w(F,E) = 7
L(D) + w(D,E) = 11
A
B
C D
E
G
F
Solved node
Unsolved node
3
6
2
7
1
2
4 5
6
8
9
3
3
Solving node
0
1
2
3
5
7
Step 6
Lecture 10 The University of New South Wales
Discrete Mathematics
Dijkstras Algorithm: Example
Page 22
n A G F B D C E Shortest Path
0 0       A
1 0 (1,A) (2,A) (3,A)  (7,A)  A,G
2 0 (1,A) (2,A) (3,A) (9,G) (7,A) (10,G) A,G,F
3 0 (1,A) (2,A) (3,A) (5,F) (7,A) (7,F) A,G,F,B
4 0 (1,A) (2,A) (3,A) (5,F) (7,A) (7,F) A,G,F,B,D
5 0 (1,A) (2,A) (3,A) (5,F) (7,A) (7,F) A,G,F,B,D,C
6 0 (1,A) (2,A) (3,A) (5,F) (7,A) (7,F) A,G,F,B,D,C,E
0 (1,A) (2,A) (3,A) (5,F) (7,A) (7,F) A,G,F,B,D,C,E
Denote:
• (L,P): where L is label of the vertice, and P is preceding vertice in the path.
Lecture 10 The University of New South Wales
Discrete Mathematics Page 23
BELLMAN-FORD
ALGORITHM
Lecture 10 The University of New South Wales
Discrete Mathematics
Bellman-Ford Algorithm
Page 24
• This algorithm can work with negative edge weights.
• Can detect a negative cycle.
• Bellman-Ford runs in O(|n|-|m|) where n vertices and
m edges.
Lecture 10 The University of New South Wales
Discrete Mathematics
Bellman-Ford Algorithm: Example
Page 25
Find path from Z to all other vertices?
U
Z
X Y
V
6
7
8
2
5
-3
-4
7
9
Solved node
Unsolved node
Solving node
Denote:
• w(A,B) = weighting coefficient in the
path from A to B. So, from the graph,
we have w(A,B) = 3.
-2
Lecture 10 The University of New South Wales
Discrete Mathematics
Bellman-Ford Algorithm: Example
Page 26
Step 0
U
Z
X Y
V
6
7
8
2
5
-3
-4
7
9
Solved node
Unsolved node
Solving node
-2
 


n Z U X V Y
0 0 ,- ,- ,- ,-
0
Lecture 10 The University of New South Wales
Discrete Mathematics
Bellman-Ford Algorithm: Example
Page 27
Step 1
U
Z
X Y
V
6
7
8
2
5
-3
-4
7
9
Solved node
Unsolved node
Solving node
-2
6,Z 

7,Z
n Z U X V Y
0 0 ,- ,- ,- ,-
1 0 6,Z 7,Z ,- ,-
From Z, can go to U, and X. Then assign
labels to U, X by adding cost of Z
(in previous step – step 0) to w(Z,U) and
w(Z,X). We denote
0
L1(U) = 6; P1(U) = Z
L1(X) = 7; P1(X) = Z
Lecture 10 The University of New South Wales
Discrete Mathematics
Bellman-Ford Algorithm: Example
Page 28
Step 2
U
Z
X Y
V
6
7
8
2
5
-3
-4
7
9
Solved node
Unsolved node
Solving node
-2
6,Z 4,X
2,U
7,Z
n Z U X V Y
0 0 ,- ,- ,- ,-
1 0 6,Z 7,Z ,- ,-
2 0 6,Z 7,Z 4,X 2,U
0
Recalculate U,X,V,Y by (L,P) in step 2:
L2(V) = min{L1(U)+w(U,V),L1(X)+w(X,V)}
= min{6+5,7-3} = 4, P2(V) = X
L2(Y) = min{L1(U)+w(U,Y), L1(X)+w(X,Y),
= min{6-4,7+9} = 2; P2(Y) = U
L2(U) = min{L1(Z)+w(Z,U),L1(V)+w(V,U)}
= min{0+6, } = 6; P2(U) = Z
L2(X) = min{L1(U)+w(U,X), L1(Z)+w(Z,X),
= min{6+8,0+7} = 7; P2(X) = Z
Lecture 10 The University of New South Wales
Discrete Mathematics
Bellman-Ford Algorithm: Example
Page 29
Step 3
U
Z
X Y
V
6
7
8
2
5
-3
-4
7
9
Solved node
Unsolved node
Solving node
-2
2,V 4,X
2,U
7,Z
n Z U X V Y
0 0 ,- ,- ,- ,-
1 0 6,Z 7,Z ,- ,-
2 0 6,Z 7,Z 4,X 2,U
3 0 2,V 7,Z 4,X 2,U
0
Recalculate U,X,V,Y by (L,P) in step 3:
L3(V) = min{L2(U)+w(U,V),L2(X)+w(X,V)}
= min{6+5,7-3} = 4, P3(V) = 4,X
L3(Y) = min{L2(U)+w(U,Y), L2(X)+w(X,Y),
= min{6-4,7+9} = 2; P3(Y) = 2,U
L3(U) = min{L2(Z)+w(Z,U),L2(V)+w(V,U)}
= min{0+6,4-2} = 2; P3(U) = 2,V
L3(X) = min{L2(U)+w(U,X), L2(Z)+w(Z,X),
= min{6+8,0+7} = 7; P3(X) = 7,Z
Lecture 10 The University of New South Wales
Discrete Mathematics
Bellman-Ford Algorithm: Example
Page 30
Step 4
U
Z
X Y
V
6
7
8
2
5
-3
-4
7
9
Solved node
Unsolved node
Solving node
-2
2,V 4,X
2,U
7,Z
n Z U X V Y
0 0 ,- ,- ,- ,-
1 0 6,Z 7,Z ,- ,-
2 0 6,Z 7,Z 4,X 2,U
3 0 2,V 7,Z 4,X 2,U
4 0 2,V 7,Z 4,X -2,U
0
Recalculate U,X,V,Y by (L,P) in step 3:
L4(V) = min{L3(U)+w(U,V),L3(X)+w(X,V)}
= min{2+5,7-3} = 4, P4(V) = X
L4(Y) = min{L3(U)+w(U,Y), L3(X)+w(X,Y),
= min{2-4,7+9} = -2; P4(Y) = U
L4(U) = min{L3(Z)+w(Z,U),L3(V)+w(V,U)}
= min{0+6,4-2} = 2; P4(U) = V
L4(X) = min{L3(U)+w(U,X), L3(Z)+w(Z,X),
= min{2+8,0+7} = 7; P4(X) = Z
Lecture 10 The University of New South Wales
Discrete Mathematics
Summary
Page 31
2 Dijkstras Algorithm
3 Bellman-Ford Algorithm
1 Shortest Path Algorithms
Ad

More Related Content

Similar to Lecture 10 - Graph part 2.pptx,discrete mathemactics (20)

Day 5 application of graph ,biconnectivity fdp on ds
Day 5 application of graph ,biconnectivity fdp on dsDay 5 application of graph ,biconnectivity fdp on ds
Day 5 application of graph ,biconnectivity fdp on ds
GUNASUNDARISAPIIICSE
 
A study on_contrast_and_comparison_between_bellman-ford_algorithm_and_dijkstr...
A study on_contrast_and_comparison_between_bellman-ford_algorithm_and_dijkstr...A study on_contrast_and_comparison_between_bellman-ford_algorithm_and_dijkstr...
A study on_contrast_and_comparison_between_bellman-ford_algorithm_and_dijkstr...
Khoa Mac Tu
 
All pairs shortest path algorithm
All pairs shortest path algorithmAll pairs shortest path algorithm
All pairs shortest path algorithm
Srikrishnan Suresh
 
GRAPH THEORY - Basic definition with examples
GRAPH THEORY - Basic definition with examplesGRAPH THEORY - Basic definition with examples
GRAPH THEORY - Basic definition with examples
Gayathri M
 
Dijksatra
DijksatraDijksatra
Dijksatra
Tanmay Baranwal
 
04 greedyalgorithmsii 2x2
04 greedyalgorithmsii 2x204 greedyalgorithmsii 2x2
04 greedyalgorithmsii 2x2
MuradAmn
 
Unit ii-ppt
Unit ii-pptUnit ii-ppt
Unit ii-ppt
Aravindharamanan S
 
2.6 all pairsshortestpath
2.6 all pairsshortestpath2.6 all pairsshortestpath
2.6 all pairsshortestpath
Krish_ver2
 
bellman-ford dynamic algorithm in data structures.ppt
bellman-ford dynamic algorithm in data structures.pptbellman-ford dynamic algorithm in data structures.ppt
bellman-ford dynamic algorithm in data structures.ppt
RAJASEKARAN G
 
Daa chpater14
Daa chpater14Daa chpater14
Daa chpater14
B.Kirron Reddi
 
Topological Sort
Topological SortTopological Sort
Topological Sort
Dr Sandeep Kumar Poonia
 
Ppt 1
Ppt 1Ppt 1
Ppt 1
Sowbhagya Lakshmi
 
Inroduction_To_Algorithms_Lect14
Inroduction_To_Algorithms_Lect14Inroduction_To_Algorithms_Lect14
Inroduction_To_Algorithms_Lect14
Naor Ami
 
Optimisation random graph presentation
Optimisation random graph presentationOptimisation random graph presentation
Optimisation random graph presentation
Venkat Sai Sharath Mudhigonda
 
algorithm Unit 3
algorithm Unit 3algorithm Unit 3
algorithm Unit 3
Monika Choudhery
 
AAD_Lec-3-B-ShortestPaths.ppt of design and analysis of algorithm
AAD_Lec-3-B-ShortestPaths.ppt  of design and analysis of algorithmAAD_Lec-3-B-ShortestPaths.ppt  of design and analysis of algorithm
AAD_Lec-3-B-ShortestPaths.ppt of design and analysis of algorithm
SantoshDood
 
Unit ix graph
Unit   ix    graph Unit   ix    graph
Unit ix graph
Tribhuvan University
 
Discrete-Chapter 11 Graphs Part III
Discrete-Chapter 11 Graphs Part IIIDiscrete-Chapter 11 Graphs Part III
Discrete-Chapter 11 Graphs Part III
Wongyos Keardsri
 
Graph theory presentation
Graph theory presentationGraph theory presentation
Graph theory presentation
Aliul Kadir Akib
 
Randomized algorithms all pairs shortest path
Randomized algorithms  all pairs shortest pathRandomized algorithms  all pairs shortest path
Randomized algorithms all pairs shortest path
Mohammad Akbarizadeh
 
Day 5 application of graph ,biconnectivity fdp on ds
Day 5 application of graph ,biconnectivity fdp on dsDay 5 application of graph ,biconnectivity fdp on ds
Day 5 application of graph ,biconnectivity fdp on ds
GUNASUNDARISAPIIICSE
 
A study on_contrast_and_comparison_between_bellman-ford_algorithm_and_dijkstr...
A study on_contrast_and_comparison_between_bellman-ford_algorithm_and_dijkstr...A study on_contrast_and_comparison_between_bellman-ford_algorithm_and_dijkstr...
A study on_contrast_and_comparison_between_bellman-ford_algorithm_and_dijkstr...
Khoa Mac Tu
 
All pairs shortest path algorithm
All pairs shortest path algorithmAll pairs shortest path algorithm
All pairs shortest path algorithm
Srikrishnan Suresh
 
GRAPH THEORY - Basic definition with examples
GRAPH THEORY - Basic definition with examplesGRAPH THEORY - Basic definition with examples
GRAPH THEORY - Basic definition with examples
Gayathri M
 
04 greedyalgorithmsii 2x2
04 greedyalgorithmsii 2x204 greedyalgorithmsii 2x2
04 greedyalgorithmsii 2x2
MuradAmn
 
2.6 all pairsshortestpath
2.6 all pairsshortestpath2.6 all pairsshortestpath
2.6 all pairsshortestpath
Krish_ver2
 
bellman-ford dynamic algorithm in data structures.ppt
bellman-ford dynamic algorithm in data structures.pptbellman-ford dynamic algorithm in data structures.ppt
bellman-ford dynamic algorithm in data structures.ppt
RAJASEKARAN G
 
Inroduction_To_Algorithms_Lect14
Inroduction_To_Algorithms_Lect14Inroduction_To_Algorithms_Lect14
Inroduction_To_Algorithms_Lect14
Naor Ami
 
AAD_Lec-3-B-ShortestPaths.ppt of design and analysis of algorithm
AAD_Lec-3-B-ShortestPaths.ppt  of design and analysis of algorithmAAD_Lec-3-B-ShortestPaths.ppt  of design and analysis of algorithm
AAD_Lec-3-B-ShortestPaths.ppt of design and analysis of algorithm
SantoshDood
 
Discrete-Chapter 11 Graphs Part III
Discrete-Chapter 11 Graphs Part IIIDiscrete-Chapter 11 Graphs Part III
Discrete-Chapter 11 Graphs Part III
Wongyos Keardsri
 
Randomized algorithms all pairs shortest path
Randomized algorithms  all pairs shortest pathRandomized algorithms  all pairs shortest path
Randomized algorithms all pairs shortest path
Mohammad Akbarizadeh
 

Recently uploaded (20)

Form View Attributes in Odoo 18 - Odoo Slides
Form View Attributes in Odoo 18 - Odoo SlidesForm View Attributes in Odoo 18 - Odoo Slides
Form View Attributes in Odoo 18 - Odoo Slides
Celine George
 
Origin of Brahmi script: A breaking down of various theories
Origin of Brahmi script: A breaking down of various theoriesOrigin of Brahmi script: A breaking down of various theories
Origin of Brahmi script: A breaking down of various theories
PrachiSontakke5
 
ANTI-VIRAL DRUGS unit 3 Pharmacology 3.pptx
ANTI-VIRAL DRUGS unit 3 Pharmacology 3.pptxANTI-VIRAL DRUGS unit 3 Pharmacology 3.pptx
ANTI-VIRAL DRUGS unit 3 Pharmacology 3.pptx
Mayuri Chavan
 
Ajanta Paintings: Study as a Source of History
Ajanta Paintings: Study as a Source of HistoryAjanta Paintings: Study as a Source of History
Ajanta Paintings: Study as a Source of History
Virag Sontakke
 
CNS infections (encephalitis, meningitis & Brain abscess
CNS infections (encephalitis, meningitis & Brain abscessCNS infections (encephalitis, meningitis & Brain abscess
CNS infections (encephalitis, meningitis & Brain abscess
Mohamed Rizk Khodair
 
The role of wall art in interior designing
The role of wall art in interior designingThe role of wall art in interior designing
The role of wall art in interior designing
meghaark2110
 
How to Share Accounts Between Companies in Odoo 18
How to Share Accounts Between Companies in Odoo 18How to Share Accounts Between Companies in Odoo 18
How to Share Accounts Between Companies in Odoo 18
Celine George
 
How to Clean Your Contacts Using the Deduplication Menu in Odoo 18
How to Clean Your Contacts Using the Deduplication Menu in Odoo 18How to Clean Your Contacts Using the Deduplication Menu in Odoo 18
How to Clean Your Contacts Using the Deduplication Menu in Odoo 18
Celine George
 
*"Sensing the World: Insect Sensory Systems"*
*"Sensing the World: Insect Sensory Systems"**"Sensing the World: Insect Sensory Systems"*
*"Sensing the World: Insect Sensory Systems"*
Arshad Shaikh
 
Redesigning Education as a Cognitive Ecosystem: Practical Insights into Emerg...
Redesigning Education as a Cognitive Ecosystem: Practical Insights into Emerg...Redesigning Education as a Cognitive Ecosystem: Practical Insights into Emerg...
Redesigning Education as a Cognitive Ecosystem: Practical Insights into Emerg...
Leonel Morgado
 
Myopathies (muscle disorders) for undergraduate
Myopathies (muscle disorders) for undergraduateMyopathies (muscle disorders) for undergraduate
Myopathies (muscle disorders) for undergraduate
Mohamed Rizk Khodair
 
Search Matching Applicants in Odoo 18 - Odoo Slides
Search Matching Applicants in Odoo 18 - Odoo SlidesSearch Matching Applicants in Odoo 18 - Odoo Slides
Search Matching Applicants in Odoo 18 - Odoo Slides
Celine George
 
How to Configure Public Holidays & Mandatory Days in Odoo 18
How to Configure Public Holidays & Mandatory Days in Odoo 18How to Configure Public Holidays & Mandatory Days in Odoo 18
How to Configure Public Holidays & Mandatory Days in Odoo 18
Celine George
 
What is the Philosophy of Statistics? (and how I was drawn to it)
What is the Philosophy of Statistics? (and how I was drawn to it)What is the Philosophy of Statistics? (and how I was drawn to it)
What is the Philosophy of Statistics? (and how I was drawn to it)
jemille6
 
How to Create Kanban View in Odoo 18 - Odoo Slides
How to Create Kanban View in Odoo 18 - Odoo SlidesHow to Create Kanban View in Odoo 18 - Odoo Slides
How to Create Kanban View in Odoo 18 - Odoo Slides
Celine George
 
Botany Assignment Help Guide - Academic Excellence
Botany Assignment Help Guide - Academic ExcellenceBotany Assignment Help Guide - Academic Excellence
Botany Assignment Help Guide - Academic Excellence
online college homework help
 
E-Filing_of_Income_Tax.pptx and concept of form 26AS
E-Filing_of_Income_Tax.pptx and concept of form 26ASE-Filing_of_Income_Tax.pptx and concept of form 26AS
E-Filing_of_Income_Tax.pptx and concept of form 26AS
Abinash Palangdar
 
MEDICAL BIOLOGY MCQS BY. DR NASIR MUSTAFA
MEDICAL BIOLOGY MCQS  BY. DR NASIR MUSTAFAMEDICAL BIOLOGY MCQS  BY. DR NASIR MUSTAFA
MEDICAL BIOLOGY MCQS BY. DR NASIR MUSTAFA
Dr. Nasir Mustafa
 
Rock Art As a Source of Ancient Indian History
Rock Art As a Source of Ancient Indian HistoryRock Art As a Source of Ancient Indian History
Rock Art As a Source of Ancient Indian History
Virag Sontakke
 
Classification of mental disorder in 5th semester bsc. nursing and also used ...
Classification of mental disorder in 5th semester bsc. nursing and also used ...Classification of mental disorder in 5th semester bsc. nursing and also used ...
Classification of mental disorder in 5th semester bsc. nursing and also used ...
parmarjuli1412
 
Form View Attributes in Odoo 18 - Odoo Slides
Form View Attributes in Odoo 18 - Odoo SlidesForm View Attributes in Odoo 18 - Odoo Slides
Form View Attributes in Odoo 18 - Odoo Slides
Celine George
 
Origin of Brahmi script: A breaking down of various theories
Origin of Brahmi script: A breaking down of various theoriesOrigin of Brahmi script: A breaking down of various theories
Origin of Brahmi script: A breaking down of various theories
PrachiSontakke5
 
ANTI-VIRAL DRUGS unit 3 Pharmacology 3.pptx
ANTI-VIRAL DRUGS unit 3 Pharmacology 3.pptxANTI-VIRAL DRUGS unit 3 Pharmacology 3.pptx
ANTI-VIRAL DRUGS unit 3 Pharmacology 3.pptx
Mayuri Chavan
 
Ajanta Paintings: Study as a Source of History
Ajanta Paintings: Study as a Source of HistoryAjanta Paintings: Study as a Source of History
Ajanta Paintings: Study as a Source of History
Virag Sontakke
 
CNS infections (encephalitis, meningitis & Brain abscess
CNS infections (encephalitis, meningitis & Brain abscessCNS infections (encephalitis, meningitis & Brain abscess
CNS infections (encephalitis, meningitis & Brain abscess
Mohamed Rizk Khodair
 
The role of wall art in interior designing
The role of wall art in interior designingThe role of wall art in interior designing
The role of wall art in interior designing
meghaark2110
 
How to Share Accounts Between Companies in Odoo 18
How to Share Accounts Between Companies in Odoo 18How to Share Accounts Between Companies in Odoo 18
How to Share Accounts Between Companies in Odoo 18
Celine George
 
How to Clean Your Contacts Using the Deduplication Menu in Odoo 18
How to Clean Your Contacts Using the Deduplication Menu in Odoo 18How to Clean Your Contacts Using the Deduplication Menu in Odoo 18
How to Clean Your Contacts Using the Deduplication Menu in Odoo 18
Celine George
 
*"Sensing the World: Insect Sensory Systems"*
*"Sensing the World: Insect Sensory Systems"**"Sensing the World: Insect Sensory Systems"*
*"Sensing the World: Insect Sensory Systems"*
Arshad Shaikh
 
Redesigning Education as a Cognitive Ecosystem: Practical Insights into Emerg...
Redesigning Education as a Cognitive Ecosystem: Practical Insights into Emerg...Redesigning Education as a Cognitive Ecosystem: Practical Insights into Emerg...
Redesigning Education as a Cognitive Ecosystem: Practical Insights into Emerg...
Leonel Morgado
 
Myopathies (muscle disorders) for undergraduate
Myopathies (muscle disorders) for undergraduateMyopathies (muscle disorders) for undergraduate
Myopathies (muscle disorders) for undergraduate
Mohamed Rizk Khodair
 
Search Matching Applicants in Odoo 18 - Odoo Slides
Search Matching Applicants in Odoo 18 - Odoo SlidesSearch Matching Applicants in Odoo 18 - Odoo Slides
Search Matching Applicants in Odoo 18 - Odoo Slides
Celine George
 
How to Configure Public Holidays & Mandatory Days in Odoo 18
How to Configure Public Holidays & Mandatory Days in Odoo 18How to Configure Public Holidays & Mandatory Days in Odoo 18
How to Configure Public Holidays & Mandatory Days in Odoo 18
Celine George
 
What is the Philosophy of Statistics? (and how I was drawn to it)
What is the Philosophy of Statistics? (and how I was drawn to it)What is the Philosophy of Statistics? (and how I was drawn to it)
What is the Philosophy of Statistics? (and how I was drawn to it)
jemille6
 
How to Create Kanban View in Odoo 18 - Odoo Slides
How to Create Kanban View in Odoo 18 - Odoo SlidesHow to Create Kanban View in Odoo 18 - Odoo Slides
How to Create Kanban View in Odoo 18 - Odoo Slides
Celine George
 
Botany Assignment Help Guide - Academic Excellence
Botany Assignment Help Guide - Academic ExcellenceBotany Assignment Help Guide - Academic Excellence
Botany Assignment Help Guide - Academic Excellence
online college homework help
 
E-Filing_of_Income_Tax.pptx and concept of form 26AS
E-Filing_of_Income_Tax.pptx and concept of form 26ASE-Filing_of_Income_Tax.pptx and concept of form 26AS
E-Filing_of_Income_Tax.pptx and concept of form 26AS
Abinash Palangdar
 
MEDICAL BIOLOGY MCQS BY. DR NASIR MUSTAFA
MEDICAL BIOLOGY MCQS  BY. DR NASIR MUSTAFAMEDICAL BIOLOGY MCQS  BY. DR NASIR MUSTAFA
MEDICAL BIOLOGY MCQS BY. DR NASIR MUSTAFA
Dr. Nasir Mustafa
 
Rock Art As a Source of Ancient Indian History
Rock Art As a Source of Ancient Indian HistoryRock Art As a Source of Ancient Indian History
Rock Art As a Source of Ancient Indian History
Virag Sontakke
 
Classification of mental disorder in 5th semester bsc. nursing and also used ...
Classification of mental disorder in 5th semester bsc. nursing and also used ...Classification of mental disorder in 5th semester bsc. nursing and also used ...
Classification of mental disorder in 5th semester bsc. nursing and also used ...
parmarjuli1412
 
Ad

Lecture 10 - Graph part 2.pptx,discrete mathemactics

  • 1. Ta Quang Hung FACULTY OF INFORMATION TECHNOLOGY Discrete Mathematics Fall, 2023 Lecture 10: Graph – Part 2
  • 2. Lecture 10 The University of New South Wales Discrete Mathematics Contents Page 2 2 Dijkstras Algorithm 3 Bellman-Ford Algorithm 1 Shortest Path Algorithms 4 Summary
  • 3. Lecture 10 The University of New South Wales Discrete Mathematics Page 3 SHORTEST PATH ALGORITHMS
  • 4. Lecture 10 The University of New South Wales Discrete Mathematics Introduction • Perhaps the most intuitive graph-processing problem is one that you encounter regularly, when using a map application or a navigation system to get directions from one place to another. A graph model is immediate: vertices correspond to intersections and edges correspond to roads, with weights on the edges that model the cost, perhaps distance or travel time. The possibility of one-way roads means that we will need to consider edge- weighted digraphs. In this model, the problem is easy to formulate: Find the lowest-cost way to get from one vertex to another Page 4
  • 5. Lecture 10 The University of New South Wales Discrete Mathematics Example: Navigation System Page 5 Vertices correspond to intersections and edges correspond to roads. Weights on the edges that model the cost, perhaps distance or travel time.
  • 6. Lecture 10 The University of New South Wales Discrete Mathematics Example: Geographic Information System (GIS) Page 6 Multiple layers are combined to build GIS
  • 7. Lecture 10 The University of New South Wales Discrete Mathematics Example: Printed Circuit Board (PCB) Page 7 Autoroute Mode: Find the shortest path for connections to minimize resistances.
  • 8. Lecture 10 The University of New South Wales Discrete Mathematics Example: PCB layout for DDR3 RAM interface Page 8
  • 9. Lecture 10 The University of New South Wales Discrete Mathematics Example: Game Page 9 Base under attack: Troops must find the shortest path back home. Game map: Look like a maze.
  • 10. Lecture 10 The University of New South Wales Discrete Mathematics Example: Uber Taxi Page 10 Fare Estimate: calculate the minimum fee for the trip.
  • 11. Lecture 10 The University of New South Wales Discrete Mathematics Properties of Shortest Path • Paths are directed. A shortest path must respect the direction of its edges. • The weights are not necessarily distances. We use examples where vertices are points in the plane and weights are Euclidean distances, such as the digraph on the facing page. But the weights might represent time or cost or an entirely different variable and do not need to be proportional to a distance at all. We are emphasizing this point by using mixed-metaphor terminology where we refer to a shortest path of minimal weight or cost. • Not all vertices need be reachable. If t is not reachable from s, there is no path at all, and therefore there is no shortest path from s to t. For simplicity, our small running example is strongly connected (every vertex is reachable from every other vertex). Page 11
  • 12. Lecture 10 The University of New South Wales Discrete Mathematics Properties of Shortest Path (con't) • Negative weights introduce complications. For the moment, we assume that edge weights are positive (or zero). • Shortest paths are normally simple. Algorithms ignore zero-weight edges that form cycles, so that the shortest paths they find have no cycles. • Shortest paths are not necessarily unique. There may be multiple paths of the lowest weight from one vertex to another; we are content to find any one of them. • Parallel edges and self-loops may be present. Only the lowest-weight among a set of parallel edges will play a role, and no shortest path contains a self-loop (except possibly one of zero weight, which we ignore). In the text, we implicitly assume that parallel edges are not present for convenience in using the notation v->w to refer unambiguously to the edge from v to w. Page 12
  • 13. Lecture 10 The University of New South Wales Discrete Mathematics Page 13 DIJKSTRAS ALGORITHM
  • 14. Lecture 10 The University of New South Wales Discrete Mathematics Dijkstras Algorithm Page 14 • This algorithm can’t work with negative edge weights. • Dijkstras algorithm runs in O(|n|+|m|log|m|) where n vertices and m edges.
  • 15. Lecture 10 The University of New South Wales Discrete Mathematics Dijkstras Algorithm: Example Page 15 S = {A} Find path from A to all other vertices? Set L(A) = 0 Step 0 A B C D E G F 3 6 2 7 1 2 4 5 6 8 9 3 3 Solved node Unsolved node Solving node Denote: • w(A,B) = weighting coefficient in the path from A to B. So, from the graph, we have w(A,B) = 3. • L(Y) = Label of Y. Minimum cost of Y or the length of the shortest path from orignating vertice to Y. L(Y) is a sum of all weighting coefficient and the cost of all vertices on the shortest path. • S: A set of all vertices that the shortest path go through.
  • 16. Lecture 10 The University of New South Wales Discrete Mathematics Dijkstras Algorithm: Example Page 16 S = {A,G} Check all paths from A to adjiacent vertices: F, G, B, C L(A) + w(A,G) = 1 L(A) + w(A,B) = 3 L(A) + w(A,F) = 2 L(A) + w(A,C) = 7 Next vertice is G, add G to set S Set L(G) = 1 A B C D E G F Solved node Unsolved node 3 6 2 7 1 2 4 5 6 8 9 3 3 Solving node 0 Step 1
  • 17. Lecture 10 The University of New South Wales Discrete Mathematics Dijkstras Algorithm: Example Page 17 S = {A,G,F} Check all paths from A, G: F, B, E, D, C. L(G) + w(G,F) = 5 L(G) + w(G,E) = 10 L(G) + w(G,D) = 9 L(G) + w(G,B) = 3 Next vertice is F, set L(F)=2, add F to set S: A B C D E G F Solved node Unsolved node 3 6 2 7 1 2 4 5 6 8 9 3 3 Solving node 0 1 L(A) + w(A,B) = 3 L(A) + w(A,F) = 2 L(A) + w(A,C) = 7 Step 2
  • 18. Lecture 10 The University of New South Wales Discrete Mathematics Dijkstras Algorithm: Example Page 18 A B C D E G F Solved node Unsolved node 3 6 2 7 1 2 4 5 6 8 9 3 3 Solving node 0 1 2 S = {A,G,F,B} Check all paths from A, G, F to adjiacent vertices: B, E, C, D. L(G) + w(G,E) = 10 L(G) + w(G,D) = 9 L(G) + w(G,B) = 3 Next vertice is B, set L(B) = 3, add B to set S: L(A) + w(A,B) = 3 L(A) + w(A,C) = 7 L(F) + w(F,E) = 7 L(F) + w(F,D) = 5 Step 3
  • 19. Lecture 10 The University of New South Wales Discrete Mathematics Dijkstras Algorithm: Example Page 19 S = {A,G,F,B,D} Check all paths from A, G, F, B to adjiacent vertices: E, C, D. L(G) + w(G,E) = 10 L(G) + w(G,D) = 9 Next vertice is D, set L(D) = 5, add D to set S: L(A) + w(A,C) = 7 L(F) + w(F,E) = 7 L(F) + w(F,D) = 5 L(B) + w(B,C) = 9 A B C D E G F Solved node Unsolved node 3 6 2 7 1 2 4 5 6 8 9 3 3 Solving node 0 1 2 3 Step 4
  • 20. Lecture 10 The University of New South Wales Discrete Mathematics Dijkstras Algorithm: Example Page 20 S = {A,G,F,B,D,C} Check all paths from A, G, F, B, D to adjiacent vertices: E, C. L(G) + w(G,E) = 10 Next vertice is C, set L(C) = 7, add C to set S: L(A) + w(A,C) = 7 L(F) + w(F,E) = 7 L(B) + w(B,C) = 9 A B C D E G F Solved node Unsolved node 3 6 2 7 1 2 4 5 6 8 9 3 3 Solving node 0 1 2 3 5 L(D) + w(D,C) = 8 L(D) + w(D,E) = 11 Step 5
  • 21. Lecture 10 The University of New South Wales Discrete Mathematics Dijkstras Algorithm: Example Page 21 S = {A,G,F,B,D,C,E} Check all paths from A, G, F, B, D, C to adjiacent vertices: E. L(G) + w(G,E) = 10 Next vertice is E, set L(E) = 7, add E to set S: L(F) + w(F,E) = 7 L(D) + w(D,E) = 11 A B C D E G F Solved node Unsolved node 3 6 2 7 1 2 4 5 6 8 9 3 3 Solving node 0 1 2 3 5 7 Step 6
  • 22. Lecture 10 The University of New South Wales Discrete Mathematics Dijkstras Algorithm: Example Page 22 n A G F B D C E Shortest Path 0 0       A 1 0 (1,A) (2,A) (3,A)  (7,A)  A,G 2 0 (1,A) (2,A) (3,A) (9,G) (7,A) (10,G) A,G,F 3 0 (1,A) (2,A) (3,A) (5,F) (7,A) (7,F) A,G,F,B 4 0 (1,A) (2,A) (3,A) (5,F) (7,A) (7,F) A,G,F,B,D 5 0 (1,A) (2,A) (3,A) (5,F) (7,A) (7,F) A,G,F,B,D,C 6 0 (1,A) (2,A) (3,A) (5,F) (7,A) (7,F) A,G,F,B,D,C,E 0 (1,A) (2,A) (3,A) (5,F) (7,A) (7,F) A,G,F,B,D,C,E Denote: • (L,P): where L is label of the vertice, and P is preceding vertice in the path.
  • 23. Lecture 10 The University of New South Wales Discrete Mathematics Page 23 BELLMAN-FORD ALGORITHM
  • 24. Lecture 10 The University of New South Wales Discrete Mathematics Bellman-Ford Algorithm Page 24 • This algorithm can work with negative edge weights. • Can detect a negative cycle. • Bellman-Ford runs in O(|n|-|m|) where n vertices and m edges.
  • 25. Lecture 10 The University of New South Wales Discrete Mathematics Bellman-Ford Algorithm: Example Page 25 Find path from Z to all other vertices? U Z X Y V 6 7 8 2 5 -3 -4 7 9 Solved node Unsolved node Solving node Denote: • w(A,B) = weighting coefficient in the path from A to B. So, from the graph, we have w(A,B) = 3. -2
  • 26. Lecture 10 The University of New South Wales Discrete Mathematics Bellman-Ford Algorithm: Example Page 26 Step 0 U Z X Y V 6 7 8 2 5 -3 -4 7 9 Solved node Unsolved node Solving node -2     n Z U X V Y 0 0 ,- ,- ,- ,- 0
  • 27. Lecture 10 The University of New South Wales Discrete Mathematics Bellman-Ford Algorithm: Example Page 27 Step 1 U Z X Y V 6 7 8 2 5 -3 -4 7 9 Solved node Unsolved node Solving node -2 6,Z   7,Z n Z U X V Y 0 0 ,- ,- ,- ,- 1 0 6,Z 7,Z ,- ,- From Z, can go to U, and X. Then assign labels to U, X by adding cost of Z (in previous step – step 0) to w(Z,U) and w(Z,X). We denote 0 L1(U) = 6; P1(U) = Z L1(X) = 7; P1(X) = Z
  • 28. Lecture 10 The University of New South Wales Discrete Mathematics Bellman-Ford Algorithm: Example Page 28 Step 2 U Z X Y V 6 7 8 2 5 -3 -4 7 9 Solved node Unsolved node Solving node -2 6,Z 4,X 2,U 7,Z n Z U X V Y 0 0 ,- ,- ,- ,- 1 0 6,Z 7,Z ,- ,- 2 0 6,Z 7,Z 4,X 2,U 0 Recalculate U,X,V,Y by (L,P) in step 2: L2(V) = min{L1(U)+w(U,V),L1(X)+w(X,V)} = min{6+5,7-3} = 4, P2(V) = X L2(Y) = min{L1(U)+w(U,Y), L1(X)+w(X,Y), = min{6-4,7+9} = 2; P2(Y) = U L2(U) = min{L1(Z)+w(Z,U),L1(V)+w(V,U)} = min{0+6, } = 6; P2(U) = Z L2(X) = min{L1(U)+w(U,X), L1(Z)+w(Z,X), = min{6+8,0+7} = 7; P2(X) = Z
  • 29. Lecture 10 The University of New South Wales Discrete Mathematics Bellman-Ford Algorithm: Example Page 29 Step 3 U Z X Y V 6 7 8 2 5 -3 -4 7 9 Solved node Unsolved node Solving node -2 2,V 4,X 2,U 7,Z n Z U X V Y 0 0 ,- ,- ,- ,- 1 0 6,Z 7,Z ,- ,- 2 0 6,Z 7,Z 4,X 2,U 3 0 2,V 7,Z 4,X 2,U 0 Recalculate U,X,V,Y by (L,P) in step 3: L3(V) = min{L2(U)+w(U,V),L2(X)+w(X,V)} = min{6+5,7-3} = 4, P3(V) = 4,X L3(Y) = min{L2(U)+w(U,Y), L2(X)+w(X,Y), = min{6-4,7+9} = 2; P3(Y) = 2,U L3(U) = min{L2(Z)+w(Z,U),L2(V)+w(V,U)} = min{0+6,4-2} = 2; P3(U) = 2,V L3(X) = min{L2(U)+w(U,X), L2(Z)+w(Z,X), = min{6+8,0+7} = 7; P3(X) = 7,Z
  • 30. Lecture 10 The University of New South Wales Discrete Mathematics Bellman-Ford Algorithm: Example Page 30 Step 4 U Z X Y V 6 7 8 2 5 -3 -4 7 9 Solved node Unsolved node Solving node -2 2,V 4,X 2,U 7,Z n Z U X V Y 0 0 ,- ,- ,- ,- 1 0 6,Z 7,Z ,- ,- 2 0 6,Z 7,Z 4,X 2,U 3 0 2,V 7,Z 4,X 2,U 4 0 2,V 7,Z 4,X -2,U 0 Recalculate U,X,V,Y by (L,P) in step 3: L4(V) = min{L3(U)+w(U,V),L3(X)+w(X,V)} = min{2+5,7-3} = 4, P4(V) = X L4(Y) = min{L3(U)+w(U,Y), L3(X)+w(X,Y), = min{2-4,7+9} = -2; P4(Y) = U L4(U) = min{L3(Z)+w(Z,U),L3(V)+w(V,U)} = min{0+6,4-2} = 2; P4(U) = V L4(X) = min{L3(U)+w(U,X), L3(Z)+w(Z,X), = min{2+8,0+7} = 7; P4(X) = Z
  • 31. Lecture 10 The University of New South Wales Discrete Mathematics Summary Page 31 2 Dijkstras Algorithm 3 Bellman-Ford Algorithm 1 Shortest Path Algorithms