Title: Lecture 2, Jan 12
1Lecture 2, Jan 12
2CSC373 Algorithm Design and Analysis
- Office hours Wed 5-6 pm, starting next week.
- Tutorials start today 8-9pm.
- Assignment 1.
3Review
- Last time have seen the interval scheduling
problem.
4Interval Scheduling
- Ingredients
- Instances Activities with starting and finishing
times ltlts1,f1gt,lts2,f2gt, ,ltsn,fngtgt. - Solutions A set of events that do not overlap.
- Cost of Solution The number of activities
scheduled. - Goal Given a set of activities, schedule as many
as possible.
5The greedy algorithm
Greedy Criteria
Earliest Finishing Time
Schedule the event who will free up your room
for someone else as soon as possible.
Motivation
6Outline of the algorithm
- Order the activities in non-decreasing order of
finishing times. Denote it by A1, A2, , An. - For i1,,n
- If Ai does not conflict with previous choices,
include it in the schedule.
7Outline of the proof generic greedy algorithm
- Since there is not backtracking in the greedy
algorithms, we must show that the algorithm never
makes irreversible mistakes. - In other words, the partial solution the
algorithm currently holds can be extended to an
optimal solution. - At the end, when the algorithm holds a solution,
it must be optimal (a solution is a partial
solution).
8Generic greedy algorithms contd
- We say that a partial solution is promising, if
it can be extended to an optimal solution. - Prove by induction the loop invariant
- any partial solution the algorithm holds is
promising - In the interval scheduling case, we proved that
the interval selections Si for the first i
intervals is promising.
9Generic greedy algorithms contd
- The induction step is usually done using a
replacement argument. - Massage the optimal solution extending the i-th
step to extend the i1-st step. - For interval scheduling, if the solution Si was
not extending Si1, it was modified by replacing
some interval in Si by an interval the algorithm
has chosen to obtain Si1.
10Generic greedy algorithms contd
- Finally, use the fact that the last partial
solution is promising to show that the algorithm
works. - For interval scheduling, the solution Sn (which
includes all the choices that algorithm made
about all the intervals) is promising, and hence
is optimal.
11Minimum Spanning Trees
- Example Problem
- You are planning a new terrestrial
telecommunications network to connect a number of
remote mountain villages in a developing country.
- The cost of building a link between pairs of
neighboring villages (u,v) has been estimated
w(u,v). - You seek the minimum cost design that ensures
each village is connected to the network. - The solution is called a minimum spanning tree.
12Properties of a Minimum Spanning Tree
- V-1 edges
- Acyclic
- Not necessarily unique
13A greedy strategy for MST Kruskals algorithm
- The idea
- expand the network at minimal cost
- Throw in the lightest edge that does not close a
cycle
14Example
15Kruskals Algorithm for computing MST
O(ElogE)
Running Time
O(ElogV)
16Kruskals algorithm correctness proof plan
- The algorithm obviously terminates
- show that at each step the partial solution the
algorithm holds is promising - can be extended to an MST
- when the algorithm terminates, show that the
solution it holds is a tree - it is promising, hence it must be an MST.
17Prims Algorithm
- A different greedy strategy.
- Build the net by choosing the cheapest link to
connect next.
18Prims Algorithm contd
- Build one tree
- Start from arbitrary root r
- At each step, add lightest edge crossing cut (VA,
V- VA), where VA vertices processed so far.
19Finding light edges quickly
- Use priority queue
- Each object in the queue is a vertex v that is
still waiting to be connected (in V-VA) - Key of v is minimum weight of any edge (u,v), u?
VA. - Each vertex knows its parent in tree by ?v.
20Prims Algorithm
O(Elog(V))
Running Time
21Prims algorithm correctness proof plan
- The algorithm obviously terminates
- show that at each step the partial solution the
algorithm holds is promising - can be extended to an MST
- when the algorithm terminates, show that the
solution it holds is a tree - it is promising, hence it must be an MST.
22Three Knapsack Examples
- 0-1 knapsack with variable value/weight
- 0-1 knapsack with fixed value/weight
- Fractional knapsack with variable value/weight