lecture 34: relaxation-based approach
DESCRIPTION
CSC 213 – Large Scale Programming. Lecture 34: Relaxation-Based Approach. Today’s Goals. Discuss what is meant by weighted graphs Where weights placed within Graph How to use Graph ’s weights to model problems How to solve problems once Graph is set up - PowerPoint PPT PresentationTRANSCRIPT
![Page 1: Lecture 34: Relaxation-Based Approach](https://reader036.vdocument.in/reader036/viewer/2022062501/5681649a550346895dd67598/html5/thumbnails/1.jpg)
LECTURE 34:RELAXATION-BASED APPROACH
CSC 213 – Large Scale Programming
![Page 2: Lecture 34: Relaxation-Based Approach](https://reader036.vdocument.in/reader036/viewer/2022062501/5681649a550346895dd67598/html5/thumbnails/2.jpg)
Today’s Goals
Discuss what is meant by weighted graphs Where weights placed within Graph How to use Graph’s weights to model
problems How to solve problems once Graph is set up
Learn about myth & legend of Edsgar Dijkstra Who was he? Why should we care? How is
it related? What was his largest contribution to graph
theory? How does Dijkstra’s algorithm find smallest
path?
![Page 3: Lecture 34: Relaxation-Based Approach](https://reader036.vdocument.in/reader036/viewer/2022062501/5681649a550346895dd67598/html5/thumbnails/3.jpg)
Weighted Graphs
Edge’s weight is cost of using edge Distance, cost, travel time, &c. usable as
the weight Weights below are distance in miles
ORD PVD
MIADFW
SFO
LAX
LGAHNL
849
802
13871743
1843
10991120
1233337
2555
142
![Page 4: Lecture 34: Relaxation-Based Approach](https://reader036.vdocument.in/reader036/viewer/2022062501/5681649a550346895dd67598/html5/thumbnails/4.jpg)
Cheapest Path Problem
Find path with min. weight between 2 vertices Sum of edge weights is the path weight
Consider the cheapest path from PVD to HNL None of edges is cheapest in this exampleORD PVD
MIADFW
SFO
LAX
LGAHNL
849
802
13871743
1843
10991120
1233337
2555
142
![Page 5: Lecture 34: Relaxation-Based Approach](https://reader036.vdocument.in/reader036/viewer/2022062501/5681649a550346895dd67598/html5/thumbnails/5.jpg)
Cheapest Path Problem
Subpath on shortest path is shortest path also Otherwise we would use shorter subpath
Tree made by all shortest paths from vertex Consider all shortest paths from PVDORD PVD
MIADFW
SFO
LAX
LGAHNL
849
802
13871743
1843
10991120
1233337
2555
142
![Page 6: Lecture 34: Relaxation-Based Approach](https://reader036.vdocument.in/reader036/viewer/2022062501/5681649a550346895dd67598/html5/thumbnails/6.jpg)
Dijkstra’s Algorithm
Finds cheapest paths from single vertex Normally, computes cheapest path to all
vertices Stop once vertex computed for single
target vertex Makes several fundamental
assumptions Connected graph needed when targeting
all vertices Only works if edge weights must be
nonnegative
![Page 7: Lecture 34: Relaxation-Based Approach](https://reader036.vdocument.in/reader036/viewer/2022062501/5681649a550346895dd67598/html5/thumbnails/7.jpg)
Dijkstra’s Algorithm
Grows cloud of vertices as it goes Cloud starts with source vetex Add vertex to cloud with each step
Tracks distances to each vertex not in cloud For each vertex, considers only cheapest
path Only uses 1 edge from cloud to vertex not
in cloud Each step uses vertex with smallest
distance Adds this vertex to cloud, if not done yet Checks if creates smaller path to any
vertices
![Page 8: Lecture 34: Relaxation-Based Approach](https://reader036.vdocument.in/reader036/viewer/2022062501/5681649a550346895dd67598/html5/thumbnails/8.jpg)
Edge Relaxation
Consider e from u to z
When u added to cloud Check adjacent
vertices Assume z not in
cloud Found faster path!
Update via relaxation New minimum
selected:
d(z) = 75z
s
u
d(u) = 50 10
e
( )
( ) ( )min
weightd z
d u e
![Page 9: Lecture 34: Relaxation-Based Approach](https://reader036.vdocument.in/reader036/viewer/2022062501/5681649a550346895dd67598/html5/thumbnails/9.jpg)
Edge Relaxation
Consider e from u to z
When u added to cloud Check adjacent
vertices Assume z not in
cloud Found faster path!
Update via relaxation New minimum
selected:
( )
( ) ( )min
weightd z
d u e
d(z) = 75z
s
u
d(u) = 50 10
ed(z) = 60
![Page 10: Lecture 34: Relaxation-Based Approach](https://reader036.vdocument.in/reader036/viewer/2022062501/5681649a550346895dd67598/html5/thumbnails/10.jpg)
Edge Relaxation
Consider e from u to z
When u added to cloud Check adjacent
vertices Assume z not in
cloud Found faster path!
Update via relaxation New minimum
selected:
( )
( ) ( )min
weightd z
d u e
d(z) = 75z
s
u
d(u) = 50 10
ed(z) = 60
![Page 11: Lecture 34: Relaxation-Based Approach](https://reader036.vdocument.in/reader036/viewer/2022062501/5681649a550346895dd67598/html5/thumbnails/11.jpg)
Edge Relaxation
Consider e from u to z
When u added to cloud Check adjacent
vertices Assume z not in
cloud Found faster path!
Update via relaxation New minimum
selected:
( )
( ) ( )min
weightd z
d u e
z
s
u
d(u) = 50 10
ed(z) = 60
![Page 12: Lecture 34: Relaxation-Based Approach](https://reader036.vdocument.in/reader036/viewer/2022062501/5681649a550346895dd67598/html5/thumbnails/12.jpg)
Dijkstra Example
CB
A
E
D
F
0
428
48
7 1
2 5
2
3 9
![Page 13: Lecture 34: Relaxation-Based Approach](https://reader036.vdocument.in/reader036/viewer/2022062501/5681649a550346895dd67598/html5/thumbnails/13.jpg)
Dijkstra Example
CB
A
E
D
F
0
328
5 11
48
7 1
2 5
2
3 9
![Page 14: Lecture 34: Relaxation-Based Approach](https://reader036.vdocument.in/reader036/viewer/2022062501/5681649a550346895dd67598/html5/thumbnails/14.jpg)
Dijkstra Example
CB
A
E
D
F
0
328
5 8
48
7 1
2 5
2
3 9
![Page 15: Lecture 34: Relaxation-Based Approach](https://reader036.vdocument.in/reader036/viewer/2022062501/5681649a550346895dd67598/html5/thumbnails/15.jpg)
Dijkstra Example
CB
A
E
D
F
0
327
5 8
48
7 1
2 5
2
3 9
![Page 16: Lecture 34: Relaxation-Based Approach](https://reader036.vdocument.in/reader036/viewer/2022062501/5681649a550346895dd67598/html5/thumbnails/16.jpg)
Dijkstra Example
CB
A
E
D
F
0
327
5 8
48
7 1
2 5
2
3 9
![Page 17: Lecture 34: Relaxation-Based Approach](https://reader036.vdocument.in/reader036/viewer/2022062501/5681649a550346895dd67598/html5/thumbnails/17.jpg)
Dijkstra Example
CB
A
E
D
F
0
327
5 8
48
7 1
2 5
2
3 9
![Page 18: Lecture 34: Relaxation-Based Approach](https://reader036.vdocument.in/reader036/viewer/2022062501/5681649a550346895dd67598/html5/thumbnails/18.jpg)
Why Dijkstra’s Algorithm Works
Ultimately, Dijkstra was smart Smarter than me, if that is possible
![Page 19: Lecture 34: Relaxation-Based Approach](https://reader036.vdocument.in/reader036/viewer/2022062501/5681649a550346895dd67598/html5/thumbnails/19.jpg)
Why Dijkstra’s Algorithm Works
Ultimately, Dijkstra was smart Smarter than me, if that is possible
![Page 20: Lecture 34: Relaxation-Based Approach](https://reader036.vdocument.in/reader036/viewer/2022062501/5681649a550346895dd67598/html5/thumbnails/20.jpg)
Why Dijkstra’s Algorithm Works
Ultimately, Dijkstra was smart Smarter than me, if that is possible
Example of a greedy algorithm Takes best choice at each point in time Vertices added in increasing distance Brings vertices closer at each step Stops when vertex cannot move closer
![Page 21: Lecture 34: Relaxation-Based Approach](https://reader036.vdocument.in/reader036/viewer/2022062501/5681649a550346895dd67598/html5/thumbnails/21.jpg)
Why No Negative-Weight Edges?
Assume edge has negative weight Greedily chose vertex before finding edge Cloud will include only one endpoint Negative weight changes everything,
however Vertices not added in order
Negative weight cycles? Repeat cycle to optimize CB
A
E
D
F
0
457
5 9
48
7 1
2 5
6
0 -8
![Page 22: Lecture 34: Relaxation-Based Approach](https://reader036.vdocument.in/reader036/viewer/2022062501/5681649a550346895dd67598/html5/thumbnails/22.jpg)
Why No Negative-Weight Edges?
Assume edge has negative weight Greedily chose vertex before finding edge Cloud will include only one endpoint Negative weight changes everything,
however Vertices not added in order
Negative weight cycles? Repeat cycle to optimize
C added when distance was 5,but cheapest distance is 1!
CB
A
E
D
F
0
457
5 9
48
7 1
2 5
6
0 -8
![Page 23: Lecture 34: Relaxation-Based Approach](https://reader036.vdocument.in/reader036/viewer/2022062501/5681649a550346895dd67598/html5/thumbnails/23.jpg)
Spanning Tree
Subgraph that is both spanning subgraph & tree Contains all vertices in graph spanning
subgraph Tree connected without any cycles
Graph
![Page 24: Lecture 34: Relaxation-Based Approach](https://reader036.vdocument.in/reader036/viewer/2022062501/5681649a550346895dd67598/html5/thumbnails/24.jpg)
Spanning Tree
Subgraph that is both spanning subgraph & tree Contains all vertices in graph spanning
subgraph Tree connected without any cycles
Tree
![Page 25: Lecture 34: Relaxation-Based Approach](https://reader036.vdocument.in/reader036/viewer/2022062501/5681649a550346895dd67598/html5/thumbnails/25.jpg)
Spanning Tree
Subgraph that is both spanning subgraph & tree Contains all vertices in graph spanning
subgraph Tree connected without any cycles
Spanning subgraph
![Page 26: Lecture 34: Relaxation-Based Approach](https://reader036.vdocument.in/reader036/viewer/2022062501/5681649a550346895dd67598/html5/thumbnails/26.jpg)
Spanning Tree
Subgraph that is both spanning subgraph & tree Contains all vertices in graph spanning
subgraph Tree connected without any cycles
Spanning tree
![Page 27: Lecture 34: Relaxation-Based Approach](https://reader036.vdocument.in/reader036/viewer/2022062501/5681649a550346895dd67598/html5/thumbnails/27.jpg)
Prim-Jarnik’s Algorithm
Similar to Dijkstra’s algorithm but for MST
Processing must start with some vertex s Grow MST using “cloud” of vertices
Label vertices with least Edge weight to cloud
At each step: Find and add vertex closest to cloud Update adjacent vertices to vertex just
added
![Page 28: Lecture 34: Relaxation-Based Approach](https://reader036.vdocument.in/reader036/viewer/2022062501/5681649a550346895dd67598/html5/thumbnails/28.jpg)
Prim-Jarnik’s Algorithm
Priority queue stores vertices outside of cloud You all should be reminded of Dijkstra's
algorithm Three decorations used for each Vertex Distance from cloud Edge connecting vertex to cloud Entry for Vertex in the priority queue
![Page 29: Lecture 34: Relaxation-Based Approach](https://reader036.vdocument.in/reader036/viewer/2022062501/5681649a550346895dd67598/html5/thumbnails/29.jpg)
Prim-Jarnik Example
B
D
C
A
F
E
7
4
2
8
5
7
3
9
8
07
2
8
![Page 30: Lecture 34: Relaxation-Based Approach](https://reader036.vdocument.in/reader036/viewer/2022062501/5681649a550346895dd67598/html5/thumbnails/30.jpg)
Prim-Jarnik Example
B
D
C
A
F
E
7
4
2
8
5
7
3
9
8
07
2
8
![Page 31: Lecture 34: Relaxation-Based Approach](https://reader036.vdocument.in/reader036/viewer/2022062501/5681649a550346895dd67598/html5/thumbnails/31.jpg)
Prim-Jarnik Example
B
D
C
A
F
E
7
4
2
8
5
7
3
9
8
07
2
8
7
![Page 32: Lecture 34: Relaxation-Based Approach](https://reader036.vdocument.in/reader036/viewer/2022062501/5681649a550346895dd67598/html5/thumbnails/32.jpg)
Prim-Jarnik Example
B
D
C
A
F
E
7
4
2
8
5
7
3
9
8
07
2
5
7
![Page 33: Lecture 34: Relaxation-Based Approach](https://reader036.vdocument.in/reader036/viewer/2022062501/5681649a550346895dd67598/html5/thumbnails/33.jpg)
Prim-Jarnik Example
B
D
C
A
F
E
7
4
2
8
5
7
3
9
8
07
2
5
7
![Page 34: Lecture 34: Relaxation-Based Approach](https://reader036.vdocument.in/reader036/viewer/2022062501/5681649a550346895dd67598/html5/thumbnails/34.jpg)
Prim-Jarnik Example
B
D
C
A
F
E
7
4
2
8
5
7
3
9
8
07
2
5
7
![Page 35: Lecture 34: Relaxation-Based Approach](https://reader036.vdocument.in/reader036/viewer/2022062501/5681649a550346895dd67598/html5/thumbnails/35.jpg)
Prim-Jarnik Example
B
D
C
A
F
E
7
4
2
8
5
7
3
9
8
07
2
5
7
![Page 36: Lecture 34: Relaxation-Based Approach](https://reader036.vdocument.in/reader036/viewer/2022062501/5681649a550346895dd67598/html5/thumbnails/36.jpg)
Prim-Jarnik Example
B
D
C
A
F
E
7
4
2
8
5
7
3
9
8
07
2
5
7
![Page 37: Lecture 34: Relaxation-Based Approach](https://reader036.vdocument.in/reader036/viewer/2022062501/5681649a550346895dd67598/html5/thumbnails/37.jpg)
Prim-Jarnik Example
B
D
C
A
F
E
7
4
2
8
5
7
3
9
8
07
2
54
7
![Page 38: Lecture 34: Relaxation-Based Approach](https://reader036.vdocument.in/reader036/viewer/2022062501/5681649a550346895dd67598/html5/thumbnails/38.jpg)
Prim-Jarnik Example
B
D
C
A
F
E
7
4
2
8
5
7
3
9
8
07
2
54
7
![Page 39: Lecture 34: Relaxation-Based Approach](https://reader036.vdocument.in/reader036/viewer/2022062501/5681649a550346895dd67598/html5/thumbnails/39.jpg)
Prim-Jarnik Example
B
AE
7
4
2
8
5
7
3
9
8
03
2
54
7D
CF
![Page 40: Lecture 34: Relaxation-Based Approach](https://reader036.vdocument.in/reader036/viewer/2022062501/5681649a550346895dd67598/html5/thumbnails/40.jpg)
Prim-Jarnik Example
B
AE
7
4
2
8
5
7
3
9
8
03
2
54
7D
CF
![Page 41: Lecture 34: Relaxation-Based Approach](https://reader036.vdocument.in/reader036/viewer/2022062501/5681649a550346895dd67598/html5/thumbnails/41.jpg)
Prim-Jarnik’s Analysis
Each connected vertex is: Decorated O(deg(v)) times going through
algorithm Priority queue will have added & removed
once Takes O((n + m) log n) time using
adjacency list Each operation on priority queue takes
O(log n) time Takes O(log n) time to decorate Vertex each
time
![Page 42: Lecture 34: Relaxation-Based Approach](https://reader036.vdocument.in/reader036/viewer/2022062501/5681649a550346895dd67598/html5/thumbnails/42.jpg)
For Next Lecture
Weekly assignment available on Angel Due at special time: before next
Monday’s quiz Programming assignment #3 designs
due Friday
Reading on more cheap paths for Friday Why does everything need to be
connected? Algorithms for the uptight who do not want
to relax?