Approaches to Problem Solving - PowerPoint PPT Presentation

About This Presentation
Title:

Approaches to Problem Solving

Description:

Approaches to Problem Solving greedy algorithms dynamic programming backtracking divide-and-conquer – PowerPoint PPT presentation

Number of Views:25
Avg rating:3.0/5.0
Slides: 27
Provided by: 2498
Learn more at: https://www.cs.rit.edu
Category:

less

Transcript and Presenter's Notes

Title: Approaches to Problem Solving


1
Approaches to Problem Solving
  • greedy algorithms
  • dynamic programming
  • backtracking
  • divide-and-conquer

2
Interval Scheduling
Input Output Objective
a set of time-intervals a subset of
non-overlapping intervals maximize of selected
intervals
3
Interval Scheduling
Input Output Objective
a set of time-intervals a subset of
non-overlapping intervals maximize of selected
intervals
Idea 1 Select interval that starts earliest,
remove overlapping intervals and recurse.
4
Interval Scheduling
Input Output Objective
a set of time-intervals a subset of
non-overlapping intervals maximize of selected
intervals
Idea 2 Select the shortest interval, remove
overlapping intervals and recurse.
5
Interval Scheduling
Input Output Objective
a set of time-intervals a subset of
non-overlapping intervals maximize of selected
intervals
Idea 3 Select the interval with the fewest
conflicts, remove overlapping intervals and
recurse.
6
Interval Scheduling
Input Output Objective
a set of time-intervals a subset of
non-overlapping intervals maximize of selected
intervals
Idea 4 Select the earliest finishing interval,
remove overlapping intervals and recurse.
7
Interval Scheduling
  • INTERVAL-SCHEDULING( (s0,f0), , (sn-1,fn-1) )
  • Remain 0,,n-1
  • Selected
  • while ( Remain gt 0 )
  • k 2 Remain is such that fk mini2Remain fi
  • Selected Selected k
  • Remain Remain k
  • for every i in Remain
  • if (si lt fk) then Remain Remain i
  • return Selected

Idea 4 Select the earliest finishing interval,
remove overlapping intervals and recurse.
8
Interval Scheduling
  • INTERVAL-SCHEDULING( (s0,f0), , (sn-1,fn-1) )
  • Remain 0,,n-1
  • Selected
  • while ( Remain gt 0 )
  • k 2 Remain is such that fk mini2Remain fi
  • Selected Selected k
  • Remain Remain k
  • for every i in Remain
  • if (si lt fk) then Remain Remain i
  • return Selected

Running time
9
Interval Scheduling
  • INTERVAL-SCHEDULING( (s0,f0), , (sn-1,fn-1) )
  • Remain 0,,n-1
  • Selected
  • while ( Remain gt 0 )
  • k 2 Remain is such that fk mini2Remain fi
  • Selected Selected k
  • Remain Remain k
  • for every i in Remain
  • if (si lt fk) then Remain Remain i
  • return Selected

Thm Algorithm works.
10
Interval Scheduling
Thm Algorithm works.
Which type of algorithm did we use ?
11
Schedule All Intervals
Input Output Objective
a set of time-intervals a partition of the
intervals, each part of the partition consists of
non-overlapping intervals minimize the number of
parts in the partition
12
Schedule All Intervals
Input Output Objective
a set of time-intervals a partition of the
intervals, each part of the partition consists of
non-overlapping intervals minimize the number of
parts in the partition
max (over time t) number of intervals that are
active at time t
Def depth
13
Schedule All Intervals
max (over time t) number of intervals that are
active at time t
Def depth
Observation 1 Need at least depth parts (labels).
  • SCHEDULE-ALL_INTERVALS ( (s0,f0), , (sn-1,fn-1)
    )
  • Sort intervals by their starting time
  • for j0 to n-1 do
  • Consider 1,,depth
  • for every iltj that overlaps with j do
  • Consider Consider Labeli
  • if Consider gt 0 then
  • Labelj anything from Consider
  • else
  • Labelj nothing
  • return Label

14
Schedule All Intervals
Thm Every interval gets a real label.
Corollary Algo returns an optimal solution (i.e.
it works!).
Running time
  • SCHEDULE-ALL_INTERVALS ( (s0,f0), , (sn-1,fn-1)
    )
  • Sort intervals by their starting time
  • for j0 to n-1 do
  • Consider 1,,depth
  • for every iltj that overlaps with j do
  • Consider Consider Labeli
  • if Consider gt 0 then
  • Labelj anything from Consider
  • else
  • Labelj nothing
  • return Label

15
Weighted Interval Scheduling
Input Output Objective
a set of time-intervals, each interval has a
cost a subset of non-overlapping
intervals maximize the sum of the costs in the
subset
6
3
4
2
10
5
1
16
Weighted Interval Scheduling
Input Output Objective
a set of time-intervals, each interval has a
cost a subset of non-overlapping
intervals maximize the sum of the costs in the
subset
  • WEIGHTED-SCHED-ATTEMPT((s0,f0,c0),,(sn-1,fn-1,cn-
    1))
  • sort intervals by their finishing time
  • return WEIGHTED-SCHEDULING-RECURSIVE (n-1)
  • WEIGHTED-SCHEDULING-RECURSIVE (j)
  • if (jlt0) then RETURN 0
  • kj
  • while (interval k and j overlap) do k--
  • return
  • max(cj WEIGHTED-SCHEDULING-RECURSIVE(k),
  • WEIGHTED-SCHEDULING-RECURSIVE(j-1))

17
Weighted Interval Scheduling
Does the algorithm below work ?
  • WEIGHTED-SCHED-ATTEMPT((s0,f0,c0),,(sn-1,fn-1,cn-
    1))
  • sort intervals by their finishing time
  • return WEIGHTED-SCHEDULING-RECURSIVE (n-1)
  • WEIGHTED-SCHEDULING-RECURSIVE (j)
  • if (jlt0) then RETURN 0
  • kj
  • while (interval k and j overlap) do k--
  • return
  • max(cj WEIGHTED-SCHEDULING-RECURSIVE(k),
  • WEIGHTED-SCHEDULING-RECURSIVE(j-1))

18
Weighted Interval Scheduling
Dynamic programming ! I.e. memorize the solution
for j
  • WEIGHTED-SCHED-ATTEMPT((s0,f0,c0),,(sn-1,fn-1,cn-
    1))
  • sort intervals by their finishing time
  • return WEIGHTED-SCHEDULING-RECURSIVE (n-1)
  • WEIGHTED-SCHEDULING-RECURSIVE (j)
  • if (jlt0) then RETURN 0
  • kj
  • while (interval k and j overlap) do k--
  • return
  • max(cj WEIGHTED-SCHEDULING-RECURSIVE(k),
  • WEIGHTED-SCHEDULING-RECURSIVE(j-1))

19
Weighted Interval Scheduling
Heart of the solution
S j max cost of a set of non-overlapping
intervals selected from the first j
intervals
Another part of the heart how to compute Sj ?
Finally, what do we return ?
20
Weighted Interval Scheduling
Heart of the solution
S j max cost of a set of non-overlapping
intervals selected from the first j
intervals
  • WEIGHTED-SCHED ((s0,f0,c0), , (sn-1,fn-1,cn-1))
  • Sort intervals by their finishing time
  • Define S-1 0
  • for j0 to n-1 do
  • k j
  • while (intervals k and j overlap) do k--
  • Sj max( Sj-1, cj Sk )
  • return Sn-1

21
Weighted Interval Scheduling
Reconstructing the solution
  • WEIGHTED-SCHED ((s0,f0,c0), , (sn-1,fn-1,cn-1))
  • Sort intervals by their finishing time
  • Define S-1 0
  • for j0 to n-1 do
  • k j
  • while (intervals k and j overlap) do k--
  • Sj max( Sj-1, cj Sk )
  • RETURN Sn-1

22
Longest Increasing Subsequence
Input Output Objective
a sequence of numbers an increasing
subsequence maximize length of the subsequence
Example
2 3 1 7 4 6 9 5
23
Longest Increasing Subsequence
Input Output Objective
a sequence of numbers an increasing
subsequence maximize length of the subsequence
Heart of the solution
24
Longest Increasing Subsequence
Input Output Objective
a sequence of numbers an increasing
subsequence maximize length of the subsequence
Heart of the solution
Sj
the maximum length of an increasing subsequence
of the first j numbers ending with the j-th number
25
Longest Increasing Subsequence
Input Output Objective
a sequence of numbers a1, a2, , an an increasing
subsequence maximize length of the subsequence
Heart of the solution
Sj
the maximum length of an increasing subsequence
of the first j numbers ending with the j-th number
Sj 1 maximum Sk where k lt j and ak lt aj
What to return ?
26
Longest Increasing Subsequence
  • LONGEST-INCR-SUBSEQ (a0,,an-1)
  • for j0 to n-1 do
  • Sj 1
  • for k0 to j-1 do
  • if akltaj and SjltSk1 then
  • Sj Sk1
  • return maxj Sj
Write a Comment
User Comments (0)
About PowerShow.com