Docsity

Interval Scheduling and Minimizing Lateness: Greedy Algorithms, Lecture notes of Algorithms and Programming

Partial preview of the text

Download Interval Scheduling and Minimizing Lateness: Greedy Algorithms and more Lecture notes Algorithms and Programming in PDF only on Docsity!

COMP9007 - Algorithms

Greedy algorithms

Presented by Dr. Matloob Khushi School of Computer Science

Week 4 recap: Graphs

Undirected graphs

  • Nodes (vertices)
  • Edges
  • Adjacency list, adjacency matrix
  • Paths, cycles
  • Connectedness, connected component, connected set
  • Trees

Directed graphs

  • Directed paths, directed cycles
  • Strong connectedness, strongly connected component
  • DAGs
  • Topological ordering

Greedy algorithms

A greedy algorithm is an algorithm that follows the problem solving heuristic of making the locally optimal choice at each stage with the hope of finding a global optimum.

Coin change problem

  • Given a set of coins, e.g. {5c, 10c, 20c, 50c}, and
  • an amount of change, e.g. 95c
  • Goal: Find the smallest set of coins which will add up to the change

Coin change greedy algorithm

  • Choose the largest coin less than the amount
  • Subtract the denomination from the amount
  • Repeat until amount=

Coin change greedy algorithm

  • Choose the largest coin less than the amount
  • Subtract the denomination from the amount
  • Repeat until amount=

Only works for some sets of coins!

E.g. Won't work for {20c, 12c, 5c} and change amount $0.

Also won't work for {20c, 12c, 1c} and $0.

Coin change greedy algorithm

Does work in easily identifiable cases

Will always work if each coin is an integer multiple of the next smallest denomination:

e.g. {40c, 20c, 10c, 1c}

Will work for base conversion

e.g. Convert 19 to binary ({…, 16, 8, 4, 2, 1}):

Coin change greedy algorithm

Does work in easily identifiable cases

Will always work if each coin is an integer multiple of the next smallest denomination:

e.g. {40c, 20c, 10c, 1c}

Will work for base conversion

e.g. Convert 19 to binary ({…, 16, 8, 4, 2, 1}):

19 = 16 +

Coin change greedy algorithm

Does work in easily identifiable cases

Will always work if each coin is an integer multiple of the next smallest denomination:

e.g. {40c, 20c, 10c, 1c}

Will work for base conversion

e.g. Convert 19 to binary ({…, 16, 8, 4, 2, 1}):

19 = 16 + 2 + 1

Coin change greedy algorithm

Does work in easily identifiable cases

Will always work if each coin is an integer multiple of the next smallest denomination:

e.g. {40c, 20c, 10c, 1c}

Will work for base conversion

e.g. Convert 19 to binary ({…, 16, 8, 4, 2, 1}):

19 = 16 + 2 + 1 = 10011 2

Interval Scheduling

  • Interval scheduling.
    • Input: Set of n jobs. Each job i starts at time s (^) j and finishes at time f (^) j.
    • Two jobs are compatible if they don't overlap in time.
    • Goal: find maximum subset of mutually compatible jobs.

0 1 2 3 4 5 6 7 8 9 10 11 Time

f g h

e

a b c d

Interval Scheduling

  • Interval scheduling.
    • Input: Set of n jobs. Each job i starts at time s (^) j and finishes at time f (^) j.
    • Two jobs are compatible if they don't overlap in time.
    • Goal: find maximum subset of mutually compatible jobs.

0 1 2 3 4 5 6 7 8 9 10 11 Time

f g h

e

a b c d

Interval Scheduling - [Earliest start time]

Time 0

A

C

F

B

D

G

E

1 2 3 4 5 6 7 8 9 10 11

H

0 1 2 3 4 5 6 7 8 9 10 11

Interval Scheduling - [Earliest start time]

Time 0

A

C

F

B

D

G

E

1 2 3 4 5 6 7 8 9 10 11

H

0 1 2 3 4 5 6 7 8 9 10 11