0% found this document useful (0 votes)
2 views

Lecture 2

The document covers key concepts in algorithm design, focusing on the Euler Theorem and greedy algorithms, particularly in the context of interval scheduling and partitioning. It explains the greedy approach to selecting compatible jobs and lectures, demonstrating its optimality through various proofs and examples. Additionally, it outlines the implementation details and complexities of these algorithms.

Uploaded by

chunwan7
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Lecture 2

The document covers key concepts in algorithm design, focusing on the Euler Theorem and greedy algorithms, particularly in the context of interval scheduling and partitioning. It explains the greedy approach to selecting compatible jobs and lectures, demonstrating its optimality through various proofs and examples. Additionally, it outlines the implementation details and complexities of these algorithms.

Uploaded by

chunwan7
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 31

Summary of Lecture 1

• Euler Theorem

• Algorithm for Euler Circuit.

• Basic techniques for designing algorithms:


greedy, divide-and-conquer, dynamic programming

03/04/2025 Page 1
Week 2: Greedy Algorithms

03/04/2025 Page 2
Greedy Algorithm
• A technique to solve problems:
– always makes the locally best choice at the moment
(local optimal).
– Hopefully, a series of locally best choices will lead to a
globally best solution.
• Greedy algorithms yield optimal solutions for
many (but not all) problems.

CS4335 Design and Analysis of


03/04/2025 Page 3
Algorithms/WANG Lusheng
Interval Scheduling
Interval Scheduling
• Interval scheduling.
– Job j starts at sj and finishes at fj.
– Two jobs compatible if they don't overlap.
– Goal: find maximum subset of mutually compatible jobs.
a
b Examples:
{a, g} is a set of
c mutually
d compatible jobs.
e {b, e, h} is also
a set of
f
mutually
g compatible jobs.
h
0 1 2 3 4 5 6 7 8 9 10 11 Time
Page 5
Ideas for Interval Scheduling
Greedy Algorithm: choose a job with the largest or smallest
score/number.
• What are possible rules for greedy score/number ?
– Choose the interval that starts earliest.
• Rationale: start using the resource as soon as possible.
– Choose the smallest interval.
• Rationale: try to have lots of small jobs.
– Choose the interval that overlaps (conflicts) with the fewest remaining
intervals.
• Rationale: keep our options open and eliminate as few intervals as
possible.

CS4335 Design and Analysis of


03/04/2025 Page 6
Algorithms/WANG Lusheng
Rules That Don’t Work

CS4335 Design and Analysis of


03/04/2025 Page 7
Algorithms/WANG Lusheng
Rules That Don’t Work

CS4335 Design and Analysis of


03/04/2025 Page 8
Algorithms/WANG Lusheng
Rules That Don’t Work

CS4335 Design and Analysis of


03/04/2025 Page 9
Algorithms/WANG Lusheng
Example: select a job with the earliest finish time
• Jobs (s, f): (0, 10), (3, 4), (2, 8), (1, 5), (4, 5), (4, 8), (5, 6) (7,9).

• Sorting based on fi:


– (3, 4) (1, 5), (4, 5) (5, 6) (4,8) (2,8) (7, 9) (0,10).

• Selecting jobs:
– (3, 4),
– (4, 5),
– (5, 6),
– (7, 9),

CS4335 Design and Analysis of


03/04/2025 Page 10
Algorithms/WANG Lusheng
Interval Scheduling: Greedy Algorithm
• Greedy algorithm. Consider jobs in increasing order of finishing time. Take each job
provided it's compatible with the ones already taken.
Sort jobs by finish times so that f1  f2  ...  fn.
jobs selected

A  
for j = 1 to n {
if (job j compatible with A)
A  A  {j}
}
return A

• Implementation. O(n log n).


– Remember job j* that was added last to A.
}how to decide if job j is compatible with A
– Job j is compatible with A if sj  fj*. Page 11
Proof of correctness: How do we prove that the algorithm is correct.
• The if statement in our algorithm ensures that we can always get a set of
mutually compatible jobs, which is considered to be a solution.
• How can we prove that the solution is optimal (has the biggest size)?
– Let A be the schedule returned by this algorithm.
– Let OPT be some optimal solution (there may be many optimal solutions!).
• It is impossible to show that A = OPT if there are multiple optimal solutions.
• Here we want to show that |A| = |OPT| or equivalently A is one of the
optimal solutions.
• Remarks:
– |A| = |OPT| is much weaker than A=OPT.
– Multiple optimal solutions do exit:
• For example, both (0, 1), (1 2) and (0, 1), (1, 3) are optimal solutions for input: (0, 1), (1, 2),
(1, 3).

CS4335 Design and Analysis of


03/04/2025 Page 12
Algorithms/WANG Lusheng
Interval Scheduling: Analysis
• Theorem. Greedy algorithm is optimal.
• Proof:
– We compare the solution obtained from greedy algorithm with an optimal solution.
– Let G=i1, i2, …, ik denote the set of jobs selected by greedy.
– Let Opt=j1, j2, …, jn denote the set of jobs in the optimal solution.
• The set of jobs are mutually compatible and the number of jobs is the largest.
– Without loss of generality, we assume that
i1=j1, i2=j2, …, ir=jr and ir+1jr+1, where r could be 0, 1, 2, ….
– Job ir+1 finishes before (or at the same time of ) jr+1 due to our greedy algorithm.

job ir+1 finishes before jr+1

G: i1 i1 ir ir+1

OPT: j1 j2 jr jr+1 ...

CS4335 Design and Analysis of


03/04/2025 Page 13
Algorithms/WANG Lusheng
Interval Scheduling: Analysis
• Theorem. Greedy algorithm is optimal.
• Pf.
– Let A=i1, i2, ... ik denote set of jobs selected by greedy.
– Let Opt=j1, j2, ... jm denote set of jobs in the optimal solution with
i1 = j1, i2 = j2, ..., ir = jr and ir+1 ≠ jr+1 (Note that r can be 0, 1, 2, …)

job ir+1 finishes before jr+1

A: i1 i1 ir ir+1

OPT’: j1 j2 jr ir+1 ...

Page 14
Another Proof for Interval Scheduling: (words without figures, expected in exams)
Let: be the solution return by greedy
Let : be an optimal solution
Both are in the order of finish time.
1. If since finishes the earliest according to our algorithm, thus finishes not
later than we can replace by in Opt. After this replacement, Opt’: is still
optimal. Now set
.
2. If earlier than set

…….
After a number of replacements:

Or

The last case is impossible, since according to our algorithm, no job starts later than the
finish time of
Therefore, G is optimal ( it have the same number of jobs as Opt)
Interval Scheduling
B
C
A
E
D
F
G
H Time
0 1 2 3 4 5 6 7 8 9 10 11

B
0 1 2 3 4 5 6 7 8 9 10 11 Page 16
Interval Scheduling
B
C
A
E
D
F
G
H Time
0 1 2 3 4 5 6 7 8 9 10 11

B C
0 1 2 3 4 5 6 7 8 9 10 11 Page 17
Interval Scheduling
B
C
A
E
D
F
G
H Time
0 1 2 3 4 5 6 7 8 9 10 11

BA
0 1 2 3 4 5 6 7 8 9 10 11 Page 18
Interval Scheduling
B
C
A
E
D
F
G
H Time
0 1 2 3 4 5 6 7 8 9 10 11

B E
0 1 2 3 4 5 6 7 8 9 10 11 Page 19
Interval Scheduling
B
C
A
E
D
F
G
H Time
0 1 2 3 4 5 6 7 8 9 10 11

B D
E
0 1 2 3 4 5 6 7 8 9 10 11 Page 20
Interval Scheduling
B
C
A
E
D
F
G
H Time
0 1 2 3 4 5 6 7 8 9 10 11

B E F
0 1 2 3 4 5 6 7 8 9 10 11 Page 21
Interval Scheduling
B
C
A
E
D
F
G
H Time
0 1 2 3 4 5 6 7 8 9 10 11

B E G
0 1 2 3 4 5 6 7 8 9 10 11 Page 22
Interval Scheduling
B
C
A
E
D
F
G
H Time
0 1 2 3 4 5 6 7 8 9 10 11

B E H
0 1 2 3 4 5 6 7 8 9 10 11 Page 23
Interval Partitioning
Interval Partitioning
• Interval partitioning.
– Lecture j starts at sj and finishes at fj.
– Goal: find minimum number of classrooms to schedule all
lectures so that no two lectures occur at the same time in
the same room.

• Ex: This schedule uses 4 classrooms to schedule 10 lectures.

e j

c d g

b h

a f i

9 9:30 10 10:30 11 11:30 12 12:30 1 1:30 2 2:30 3 3:30 4 4:30


Time
Page 25
Interval Partitioning
• Interval partitioning.
– Lecture j starts at sj and finishes at fj.
– Goal: find minimum number of classrooms to schedule all
lectures so that no two occur at the same time in the same
room.

• Ex: This schedule uses only 3.

c d f j

b g i

a e h

9 9:30 10 10:30 11 11:30 12 12:30 1 1:30 2 2:30 3 3:30 4 4:30


Time
Page 26
Interval Partitioning: Lower Bound on Optimal Solution
• Def. The depth of a set of open intervals is the maximum
number of overlapped lectures during the whole period.
• Key observation. Number of classrooms needed  depth.
• Ex: Depth of schedule below = 3  schedule below is optimal.
a, b, c all contain 9:30

• Q. Does there always exist a schedule equal to depth of


intervals?

c d f j

b g i

a e h

9 9:30 10 10:30 11 11:30 12 12:30 1 1:30 2 2:30 3 3:30 4 4:30


Time
Page 27
Interval Partitioning: Greedy Algorithm
• Greedy algorithm. Consider lectures in increasing order of start time: assign lecture
to any compatible classroom (Don’t open any new classroom unless necessary).

Sort intervals by starting time so that s1  s2  ...  sn.


number of allocated classrooms
d  0
for j = 1 to n {
if (lecture j is compatible with some classroom k)
schedule lecture j in classroom k
else
allocate a new classroom d + 1
schedule lecture j in classroom d + 1
d  d + 1
}

• Implementation. O(n log n).


– For each classroom k, maintain the finish time of the last job added.
– Keep the classrooms in a priority queue.

Page 28
Greedy Algorithm:

c d g j
b f i
Depth:aThe maximum No.e of jobs required at
h any time.

Depth
:3

CS4335 Design and Analysis of


03/04/2025 Page 29
Algorithms/WANG Lusheng
Interval Partitioning: Greedy Analysis
• Observation. Greedy algorithm never schedules two incompatible
lectures in the same classroom (its solution is always feasible).
• Theorem. Greedy algorithm is optimal.
• Pf.
– Let d = number of classrooms that the greedy algorithm
allocates.
– Classroom d is opened because we needed to schedule a job,
say j, that is incompatible with all d-1 other classrooms.
– Since we sorted all lectures by start time, all these d-1
classrooms are occupied by d-1 lectures starting no later than sj.
– Thus, we have d lectures overlapping at time sj .
– ddepth
– Key observation  all schedules need to use  depth
classrooms.
Page 30
Greedy Analysis Strategies

• Exchange argument. Gradually transform any


solution to the one found by the greedy algorithm
without hurting its quality.

• Structural. Discover a simple "structural" bound


asserting that every possible solution must have a
certain value. Then show that your algorithm
always achieves this bound.
Page 31

You might also like