Professional Documents
Culture Documents
Data Structures IITK
Data Structures IITK
(CS210/ESO207/ESO211)
Lecture 36
Sorting
beyond O(n log n) bound
1
sort:
Insertion
O()
Selection sort:
O()
Bubble sort:
O()
Merge sort:
O( log )
Quick sort: worst case O(), average case O( log )
Heap sort:
O( log )
Integer sorting
2A
Count 2
1
Place
2
2A
Count 2
1
Place
2
2A
Count 2
1
Place
1
Algorithm (A[...], )
For =0 to do Count[] 0;
For =0 to do Count[A[]] Count[A[]] +1;
For =0 to do Place[] Count[];
For =1 to do Place[] Place[] + Count[];
For = to do
{
??
] A[];
B[
Place[A[]]-1
Place[A[]] Place[A[]]-1;
}
return B;
Practice sheet 6
We shall solve exercises 5 and 1 from
this sheet
I*
I*
I
I
The problem is that some deleted interval (in this case I) could have
been used for intersecting many intervals if it were not deleted. But
deleting it from the instance disallows it to be selected in the solution.
An Observation
We can delete all intervals whose finish time is before finish time of I because
any interval overlapped by such intervals will anyway be overlapped by I. Let
us consider intervals which overlap with I, but have finish time greater than
that of I. In the example shown below, these intervals are those three intervals
which cross the red line.
I*
I
I
Observation1: Among the intervals crossing the red line, we need to keep only
that interval which has maximum finish time. (I in this picture)
Proof: Notice that each of these intervals are anyway intersected by I. As far
as using them to intersect other intervals in concerned, we may better choose
I for this purpose.
So from now onwards, we shall assume that there is exactly one interval I in A
which overlaps I (intersects the red line) and has finish time larger than I.
I1
I2
Constructing A from A
Constructing A from A
I*
I
I
Weneed
can partition
We
to take these
intervals
into two sets.
care
of intervals
Case1:
There
D:Now
those
which
overlap
whose
starting
we
shall describe
I.
point
iswith
to the
the
two
cases for
E: those
that
start after
right
of red
line
construction
of A.
the end
of I
and hence
(finish
time
of I).
do not overlap with I.
D
E
is an interval I D uniquely covered by I
I
A
Constructing A from A
Constructing A from A
I*
I
I
D
Constructing A from A
Case1:
I*
I
I
D
I
I
Case1:
I*
I
I
D
I
A
Case2:
I*
I
I
D
Consider
So
we just any
needoptimal
to take care of
solution for
A.ANote
that
intervals
from
which
intersect
thisred
optimal
solution
the
line. These
are taken
takes
of D and
carecare
by adding
I E.
to this
solution. We are done.
Case2:
I*
I
I
D
Using
If I isLemma1,
not in this itoptimal
follows
that
there
ancase
we
canisthe
see
that
Sosolution,
let us
consider
optimal
for
Aoptimal
removing
I
thisin
when Isolution
is from
present
the
The
problem
is
that
I
is the
not
Notice
that
I
can
serve
solution
gives
a
valid
solution
optimal
containing
solution
I
.
of
A.
We
replace in
I A,
by the
interval
from
present
so we
need
a D
purpose
of
overlapping
of
which
intersects
the
violet A.
line and
for
A
substitute
of D
I.only.
from
intervals
from
has earliest
start
time. So
Seewe
the
should
search
for
substitute
following slide for its justification.
for I from D only.
Theorem:
There is a polynomial time algorithm for computing smallest
subset of intervals overlapping a given set of intervals.
Problem 1