Computer science 40 dijkstra-algorithm.ppt.pdf

JoshuaBicknese 12 views 23 slides Aug 16, 2024
Slide 1
Slide 1 of 23
Slide 1
1
Slide 2
2
Slide 3
3
Slide 4
4
Slide 5
5
Slide 6
6
Slide 7
7
Slide 8
8
Slide 9
9
Slide 10
10
Slide 11
11
Slide 12
12
Slide 13
13
Slide 14
14
Slide 15
15
Slide 16
16
Slide 17
17
Slide 18
18
Slide 19
19
Slide 20
20
Slide 21
21
Slide 22
22
Slide 23
23

About This Presentation

Computer science algorithm


Slide Content

Edge Relaxation




u
pred(v)

w


pred(v)


Shortest path algorithms differ in
the number of times the edges are
relaxed and the order in which they
are relaxed.

Shortests Paths in DAGs
edges according to a topological ordering of vertices.


When the graph is a directed acyclic graph (DAG), relax

An Example
Topological order: s, r, y, x, z, t
s
t
z
y
r
9



0


3
2
9
6
5
3
4
1
x

s
t
z
y
r
9
3

9
0

2
2
9
6
5
3
4
Relax edges 〈s, r〉, 〈s, x〉, 〈s,
t〉.
1
3
x
Topological order: s, r, y, x, z, t

s
t
z
y
r
9
3
7
9
0
1
1
2
2
9
6
5
3
4
Relax edges 〈r, x〉, 〈r, y〉, 〈r, z〉.
1
3
x
Topological order: s, r, y, x, z, t

s
t
z
y
r
9
3
7
9
0
1
0
2
2
9
6
5
3
4
Relax edge 〈y, z〉.
1
3
x
Topological order: s, r, y, x, z, t

s
t
z
y
r
9
3
7
9
0
7
2
2
9
6
5
3
4
Relax edge 〈x, z〉.
1
3
x
Topological order: s, r, y, x, z, t

Finish
s
t
z
y
r
9
3
7
8
0
7
2
2
9
6
5
3
4
Relax edge 〈z, t〉.
1
3
x
Topological order: s, r, y, x, z, t

Correctness
Path relaxation property: If is a shortest



The topological order guarantees that edges on every path from the
source will be relaxed in the order in which they appear on the path.
Thus, the path relaxation property is satisfied and the algorithm
works correctly.

Dijkstra’s Algorithm








Applicable when all edge weights are non-negative.
Greedy strategy (same as breadth-first search if all weights are 1):
...

How to Find the Next Closest?

Relaxation

An Example
b
d
2
7
5
4
1
4
3
4
1
5
7
0

2







a
e
f
s
c

b
d
2
7
5
4
1
4
3
4
1
5
7
0
2
5
4
2



k
s
e
f
c
a

b
d
s
2
7
5
4
1
4
3
4
1
5
7
0
2
4
4
9
2


a
c
e
f

a
b
d
s
2
7
5
4
1
4
3
4
1
5
7
0
2
4
4
8
7
2

e
f
c

a
b
d
s
2
7
5
4
1
4
3
4
1
5
7
0
2
4
4
8
7
2

c
e
f

a
f
c
b
d
s
2
7
5
4
1
4
3
4
1
5
7
0
2
4
4
8
7
14
2
e

a
b
d
s
2
7
5
4
1
4
3
4
1
5
7
0
2
4
4
8
7
13
2
f
e
c

a
fb
d
s
2
7
5
4
1
4
3
4
1
5
7
0
2
4
4
8
7
13
2
c
e

Algorithm Description

Analysis



#operations 1 |V | |E|

What We’ve Learned from 228
Java
♦ Inheritance & abstraction
♦ Interface vs abstract classes
♦ Dynamic binding
♦ Method overriding
♦ Cloning, shallow vs deep copying
♦ Generic programming
♦ Wild cards
Data structures
♣ Linked lists (singly, doubly, circular)
♣ Stacks
♣ Trees (general, binary, BSTs, splay)
♣ Sets & maps
♣ Hash tables
♣ Graphs
Algorthms & Analysis
♥ Big-O
♥ Binary search
♥ Sorting
∙ Selection sort
∙ Insertion sort
∙ Mergesort
∙ Quicksort
∙ Heap sort
♥ Euclid’s GCD algorithm
♥ Infix & postfix conversions
♥ Graham’s scan
♥ BFS & DFS
♥ Topological sort
♥ Dijkstra’s algorithm
Tags