SlideShare a Scribd company logo
1 of 15
Discussion #35
Dijkstra’s Algorithm

Discussion #35

Chapter 7, Section 5.5

1/15
Topics
• Shortest path
• Greedy algorithms
• Dijkstra’s algorithm

Discussion #35

Chapter 7, Section 5.5

2/15
Shortest Path
• Minimum length path from one
node to another (e.g. from a to
e, the shortest path is 2)
• Algorithms

b

a

c

d

e

– Breadth-First-Search, O(n2) in the
worst case
– Floyd’s, O(n3); but finds all
– BFS, starting at each node,
O(nm), which beats Floyd’s for
sparse graphs
Discussion #35

Chapter 7, Section 5.5

3/15
Shortest Path in Weighted Graphs
• Weights on edges: positive numbers
• Algorithms
– Simple BFS no longer works (e.g. the
shortest path from a to b is not the
edge that connects them)
– Floyd’s, O(n3); finds all

b
5

2
2

a
1

c

10

d

1
1

e

• Is there an algorithm that beats
Floyd’s?
– for the single best path?
– for all paths?
Discussion #35

Chapter 7, Section 5.5

4/15
Dijkstra’s Algorithm
• Find the shortest weighted path
between two given nodes.
(Easier to find the minimum
from one to all.)
• Intuitively, special BFS: choose
smallest accumulated path length
• Algorithm

b
5

2
2

a

c

10

1

d

1
1

e

1. Initialize D (distance) table.
2. Until all nodes are settled,
2a. find smallest distance & settle node.
2b. adjust distances in D for unsettled nodes.
Discussion #35

Chapter 7, Section 5.5

5/15
Important Aside: Greedy Algorithms
• A greedy algorithm always takes the best
immediate or local solution while finding an
answer.
• Greedy algorithms find optimal solutions for some
optimization problems, but may find (far) lessthan-optimal solutions for other optimization
problems.
– Dijkstra’s algorithm is greedy.
– There is no greedy algorithm for the traveling salesman
problem (find the shortest path to visit all nodes).

• When greedy algorithms work, they are usually
best.
Discussion #35

Chapter 7, Section 5.5

6/15
b

Trace of Dijkstra’s Algorithm

5

1. Initialize D (start=a)
2. Until all nodes are settled:
-find smallest distance & settle node.
-adjust distances in D for unsettled nodes.

iteration

adjust w/d
adjust w/c
adjust w/e

0
1
2
3
4

b
5
5
54
4
4

c
2
2
2

2

a

settled

d
1
1

e
∞
∞2
2
2

c

10

1

distance

a
0

2

d

1
1

e

a
a,d
a,d,c
a,d,c,e
a,d,c,e,b
all nodes settled

Circled numbers: shortest path lengths from a.
Discussion #35

Chapter 7, Section 5.5

7/15
b

Trace with Different Weights
1. Initialize D (start=a)
2. Until all nodes are settled:
-find smallest distance & settle node.
-adjust distances in D for unsettled nodes.

iteration

adjust w/d
adjust w/e
adjust w/c

0
1
2
3
4

b
6
6
6
65
5

c
4
4
43
3

2
4

a

settled

d
1
1

e
∞
∞2
2

c

10

1

distance

a
0

6

d

1
1

e

a
a,d
a,d,e
a,d,e,c
a,d,e,c,b
all nodes settled

Circled numbers: shortest path lengths from a.
Discussion #35

Chapter 7, Section 5.5

8/15
Proof that Dijkstra’s Algorithm Works
• Loop Invariant: For each settled node x, the
minimum distance from the start node s to x is
D(x).
• Additional observations (which help us in the
proof) are also loop invariants.
– (a) The shortest path from s to any settled node v
consists only of settled nodes.
– (b) The shortest path from s to any unsettled node
y is at least the largest settled difference so far.

Discussion #35

Chapter 7, Section 5.5

9/15
Proof by Induction
(on the number of iterations)

• Basis: 0 iterations: x = s, D(s) = 0 (which is the
minimum since all weights are positive)
– For (a): initially s = v, the only settled node
– For (b): initially all nodes except s are unsettled, and
the shortest path to each is at least 0, indeed > 0.

• Induction: Assume that the main loop invariant
and the loop invariants (a) and (b) hold for k
iterations and show that they hold for k+1
iterations.
Discussion #35

Chapter 7, Section 5.5

10/15
Proof of Induction Part (Sketch)
k settled

Assume Dijkstra’s
algorithm selects x
to be settled.

s...v x

k+1 settled

s...v x

Assume that the main loop invariant does not hold for the k+1st iteration, then there
is a shorter path from s to x. By the induction hypotheses, since Dijkstra’s algorithm

s .. . . v  x
..w

y

Discussion #35

chooses x, the path from s to x is the shortest using
settled nodes, and the path from s to y is no shorter than
s to x. Since all weights are positive, the path from y to
x is at least 1, so that the assumed shorter path cannot
exist. Further (a) and (b) continue to hold: (a) After
settling x, the path from s to x consists of only settled
nodes. (b) The adjustments are only for paths from x to
any unsettled nodes. Since the distance from s to any
unsettled node y is at least D(x), and D(x) is the largest
settled distance so far.
Chapter 7, Section 5.5

11/15
Big-Oh for Dijkstra’s Algorithm
Adjacency List:

b
5

d

1

(c,2)

b

(a,5)

(c,2)

c

(a,2)

1

e

(b,5)

2
1

a

5

c

10

1

√ 0

2
2

a

D(x)

d

n
+n+
…
(a,1) + k1 (c,10) k2 +(e,1)+ n + k(n-1)

∞

e

(d,1)

(d,1)

Unroll the loop:
(b,2)
(d,10)

(e,1)

(c,1)

1. Initialize D (start=a)
2. Until all nodes are settled:
2a. find smallest distance & settle node. = (n−1)n = O(n2)
2b. adjust distances in D for unsettled nodes.

1.

= 2(m − # of edges in the
O(n)  visit at most all edges for a node  can’t=be more than
initialization) O(m)

{

2
O(n2.
)

dominates

n
O(n)  each node needs to be settled
2a.
O(n)  look through D(x) list to find smallest
2b.
O(k)  for chosen node, go through list of length k

Discussion #35

Over all iterations, the k’s add up to 2(m − # of edges in the initialization).
Chapter 7, Section 5.5

12/15
Big-Oh for Dijkstra’s Algorithm
Adjacency List:

b
5

d

1

(c,2)

b

(a,5)

(c,2)

c

(a,2)

(b,2)

(d,10)

1

d

(a,1)

(c,10)

(e,1)

∞

e

(b,5)

2
1

a

5

c

10

1

√ 0

2
2

a

D(x)

e

(d,1)

(c,1)

(d,1)
(e,1)

1. Initialize D (start=a)
2. Until all nodes are settled:
2a. find smallest distance & settle node.
2b. adjust distances in D for unsettled nodes.

Clever using POTs:
1.
2.
O(mlogn)

O(nlogn)  POT (Partially Ordered Tree) construction dominates
O(n)  each node needs to be settled
2a.
O(logn)  swap and bubble down
2b.
O(klogn)  for chosen node, go through list of length k

{

dominates
Discussion #35

Over all iterations, the k’s add up to 2(m − # of edges in the initialization).
Chapter 7, Section 5.5

13/15
POT Construction/Manipulation
b
5

POT construction (start = a)
2

2

a

c

10

1

d

(b,5) ⇒

(b,5)
(c,2)

⇒

(c,2)
(b,5)

⇒

(d,1)

(d,1)

⇒
⇒
(b,5) (d,1) (b,5) (c,2)
(b,5) (c,2)

1
1

(c,2)

(e,∞)
e

Find smallest  always on top; then swap and bubble down
no
swap
bubble down
adjust
bubbling
(d,1)
(e,∞)
(c,2)
(c,2)
(c,2)
⇒
⇒
⇒
⇒ needed ⇒
⇒
(b,5) (c,2)
(b,5) (e,∞) (b,5) (e,2)
(b,5) (c,2)
(b,5) (e,2)
(e,∞)

(d,1)

Adjust distances  POT doubly linked to adjacency list nodes  bubble up and
down, as needed, when going through list of chosen node to adjust values
Discussion #35

Chapter 7, Section 5.5

14/15

…
Dijkstra’s Algorithm vs. Floyd’s
• Dijkstra’s algorithm is O(mlogn) = O(n2logn) in the
worst case.
• Floyd’s algorithm is O(n3), but computes all shortest
paths.
• Dijkstra’s algorithm can compute all shortest paths
by starting it n times, once for each node. Thus, to
compute all paths, Dijkstra’s algorithm is O(nmlogn)
= O(n3logn) in the worst case.
• Which is better depends on the application.
– Dijkstra’s algorithm is better if only a path from a single
node to another or to all others is needed.
– For all paths, Dijkstra’s algorithm is better for sparse
graphs, and Floyd’s algorithm is better for dense graphs.
Discussion #35

Chapter 7, Section 5.5

15/15

More Related Content

What's hot

Randomized algorithms all pairs shortest path
Randomized algorithms  all pairs shortest pathRandomized algorithms  all pairs shortest path
Randomized algorithms all pairs shortest pathMohammad Akbarizadeh
 
Prim's Algorithm on minimum spanning tree
Prim's Algorithm on minimum spanning treePrim's Algorithm on minimum spanning tree
Prim's Algorithm on minimum spanning treeoneous
 
Deep Recurrent Neural Networks with Layer-wise Multi-head Attentions for Punc...
Deep Recurrent Neural Networks with Layer-wise Multi-head Attentions for Punc...Deep Recurrent Neural Networks with Layer-wise Multi-head Attentions for Punc...
Deep Recurrent Neural Networks with Layer-wise Multi-head Attentions for Punc...Seokhwan Kim
 
International Journal of Mathematics and Statistics Invention (IJMSI)
International Journal of Mathematics and Statistics Invention (IJMSI) International Journal of Mathematics and Statistics Invention (IJMSI)
International Journal of Mathematics and Statistics Invention (IJMSI) inventionjournals
 
Goldberg-Coxeter construction for 3- or 4-valent plane maps
Goldberg-Coxeter construction for 3- or 4-valent plane mapsGoldberg-Coxeter construction for 3- or 4-valent plane maps
Goldberg-Coxeter construction for 3- or 4-valent plane mapsMathieu Dutour Sikiric
 
Inversion Theorem for Generalized Fractional Hilbert Transform
Inversion Theorem for Generalized Fractional Hilbert TransformInversion Theorem for Generalized Fractional Hilbert Transform
Inversion Theorem for Generalized Fractional Hilbert Transforminventionjournals
 
Graph Traversal Algorithms - Depth First Search Traversal
Graph Traversal Algorithms - Depth First Search TraversalGraph Traversal Algorithms - Depth First Search Traversal
Graph Traversal Algorithms - Depth First Search TraversalAmrinder Arora
 
A Note on Latent LSTM Allocation
A Note on Latent LSTM AllocationA Note on Latent LSTM Allocation
A Note on Latent LSTM AllocationTomonari Masada
 
Networks dijkstra's algorithm- pgsr
Networks  dijkstra's algorithm- pgsrNetworks  dijkstra's algorithm- pgsr
Networks dijkstra's algorithm- pgsrLinawati Adiman
 
Directed Acyclic Graph
Directed Acyclic Graph Directed Acyclic Graph
Directed Acyclic Graph AJAL A J
 

What's hot (19)

20 Single Source Shorthest Path
20 Single Source Shorthest Path20 Single Source Shorthest Path
20 Single Source Shorthest Path
 
Randomized algorithms all pairs shortest path
Randomized algorithms  all pairs shortest pathRandomized algorithms  all pairs shortest path
Randomized algorithms all pairs shortest path
 
Prim's Algorithm on minimum spanning tree
Prim's Algorithm on minimum spanning treePrim's Algorithm on minimum spanning tree
Prim's Algorithm on minimum spanning tree
 
Dsp
DspDsp
Dsp
 
Fdtd ppt for mine
Fdtd ppt   for mineFdtd ppt   for mine
Fdtd ppt for mine
 
Admissible Labelings
Admissible LabelingsAdmissible Labelings
Admissible Labelings
 
Deep Recurrent Neural Networks with Layer-wise Multi-head Attentions for Punc...
Deep Recurrent Neural Networks with Layer-wise Multi-head Attentions for Punc...Deep Recurrent Neural Networks with Layer-wise Multi-head Attentions for Punc...
Deep Recurrent Neural Networks with Layer-wise Multi-head Attentions for Punc...
 
International Journal of Mathematics and Statistics Invention (IJMSI)
International Journal of Mathematics and Statistics Invention (IJMSI) International Journal of Mathematics and Statistics Invention (IJMSI)
International Journal of Mathematics and Statistics Invention (IJMSI)
 
Biconnectivity
BiconnectivityBiconnectivity
Biconnectivity
 
Goldberg-Coxeter construction for 3- or 4-valent plane maps
Goldberg-Coxeter construction for 3- or 4-valent plane mapsGoldberg-Coxeter construction for 3- or 4-valent plane maps
Goldberg-Coxeter construction for 3- or 4-valent plane maps
 
Inversion Theorem for Generalized Fractional Hilbert Transform
Inversion Theorem for Generalized Fractional Hilbert TransformInversion Theorem for Generalized Fractional Hilbert Transform
Inversion Theorem for Generalized Fractional Hilbert Transform
 
pairs
pairspairs
pairs
 
Suffix arrays
Suffix arraysSuffix arrays
Suffix arrays
 
Graph Traversal Algorithms - Depth First Search Traversal
Graph Traversal Algorithms - Depth First Search TraversalGraph Traversal Algorithms - Depth First Search Traversal
Graph Traversal Algorithms - Depth First Search Traversal
 
A Note on Latent LSTM Allocation
A Note on Latent LSTM AllocationA Note on Latent LSTM Allocation
A Note on Latent LSTM Allocation
 
Networks dijkstra's algorithm- pgsr
Networks  dijkstra's algorithm- pgsrNetworks  dijkstra's algorithm- pgsr
Networks dijkstra's algorithm- pgsr
 
19 Minimum Spanning Trees
19 Minimum Spanning Trees19 Minimum Spanning Trees
19 Minimum Spanning Trees
 
Directed Acyclic Graph
Directed Acyclic Graph Directed Acyclic Graph
Directed Acyclic Graph
 
5th Semester Electronic and Communication Engineering (2013-June) Question Pa...
5th Semester Electronic and Communication Engineering (2013-June) Question Pa...5th Semester Electronic and Communication Engineering (2013-June) Question Pa...
5th Semester Electronic and Communication Engineering (2013-June) Question Pa...
 

Viewers also liked

2011Online Adjunct Faculty Training
2011Online Adjunct Faculty Training2011Online Adjunct Faculty Training
2011Online Adjunct Faculty TrainingWCET
 
2011Students Instructors and Administrators Speak Out
2011Students Instructors and Administrators Speak Out2011Students Instructors and Administrators Speak Out
2011Students Instructors and Administrators Speak OutWCET
 
Collaborating for student_success
Collaborating for student_successCollaborating for student_success
Collaborating for student_successWCET
 
2010 Developing high quality online doctoral programs2
2010 Developing high quality online doctoral programs22010 Developing high quality online doctoral programs2
2010 Developing high quality online doctoral programs2WCET
 
Learn BEM: CSS Naming Convention
Learn BEM: CSS Naming ConventionLearn BEM: CSS Naming Convention
Learn BEM: CSS Naming ConventionIn a Rocket
 
SEO: Getting Personal
SEO: Getting PersonalSEO: Getting Personal
SEO: Getting PersonalKirsty Hulse
 

Viewers also liked (6)

2011Online Adjunct Faculty Training
2011Online Adjunct Faculty Training2011Online Adjunct Faculty Training
2011Online Adjunct Faculty Training
 
2011Students Instructors and Administrators Speak Out
2011Students Instructors and Administrators Speak Out2011Students Instructors and Administrators Speak Out
2011Students Instructors and Administrators Speak Out
 
Collaborating for student_success
Collaborating for student_successCollaborating for student_success
Collaborating for student_success
 
2010 Developing high quality online doctoral programs2
2010 Developing high quality online doctoral programs22010 Developing high quality online doctoral programs2
2010 Developing high quality online doctoral programs2
 
Learn BEM: CSS Naming Convention
Learn BEM: CSS Naming ConventionLearn BEM: CSS Naming Convention
Learn BEM: CSS Naming Convention
 
SEO: Getting Personal
SEO: Getting PersonalSEO: Getting Personal
SEO: Getting Personal
 

Similar to 35 dijkstras alg

Shortest Path Problem.docx
Shortest Path Problem.docxShortest Path Problem.docx
Shortest Path Problem.docxSeethaDinesh
 
APznzaZLM_MVouyxM4cxHPJR5BC-TAxTWqhQJ2EywQQuXStxJTDoGkHdsKEQGd4Vo7BS3Q1npCOMV...
APznzaZLM_MVouyxM4cxHPJR5BC-TAxTWqhQJ2EywQQuXStxJTDoGkHdsKEQGd4Vo7BS3Q1npCOMV...APznzaZLM_MVouyxM4cxHPJR5BC-TAxTWqhQJ2EywQQuXStxJTDoGkHdsKEQGd4Vo7BS3Q1npCOMV...
APznzaZLM_MVouyxM4cxHPJR5BC-TAxTWqhQJ2EywQQuXStxJTDoGkHdsKEQGd4Vo7BS3Q1npCOMV...KUSHDHIRRA2111026030
 
Chap10 slides
Chap10 slidesChap10 slides
Chap10 slidesHJ DS
 
Prim Algorithm and kruskal algorithm
Prim Algorithm and kruskal algorithmPrim Algorithm and kruskal algorithm
Prim Algorithm and kruskal algorithmAcad
 
19 prim,kruskal alg. in data structure
19 prim,kruskal alg. in data structure19 prim,kruskal alg. in data structure
19 prim,kruskal alg. in data structureEMEY GUJJAR
 
Lecture 16 - Dijkstra's Algorithm.pdf
Lecture 16 - Dijkstra's Algorithm.pdfLecture 16 - Dijkstra's Algorithm.pdf
Lecture 16 - Dijkstra's Algorithm.pdfiftakhar8
 
01-05-2023, SOL_DU_MBAFT_6202_Dijkstra’s Algorithm Dated 1st May 23.pdf
01-05-2023, SOL_DU_MBAFT_6202_Dijkstra’s Algorithm Dated 1st May 23.pdf01-05-2023, SOL_DU_MBAFT_6202_Dijkstra’s Algorithm Dated 1st May 23.pdf
01-05-2023, SOL_DU_MBAFT_6202_Dijkstra’s Algorithm Dated 1st May 23.pdfDKTaxation
 
04 greedyalgorithmsii 2x2
04 greedyalgorithmsii 2x204 greedyalgorithmsii 2x2
04 greedyalgorithmsii 2x2MuradAmn
 
Algorithm chapter 9
Algorithm chapter 9Algorithm chapter 9
Algorithm chapter 9chidabdu
 
Dijkstra's algorithm presentation
Dijkstra's algorithm presentationDijkstra's algorithm presentation
Dijkstra's algorithm presentationSubid Biswas
 
Lecture warshall floyd
Lecture warshall floydLecture warshall floyd
Lecture warshall floydDivya Ks
 
2.4 mst prim &kruskal demo
2.4 mst  prim &kruskal demo2.4 mst  prim &kruskal demo
2.4 mst prim &kruskal demoKrish_ver2
 

Similar to 35 dijkstras alg (20)

Shortest Path Problem.docx
Shortest Path Problem.docxShortest Path Problem.docx
Shortest Path Problem.docx
 
Shortest path algorithm
Shortest  path algorithmShortest  path algorithm
Shortest path algorithm
 
Dijesktra 1.ppt
Dijesktra 1.pptDijesktra 1.ppt
Dijesktra 1.ppt
 
APznzaZLM_MVouyxM4cxHPJR5BC-TAxTWqhQJ2EywQQuXStxJTDoGkHdsKEQGd4Vo7BS3Q1npCOMV...
APznzaZLM_MVouyxM4cxHPJR5BC-TAxTWqhQJ2EywQQuXStxJTDoGkHdsKEQGd4Vo7BS3Q1npCOMV...APznzaZLM_MVouyxM4cxHPJR5BC-TAxTWqhQJ2EywQQuXStxJTDoGkHdsKEQGd4Vo7BS3Q1npCOMV...
APznzaZLM_MVouyxM4cxHPJR5BC-TAxTWqhQJ2EywQQuXStxJTDoGkHdsKEQGd4Vo7BS3Q1npCOMV...
 
Chap10 slides
Chap10 slidesChap10 slides
Chap10 slides
 
Prim Algorithm and kruskal algorithm
Prim Algorithm and kruskal algorithmPrim Algorithm and kruskal algorithm
Prim Algorithm and kruskal algorithm
 
Dijkstra.ppt
Dijkstra.pptDijkstra.ppt
Dijkstra.ppt
 
19 prim,kruskal alg. in data structure
19 prim,kruskal alg. in data structure19 prim,kruskal alg. in data structure
19 prim,kruskal alg. in data structure
 
Spsp fw
Spsp fwSpsp fw
Spsp fw
 
Lecture 16 - Dijkstra's Algorithm.pdf
Lecture 16 - Dijkstra's Algorithm.pdfLecture 16 - Dijkstra's Algorithm.pdf
Lecture 16 - Dijkstra's Algorithm.pdf
 
19 primkruskal
19 primkruskal19 primkruskal
19 primkruskal
 
DIJKSTRA_123.pptx
DIJKSTRA_123.pptxDIJKSTRA_123.pptx
DIJKSTRA_123.pptx
 
Optimisation random graph presentation
Optimisation random graph presentationOptimisation random graph presentation
Optimisation random graph presentation
 
01-05-2023, SOL_DU_MBAFT_6202_Dijkstra’s Algorithm Dated 1st May 23.pdf
01-05-2023, SOL_DU_MBAFT_6202_Dijkstra’s Algorithm Dated 1st May 23.pdf01-05-2023, SOL_DU_MBAFT_6202_Dijkstra’s Algorithm Dated 1st May 23.pdf
01-05-2023, SOL_DU_MBAFT_6202_Dijkstra’s Algorithm Dated 1st May 23.pdf
 
04 greedyalgorithmsii 2x2
04 greedyalgorithmsii 2x204 greedyalgorithmsii 2x2
04 greedyalgorithmsii 2x2
 
Algorithm chapter 9
Algorithm chapter 9Algorithm chapter 9
Algorithm chapter 9
 
Dijkstra's algorithm presentation
Dijkstra's algorithm presentationDijkstra's algorithm presentation
Dijkstra's algorithm presentation
 
Lecture warshall floyd
Lecture warshall floydLecture warshall floyd
Lecture warshall floyd
 
DAA_Presentation - Copy.pptx
DAA_Presentation - Copy.pptxDAA_Presentation - Copy.pptx
DAA_Presentation - Copy.pptx
 
2.4 mst prim &kruskal demo
2.4 mst  prim &kruskal demo2.4 mst  prim &kruskal demo
2.4 mst prim &kruskal demo
 

Recently uploaded

Slack Application Development 101 Slides
Slack Application Development 101 SlidesSlack Application Development 101 Slides
Slack Application Development 101 Slidespraypatel2
 
Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024The Digital Insurer
 
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUnderstanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUK Journal
 
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
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slidevu2urc
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfsudhanshuwaghmare1
 
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
 
CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Servicegiselly40
 
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...Neo4j
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Drew Madelung
 
Real Time Object Detection Using Open CV
Real Time Object Detection Using Open CVReal Time Object Detection Using Open CV
Real Time Object Detection Using Open CVKhem
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonAnna Loughnan Colquhoun
 
Breaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountBreaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountPuma Security, LLC
 
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
 
[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
 
Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)wesley chun
 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsMaria Levchenko
 
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
 
What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?Antenna Manufacturer Coco
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsJoaquim Jorge
 

Recently uploaded (20)

Slack Application Development 101 Slides
Slack Application Development 101 SlidesSlack Application Development 101 Slides
Slack Application Development 101 Slides
 
Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024
 
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUnderstanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
 
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
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slide
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdf
 
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
 
CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Service
 
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
 
Real Time Object Detection Using Open CV
Real Time Object Detection Using Open CVReal Time Object Detection Using Open CV
Real Time Object Detection Using Open CV
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt Robison
 
Breaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountBreaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path Mount
 
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
 
[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
 
Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)
 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed texts
 
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
 
What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and Myths
 

35 dijkstras alg

  • 1. Discussion #35 Dijkstra’s Algorithm Discussion #35 Chapter 7, Section 5.5 1/15
  • 2. Topics • Shortest path • Greedy algorithms • Dijkstra’s algorithm Discussion #35 Chapter 7, Section 5.5 2/15
  • 3. Shortest Path • Minimum length path from one node to another (e.g. from a to e, the shortest path is 2) • Algorithms b a c d e – Breadth-First-Search, O(n2) in the worst case – Floyd’s, O(n3); but finds all – BFS, starting at each node, O(nm), which beats Floyd’s for sparse graphs Discussion #35 Chapter 7, Section 5.5 3/15
  • 4. Shortest Path in Weighted Graphs • Weights on edges: positive numbers • Algorithms – Simple BFS no longer works (e.g. the shortest path from a to b is not the edge that connects them) – Floyd’s, O(n3); finds all b 5 2 2 a 1 c 10 d 1 1 e • Is there an algorithm that beats Floyd’s? – for the single best path? – for all paths? Discussion #35 Chapter 7, Section 5.5 4/15
  • 5. Dijkstra’s Algorithm • Find the shortest weighted path between two given nodes. (Easier to find the minimum from one to all.) • Intuitively, special BFS: choose smallest accumulated path length • Algorithm b 5 2 2 a c 10 1 d 1 1 e 1. Initialize D (distance) table. 2. Until all nodes are settled, 2a. find smallest distance & settle node. 2b. adjust distances in D for unsettled nodes. Discussion #35 Chapter 7, Section 5.5 5/15
  • 6. Important Aside: Greedy Algorithms • A greedy algorithm always takes the best immediate or local solution while finding an answer. • Greedy algorithms find optimal solutions for some optimization problems, but may find (far) lessthan-optimal solutions for other optimization problems. – Dijkstra’s algorithm is greedy. – There is no greedy algorithm for the traveling salesman problem (find the shortest path to visit all nodes). • When greedy algorithms work, they are usually best. Discussion #35 Chapter 7, Section 5.5 6/15
  • 7. b Trace of Dijkstra’s Algorithm 5 1. Initialize D (start=a) 2. Until all nodes are settled: -find smallest distance & settle node. -adjust distances in D for unsettled nodes. iteration adjust w/d adjust w/c adjust w/e 0 1 2 3 4 b 5 5 54 4 4 c 2 2 2 2 a settled d 1 1 e ∞ ∞2 2 2 c 10 1 distance a 0 2 d 1 1 e a a,d a,d,c a,d,c,e a,d,c,e,b all nodes settled Circled numbers: shortest path lengths from a. Discussion #35 Chapter 7, Section 5.5 7/15
  • 8. b Trace with Different Weights 1. Initialize D (start=a) 2. Until all nodes are settled: -find smallest distance & settle node. -adjust distances in D for unsettled nodes. iteration adjust w/d adjust w/e adjust w/c 0 1 2 3 4 b 6 6 6 65 5 c 4 4 43 3 2 4 a settled d 1 1 e ∞ ∞2 2 c 10 1 distance a 0 6 d 1 1 e a a,d a,d,e a,d,e,c a,d,e,c,b all nodes settled Circled numbers: shortest path lengths from a. Discussion #35 Chapter 7, Section 5.5 8/15
  • 9. Proof that Dijkstra’s Algorithm Works • Loop Invariant: For each settled node x, the minimum distance from the start node s to x is D(x). • Additional observations (which help us in the proof) are also loop invariants. – (a) The shortest path from s to any settled node v consists only of settled nodes. – (b) The shortest path from s to any unsettled node y is at least the largest settled difference so far. Discussion #35 Chapter 7, Section 5.5 9/15
  • 10. Proof by Induction (on the number of iterations) • Basis: 0 iterations: x = s, D(s) = 0 (which is the minimum since all weights are positive) – For (a): initially s = v, the only settled node – For (b): initially all nodes except s are unsettled, and the shortest path to each is at least 0, indeed > 0. • Induction: Assume that the main loop invariant and the loop invariants (a) and (b) hold for k iterations and show that they hold for k+1 iterations. Discussion #35 Chapter 7, Section 5.5 10/15
  • 11. Proof of Induction Part (Sketch) k settled Assume Dijkstra’s algorithm selects x to be settled. s...v x k+1 settled s...v x Assume that the main loop invariant does not hold for the k+1st iteration, then there is a shorter path from s to x. By the induction hypotheses, since Dijkstra’s algorithm s .. . . v  x ..w  y Discussion #35 chooses x, the path from s to x is the shortest using settled nodes, and the path from s to y is no shorter than s to x. Since all weights are positive, the path from y to x is at least 1, so that the assumed shorter path cannot exist. Further (a) and (b) continue to hold: (a) After settling x, the path from s to x consists of only settled nodes. (b) The adjustments are only for paths from x to any unsettled nodes. Since the distance from s to any unsettled node y is at least D(x), and D(x) is the largest settled distance so far. Chapter 7, Section 5.5 11/15
  • 12. Big-Oh for Dijkstra’s Algorithm Adjacency List: b 5 d 1 (c,2) b (a,5) (c,2) c (a,2) 1 e (b,5) 2 1 a 5 c 10 1 √ 0 2 2 a D(x) d n +n+ … (a,1) + k1 (c,10) k2 +(e,1)+ n + k(n-1) ∞ e (d,1) (d,1) Unroll the loop: (b,2) (d,10) (e,1) (c,1) 1. Initialize D (start=a) 2. Until all nodes are settled: 2a. find smallest distance & settle node. = (n−1)n = O(n2) 2b. adjust distances in D for unsettled nodes. 1. = 2(m − # of edges in the O(n)  visit at most all edges for a node  can’t=be more than initialization) O(m) { 2 O(n2. ) dominates n O(n)  each node needs to be settled 2a. O(n)  look through D(x) list to find smallest 2b. O(k)  for chosen node, go through list of length k Discussion #35 Over all iterations, the k’s add up to 2(m − # of edges in the initialization). Chapter 7, Section 5.5 12/15
  • 13. Big-Oh for Dijkstra’s Algorithm Adjacency List: b 5 d 1 (c,2) b (a,5) (c,2) c (a,2) (b,2) (d,10) 1 d (a,1) (c,10) (e,1) ∞ e (b,5) 2 1 a 5 c 10 1 √ 0 2 2 a D(x) e (d,1) (c,1) (d,1) (e,1) 1. Initialize D (start=a) 2. Until all nodes are settled: 2a. find smallest distance & settle node. 2b. adjust distances in D for unsettled nodes. Clever using POTs: 1. 2. O(mlogn) O(nlogn)  POT (Partially Ordered Tree) construction dominates O(n)  each node needs to be settled 2a. O(logn)  swap and bubble down 2b. O(klogn)  for chosen node, go through list of length k { dominates Discussion #35 Over all iterations, the k’s add up to 2(m − # of edges in the initialization). Chapter 7, Section 5.5 13/15
  • 14. POT Construction/Manipulation b 5 POT construction (start = a) 2 2 a c 10 1 d (b,5) ⇒ (b,5) (c,2) ⇒ (c,2) (b,5) ⇒ (d,1) (d,1) ⇒ ⇒ (b,5) (d,1) (b,5) (c,2) (b,5) (c,2) 1 1 (c,2) (e,∞) e Find smallest  always on top; then swap and bubble down no swap bubble down adjust bubbling (d,1) (e,∞) (c,2) (c,2) (c,2) ⇒ ⇒ ⇒ ⇒ needed ⇒ ⇒ (b,5) (c,2) (b,5) (e,∞) (b,5) (e,2) (b,5) (c,2) (b,5) (e,2) (e,∞) (d,1) Adjust distances  POT doubly linked to adjacency list nodes  bubble up and down, as needed, when going through list of chosen node to adjust values Discussion #35 Chapter 7, Section 5.5 14/15 …
  • 15. Dijkstra’s Algorithm vs. Floyd’s • Dijkstra’s algorithm is O(mlogn) = O(n2logn) in the worst case. • Floyd’s algorithm is O(n3), but computes all shortest paths. • Dijkstra’s algorithm can compute all shortest paths by starting it n times, once for each node. Thus, to compute all paths, Dijkstra’s algorithm is O(nmlogn) = O(n3logn) in the worst case. • Which is better depends on the application. – Dijkstra’s algorithm is better if only a path from a single node to another or to all others is needed. – For all paths, Dijkstra’s algorithm is better for sparse graphs, and Floyd’s algorithm is better for dense graphs. Discussion #35 Chapter 7, Section 5.5 15/15

Editor's Notes

  1. Distances from a if two the same, choose the first one All circled nodes are shortest distances from node a to node x Settled nodes are the shortest distances Can I derive a shortest path?
  2. Distances from a if two the same, choose the first one All circled nodes are shortest distances from node a to node x Settled nodes are the shortest distances Can I derive a shortest path?