Algorithm Paradigms - PowerPoint PPT Presentation

1 / 11
About This Presentation
Title:

Algorithm Paradigms

Description:

0 1 knapsack problem: include or reject each item ... max ( don't include item i, or do and optimize remaining space in knapsack) ... – PowerPoint PPT presentation

Number of Views:34
Avg rating:3.0/5.0
Slides: 12
Provided by: Fro11
Category:

less

Transcript and Presenter's Notes

Title: Algorithm Paradigms


1
Algorithm Paradigms
2
Algorithm Paradigms
  • Search (BFS, DFS)
  • often good when guided by a heuristic
  • Divide and Conquer
  • Good when subproblems are independent
  • Randomization
  • Good when any choice is equally good

3
Algorithm Paradigms
  • Greedy Algorithms
  • Dijkstras Algorithm for shortest distance
  • Dynamic Programming
  • Longest common subsequence
  • (programming isnt computer programming)

4
Greedy Algorithm
  • A greedy approach does not always lead to an
    optimal solution
  • Appropriate if a good local choice is also a good
    global choice
  • Examples
  • Huffman coding (computing optimal code)
  • Kruskals algorithm

5
Dynamic Programming
  • Simple subproblems
  • Subproblem optimization
  • Subproblem overlap
  • Optimal solutions to subproblems are computed
    only once, and stored in a table
  • consider the space complexity

6
Knapsack problem
  • n items, each worth vi and weighing wi
  • Goal is to fill knapsack of capacity W pounds
    with maximum value of items
  • 0 1 knapsack problem include or reject each
    item
  • fractional knapsack problem can include
    fractional amounts of each item

7
Fractional Knapsack problem
  • Greedy algorithm
  • Compute each items value per pound vi / wi
  • Sort items by value per pound
  • Fill knapsack by taking most valuable item first,
    until knapsack is full

8
0 1 Knapsack problem
  • Greedy approach is not optimal
  • Item 1 value 5 weight 3 pounds 1.67
  • Item 2 value 3.1 weight 2 pounds 1.55
  • Item 3 value 2 weight 2 pounds 1.00
  • Knapsack holds 4 pounds

9
0 1 Knapsack problem
  • Exhaustive enumeration (brute-force approach)
    isnt too speedy
  • There are n items, so 2n subsets.
  • Dont want to check each one!

10
0 1 Knapsack problem
  • Dynamic programming approach
  • Build a 2-D table Ti,j
  • i means using items 1 i ( 1 i n )
  • j means max capacity ( 1 j w )
  • Ti,j is the maximum value obtainable with items
    1 through i weighing at most j
  • Ti,j max (Ti-1,j , vi Ti-1, j-wi )
  • max ( dont include item i, or do and optimize
    remaining space in knapsack)

11
0 1 Knapsack problem
  • Ti,j max (Ti-1,j , vi Ti-1, j-wi )
  • Fill in i 1 row with vi if wi gt j
  • T , lt 0 is 0

j 1 2 3 4 1 0 0 5 5
i 2 0 3.1 5 5 3 0 3.1 5 5.1
Write a Comment
User Comments (0)
About PowerShow.com