Lecture6 - Analysing Recursive Algorithms - Divide and Conquer; Sorting
Transcript of Lecture6 - Analysing Recursive Algorithms - Divide and Conquer; Sorting
-
8/14/2019 Lecture6 - Analysing Recursive Algorithms - Divide and Conquer; Sorting
1/38
COMP2230 Introduction toAlgorithmics
Lecture 6
A/Prof Ljiljana Brankovic
-
8/14/2019 Lecture6 - Analysing Recursive Algorithms - Divide and Conquer; Sorting
2/38
Lecture Overview
Revision from Lecture 3:Analysis of Recursive Algorithms
Divide and ConquerText, Chapter 5, Sections 5.1 and 5.2
Sorting Algorithms
Text, Chapter 6
Next week: Sorting Algorithms, Text, Chapter 6;Greedy Algorithms, Text, Chapter 7
-
8/14/2019 Lecture6 - Analysing Recursive Algorithms - Divide and Conquer; Sorting
3/38
Revision from Lecture 3:Analysis of Recursive Algorithms
We shall not try to calculate the exact time needed toexecute an algorithm; this would be very difficult todo, and it would depend on the implementation,platform, etc.
We shall only be estimatingthe time needed foralgorithm execution, as a function of the size of theinput.
We shall estimate the running time by counting somedominant instructions.
A barometerinstruction is an instruction that isexecuted at least as often as any other instruction in
the algorithm.
-
8/14/2019 Lecture6 - Analysing Recursive Algorithms - Divide and Conquer; Sorting
4/38
Example 2.4.3
example(n) {if (n== 1)
return
for i= 1 to nx= x+ 1
example(n/2)}
Recurrence relation:C1= 0,
Cn = n + Cn/2, n>1
-
8/14/2019 Lecture6 - Analysing Recursive Algorithms - Divide and Conquer; Sorting
5/38
Example: Towers of HanoiAfter creating the world, God set on Earth 3 diamond rods and 64
golden rings, all of different size. All the rings were initially onthe first rod, in order of size, the smallest at the top. God alsocreated a monastery nearby where monks task in life is totransfer all the rings onto the second rod; they are only allowedto move a single ring from one rod to another at the time, and a
ring can never be placed on top of another smaller ring.According to the legend, when monks have finished their task,the world will come to an end.
If monks move one ring per second and never stop, it will take
them more that 500,000 million years to finish the job (morethan 25 times the estimated age of the universe!).
-
8/14/2019 Lecture6 - Analysing Recursive Algorithms - Divide and Conquer; Sorting
6/38
Example: Towers of Hanoi
The following is a way to move 3 rings from rod1 to rod 2.
-
8/14/2019 Lecture6 - Analysing Recursive Algorithms - Divide and Conquer; Sorting
7/38
Example: Towers of Hanoi
The following is an algorithm that moves mrings from rod i to rod j.
Hanoi (m,i,j){
\\Moves the msmallest rings from rod ito rod j
ifm > 0then{
Hanoi(m-1,i,6-i-j)
writei jHanoi(m-1,6-i-j,j)
}
}
-
8/14/2019 Lecture6 - Analysing Recursive Algorithms - Divide and Conquer; Sorting
8/38
Example: Towers of Hanoi
Recurrence relation:
Or, equivalently,t0= 0
tn= 2tn-1+ 1, n > 0
0if,0
otherwise,1)1(2)(
m
mtmt
-
8/14/2019 Lecture6 - Analysing Recursive Algorithms - Divide and Conquer; Sorting
9/38
Example: Fibonacci sequenceSuppose that at the beginning of the year there is one pair of
rabbits and that every month each pair produces a new pairthat becomes productive after one month. Suppose furtherthat no deaths occur. Let andenote the number of rabbitsat the end of nth month. Show that an= fn+1, n 1.
Months Pairs Comment
1 1 The pair did not multiply because it is not productiveyet.
2 2 The first pair became productive and multiplied
3 3 Only first pair multiplied
n-2 an-2
n-1 an-1
n an-1+
an-2
Each pair that was alive 2 months ago reproduced
-
8/14/2019 Lecture6 - Analysing Recursive Algorithms - Divide and Conquer; Sorting
10/38
Example: Fibonacci sequence
Fibonacci(n){
\\Calculatesthe nthFibonacci number
ifn 0
http://localhost/var/www/apps/conversion/tmp/scratch_10//Calculateshttp://localhost/var/www/apps/conversion/tmp/scratch_10//Calculateshttp://localhost/var/www/apps/conversion/tmp/scratch_10//Calculateshttp://localhost/var/www/apps/conversion/tmp/scratch_10//Calculates -
8/14/2019 Lecture6 - Analysing Recursive Algorithms - Divide and Conquer; Sorting
11/38
More Examples
Factorial(n){ifn=0 return1
elsereturnFactorial(n-1)n
}
Write a recurrence relation!
t0
= 0
tn= tn-1+ 1, n > 0
-
8/14/2019 Lecture6 - Analysing Recursive Algorithms - Divide and Conquer; Sorting
12/38
-
8/14/2019 Lecture6 - Analysing Recursive Algorithms - Divide and Conquer; Sorting
13/38
Divide and Conquer
Divide and Conquer is a basic algorithmic designtechnique that uses recursion to split large problemsinto smaller sub-problems that can be solved easily.
Works with two basic steps:If the problem is small enough, solve it.Otherwise split the problem into smaller instances of
the same problem and recurse.
Not every problem can be solved with thistechnique.
-
8/14/2019 Lecture6 - Analysing Recursive Algorithms - Divide and Conquer; Sorting
14/38
Divide and Conquer
Subproblem1of size n/2
Merge the two solutions into
a solution of the original problem
Find a solution to
subproblem1
Find a solution to
subproblem2
Subproblem2of size n/2
Problem
of size n
-
8/14/2019 Lecture6 - Analysing Recursive Algorithms - Divide and Conquer; Sorting
15/38
Divide and Conquer
In general, the problem will be divided into bsubproblems of size n/b and a of theseproblems will need to be solved; f(n) is the
time required to merge the solutions tosubproblems into a solution of the originalproblem.
T(n) = aT(n/b) + f(n)
-
8/14/2019 Lecture6 - Analysing Recursive Algorithms - Divide and Conquer; Sorting
16/38
Divide and Conquer
T(n) = aT(n/b) + f(n)
To solve this recurrence relation, we use MasterTheorem:
If T(n) = aT(n/b) + f(n)and f(n) = (n k)then
(n k) if a < b k
T(n) = (n k log n) if a = b k
(n logba) if a > b k
-
8/14/2019 Lecture6 - Analysing Recursive Algorithms - Divide and Conquer; Sorting
17/38
Tiling a Deficient Plane
Imagine we have a set of L shaped tiles and aplane with one square missing.
How might we use a D & C approach to tile theplane?
-
8/14/2019 Lecture6 - Analysing Recursive Algorithms - Divide and Conquer; Sorting
18/38
Tiling a Deficient Plane
-
8/14/2019 Lecture6 - Analysing Recursive Algorithms - Divide and Conquer; Sorting
19/38
Algorithm 5.1.4 Tiling a Deficient Board withTrominoes
This algorithm constructs a tiling by trominoes of a deficient nnboard where
nis a power of 2.Input Parameters: n, a power of 2 (the board size);
the location Lof the missing squareOutput Parameters: Nonetile(n,L) {
if (n== 2) {
// the board is a right tromino Ttile with Treturn
}divide the board into four n/2 n/2 subboardsplace one tromino as in Figure 5.1.4(b)// each of the 1 1 squares in this tromino
// is considered as missinglet m1,m2,m3,m4be the locations of the missing squarestile(n/2,m1)tile(n/2,m2)tile(n/2,m3)tile(n/2,m4)
}
Write the corresponding recurrence relation!
-
8/14/2019 Lecture6 - Analysing Recursive Algorithms - Divide and Conquer; Sorting
20/38
MergeSort
10 36 40 12 1 7 30 9 44 15
Split
10 36 40 12 1 7 30 9 44 15
10 36 40 12 17 30 9 44 15
10 36 40 12 1
112
7 30 9 44 15
44 15
-
8/14/2019 Lecture6 - Analysing Recursive Algorithms - Divide and Conquer; Sorting
21/38
MergeSort
10 36 40 12 1 7 30 9 44 15
Split
10 36 40 12 1 7 30 9 44 15
10 36 40 12 1 7 30 9 44 15
10 36 40 12 1
112
7 30 9 44 15
44 15
1 12 15 44
10 361 12 40 7 30 9 15 44
1 10 12 36 40 7 9 15 30 44
1 7 9 10 12 15 30 36 40 44
-
8/14/2019 Lecture6 - Analysing Recursive Algorithms - Divide and Conquer; Sorting
22/38
How do we merge?
1 10 12 36 40 7 9 15 30 44
1
1 10 12 36 40 7 9 15 30 44
1 7
-
8/14/2019 Lecture6 - Analysing Recursive Algorithms - Divide and Conquer; Sorting
23/38
1 10 12 36 40 7 9 15 30 44
1 7 9
1 10 12 36 40 7 9 15 30 44
1 9 107
1 10 12 36 40 7 9 15 30 44
1 9 10 127
-
8/14/2019 Lecture6 - Analysing Recursive Algorithms - Divide and Conquer; Sorting
24/38
1 10 12 36 40 7 9 15 30 44
1 7 9 10 12 15
1 10 12 36 40 7 9 15 30 44
1 9 10 12 15 307
1 10 12 36 40 7 9 15 30 44
1 9 10 12 15 30 367
-
8/14/2019 Lecture6 - Analysing Recursive Algorithms - Divide and Conquer; Sorting
25/38
1 10 12 36 40 7 9 15 30 44
1 7 9 10 12 15 30 36 40
1 10 12 36 40 7 9 15 30 44
1 9 10 12 15 30 36 40 447
1 10 12 36 40 7 9 15 30 44
1 9 10 12 15 30 36 40 447
-
8/14/2019 Lecture6 - Analysing Recursive Algorithms - Divide and Conquer; Sorting
26/38
Algorithm 5.2.2 MergeThis algorithm receives as input indexes i, m, and j, and an array a, wherea[i], ... , a[m] and a[m+1], ... , a[j] are each sorted in nondecreasing order.These two nondecreasing subarrays are merged into a single nondecreasing
array.Input Parameters: a,i,m,jOutput Parameter: amerge(a,i,m,j) {
p= i// index in a[i], ... , a[m]q= m+ 1 // index in a[m+ 1], ... , a[j]
r= i// index in a local array cwhile (p m&& q j) {
// copy smaller value to cif (a[p] a[q]) {
c[r] = a[p]p= p+ 1
}
else {c[r] = a[q]q= q+ 1
}r= r+ 1
}
-
8/14/2019 Lecture6 - Analysing Recursive Algorithms - Divide and Conquer; Sorting
27/38
// copy remainder, if any, of first subarray to c
while (p m) {c[r] = a[p]p= p+ 1r= r+ 1
}// copy remainder, if any, of second subarray to c
while (q j) {c[r] = a[q]q= q+ 1r= r+ 1
}// copy cback to a
for r= ito ja[r] = c[r]
}
-
8/14/2019 Lecture6 - Analysing Recursive Algorithms - Divide and Conquer; Sorting
28/38
Algorithm 5.2.3 MergesortThis algorithm sorts the array a[i], ... , a[j] in nondecreasing order. It uses
the merge algorithm (Algorithm 5.2.2).
Input Parameters: a,i,jOutput Parameter: amergesort(a,i,j) {
// if only one element, just return
if (i== j)return
// divide a into two nearly equal partsm= (i+ j)/2// sort each halfmergesort(a,i,m)mergesort(a,m+ 1,j)// merge the two sorted halvesmerge(a,i,m,j)
}
-
8/14/2019 Lecture6 - Analysing Recursive Algorithms - Divide and Conquer; Sorting
29/38
Stable sort algorithms
A sorting algorithm is stable if it preservesthe original ordering of equal elements.
Mergesort is stable.
-
8/14/2019 Lecture6 - Analysing Recursive Algorithms - Divide and Conquer; Sorting
30/38
Bubble Sort
Very, very simple sorting algorithm.
Works by swapping adjacent elements iftheyre out of order.
Starts at the end, bubbling the lowestelement down, incrementally working onsmaller sections of the array.
Can work the other way of course, taking thehighest to the end.
-
8/14/2019 Lecture6 - Analysing Recursive Algorithms - Divide and Conquer; Sorting
31/38
Bubble Sort
bubbleSort(int[] A){
for (int i = 0; i < A.length; i++){
for (int j = A.length-1; j > i; j--){
if (A[j] < A[j-1]){
int temp = A[j];
A[j] = A[j-1];
A[j-1] = temp;
}
}
}
}
-
8/14/2019 Lecture6 - Analysing Recursive Algorithms - Divide and Conquer; Sorting
32/38
Bubble Sort
4 10 3 1 5i
j
4 10 3 1 5
i j
4 10 1 3 5
i j
4 1 10 3 5
i j
1 4 10 3 5
i j
1 4 10 3 5
i j
1 4 10 3 5
i j
1 4 3 10 5
i j
1 3 4 10 5
i j
1 3 4 10 5
1 3 4 5 10
i j
1 3 4 5 10
i j
i j
-
8/14/2019 Lecture6 - Analysing Recursive Algorithms - Divide and Conquer; Sorting
33/38
Bubble Sort
Worst case time T(n2).
Easy to see from two loops.
What is the worst possible input?
Bubble sort is not commonly used in practice,though it can be sped up through variousmethods, but this does not change theasymptotic time.
-
8/14/2019 Lecture6 - Analysing Recursive Algorithms - Divide and Conquer; Sorting
34/38
Insertion Sort
Works by making a sorted area at the front
of the array (just by first partitioning the firstelement, which is a sorted one element array),
into which it inserts subsequent elements by
shuffling elements up until it finds the correctplace.
-
8/14/2019 Lecture6 - Analysing Recursive Algorithms - Divide and Conquer; Sorting
35/38
Algorithm 6.1.2 Insertion Sort
This algorithm sorts the array aby first inserting a[2] into the sorted array a[1]; nextinserting a[3] into the sorted array a[1], a[2]; and so on; and finally inserting a[n] into thesorted array a[1], ... , a[n- 1].
Input Parameters: aOutput Parameters: Noneinsertion_sort(a) {
n= a.lastfor i= 2 to n{
val= a[i] // save a[i] so it can be insertedj= i1 // into the correct place// if val< a[j],move a[j] right to make room for a[i]while (j 1 && val< a[j]) {
a[j+ 1] = a[j]j= j- 1}a[j+ 1] = val// insert val
}}
-
8/14/2019 Lecture6 - Analysing Recursive Algorithms - Divide and Conquer; Sorting
36/38
Insertion Sort Example
4 10 3 1 5
Sorted
area
val=10
4 10 3 1 5 val=3
4 10 10 1 5 val=3
3 4 10 1 5 val=3
3 4 10 1 5 val=1
3 4 10 10 5 val=1
3 4 4 10 5 val=1
3 3 4 10 5 val=1
-
8/14/2019 Lecture6 - Analysing Recursive Algorithms - Divide and Conquer; Sorting
37/38
Insertion Sort Example
1 3 4 10 5
Sorted
area
val=1
1 3 4 10 5 val=5
1 3 4 10 10 val=5
1 3 4 5 10 val=5
-
8/14/2019 Lecture6 - Analysing Recursive Algorithms - Divide and Conquer; Sorting
38/38
Insertion Sort
Again, in the worst case, running time is T(n2).
The worst case is where val has to be inserted in the
first position, giving the running time of:
Prove this! That is, prove both O and .
i=1
n-1
i = n(n-1)/2 =
(n
2
)