This preview shows page 1. Sign up to view the full content.
Unformatted text preview: ms by decreasing unit profit: i=1 while K > 0 do if K > w[i] then xi = 1 and K = K  w[i] else xi = K/w[i] and K = 0 end if i=i+1 end while c Xin He (University at Buffalo) CSE 431/531 Algorithm Analysis and Design 28 / 49 Fractional Knapsack Problem
Although the Fractional Knapsack Problem looks very similar to the 0/1 Knapsack Problem, it is much much easier. The Greedy Strategy 3 works. GreedyFractionalKnapsack
1: 2: 3: 4: 5: 6: 7: 8: 9: 10:
p[1] w[1] p[2] w[2] p[3] w[3] Sort the items by decreasing unit profit: i=1 while K > 0 do if K > w[i] then xi = 1 and K = K  w[i] else xi = K/w[i] and K = 0 end if i=i+1 end while It can be shown the Greedy Choice Property holds in this case.
c Xin He (University at Buffalo) CSE 431/531 Algorithm Analysis and Design 28 / 49 Outline 1 Greedy Algorithms Elements of Greedy Algorithms Greedy Choice Property for Kruskal's Algorithm 0/1 Knapsack Problem Activity Selection Problem Scheduling All Intervals 2 3 4 5 6 c Xin He (University at Buffalo) CSE 431/531 Algorithm Analysis and Design 29 / 49 Activity Selection Problem
Activity Selection Problem
A set S = {1, 2, . . . , n} of activities. c Xin He (University at Buffalo) CSE 431/531 Algorithm Analysis and Design 30 / 49 Activity Selection Problem
Activity Selection Problem
A set S = {1, 2, . . . , n} of activities. Each activity i has a staring time si and a finishing time fi (si fi ). c Xin He (University at Buffalo) CSE 431/531 Algorithm Analysis and Design 30 / 49 Activity Selection Problem
Activity Selection Problem
A set S = {1, 2, . . . , n} of activities. Each activity i has a staring time si and a finishing time fi (si fi ). Two activities i and j are compatible if the interval [si , fi ) and [sj , fj ) do not overlap. c Xin He (University at Buffalo) CSE 431/531 Algorithm Analysis and Design 30 / 49 Activity Selection Problem
Activity Selection Problem
A set S = {1, 2, . . . , n} of activities. Each activity i has a staring time si and a finishing time fi (si fi ). Two activities i and j are compatible if the interval [si , fi ) and [sj , fj ) do not overlap. Goal: Select a subset A S of mutually compatible activities so that A is maximized. c Xin He (University at Buffalo) CSE 431/531 Algorithm Analysis and Design 30 / 49 Activity Selection Problem
Activity Selection Problem
A set S = {1, 2, . . . , n} of activities. Each activity i has a staring time si and a finishing time fi (si fi ). Two activities i and j are compatible if the interval [si , fi ) and [sj , fj ) do not overlap. Goal: Select a subset A S of mutually compatible activities so that A is maximized. Application
Consider a single CPU computer. It can run only one job at any time. c Xin He (University at Buffalo) CSE 431/531 Algorithm Analysis and Design 30 / 49 Activity Selection Problem
Activity Selection Problem
A set S = {1, 2, . . . , n} of activities. Each activity i has a staring time si and a finishing time fi (si fi ). Two activities i and j are compatible if the interval [si , fi ) and [sj , fj ) do not overlap. Goal: Select a subset A S of mutually compatible activities so that A is maximized. Application
Consider a single CPU computer. It can run only one job at any time. Each activity i is a job to be run on the CPU that must start at time si and finish at time fi . c Xin He (University at Buffalo) CSE 431/531 Algorithm Analysis and Design 30 / 49 Activity Selection Problem
Activity Selection Problem
A set S = {1, 2, . . . , n} of activities. Each activity i has a staring time si and a finishing time fi (si fi ). Two activities i and j are compatible if the interval [si , fi ) and [sj , fj ) do not overlap. Goal: Select a subset A S of mutually compatible activities so that A is maximized. Application
Consider a single CPU computer. It can run only one job at any time. Each activity i is a job to be run on the CPU that must start at time si and finish at time fi . How to select a maximum subset A of jobs to run on CPU?
c Xin He (University at Buffalo) CSE 431/531 Algorithm Analysis and Design 30 / 49 Greedy Algorithm for Activity Selection Problem
Greedy Strategy
At any moment t, select the activity i with the smallest finish time fi . c Xin He (University at Buffalo) CSE 431/531 Algorithm Analysis and Design 31 / 49 Greedy Algorithm for Activity Selection Problem
Greedy Strategy
At any moment t, select the activity i with the smallest finish time fi . GreedyActivitySelection 1: Sort the activities by increasing finish time: f1 f2 fn 2: A = {1} (A is the set of activities to be selected.) 3: j = 1 (j is the current activity being considered.) 4: for i = 2 to n do 5: if si fj then 6: A = A {i} 7: j=i 8: end if 9: end for 10: return A
c Xin He (University at Buffalo) CSE 431/531 Algorithm Analysis and Design 31 / 49 Example Input 0 1 2 3 4 5 6 7 8 9 10 11 After Sorting Solid lines are selected activities Dashed lines are not selected 0 1 2 3 4 5 6 7 8 9 10 11 c Xin He (University at Buffalo) CSE 431/531 Algorithm Analysis and Design 32 / 49 Example Input 0 1 2 3 4 5 6 7 8 9 10 11 After Sorting Solid lines are selec...
View
Full
Document
This note was uploaded on 02/27/2012 for the course CSE 431/531 taught by Professor Xinhe during the Fall '11 term at SUNY Buffalo.
 Fall '11
 XINHE
 Algorithms

Click to edit the document details