{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

Lecture topic-BruteForce

# Weight 0 1 2 1 2 5 2 3 10 3 4 5 4 12 7 12 13 12 13 14

This preview shows page 1. Sign up to view the full content.

This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: k Find the most valuable subset of the items that fit into the knapsack knapsack Example: item weight 1 2 2 5 3 10 10 4 5 value \$20 \$20 \$30 \$30 \$50 \$10 \$10 Knapsack capacity W=16 Design and Analysis of Algorithms – Chapter 3 55 Knapsack by exhaustive search Subset Total weight ∅ 0 {1} 2 {1} {2} 5 {2} {3} 10 {3} {4} 5 {4} {1,2} 7 {1,2} {1,3} 12 {1,3} {1,4} 7 {1,4} {2,3} 15 {2,3} {2,4} 10 {2,4} {3,4} 15 {3,4} {1,2,3} 17 {1,2,3} {1,2,4} 12 {1,2,4} {1,3,4} 17 {1,3,4} {2,3,4} 20 {2,3,4} {1,2,3,4} 22 Total value \$0 \$20 \$30 \$50 \$10 \$50 \$70 \$30 \$80 \$40 \$60 not feasible \$60 not feasible not feasible not feasible Most valuable subset? Efficiency: Design and Analysis of Algorithms – Chapter 3 56 0-1 Knapsack problem: brute-force 0-1 approach approach a Algorithm: • We go through all combinations and find the one We with maximum value and with total weight less or equal to W a Efficiency: • Since there are n items, there are 2n possible Since combinations of items. combinations • Thus, the running time will be O(2n) Thus, O(2 Design and Analysis of Algorithms – Chapter 3 57 Brute force strengths and Brute weaknesses weaknesses a Strengths: • wide applicability • simplicity • yields reasonable algorithms for some important problems – sorting; matrix multiplication; closest-pair; convex-hull • yields standard algorithms for simple computational tasks yields and graph traversal problems and Design and Analysis of Algorithms – Chapter 3 58 Brute force strengths and Brute weaknesses weaknesses a Weaknesses: • rarely yields efficient algorithms • some brute force algorithms unacceptably slow some – e.g., the recursive algorithm for computing Fibonacci numbers • not as constructive/creative as some other design techniques Design and Analysis of Algorithms – Chapter 3 59...
View Full Document

{[ snackBarMessage ]}

Ask a homework question - tutors are online