Docsity

Exploring Shortest Paths & Negative Cycles: Dijkstra's & Bellman-Ford Algorithms, Study notes of Algorithms and Programming

Partial preview of the text

Download Exploring Shortest Paths & Negative Cycles: Dijkstra's & Bellman-Ford Algorithms and more Study notes Algorithms and Programming in PDF only on Docsity!

CS 473ug: Algorithms

Mahesh Viswanathan [email protected] 3232 Siebel Center

University of Illinois, Urbana-Champaign

Spring 2008

Shortest Paths in GraphsKnapsack Detecting Negative CyclesNetwork Routing

Part I

Dynamic Programming: Wrapup

Shortest Paths in GraphsKnapsack Detecting Negative CyclesNetwork Routing Greedy AttemptDynamic Programming Solution

Knapsack Problem

Input Given a Knapsack of capacity W lbs. and n objects with ith object having weight wi and value vi

Goal Fill the Knapsack without exceeding weight limit while maximizing value.

Shortest Paths in GraphsKnapsack Detecting Negative CyclesNetwork Routing Greedy AttemptDynamic Programming Solution

Knapsack Problem

Input Given a Knapsack of capacity W lbs. and n objects with ith object having weight wi and value vi ; assume W , wi , vi are all positive integers Goal Fill the Knapsack without exceeding weight limit while maximizing value.

Shortest Paths in GraphsKnapsack Detecting Negative CyclesNetwork Routing Greedy AttemptDynamic Programming Solution

Knapsack Example

Example Item 1 2 3 4 5 Value 1 6 18 22 28 Weight 1 2 5 6 7

If W = 11, the best is { 3 , 4 } giving value 40.

Special Case When vi = wi , the Knapsack problem is called the Subset Sum Problem.

Shortest Paths in GraphsKnapsack Detecting Negative CyclesNetwork Routing Greedy AttemptDynamic Programming Solution

Applications

Application to Scheduling Knapsack interpreted as a single machine, objects as jobs to be scheduled, weights as processing times, and values as priorities.

Shortest Paths in GraphsKnapsack Detecting Negative CyclesNetwork Routing Greedy AttemptDynamic Programming Solution

Greedy Approach

Pick objects with greatest value Let W = 2, w 1 = w 2 = 1, w 3 = 2, v 1 = v 2 = 2 and v 3 = 3; greedy strategy will pick { 3 }, but the optimal is { 1 , 2 }

Shortest Paths in GraphsKnapsack Detecting Negative CyclesNetwork Routing Greedy AttemptDynamic Programming Solution

Greedy Approach

Pick objects with greatest value Let W = 2, w 1 = w 2 = 1, w 3 = 2, v 1 = v 2 = 2 and v 3 = 3; greedy strategy will pick { 3 }, but the optimal is { 1 , 2 } Pick objects with smallest weight

Shortest Paths in GraphsKnapsack Detecting Negative CyclesNetwork Routing Greedy AttemptDynamic Programming Solution

Greedy Approach

Pick objects with greatest value Let W = 2, w 1 = w 2 = 1, w 3 = 2, v 1 = v 2 = 2 and v 3 = 3; greedy strategy will pick { 3 }, but the optimal is { 1 , 2 } Pick objects with smallest weight Let W = 2, w 1 = 1, w 2 = 2, v 1 = 1 and v 2 = 3; greedy strategy will pick { 1 }, but the optimal is { 2 } Pick objects with largest vi /wi ratio

Shortest Paths in GraphsKnapsack Detecting Negative CyclesNetwork Routing Greedy AttemptDynamic Programming Solution

Greedy Approach

Pick objects with greatest value Let W = 2, w 1 = w 2 = 1, w 3 = 2, v 1 = v 2 = 2 and v 3 = 3; greedy strategy will pick { 3 }, but the optimal is { 1 , 2 } Pick objects with smallest weight Let W = 2, w 1 = 1, w 2 = 2, v 1 = 1 and v 2 = 3; greedy strategy will pick { 1 }, but the optimal is { 2 } Pick objects with largest vi /wi ratio Let W = 4, w 1 = w 2 = 2, w 3 = 3, v 1 = v 2 = 3 and v 3 = 5; greedy strategy will pick { 3 }, but the optimal is { 1 , 2 }

Shortest Paths in GraphsKnapsack Detecting Negative CyclesNetwork Routing Greedy AttemptDynamic Programming Solution

Formulating sub-problems

Let Opt(i) be maximum value obtained for items 1,... i Case n is not selected.

Shortest Paths in GraphsKnapsack Detecting Negative CyclesNetwork Routing Greedy AttemptDynamic Programming Solution

Formulating sub-problems

Let Opt(i) be maximum value obtained for items 1,... i Case n is not selected. The optimal choice is the optimal choice of the first 1 though n − 1 items, i.e., Opt(n − 1)

Shortest Paths in GraphsKnapsack Detecting Negative CyclesNetwork Routing Greedy AttemptDynamic Programming Solution

Formulating sub-problems

Let Opt(i) be maximum value obtained for items 1,... i Case n is not selected. The optimal choice is the optimal choice of the first 1 though n − 1 items, i.e., Opt(n − 1) Case n is selected. Need to pick items from 1 through n − 1 that leaves room for item n

Shortest Paths in GraphsKnapsack Detecting Negative CyclesNetwork Routing Greedy AttemptDynamic Programming Solution

Formulating sub-problems

Let Opt(i) be maximum value obtained for items 1,... i Case n is not selected. The optimal choice is the optimal choice of the first 1 though n − 1 items, i.e., Opt(n − 1) Case n is selected. Need to pick items from 1 through n − 1 that leaves room for item n Need more subproblems.