CSE 101 Algorithm Design and Analysis Sanjoy Dasgupta Russell - - PowerPoint PPT Presentation

β–Ά
cse 101
SMART_READER_LITE
LIVE PREVIEW

CSE 101 Algorithm Design and Analysis Sanjoy Dasgupta Russell - - PowerPoint PPT Presentation

CSE 101 Algorithm Design and Analysis Sanjoy Dasgupta Russell Impagliazzo Ragesh Jaiswal (Thanks Miles Jones) Lecture 29: Dynamic Programming (Shortest Paths) SHORTEST PATHS Given a graph with edge weights, find paths that minimize total


slide-1
SLIDE 1

Algorithm Design and Analysis

Sanjoy Dasgupta Russell Impagliazzo Ragesh Jaiswal (Thanks Miles Jones) Lecture 29: Dynamic Programming (Shortest Paths)

CSE 101

slide-2
SLIDE 2

Given a graph with edge weights, find paths that minimize total weight

  • f edges used

Dijkstra’s shortest path algorithm assumed non-negative edge weights Another easy case: DAG

SHORTEST PATHS

slide-3
SLIDE 3

What if the graph has both negative weights and cycles? If the graph has negative cycles, some distances are negative infinity. Otherwise, 𝐸𝑗𝑑𝑒 𝐷 ≑ min

! !,# $% &' ()*(}{𝐸𝑗𝑑𝑒 𝑀 + π‘₯

𝑀, 𝐷 } But this doesn’t give a consistent recursion, because the recursion graph is not a DAG, so recursion loops around cycles…… How can we make recursion acyclic when the graph is cyclic?

WHAT IF THERE ARE CYCLES?

slide-4
SLIDE 4

Generalizing problems to allow recursion and hence DP Keeping recursions simple Trying multiple recursive algorithms based on different types of case analysis leading to different DP algorithms Using reductions (again)

LESSONS

slide-5
SLIDE 5

WHY WE MIGHT WANT TO DO THIS: ARBITRAGE

slide-6
SLIDE 6

What vertex do we visit first (after 𝑑)? What vertex do we visit last (before 𝑒)? What is the middle vertex along the path? Do we ever visit vertex 𝑀?

SOME POSSIBILITIES

slide-7
SLIDE 7

FIRST VERTEX

Attempt 1: BTSP(𝑑, 𝑒, 𝐻) β–ͺ If 𝑑 = 𝑒 return 0 β–ͺ 𝑁 := ∞ β–ͺ For each 𝑀 ∈ 𝑂(𝑑) do: β–ͺ 𝑁 := min(𝑁, (π‘₯(𝑑, 𝑀) + BTSP(𝑀, 𝑒, 𝐻))) β–ͺ return 𝑁

slide-8
SLIDE 8

Cycles in graph = cycles in recursive calls

PROBLEM: LOOPING

slide-9
SLIDE 9

FIRST VERTEX

Attempt 2: BTSP(𝑑, 𝑒, 𝐻) β–ͺ If 𝑑 = 𝑒 return 0 β–ͺ 𝑁 := ∞ β–ͺ For each 𝑀 ∈ 𝑂(𝑑) do: β–ͺ 𝑁 := min(𝑁, (π‘₯(𝑑, 𝑀) + BTSP(𝑀, 𝑒, 𝐻 βˆ’ {𝑑}))) β–ͺ return 𝑁

slide-10
SLIDE 10

Changing G in recursive calls: exponentially many sub-graphs. Usable for longest simple path = TSP = NP-complete, so approach seems inherently exponential.

PROBLEM

slide-11
SLIDE 11

Let’s put a budget on how deep we’ll recurse, π‘ˆ That corresponds to looking at which paths? How large should π‘ˆ be?

PREVENTING INFINITE RECURSION

slide-12
SLIDE 12

Let’s put a budget on how deep we’ll recurse, π‘ˆ That corresponds to looking at paths with at most T edges How large should π‘ˆ be: π‘ˆ = π‘œ : search all simple paths. Not all paths we search are simple, but that’s OK

PREVENTING INFINITE RECURSION

slide-13
SLIDE 13

If there is a negative cycle, we can keep going around it. But we can break up any cycle into a set of simple cycles.

MINIMUM LENGTH NEGATIVE CYCLE

slide-14
SLIDE 14

If there is a negative cycle, we can keep going around it. But we can break up any cycle into a set of simple cycles.

MINIMUM LENGTH NEGATIVE CYCLE

If the overall cycle is negative, at least one simple cycle is negative. So if there is any negative cycle, there is one of size at most π‘œ

slide-15
SLIDE 15

Let’s put a budget on how deep we’ll recurse, π‘ˆ That corresponds to looking at paths of length at most π‘ˆ If there’s no negative cycle, then the shortest paths are simple, don’t repeat vertices. So the shortest path between any two vertices is of length at most |π‘Š| = π‘œ

PREVENTING INFINITE RECURSION

slide-16
SLIDE 16

FIRST VERTEX

slide-17
SLIDE 17

BF[𝑑, 𝑒, π‘ˆ]: shortest weight of a path from 𝑑 to 𝑒 with at most π‘ˆ edges Note: G is also an input to recursion, but G doesn’t change. Only need changing parts as parameters in matrix

BELLMAN-FORD SUBPROBLEMS

slide-18
SLIDE 18

Base case : π‘ˆ = ? BF[𝑑, 𝑒,?] = 0 if ?? ?? otherwise

BASE CASES:

slide-19
SLIDE 19

Base case : π‘ˆ = 0 BF[𝑑, 𝑒, 0] = 0 if 𝑑 = 𝑒 ∞, otherwise

BASE CASES:

slide-20
SLIDE 20

Check all first steps along path BF[𝑑, 𝑒, π‘ˆ] := min (BF[𝑑, 𝑒, π‘ˆ βˆ’ 1], min

  • ∈/ %

𝐢𝐺 𝑣, 𝑒, π‘ˆ βˆ’ 1 + π‘₯ 𝑑, 𝑣 )

RECURSIVE PART:

slide-21
SLIDE 21

BELLMAN-FORD METHOD

slide-22
SLIDE 22

BELLMAN-FORD ALGORITHM

slide-23
SLIDE 23

BELLMAN-FORD ALGORITHM TIME ANALYSIS

slide-24
SLIDE 24

We never use BF(𝑣, 𝑀, π‘ˆ) beyond step π‘ˆ + 1. It’s OK if we’ve made BF(𝑣, 𝑀, π‘ˆ) smaller as long as we never make it less than the shortest path length. So we can just use one matrix BF(𝑣, 𝑀) rather than π‘œ different matrices, and make both read and write steps from the same Matrix.

SAVING MEMORY

slide-25
SLIDE 25

Bellman-Ford’s recursion based on "What is the first vertex on the path from 𝑣 to 𝑀?" What other question could we ask about the path? What is the last vertex on the path? Symmetric

ALTERNATIVE RECURSION

slide-26
SLIDE 26

Bellman-Ford’s recursion based on "What is the first vertex on the path from 𝑣 to 𝑀?" What other question could we ask about the path? What is the last vertex ? (BF in reverse) What is the middle vertex of the path? (Min plus matrix multiply method) Is 𝑀' ever used in the path? (Floyd-Warshall algorithm)

ALTERNATIVE RECURSION

slide-27
SLIDE 27

MIN-PLUS METHOD

slide-28
SLIDE 28

MIN PLUS ALGORITHM

slide-29
SLIDE 29

MIN PLUS ALGORITHM

slide-30
SLIDE 30

WHICH IS BETTER?

slide-31
SLIDE 31

Is 𝑀 used at all? Need to eliminate vertices in order, so that remaining vertices are consecutive. Subtlety one: need to distinguish between endpoints and set of intermediate points Subtlety two: implicitly assumes no negative cycles, so that shortest paths can be simple.

FLOYD WARSHALL

slide-32
SLIDE 32

Assuming the shortest paths are simple, find the shortest path in G from 𝑑 to 𝑒 using intermediate nodes 𝑉 = {𝑣!, … , 𝑣"}, where 𝑑 and 𝑒 may or may not be in 𝑉. BTFW(𝑑, 𝑒, 𝐻, 𝑉) IF 𝑉 is empty, and 𝑑 = 𝑒 return 0 IF 𝑉 is empty and (𝑑, 𝑒) is an edge return w(𝑑, 𝑒) IF 𝑉 is empty, return infinity Usecase:= BTFW(𝑑, 𝑣", 𝐻, 𝑉 βˆ’ {𝑣"}) + BTFW(𝑣", 𝑒, 𝐻, 𝑉 βˆ’ {𝑣"}) //Use u_k at most once Dontusecase:=BTFW(𝑑, 𝑒, 𝐻, 𝑉 βˆ’ {𝑣"}) //Don’t use 𝑣" Return max(Usecase, Dontusecase)

FLOYD WARSHALL GENERALIZATION

slide-33
SLIDE 33

𝑑 can be arbitrary, 𝑒 can be arbitrary, 𝑉 = {𝑀0, … 𝑀1} for some 0 ≀ 𝑙 ≀ π‘œ FW[𝑑, 𝑒, 𝑙] : Best path from 𝑑 to 𝑒 using only 𝑀0, … , 𝑀1 as intermediate vertices Base case: 𝑉 is empty, 𝑙 = 0, FW[𝑑, 𝑑, 0]=0, FW(𝑑, 𝑒)=w(𝑑, 𝑒) for edge (𝑑, 𝑒), FW[𝑑, 𝑒, 0]=∞ Recursion: FW[𝑑, 𝑒, 𝑙] = min ( , )

REACHABLE SUBPROBLEMS

slide-34
SLIDE 34

𝑑 can be arbitrary, 𝑒 can be arbitrary, 𝑉 = {𝑀0, … 𝑀1} for some 0 ≀ 𝑙 ≀ π‘œ FW[𝑑, 𝑒, 𝑙] : Best path from 𝑑 to 𝑒 using only 𝑀0, … , 𝑀1 as intermediate vertices Base case: 𝑉 is empty, 𝑙 = 0, FW[𝑑, 𝑑, 0]=0, FW(𝑑, 𝑒)=w(𝑑, 𝑒) for edge (𝑑, 𝑒), FW[𝑑, 𝑒, 0]=∞ Recursion: FW[𝑑, 𝑒, 𝑙] = min(F[𝑑, 𝑀1, 𝑙 βˆ’ 1]+F[𝑀1, 𝑒, 𝑙 βˆ’ 1], F[𝑑, 𝑒, 𝑙 βˆ’ 1])) Top down: k decreases, bottom-up : k increases

REACHABLE SUBPROBLEMS

slide-35
SLIDE 35

DPFW(𝐻) Initialize array FW[π‘Š, π‘Š, 1 … |π‘Š|] For 𝑑 in π‘Š do: FW[𝑑, 𝑑, 0]:= 0 For edges 𝑑, 𝑒 ∈ E do: FW[𝑑, 𝑒, 0] = w(𝑑, 𝑒) For 𝑑 in π‘Š do: For 𝑒 in π‘Š βˆ’ {𝑑} do: F[𝑑, 𝑒, 0]:= ∞ For 𝑙 = 1 to |π‘Š| do: For 𝑑 in π‘Š do: For 𝑒 in π‘Š do:

FW[𝑑, 𝑒, 𝑙] = min(FW[𝑑, 𝑀!, 𝑙 βˆ’ 1]+FW[𝑀!, 𝑒, 𝑙 βˆ’ 1],FW[𝑑, 𝑒, 𝑙 βˆ’ 1]))

Return the matrix FW[𝑑, 𝑒, |π‘Š|] (Note: can save space by reusing single matrix)

FW ALGORITHM

slide-36
SLIDE 36

WHICH IS BETTER?