Professional Documents
Culture Documents
Divide and
conquer, Abstract
Data Types, Lists,
Iterators, Heaps
Todd Veldhuizen
tveldhui@acm.org
ECE750 Lecture 3: Divide and conquer,
Abstract Data Types, Lists, Iterators, Heaps
Todd Veldhuizen
tveldhui@acm.org
Electrical & Computer Engineering
University of Waterloo
Canada
Sept. 28, 2007
1 / 68
ECE750 Lecture 3:
Divide and
conquer, Abstract
Data Types, Lists,
Iterators, Heaps
Todd Veldhuizen
tveldhui@acm.org
Part I
Divide and conquer
2 / 68
ECE750 Lecture 3:
Divide and
conquer, Abstract
Data Types, Lists,
Iterators, Heaps
Todd Veldhuizen
tveldhui@acm.org
Divide And Conquer
BinarySearch is an (arguably degenerate) instance of a
basic algorithm design pattern:
Divide And Conquer
1. If the problem is of trivial size, solve it and return.
2. Otherwise:
2.1 Divide the problem into several problems of smaller size.
2.2 Conquer these smaller problems by recursively applying
the divide and conquer pattern.
2.3 Combine the answers to the smaller problems into an
answer to the whole problem.
3 / 68
ECE750 Lecture 3:
Divide and
conquer, Abstract
Data Types, Lists,
Iterators, Heaps
Todd Veldhuizen
tveldhui@acm.org
Divide and Conquer
Binary Search as Divide-and-Conquer:
1. If the problem is of size n = 1, answer is obvious
return.
2. Otherwise:
Search the two halves: for one half, call self recursively;
for other half, answer is false.
A landmark paper:
Volker Strassen, Gaussian Elimination is not optimal
(1969). [1]
An o(n
3
) divide-and-conquer approach to matrix
multiplication.
6 / 68
ECE750 Lecture 3:
Divide and
conquer, Abstract
Data Types, Lists,
Iterators, Heaps
Todd Veldhuizen
tveldhui@acm.org
Strassens method
Strassen: If A, B are matrices of order m2
k+1
to be
multiplied, write
A =
A
11
A
12
A
21
A
22
B =
B
11
B
12
B
21
B
22
C =
C
11
C
12
C
21
C
22
where the A
ik
, B
ik
, C
ik
matrices are of order m2
k
...
7 / 68
ECE750 Lecture 3:
Divide and
conquer, Abstract
Data Types, Lists,
Iterators, Heaps
Todd Veldhuizen
tveldhui@acm.org
Strassens method
Then compute
(7 subproblems)
I = (A
11
+ A
22
)(B
11
+ B
22
)
II = (A
21
+ A
22
)B
11
III = A
11
(B
12
B
22
)
IV = A
22
(B
11
+ B
21
)
V = (A
11
+ A
12
)B
22
VI = (A
11
+ A
21
)(B
11
+ B
12
)
VII = (A
12
A
22
)(B
21
+ B
22
)
(and combine)
C
11
= I + IV V + VII
C
21
= II + IV
C
12
= III + V
C
22
= I + III II + VI
8 / 68
ECE750 Lecture 3:
Divide and
conquer, Abstract
Data Types, Lists,
Iterators, Heaps
Todd Veldhuizen
tveldhui@acm.org
Strassens method
1. Subproblems: compute 7 matrix multiplications of size
n/2.
2. Constructing the subproblems and combining the
answers is done with matrix additions/subtractions,
taking (n
2
) time.
3. Apply general divide-and-conquer recurrence with
k = 7, s(n) = n/2, f (n) = (n
2
):
T(n) = c + 7T(n/2) + (n
2
)
9 / 68
ECE750 Lecture 3:
Divide and
conquer, Abstract
Data Types, Lists,
Iterators, Heaps
Todd Veldhuizen
tveldhui@acm.org
Strassens method
Recall that (n
2
) means some function f (n) for which
f (n)
n
2
is eventually restricted to some nite positive interval
[c
1
, c
2
].
Pick a value c > c
2
; then eventually f (n) cn
2
.
Solve recurrence:
T(n) = c + 7T(n/2) + cn
2
This will give an asymptotically correct bound, but possibly
T(n) is less than the actual time required for small n.
10 / 68
ECE750 Lecture 3:
Divide and
conquer, Abstract
Data Types, Lists,
Iterators, Heaps
Todd Veldhuizen
tveldhui@acm.org
Strassens method
Let r () = T(2
), and = log
2
n. Recurrence becomes
r () = c + 7r ( 1) + c(2
)
2
= c + 7r ( 1) + c(4
Z-transform:
Z [c] =
c
1 z
1
Z [7r ( 1)] = 7z
1
R(z)
Z [c(4
)] =
c
1 4z
1
Solve:
R(z) =
c2(1
5
2
z
1
)
(1 z
1
)(1 4z
1
)(1 7z
1
)
11 / 68
ECE750 Lecture 3:
Divide and
conquer, Abstract
Data Types, Lists,
Iterators, Heaps
Todd Veldhuizen
tveldhui@acm.org
Strassens method
(n) = kT
(n/s) + cn
d
(2)
(n).
with N, we can
change variables to turn Eqn. (2) into:
r () = kr ( 1) + c(s
d
)
13 / 68
ECE750 Lecture 3:
Divide and
conquer, Abstract
Data Types, Lists,
Iterators, Heaps
Todd Veldhuizen
tveldhui@acm.org
Divide-and-conquer recurrences II
Z-transform:
R(z) = kz
1
R(z) +
c
1 s
d
z
1
Solve:
R(z) =
c
(1 kz
1
)(1 s
d
z
1
)
Three cases: s
d
< k, s
d
= k, s
d
> k.
1. s
d
< k. Then the dominant pole is z = k. Get
r () k
Since n = s
, use = log
s
n:
T
(n) k
log
s
n
= (2
log k
)
log n
log s
= (2
log n
)
log k
log s
= n
log k
log s
14 / 68
ECE750 Lecture 3:
Divide and
conquer, Abstract
Data Types, Lists,
Iterators, Heaps
Todd Veldhuizen
tveldhui@acm.org
Divide-and-conquer recurrences III
2. s
d
= k. Then get a double pole at z = k. Recall that
Z
1
kz
1
(1 kz
1
)
2
= k
End up with
r () k
(n) (log
s
n)k
log
s
n
(log n)2
log klog n
log s
= n
log k
log s
log n
= n
log s
d
log s
log n = n
d
log n
3. s
d
> k. Then dominant singularity is z = s
d
. Get
r () (s
d
)
(n) (s
d
)
log
s
n
= (2
d log s
)
log n
log s
= n
d
15 / 68
ECE750 Lecture 3:
Divide and
conquer, Abstract
Data Types, Lists,
Iterators, Heaps
Todd Veldhuizen
tveldhui@acm.org
Master Theorem
Theorem (Master)
The solution to a divide-and-conquer recurrence of the form
T(n) = kT(n/s|) + (n
d
)
where s > 1, is
T(n) =
n
log k
log s
if s
d
< k
n
d
log n
if s
d
= k
(n
d
) if s
d
> k
Examples:
An ADT consists of
1. Some types that may be required to provide operations,
relations, and satisfy properties. e.g. a totally ordered
set.
2. An interface: the capabilities provided by the ADT.
18 / 68
ECE750 Lecture 3:
Divide and
conquer, Abstract
Data Types, Lists,
Iterators, Heaps
Todd Veldhuizen
tveldhui@acm.org
Abstract Data Types
Some relations R
1
, R
2
, .
This is the standard notation for a structure in logic:
could be
Types:
Operations:
insert frequently?
delete frequently?
search frequently?
Dierent ADT implementations may oer dierent
performance characteristics. By understanding the
typical mixture of operations you can choose an
implementation with the most suitable performance
characteristics.
24 / 68
ECE750 Lecture 3:
Divide and
conquer, Abstract
Data Types, Lists,
Iterators, Heaps
Todd Veldhuizen
tveldhui@acm.org
ADT: Array[V]
Array[V]
Types :
Operations :
Types:
Operations:
Types:
Operations:
Last In, First Out (LIFO): the item added most recently
goes next. A stack: like an in-box of work where new
items are placed on the top, and what ever is on the top
of the stack gets processed next.
Types :
Operations :
Types :
Operations :
Types :
Operations :
class Node<T>
T data; // Data item
Node<T> next; // Next node in the list , if any
Node(T data, Node<T> next)
data = data;
next = next;
34 / 68
ECE750 Lecture 3:
Divide and
conquer, Abstract
Data Types, Lists,
Iterators, Heaps
Todd Veldhuizen
tveldhui@acm.org
Data Structure: Linked List I
if (head == null)
throw new RuntimeException(List is empty.);
else
T data = head.data;
head = head.next;
return data;
35 / 68
ECE750 Lecture 3:
Divide and
conquer, Abstract
Data Types, Lists,
Iterators, Heaps
Todd Veldhuizen
tveldhui@acm.org
Data Structure: Linked List II
36 / 68
ECE750 Lecture 3:
Divide and
conquer, Abstract
Data Types, Lists,
Iterators, Heaps
Todd Veldhuizen
tveldhui@acm.org
Implementing Stack[V] with a Linked List
Types:
Operations:
if (node == null)
throw new RuntimeException(Tried to iterate past end of list );
T data = node.data;
node = node.next;
return data;
39 / 68
ECE750 Lecture 3:
Divide and
conquer, Abstract
Data Types, Lists,
Iterators, Heaps
Todd Veldhuizen
tveldhui@acm.org
Iterators
Example usage of an iterator:
ListIterator <Integer> iter = new ListIterator <Integer>( list );
while ( iter . hasNext())
40 / 68
ECE750 Lecture 3:
Divide and
conquer, Abstract
Data Types, Lists,
Iterators, Heaps
Todd Veldhuizen
tveldhui@acm.org
Implementing Queue[V] with a Linked List
data = data;
next = next;
next. prev = this;
42 / 68
ECE750 Lecture 3:
Divide and
conquer, Abstract
Data Types, Lists,
Iterators, Heaps
Todd Veldhuizen
tveldhui@acm.org
Bidirectional Linked List
43 / 68
ECE750 Lecture 3:
Divide and
conquer, Abstract
Data Types, Lists,
Iterators, Heaps
Todd Veldhuizen
tveldhui@acm.org
Bidirectional Linked List
public class BiLinkedList <T>
BiNode<T> head; // Pointer to the rst node in the list
BiNode<T> tail; // Pointer to the last node in the list
public void insert (T data)
44 / 68
ECE750 Lecture 3:
Divide and
conquer, Abstract
Data Types, Lists,
Iterators, Heaps
Todd Veldhuizen
tveldhui@acm.org
Bidirectional Linked List
public T removeLast()
if ( tail == null)
throw new RuntimeException(List is empty.);
else
T data = tail . data;
if ( tail . prev == null)
head = null;
tail = null;
else
tail = tail . prev;
tail . next = null;
return data;
45 / 68
ECE750 Lecture 3:
Divide and
conquer, Abstract
Data Types, Lists,
Iterators, Heaps
Todd Veldhuizen
tveldhui@acm.org
Implementing a Queue < V > with a
Bidirectional Linked List
public class Queue<V>
BiLinkedList <V> list;
public void insert (V v) list . insert (v);
public boolean isEmpty() return list . isEmpty();
public V next() return list . removeLast();
data = data;
next = null;
prev = null;
49 / 68
ECE750 Lecture 3:
Divide and
conquer, Abstract
Data Types, Lists,
Iterators, Heaps
Todd Veldhuizen
tveldhui@acm.org
Bidirectional Linked List: Invariants
56 / 68
ECE750 Lecture 3:
Divide and
conquer, Abstract
Data Types, Lists,
Iterators, Heaps
Todd Veldhuizen
tveldhui@acm.org
Bidirectional Linked List: Implementation
public void insertBack(T data)
57 / 68
ECE750 Lecture 3:
Divide and
conquer, Abstract
Data Types, Lists,
Iterators, Heaps
Todd Veldhuizen
tveldhui@acm.org
Bidirectional Linked List: Implementation
public T removeFront()
return data;
58 / 68
ECE750 Lecture 3:
Divide and
conquer, Abstract
Data Types, Lists,
Iterators, Heaps
Todd Veldhuizen
tveldhui@acm.org
Bidirectional Linked List: Implementation
public T removeBack()
return data;
59 / 68
ECE750 Lecture 3:
Divide and
conquer, Abstract
Data Types, Lists,
Iterators, Heaps
Todd Veldhuizen
tveldhui@acm.org
Bibliography
Part IV
Heaps
60 / 68
ECE750 Lecture 3:
Divide and
conquer, Abstract
Data Types, Lists,
Iterators, Heaps
Todd Veldhuizen
tveldhui@acm.org
Bibliography
Heaps
Parent(j ) = j /2|
LeftChild(i ) = 2 i
RightChild(i ) = 2 i + 1
Choose the largest of the root and its two children, and
make this the root; perform this procedure recursively
until the heap invariant is satised.
64 / 68
ECE750 Lecture 3:
Divide and
conquer, Abstract
Data Types, Lists,
Iterators, Heaps
Todd Veldhuizen
tveldhui@acm.org
Bibliography
Heap: insert example