You are on page 1of 35

Transform & Conquer

Two Stage Solution Initial Problem

Transform into Another Problem


Solve New Problem New
Representation

Three Variations
Solution
Instance Simplification
Representation Change
Problem Reduction
Reducing the Problem to a Simpler
One
Techniques:
Presorting
Gaussian Elimination
Search Trees

Presorting
Presorting is an old idea, you sort
the data and that allows you to
more easily compute some answer
Element Uniqueness

Unique
Given a list A of n orderable elements,
determine if there are any duplicates
of any element

Brute Force: Presorting:

for each x A Sort A


for each y {A x} for i 1 to n-1
if x = y return not unique if A[i] = A[i+1] return not unique
return unique return unique
Runtime?

Unique

Brute Force Presorting

for each x A Sort A


for each y {A x} for i 1 to n-1
if x = y return not unique if A[i] = A[i+1] return not unique
return unique return unique
Old Idea, Many Different Ways
Efficiency Dependent on Algorithm
Compare Benefits vs. Time Required
Heap, a data structure
Max-Heapify procedure
Building a max-heap
Heapsort

Heap
Max-Heapify
A heap is a nearly complete binary
tree which can be easily implemented
on an array.
1
6
2 3
6 5 3 2 4 1

4 5 6

(Heap)

1
6
2 3
6 5 3 2 4 1

4 5 6
Every level except bottom is complete.
On the bottom, nodes are placed as
left as possible.
1 1

6 6
3 2 3
2

4 5 6
4 5 6

No! Yes!

1 1

6 6
3 2 3
2

4 5 6
4 5 6

No! Yes!
Parent (i )
return i / 2 ;

Left(i )
return 2i;

Right (i )
return 2i 1;

Max-Heap
Min-Heap
In a max - heap, every node i other than the
root satisfies the following property :
A[Parent (i )] A[i].

i Max-Heap

A[Parent (i )] A[i ].
In a min - heap, every node i other than the
root satisfies the following property :
A[Parent (i )] A[i ].

i Min-Heap

A[Parent (i )] A[i ].
Three algorithms associated with heap.
In addition, it is associated with two
more algorithms: Max-Heapify and
Build-Max-Heap.

Heap

Build-Max-Heap Max-Heapify
Max-Heapify(A,i) is a subroutine.
When it is called, two subtrees rooted
at Left(i) and Right(i) are max-heaps,
but A[i] may not satisfy the max-heap
property.
Max-Heapify(A,i) makes the subtree
rooted at A[i] become a max-heap by
letting A[i] float down .

Max-Heapify(A,i)

Right(i) Left(i)
A[i] max-heaps
max-heap
Max-Heapify(A,i)
A[i]
A[i]"
14

14

Max - Heapify ( A, i )
l Left (i );
r Right (i );
if l heap size[ A] and A[l ] A[i ]
then largest l
else largest i;
if r heap size[ A] and A[r ] A[largest ]
then largest r;
if largest i
then begin exchange A[i ] A[largest ];
Max - Heapify ( A, largest );
end - if
Build - Max - Heap ( A)
heap - size[ A] length[ A];
for i length[ A] / 2 downto 1
do Max - Heapify ( A, i );
e.g., 4, 1, 3, 2, 16, 9, 10, 14, 8, 7.

The last location who has a child is n / 2 .


. n/2

2 9
n/2

14 8
n
Build - Max - Heap ( A)
heap - size[ A] length[ A];
for i length[ A] / 2 downto 1
do Max - Heapify ( A, i );
e.g., 4, 1, 3, 2, 16, 9, 10, 14, 8, 7.

2 9

14 8
2 9

14 8

9
14

2 8
14 9

2 8

14 9

2 8
14 9

2 8

14 9

2 8
14 9

2 8

14 9

2 8
4 9

2 8

9
8

2 4
Heapsort( A)
Buid - Max - Heap ( A);
for i length[ A] downto 2
do begin
exchange A[1] A[i ];
heap - size[ A] heap - size[ A] 1;
Max - Heapify ( A,1);
end - for
Input: 4, 1, 3, 2, 16, 9, 10, 14, 8, 7.
Build a max-heap

8 9

2 4

16, 14, 10, 8, 7, 9, 3, 2, 4, 1


9
8

2 4

8 9

2 4

1, 14, 10, 8, 7, 9, 3, 2, 4, 16
9
4

2 1

4 9

2 1

14, 8, 10, 4, 7, 9, 3, 2, 1, 16
9
4

2 14

4 9

2 14

1, 8, 10, 4, 7, 9, 3, 2, 14, 16
1
4

2 14

4 1

2 14

10, 8, 9, 4, 7, 1, 3, 2, 14, 16
1
4

10 14

4 1

10 14

2, 8, 9, 4, 7, 1, 3, 10, 14, 16
1
4

10 14

4 1

10 14

9, 8, 3, 4, 7, 1, 2, 10, 14, 16
1
4

10 14

4 1

10 14
4 8

10 14

1 8

10 14
1 8

10 14

1 8

10 14
4 8

10 14

4 8

10 14
4 8

10 14

4 8

10 14
4 8

10 14

Heapsort( A)
Buid - Max - Heap ( A);
for i length[ A] downto 2 O(n)
do begin
exchange A[1] A[i ];
heap - size[ A] heap - size[ A] 1;
Max - Heapify ( A,1); O(lg n)
end - for O(n lg n)

You might also like