Title: Algorithms and Data Structures Lecture III
1Algorithms and Data StructuresLecture III
- Simonas Šaltenis
- Aalborg University
- simas_at_cs.auc.dk
2This Lecture
- Divide-and-conquer technique for algorithm
design. Example problems - Tiling
- Searching (binary search)
- Sorting (merge sort).
3Tiling
A tromino tile
And a 2nx2n board with a hole
4Tiling Trivial Case (n 1)
- Trivial case (n 1) tiling a 2x2 board with a
hole
- Idea try somehow to reduce the size of the
original problem, so that we eventually get to
the 2x2 boards which we know how to solve
5Tiling Dividing the Problem
- To get smaller square boards lets divide the
original board into for boards
- Great! We have one problem of the size 2n-1x2n-1!
- But The other three problems are not similar to
the original problems they do not have holes!
6Tiling Dividing the Problem
- Idea insert one tromino at the center to get
three holes in each of the three smaller boards
- Now we have four boards with holes of the size
2n-1x2n-1. - Keep doing this division, until we get the 2x2
boards with holes we know how to tile those
7Tiling Algorithm
- INPUT n the board size (2nx2n board), L
location of the hole. - OUTPUT tiling of the board
- Tile(n, L)
- if n 1 then
- Trivial case
- Tile with one tromino
- return
- Divide the board into four equal-sized boards
- Place one tromino at the center to cut out 3
additional holes - Let L1, L2, L3, L4 denote the positions of the
4 holes - Tile(n-1, L1)
- Tile(n-1, L2)
- Tile(n-1, L3)
- Tile(n-1, L4)
8Divide and Conquer
- Divide-and-conquer method for algorithm design
- If the problem size is small enough to solve it
in a straightforward manner, solve it. Else - Divide Divide the problem into two or more
disjoint subproblems - Conquer Use divide-and-conquer recursively to
solve the subproblems - Combine Take the solutions to the subproblems
and combine these solutions into a solution for
the original problem
9Tiling Divide-and-Conquer
- Tiling is a divide-and-conquer algorithm
- Just do it trivially if the board is 2x2, else
- Divide the board into four smaller boards
(introduce holes at the corners of the three
smaller boards to make them look like original
problems) - Conquer using the same algorithm recursively
- Combine by placing a single tromino in the center
to cover the three introduced holes
10Binary Search
- Find a number in a sorted array
- Just do it trivially if the array is of one
element - Else divide into two equal halves and solve each
half - Combine the results
INPUT A1..n a sorted (non-decreasing) array
of integers, s an integer. OUTPUT an index j
such that Aj s. NIL, if "j (1jn) Aj ¹ s
Binary-search(A, p, r, s) if p r then
if Ap s then return p else return NIL
që(pr)/2û ret Binary-search(A, p, q,
s) if ret NIL then return
Binary-search(A, q1, r, s) else return ret
11Recurrences
- Running times of algorithms with Recursive calls
can be described using recurrences - A recurrence is an equation or inequality that
describes a function in terms of its value on
smaller inputs - Example Binary Search
12Binary Search (improved)
- T(n) Q(n) not better than brute force!
- Clever way to conquer
- Solve only one half!
- INPUT A1..n a sorted (non-decreasing) array
of integers, s an integer. - OUTPUT an index j such that Aj s. NIL, if "j
(1jn) Aj ¹ s - Binary-search(A, p, r, s)
- if p r then
- if Ap s then return p
- else return NIL
- që(pr)/2û
- if Aq s then return Binary-search(A, p, q,
s) - else return Binary-search(A, q1, r, s)
13Running Time of BS
14Merge Sort Algorithm
- Divide If S has at least two elements (nothing
needs to be done if S has zero or one elements),
remove all the elements from S and put them into
two sequences, S1 and S2 , each containing about
half of the elements of S. (i.e. S1 contains the
first én/2ù elements and S2 contains the
remaining ën/2û elements). - Conquer Sort sequences S1 and S2 using Merge
Sort. - Combine Put back the elements into S by merging
the sorted sequences S1 and S2 into one sorted
sequence
15Merge Sort Algorithm
Merge-Sort(A, p, r) if p lt r then q
ë(pr)/2û Merge-Sort(A, p, q)
Merge-Sort(A, q1, r) Merge(A, p, q, r)
Merge(A, p, q, r) Take the smallest of the two
topmost elements of sequences Ap..q and
Aq1..r and put into the resulting sequence.
Repeat this, until both sequences are empty. Copy
the resulting sequence into Ap..r.
16MergeSort (Example) - 1
17MergeSort (Example) - 2
18MergeSort (Example) - 3
19MergeSort (Example) - 4
20MergeSort (Example) - 5
21MergeSort (Example) - 6
22MergeSort (Example) - 7
23MergeSort (Example) - 8
24MergeSort (Example) - 9
25MergeSort (Example) - 10
26MergeSort (Example) - 11
27MergeSort (Example) - 12
28MergeSort (Example) - 13
29MergeSort (Example) - 14
30MergeSort (Example) - 15
31MergeSort (Example) - 16
32MergeSort (Example) - 17
33MergeSort (Example) - 18
34MergeSort (Example) - 19
35MergeSort (Example) - 20
36MergeSort (Example) - 21
37MergeSort (Example) - 22
38Merge Sort Summarized
- To sort n numbers
- if n1 done!
- recursively sort 2 lists of numbers ën/2û and
én/2ù elements - merge 2 sorted lists in Q(n) time
- Strategy
- break problem into similar (smaller) subproblems
- recursively solve subproblems
- combine solutions to answer
39Running time of MergeSort
- Again the running time can be expressed as a
recurrence
40Repeated Substitution Method
- Lets find the running time of merge sort (lets
assume that n2b, for some b).
41Example Finding Min and Max
- Given an unsorted array, find a minimum and a
maximum element in the array
INPUT Al..r an unsorted array of integers, l
r. OUTPUT (min, max) such that "j (ljr)
Aj ³ min and Aj max MinMax(A, l, r)
if l r then return (Al, Ar) Trivial
case q ë(lr)/2û
Divide
(minl, maxl) MinMax(A, l, q)
(minr, maxr) MinMax(A, q1, r) if minl lt
minr then min minl else min minr if maxl gt
maxr then max maxl else max maxr return
(min, max)
Conquer
Combine
42Next Week
- Analyzing the running time of recursive
algorithms (such as divide-and-conquer)