Вы находитесь на странице: 1из 29

Data Structures and Algorithms

(CS210/ESO207/ESO211)

Lecture 33
Greedy strategy: a new algorithm paradigm
Part II
1

Continuing Problem from


last class
JOB Scheduling
Largest subset of non-overlapping
job

A job scheduling problem


Description
INPUT:
A set of jobs {, ,, } where job is specified by two real numbers
s(): start time of job
f(): finish time of job
A single server
Constraint: Server can execute at most one job at any moment of
time.
Aim: To select the largest subset of non-overlapping jobs which can
be executed by the server.

(job is said to be non-overlapping with job if [s(), f()]


)

[s(), f()] =

Notation
Overlap(): the subset consisting of and every job of which
overlaps .
3
Opt(): the size of an optimal solution for .

Designing algorithm for the


problem
strategy:
Intuition:

Select the job with earliest finish


time

Selecting such a job will free the server earliest and hence more no. of jobs
will get scheduled.

Designing algorithm for the


problem
strategy:
Intuition:

Select the job with earliest finish


time

Selecting such a job will free the server earliest and hence more no. of jobs
will get scheduled.

Designing algorithm for the


problem
strategy:
Intuition:

Select the job with earliest finish


time

Selecting such a job will free the server earliest and hence more no. of jobs
will get scheduled.

Designing algorithm for the


problem
strategy:
Intuition:

Select the job with earliest finish


time

Selecting such a job will free the server earliest and hence more no. of jobs
will get scheduled.

Designing algorithm for the


problem
strategy:
Intuition:

Select the job with earliest finish


time

Selecting such a job will free the server earliest and hence more no. of jobs
will get scheduled.

Algorithm earliest finish time


1. Define ;

Algorithm (Input : set of jobs.)


2. While <> do
{

Let be the job from with earliest finish time;


U {};
\Overlap();

}
3. Return ;

Let be the job with earliest finish time.


Lemma1 (last class): There exists an optimal solution for in which
is present.
Let = \Overlap().
Theorem: Opt() = Opt() + 1.

Salient features of
= Opt() + 1.
Theorem: Opt()

the optimal solution of original instance of the problem
the optimal solution of (smaller) instance obtained by
the greedy step

Useful in proving correctness of most of the greedy


algorithms.

Proof has two parts and is a proof by construction


Opt() Opt() + 1
Opt() Opt() - 1
10

Algorithm earliest finish time

Proving Opt() Opt() + 1.


Observation: start time of every job in is greater than finish time of .
Let be any optimal solution for .
None of the jobs in overlaps with .
Hence U{} is a subset of non-overlapping jobs.
Therefore Opt() || + 1 = Opt() + 1.

Overlap()

11

Algorithm earliest finish time

Proving Opt() Opt() - 1.


It follows from Lemma 1 that there exists an optimal solution for
containing .
Let be an optimal solution for containing .
None of the jobs in overlaps with . So every job from other than
belongs to .
Hence \{} is a subset of non-overlapping jobs from .
- 1.
Therefore Opt() || - 1 = Opt()

Overlap()

12


Theorem: Given any set of jobs, the algorithm based on
earliest finish time approach computes the largest subset of
non-overlapping job.

13

O( log ) implementation of the

Algorithm

Algorithm (Input : set of
jobs.)
1. Define ;
2. While <> do
{

Let have earliest finish

time;
U {};
\Overlap();
}
3. Return ;

Sort in the increasing


order of start time.

Maintain a binary min-heap


for based on finish time
as the key.

14

Problem 2
First we shall give motivation.

15

Motivation:
A road or telecommunication
network

Suppose there is a collection of possible links/roads that can


be laid.
But laying down each possible link/road is costly.

Aim: To lay down least number of links/roads to ensure


connectivity between each pair of nodes/cities.16

Motivation
Formal description of the problem
Input: an undirected graph G=(V,E).
Aim: compute a subgraph (V,E), E E such that
Connectivity among all V is guaranteed in the subgraph.
|E| is minimum.

How will such a subgraph look


like ?

17

A road or telecommunication
network

18

A road or telecommunication
network

This is how a sample subgraph (connected and having least no. of


edges) would look like.
Does it remind you of any familiar graph theoretic object ?
19

A tree
The
following definitions are equivalent.

An undirected graph which is connected but does not have


any cycle.

An undirected graph where each pair of vertices has a


unique path between them.

An undirected connected graph on vertices and edges.

An undirected graph on vertices and edges and without


any cycle.
20

A Spanning tree
Definition: For an undirected graph (V,E), spanning tree is a
subgraph (V,E), E E which is a tree.

Observation: Given a spanning tree of a graph , adding a nontree


edge to creates a unique cycle. There will be total such cycles. These
are called fundamental cycles in induced by the spanning tree .
21

A road or telecommunication
network
Assign each edge a weight/cost.

33

54

63

35

70
64

78

31

50
41

53
80

49

170

54 49
44

29

81
52

60

57

42
50

30

102
Adding more reality to the
problem
22

A road or telecommunication
network
170
33

54

63

35

70
64

78

41

53
80

49

31

50

54 49
44

57

81
52

60

42
50

30

102
Any arbitrary 29
spanning tree (like the
one shown above) will not
serve our goal.
We need to select the spanning tree with least weight/cost.
23

Problem 2
Minimum spanning tree

24

Problem Description

Input: an undirected graph G=(V,E) with w: E ,
Aim: compute a spanning tree (V,E), E E such that is
minimum.

25

How to compute a MST ?


170
33

54

63

35

70
64

78

41

53
80

49

31

50

54 49
44

29

81
52

60

57

42
50

30

102

Use the inspiration from the solution of job scheduling


problem.
26

Let be the edge of least weight in the given graph.


Lemma2: There is a MST containing .
Proof: Consider any MST . Let .
Consider the fundamental cycle defined by in .
Swap with any edge present in .

27

Let be the edge of least weight in the given graph.


Lemma2: There is a MST containing .
Proof: Consider any MST . Let .
Consider the fundamental cycle defined by in .
Swap with any edge present in .
We get a spanning tree of weight w().

28

Try to translate Lemma2 to an


algorithm for MST ?
(with the inspiration from the job scheduling
problem)

29

Вам также может понравиться