Lecture 2, Jan 12 - PowerPoint PPT Presentation

1 / 22
About This Presentation
Title:

Lecture 2, Jan 12

Description:

Instances: Activities with starting and finishing times s1,f1 , s2,f2 ,... , sn,fn ... weight of any edge (u,v), u VA. Each vertex knows its parent in tree ... – PowerPoint PPT presentation

Number of Views:31
Avg rating:3.0/5.0
Slides: 23
Provided by: jeffed
Category:

less

Transcript and Presenter's Notes

Title: Lecture 2, Jan 12


1
Lecture 2, Jan 12
2
CSC373 Algorithm Design and Analysis
  • Office hours Wed 5-6 pm, starting next week.
  • Tutorials start today 8-9pm.
  • Assignment 1.

3
Review
  • Last time have seen the interval scheduling
    problem.

4
Interval 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.

5
The greedy algorithm
Greedy Criteria
Earliest Finishing Time
Schedule the event who will free up your room
for someone else as soon as possible.
Motivation
6
Outline 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.

7
Outline 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).

8
Generic 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.

9
Generic 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.

10
Generic 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.

11
Minimum 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.

12
Properties of a Minimum Spanning Tree
  • V-1 edges
  • Acyclic
  • Not necessarily unique

13
A 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

14
Example
  • Kruskals Algorithm

15
Kruskals Algorithm for computing MST
O(ElogE)
Running Time
O(ElogV)
16
Kruskals 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.

17
Prims Algorithm
  • A different greedy strategy.
  • Build the net by choosing the cheapest link to
    connect next.

18
Prims 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.

19
Finding 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.

20
Prims Algorithm
O(Elog(V))
Running Time
21
Prims 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.

22
Three Knapsack Examples
  • 0-1 knapsack with variable value/weight
  • 0-1 knapsack with fixed value/weight
  • Fractional knapsack with variable value/weight
Write a Comment
User Comments (0)
About PowerShow.com