Presentation is loading. Please wait.

Presentation is loading. Please wait.

Divide and Conquer. Problem Solution 4 Example.

Similar presentations


Presentation on theme: "Divide and Conquer. Problem Solution 4 Example."— Presentation transcript:

1 Divide and Conquer

2 Problem

3 Solution

4 4 Example

5 Dynamic Programming

6 Problem 1

7 Solution Correctness

8 Problem 2

9 Solution Correctness

10 10 0-1 knapsack

11 11 The Knapsack Problem A thief robbing a store finds n items The i th item is worth b i and weighs w i pounds Thief’s knapsack can carry at most W pounds Variables b i, w i and W are integers Problem: What items to select to maximize profit?

12 12 0-1 Knapsack Problem Let x i =1 denote item i is in the knapsack and x i =0 denote it is not in the knapsack Problem stated formally as follows maximize subject to (total profit) (weight constraint)

13 13 50 0-1 Knapsack - Greedy Strategy 10 20 30 50 Item 1 Item 2 Item 3 $60$100$120 10 20 $60 $100 + $160 50 20 $100 $120 + $220 30 $6/pound$5/pound$4/pound The greedy choice property does not hold

14 14 Define the problem recursively... Consider the first item i=1 1.If it is selected (put in the knapsack) 2.If it is not selected Compute both cases, select the better one maximizesubject to maximizesubject to

15 15 Recursive Solution Let us define P[i,k] as the maximum profit possible using items {i, i+1,…, n} and residual (knapsack) capacity k We can define P[i,k] recursively as follows     kiP bnbn kiP ],1[ ≤   ≤  kwiwi ni kwiwi ni k wnwn ni k wnwn ni & & & &0 ],[ max{P[ i + 1, k], b i + P[ i + 1, k - w i ]}

16 16 Example n=5, W=10, w = [2, 2, 6, 5, 4], b = [2, 3, 5, 4, 6] i\k 0123456789 10 500006666666 4000066666 3000066666 11 20033669991011 100336699 x = [0,0,1,0,1] x = [1,1,0,0,1]

17 17 Time complexity Running time: O(nW) Technically, this is not a poly-time algorithm This class of algorithms is called pseudo-polynomial

18 Greedy

19 19 Fractional Knapsack Given a set S of n items, such that each item i has a positive benefit b i and a positive weight w i ; the size of the knapsack W The problem is to find the amount x i of each item i which maximizes the total benefit under the condition that 0 ≤ x i ≤ w i and

20 20 Fractional Knapsack Time complexity is O(n log n) Fact: Greedy strategy is optimal for the fractional knapsack problem Proof: We will show that the problem has the optimal substructure and the algorithm satisfies the greedy-choice property

21 21 Greedy Choice Items (sorted by b i /w i ) 1 2 3 … j … n “ Optimal ” solution: x 1 x 2 x 3 x j x n Greedy solution: x 1 ’ x 2 ’ x 3 ’ x j ’ x n ’ We want to prove that taking as much as possible from item 1 is optimal Let us assume that is not the case, and in the optimal solution x 1 < x 1 ’ Because we taking more of item 1 in the greedy solution, we have to decrease the quantity taken of some other item j Therefore, in the greedy solution x j is decreased by (x 1 ’ - x 1 ) In the greedy solution, we gain, and we lose True, since x 1 had the best benefit/weight ratio

22 22 Optimal substructure Items: 1 2 3 … j … n Solution U: x 1 x 2 x 3 … x j … x n Solution U ’ : 0x 2 ’ x 3 ’ … x j ’ … x n ’ (S,W) is the original problem, assume U is optimal for (S,W) S ’ is the sub-problem {2,3,…,n} U contains the greedy choice x 1 Prove that U ’ is optimal for (S ’,W-x 1 ) where x i ’ = x i for all i>1 By contradiction: if U ’ was not optimal, then U ’’ exists such that But which means that U was not optimal for (S,W) → contradiction

23 Problem: The following statements may or may not be correct. In each case, either prove it (if it is correct) or give a counterexample (if it isn't correct). Always assume that the graph G = (V;E) is undirected. Do not assume that edge weights are distinct unless this is specically stated. a)If the lightest edge in a graph is unique, then it must be part of every MST. b)If e is part of some MST of G, then it must be a lightest edge across some cut of G. c)The shortest-path tree computed by Dijkstra's algorithm is necessarily an MST. d)Prim's algorithm works correctly when there are negative edges.

24 Problem: The following statements may or may not be correct. In each case, either prove it (if it is correct) or give a counterexample (if it isn't correct). Always assume that the graph G = (V;E) is undirected. Do not assume that edge weights are distinct unless this is specically stated. a)If the lightest edge in a graph is unique, then it must be part of every MST.

25 Problem: The following statements may or may not be correct. In each case, either prove it (if it is correct) or give a counterexample (if it isn't correct). Always assume that the graph G = (V;E) is undirected. Do not assume that edge weights are distinct unless this is specically stated. a)If the lightest edge in a graph is unique, then it must be part of every MST. True, if not, there exists a cycle connecting the two endpoints of e, so adding e and removing another edge of the cycle, produces a lighter tree.

26 Problem: The following statements may or may not be correct. In each case, either prove it (if it is correct) or give a counterexample (if it isn't correct). Always assume that the graph G = (V;E) is undirected. Do not assume that edge weights are distinct unless this is specically stated. b)If e is part of some MST of G, then it must be a lightest edge across some cut of G.

27 Problem: The following statements may or may not be correct. In each case, either prove it (if it is correct) or give a counterexample (if it isn't correct). Always assume that the graph G = (V;E) is undirected. Do not assume that edge weights are distinct unless this is specically stated. b)If e is part of some MST of G, then it must be a lightest edge across some cut of G. True, consider the cut that has u in one side and v in the other, where e = (u, v).

28 Problem: The following statements may or may not be correct. In each case, either prove it (if it is correct) or give a counterexample (if it isn't correct). Always assume that the graph G = (V;E) is undirected. Do not assume that edge weights are distinct unless this is specically stated. c)The shortest-path tree computed by Dijkstra's algorithm is necessarily an MST.

29 Problem: The following statements may or may not be correct. In each case, either prove it (if it is correct) or give a counterexample (if it isn't correct). Always assume that the graph G = (V;E) is undirected. Do not assume that edge weights are distinct unless this is specically stated. c)The shortest-path tree computed by Dijkstra's algorithm is necessarily an MST. False. In the following graph, the MST has edges (u,w) and (v,w) while Dijkstra’s algorithm gives (u, v), (u,w).

30 Homework 5

31 Solution

32 Problem 4

33 Solution

34 Problem 5

35 Solution

36

37


Download ppt "Divide and Conquer. Problem Solution 4 Example."

Similar presentations


Ads by Google