Docsity

Dynamic programming, Study notes of Algorithms and Programming

Partial preview of the text

Download Dynamic programming and more Study notes Algorithms and Programming in PDF only on Docsity!

Good Bye to Brute Force

Graphs/Dynamic Programming

Lecture 14

Dijkstra’s Algorithm - Analysis

ó Min-priority queue is maintained by calling 3 priority-queue operations:

ó Insert – line 3

ó Extract-min – line 5

ó Decrease-key (implicit in relax) – line 8

ó There are a total of |E| decrease-key operations

ó Running time depends on how the min-priority queue is implemented

ó If implemented as a binary heap, then O(ElgV)

ó If implemented as a Fibonacci heap, then O(VlgV + E)

Dijkstra’s Algorithm - negative weights?

Bellman-Ford Algorithm - Idea

Bellman-Ford Algorithm - Example

Bellman-Ford Algorithm - Example

Bellman-Ford Algorithm - Example

Bellman-Ford Algorithm - Example

Bellman-Ford Algorithm - Complexity

SSSP-BellmanFord ( graph (G,w), vertex s)

InitializeSingleSource (G, s)

for i ¨ 1 to |V[G] - 1| do

for (u,v) Œ E[G] do

Relax (u,v,w)

for (u,v) Œ E[G] do

if d[v] > d[u] + w(u,v) then

return false

return true

executed Q(V) times Q(E) Q(E)

Q(1)

Bellman-Ford Algorithm - Complexity

InitializeSingleSource TI (V,E) = Q(V) Relax TR(V,E) = Q(1)? SSSP-BellmanFord T(V,E) = TI (V,E) + V E TR(V,E) + E = == QQ(V) + V E(V E) Q(1) + E =

Dynamic Programming

ó The algorithm works by splitting a problem into a sequence of optimization sub problems ó The sub problems are solved and then combined to obtain solution to the main problem ó The dynamic programming approach seems to be similar to divide-and-conquer approach ó However, unlike the divide-and-conquer method, the dynamic programming approach isapplicable when the sub-problems are not independent i.e. dynamic programming splits the problem into overlapping sub problems. ó Further, the optimal solutions to sub problems are stored into a table, which is used to buildthe final or global optimal solution.

Overlapping Sub-problems

ó A problem is said to have overlapping sub-problems if it can

be broken down into sub-problems which are reused

multiple times.

ó A problem is said to have overlapping subproblems if it can

be broken down into subproblems which are reused multiple

times. This is closely related to recursion.

Fibonacci Numbers

ó def fib(n):

if n == 0:

return 0

if n == 1

return 1

return fib(n-1) + fib(n-2)

ó The problem of calculating fib(n) thus

depends on both fib(n-1) and fib(n-2). To

see how these sub-problems overlap look at

how many times fib is called and with what

arguments when we try to calculate

ó fib(5) ó fib(4) + fib(3) ó fib(3) + fib(2) + fib(2) + fib(1) ó fib(2) + fib(1) + fib(1) + fib(0) + fib(1) + fib(0) + fib(1) ó fib(1) + fib(0) + fib(1) + fib(1) + fib(0) + fib(1) + fib(0) +fib(1)

optimal substructure

ó A problem is said to have optimal substructure if the globally

optimal solution can be constructed from locally optimal solutions

to sub-problems.

ó The general form of problems in which optimal substructure plays

a roll goes something like this. Let's say we have a collection of

objects called A.

ó For each object o in A we have a "cost," c(o). Now find the subset

of A with the maximum (or minimum) cost, perhaps subject to

certain constraints.