lecture 10: shortest path advanced...

19
ADVANCED ALGORITHMS LECTURE 10: SHORTEST PATH 1

Upload: others

Post on 28-Sep-2020

12 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: LECTURE 10: SHORTEST PATH ADVANCED ALGORITHMStheory.cs.utah.edu/fall18/algorithms/slides/lecture10.pdf · LECTURE 10 SHORTEST PATH PROBLEM 5 Shortest path: given a (possibly directed)

ADVANCED ALGORITHMS LECTURE 10: SHORTEST PATH

�1

Page 2: LECTURE 10: SHORTEST PATH ADVANCED ALGORITHMStheory.cs.utah.edu/fall18/algorithms/slides/lecture10.pdf · LECTURE 10 SHORTEST PATH PROBLEM 5 Shortest path: given a (possibly directed)

LECTURE 10

ANNOUNCEMENTS

▸ Homework 2 due next Friday

▸ Contacting the TAs: [email protected]

▸ Notes for this class: http://jeffe.cs.illinois.edu/teaching/algorithms/notes/21-sssp.pdf

�2

Page 3: LECTURE 10: SHORTEST PATH ADVANCED ALGORITHMStheory.cs.utah.edu/fall18/algorithms/slides/lecture10.pdf · LECTURE 10 SHORTEST PATH PROBLEM 5 Shortest path: given a (possibly directed)

LECTURE 10

LAST CLASS

�3

▸ Local search and gradient descent

▸ Shortest path problem

if 2 approx formatching

Page 4: LECTURE 10: SHORTEST PATH ADVANCED ALGORITHMStheory.cs.utah.edu/fall18/algorithms/slides/lecture10.pdf · LECTURE 10 SHORTEST PATH PROBLEM 5 Shortest path: given a (possibly directed)

LECTURE 10

GRAPH ALGORITHMS

�4

Page 5: LECTURE 10: SHORTEST PATH ADVANCED ALGORITHMStheory.cs.utah.edu/fall18/algorithms/slides/lecture10.pdf · LECTURE 10 SHORTEST PATH PROBLEM 5 Shortest path: given a (possibly directed)

LECTURE 10

SHORTEST PATH PROBLEM

�5

Shortest path: given a (possibly directed) graph G = (V, E), and two vertices u, v, find the length of the shortest path from u to v

▸ What if the graph is not weighted? — breadth first search

▸ Negative weight edges?

all lengths 101mm

Problem is notFell ifu

grapmhtfsdes.ve

Page 6: LECTURE 10: SHORTEST PATH ADVANCED ALGORITHMStheory.cs.utah.edu/fall18/algorithms/slides/lecture10.pdf · LECTURE 10 SHORTEST PATH PROBLEM 5 Shortest path: given a (possibly directed)

LECTURE 10

BFS + DISTANCE UPDATES?

�6

▸ Maintain array dist[w] for all vertices w

▸ Initialize using BFS

▸ For each edge ij, if dist[j] < dist[i] + length(i,j), update dist[j]

▸ Repeat until the dist[] values stop changing

OU O W

ListEw

i

oj

dhe w

w

Page 7: LECTURE 10: SHORTEST PATH ADVANCED ALGORITHMStheory.cs.utah.edu/fall18/algorithms/slides/lecture10.pdf · LECTURE 10 SHORTEST PATH PROBLEM 5 Shortest path: given a (possibly directed)

LECTURE 10

OBSERVATIONS

�7

▸ At every point of the algorithm, dist[w] is an upper bound on true shortest path length d(u, w)

▸ dist[w] is monotone

Claim: when the algorithm terminates, dist[w] = d(u, w) for all w

true because dist w is thelengthof sp to w found

so far

Page 8: LECTURE 10: SHORTEST PATH ADVANCED ALGORITHMStheory.cs.utah.edu/fall18/algorithms/slides/lecture10.pdf · LECTURE 10 SHORTEST PATH PROBLEM 5 Shortest path: given a (possibly directed)

LECTURE 10

PROOF

�8

Suppose we did k iterations of the algorithm. Is there something nice to be said about dist[w] values?

Owof

itt o o

Consider all vertices w for awhich shortest path from

Ito u w has lingulatek

a ti i

Page 9: LECTURE 10: SHORTEST PATH ADVANCED ALGORITHMStheory.cs.utah.edu/fall18/algorithms/slides/lecture10.pdf · LECTURE 10 SHORTEST PATH PROBLEM 5 Shortest path: given a (possibly directed)

LECTURE 10

PROOF (CONTINUED)

�9

Prone claim by induction

k 0 is trivialSuppose it holds for k now connider htt

IrmasutEf for all vertices w s't hops in shortestpathEk

Owe found the path

Consider any w s t shortest pathfrom u has

htt hops go ee

affirmedIttihadw

THEcorrectvalue ftp.estfwI

Page 10: LECTURE 10: SHORTEST PATH ADVANCED ALGORITHMStheory.cs.utah.edu/fall18/algorithms/slides/lecture10.pdf · LECTURE 10 SHORTEST PATH PROBLEM 5 Shortest path: given a (possibly directed)

Optimum path from a to w has hops

Ek

Reach Optimal sub structure property ofshortest paths

Page 11: LECTURE 10: SHORTEST PATH ADVANCED ALGORITHMStheory.cs.utah.edu/fall18/algorithms/slides/lecture10.pdf · LECTURE 10 SHORTEST PATH PROBLEM 5 Shortest path: given a (possibly directed)

LECTURE 10

RUNNING TIME BOUND (SHIMBEL’S ALGORITHM)

�10

BellmanforTalgorithm

Previous claim aftern iterationswe havefoundthortestpaths toll w

iterations In

Time for each iteration 0cm

Overall runtime Ofm nIt also works when some edgewts are

we

Page 12: LECTURE 10: SHORTEST PATH ADVANCED ALGORITHMStheory.cs.utah.edu/fall18/algorithms/slides/lecture10.pdf · LECTURE 10 SHORTEST PATH PROBLEM 5 Shortest path: given a (possibly directed)

LECTURE 10

BETTER THAN M*N?

�11

▸ Remember, this is an algorithm that also works when there are negative edges

▸ Can we use positivity in a nicer way?

Page 13: LECTURE 10: SHORTEST PATH ADVANCED ALGORITHMStheory.cs.utah.edu/fall18/algorithms/slides/lecture10.pdf · LECTURE 10 SHORTEST PATH PROBLEM 5 Shortest path: given a (possibly directed)

▸ Can we “fix” BFS?

▸ Recall: main idea of BFS — find all vertices at certain distance from u

▸ Problem: in BFS, vertex at distance 2 is nbr of vertex at distance 1

LECTURE 10

MAIN IDEA — DIJKSTRA’S ALGORITHM

�12

1958Dijkstra

u

xm

ooo

9K

Page 14: LECTURE 10: SHORTEST PATH ADVANCED ALGORITHMStheory.cs.utah.edu/fall18/algorithms/slides/lecture10.pdf · LECTURE 10 SHORTEST PATH PROBLEM 5 Shortest path: given a (possibly directed)

LECTURE 10

EXTENDING BFS

�13W

WIKes

winIntuitiveidas

Grow a ball of a certainradiusaround u

Add one vertex at a time Iqq joformalitalgorithmi start with vertex n

Add vertex that has min distancetofu

Add vertex l st u

via u Eiti E

tou via E EE't

Page 15: LECTURE 10: SHORTEST PATH ADVANCED ALGORITHMStheory.cs.utah.edu/fall18/algorithms/slides/lecture10.pdf · LECTURE 10 SHORTEST PATH PROBLEM 5 Shortest path: given a (possibly directed)

LECTURE 10

PRIORITY QUEUE

�14

Want a data structure that

maintains 6h dist w pair

distfw is defined quicklyobtain the

w thathas minimumt.be dstme distf value

to a via the sitar quickly thedisowns

Oflog n

Read in notes Offmtn loginfm 7 nlognl

Page 16: LECTURE 10: SHORTEST PATH ADVANCED ALGORITHMStheory.cs.utah.edu/fall18/algorithms/slides/lecture10.pdf · LECTURE 10 SHORTEST PATH PROBLEM 5 Shortest path: given a (possibly directed)

LECTURE 10

CORRECTNESS

�15

Claim whenever we add a vertex to the setball

we are maintainingthe distt value is indeed

the shortest distance to n

i

Page 17: LECTURE 10: SHORTEST PATH ADVANCED ALGORITHMStheory.cs.utah.edu/fall18/algorithms/slides/lecture10.pdf · LECTURE 10 SHORTEST PATH PROBLEM 5 Shortest path: given a (possibly directed)

LECTURE 10

RUNNING TIME

�16

Page 18: LECTURE 10: SHORTEST PATH ADVANCED ALGORITHMStheory.cs.utah.edu/fall18/algorithms/slides/lecture10.pdf · LECTURE 10 SHORTEST PATH PROBLEM 5 Shortest path: given a (possibly directed)

LECTURE 10

RUNNING TIME

�17

Page 19: LECTURE 10: SHORTEST PATH ADVANCED ALGORITHMStheory.cs.utah.edu/fall18/algorithms/slides/lecture10.pdf · LECTURE 10 SHORTEST PATH PROBLEM 5 Shortest path: given a (possibly directed)

LECTURE 10

SHORTEST PATHS — SUMMARY

�18

▸ Shimbel/Bellman-Ford algorithm — can handle negative weights, but is slower

▸ Dijkstra’s algorithm — (m+n) log n

▸ Don’t know if log n is necessary!