CSC323 Module 2 Classical Design Techniques NEW
CSC323 Module 2 Classical Design Techniques NEW
Best-case = m comparisons
At the worst case, the algorithm may have to make all m comparisons before
shifting the pattern, and this can happen for each of the n – m + 1 tries.
Thus, in the worst case, the algorithm makes m (n – m + 1) character
comparisons. m << m(n-m+1). The overall time complexity is O(nm)
Brute Force String Matching
Examples
How many comparisons are made by the brute-force string matching algorithm in
searching for the following patterns in a binary text of 1000 zeros?
a) 00001
There will be a total of (1000 – 5 + 1) iterations. In each iteration, we will have to
do 5 comparisons, as the first 4 bits will match and only the last bit will not
match. Hence, the total number of comparisons = 996 * 5 = 4,980
b) 10000
There will be a total of (1000 – 5 + 1) iterations. In each iteration, the first
comparison itself will be a failure. Hence, there will be a total of 996 * 1 = 996
comparisons.
c) 01010
There will be a total of (1000 – 5 + 1) iterations. In each iteration, we will do 2
comparisons (the first comparison will be successful and the second one is
not). Hence, there will be a total of 996*2 = 1,992 comparisons.
Brute Force String Matching
Examples
Consider the problem of counting the number of sub strings that start
with an A and end with a B in a given string of alphabets:
DAAXBABAGBD.
Scan the given string from left to right. Initialize the number of sub strings
to zero. Keep track of the number of As encountered. Each time a B
is encountered, set the number of sub strings to be number of sub
strings + the number of As encountered until then. Since we do a
linear pass on the given string and do one comparison per character,
the time complexity is Θ(n), where n is the length of the string.
2.2 Decrease and Conquer
Decrease by One: Insertion Sort
• Given an array A[0…n-1], at any time, we have the array
divided into two parts: A[0,…,i-1] and A[i…n-1].
– The A[0…i-1] is the sorted part and A[i…n-1] is the unsorted part.
– In any iteration, we pick an element v = A[i] and scan through the
sorted sequence A[0…i-1] to insert v at the appropriate position.
• The scanning is proceeded from right to left (i.e., for index j
running from i-1 to 0) until we find the right position for v.
• During this scanning process, v = A[i] is compared with A[j].
• If A[j] > v, then we v has to be placed somewhere before A[j] in the
final sorted sequence. So, A[j] cannot be at its current position (in
the final sorted sequence) and has to move at least one position to
the right. So, we copy A[j] to A[j+1] and decrement the index j, so
that we now compare v with the next element to the left.
∑1 = (n − 1) − 1 + 1 = (n − 1)
i =1
The comparison A[j] > v is the basic operation.
Worst Case (if the array is reverse-sorted): the element v at A[i] has to be moved
all the way to index 0, by scanning through the entire sequence A[0…i-1].
n −1 0 n −1 i −1 n −1 n −1
n(n − 1)
∑ ∑
i =1 j = i −1
1 = ∑ ∑1 = ∑ (i − 1) − 0 + 1 = ∑ (i − 1) =
i =1 j = 0 i =1 i =1 2
Insertion Sort: Analysis and Example
Average Case: On average for a random input sequence, we would be visiting half
of the sorted sequence A[0…i-1] to put A[i] at the proper position.
n −1 ( i −1) / 2 n −1 n −1
(i − 1) (i + 1)
C (n) = ∑ ∑ 1 = ∑ + 1 = ∑ =Θ(n 2 )
i =1 j =i −1 i =1 2 i =1 2
Example: Given sequence (also initial): 45 23 8 12 90 21
Index
Iteration 1 (v = 23): Overall time complexity
-1 Iteration 4 (v = 90):
45 45 8 12 90 21 8 12 23 45 90 21
23 45 8 12 90 21 9 12 23 45 90 21
Iteration 2 (v = 8):
Iteration 5 (v = 21):
23 45 45 12 90 21
9 12 23 45 90 90
23 23 45 12 90 21
9 12 23 45 45 90
8 23 45 12 90 21
9 12 23 23 45 90 O(n2)
Iteration 3 (v = 12): 9 12 21 23 45 90
8 23 45 45 90 21
8 23 23 45 90 21 The colored elements are in the sorted sequence
8 12 23 45 90 21 and the circled element is at index j of the algorithm.
2.3 Divide and Conquer
Divide-and-Conquer
The most-well known
algorithm design strategy:
1. We divide a problem of
instance size ‘n’ into
several sub problems
(each of size n/b);
2. Solve ‘a’ of these sub
problems (a ≥ 1; b > 1)
recursively and
3. Combine the solutions
to these sub problems to
obtain a solution for the
larger problem.
4) T(n) = 2T(n/2) + 1
a = 2; b = 2; d = 0 a > bd
( )
T (n) = Θ n log 2 2 = Θ(n)
Master Theorem: More Problems
Merge Sort
• Split array A[0..n-1] in two about equal halves and make
copies of each half in arrays B and C
• Sort arrays B and C recursively
• Merge sorted arrays B and C into array A as follows:
– Repeat the following until no elements remain in one of
the arrays:
• compare the first elements in the remaining
unprocessed portions of the arrays
• copy the smaller of the two into A, while
incrementing the index indicating the unprocessed
portion of that array
– Once all elements in one of the arrays are processed,
copy the remaining unprocessed elements from the
other array into A.
Merge Sort
Merge Algorithm
Example for Merge Sort
Analysis of Merge Sort
Binary Search
• Binary search is a Θ(log n), highly efficient search
algorithm, in a sorted array.
• It works by comparing a search key K with the array’s
middle element A[m]. If they match, the algorithm stops;
otherwise, the same operation is repeated recursively for
the first half of the array if K < A[m], and for the second
half if K > A[m].
• Though binary search in based on a recursive idea, it can
be easily implemented as a non-recursive algorithm.
Example
Binary Search
Search Key
K = 70
l=0 r=12 m=6
l=7 r=12 m=9
l=7 r=8 m=7
27 81
39 70 93
3
14 31 42 74 91 98
The keys that will require the largest number of comparisons: 14, 31, 42, 74, 91, 98
Average # Comparisons for Successful Search
Avg # comparisons
Keys # comparisons = [Sum of the product of the # keys
55 1 with certain # comparisons] / [ Total
27, 81 2 Number of keys]
3, 39, 70, 93 3 = [(1)(1) + (2)(2) + (3)(4) + (4)(6)] /13
14, 31, 42, 74, 91, 98 4 = 3.15
55
27 81
39 70 93
3
14 31 42 74 91 98
Not a
Heap
• The key at each node is ≥ keys at its children (Max. Heap)
• We will focus on Max. Heap in this chapter. Note that for a Min. Heap,
the value for the key at a node is <= the value for the keys at its
children. [In other words, Max. Heap is the one whose root has the
largest value; Min. Heap is the one whose Root has smallest value]
• Heap’s elements are ordered top down (along any path down from its
root), but they are not ordered left to right
Important Properties of a Heap
• Given n, there exists a unique binary tree with n nodes
that is essentially complete, with height, h = log2 n
The root contains the largest key (Max. Heap)
• The sub tree rooted at any node of a heap is also a heap
• A heap can be represented as an array
The key to be deleted is swapped with the last key after which the smaller tree
is “heapified” by exchanging the new key in its root with the right most key
among its leaf nodes (at the maximum height), until the parent dominance
requirement is satisfied
Heap Sort
• Stage 1:
– (Bottom-up approach) Construct a heap for a given list of n keys: Θ(n)
time
– (Top-down approach) Construct a heap by inserting one key at a time to
an already existing heap: Θ(nlogn) time
• Stage 2: Repeat operation of root removal n-1 times: Θ(nlogn)
time
– Exchange keys in the root and in the last (rightmost) leaf
– Decrease heap size by 1
– If necessary, swap new root with larger child until the heap
condition holds
1 8 1 5 1 5 1 2
2 5 -10000 5 2 3 1 8
5 3 2 3
1 1
Array sorting in progress
Iteration # 2: Remove key 5
3 -10000 3 2 1 5 8
2 1
1 2 -10000 2 1 3 5 8
2 1
Array sorting in progress
Iteration # 4: Remove key 2
-10000 1 2 3 5 8
1
Final sorted array
Iteration # 5: Remove key 1
-10000 1 2 3 5 8
Example 1 Top-Down Construction
2, 5, 3, 1, 8
2 2 5 5 5
5 2 2 3 2 3
5 5 8
2 3 8 3 5 3
1 8 1 2 1 2
Final-Heap
(Top-down) 8
5 3
1 2
Example 2
7, 5, 9, 6, 3 Bottom-Up Construction
7 7 9
5 9 6 9 6 7
6 3 5 3 5 3
Example 2
7, 5, 9, 6, 3 Top-Down Construction
7 7 7 9 9 9
5 5 9 5 7 5 7 6 7
6 5
6 7
5 3
Example 3 Bottom-Up Construction
1, 8, 6, 5, 3, 7, 4
1 1 8 8
8 6 8 7 1 7 5 7
5 3 7 4 5 3 6 4 5 3 6 4 1 3 6 4
1 3 6 1 3 6 1 3 4
Iteration # 2: Remove key 7 Array sorting in progress
-10000 6 5 4 1 3 7 8
4 6
5 6 5 4
1 3 1 3
1 1
3 1
1 1 8 8 8 8
8 1 1 6 1 6 5 6
5 1
8 8 8 8
5 6 5 6 5 7 5 7
1 3 1 3 1 3 1 3
7 6 6 4
Example 4 Bottom-Up
1, 2, 3
Construction
1 3 Array (satisfying the heap property)
2 3 2 1 -10000 3 2 1
1, 2, 3 Top-Down
Construction
1 1 2 2 3 3
2 1 1 3 1 2 1 2
Thus, for a given input sequence, the arrays (satisfying the heap property)
that are constructed using the bottom-up approach and the top-down
approach need not always be the same, as observed in the above example.
2.5 Space-Time Tradeoff
In-place vs. Out-of-place Algorithms
• An algorithm is said to be “in-place” if it uses a minimal
and/or constant amount of extra storage space to
transform or process an input to obtain the desired output.
– Depending on the nature of the problem, an in-place algorithm may
sometime overwrite an input to the desired output as the algorithm
executes (as in the case of in-place sorting algorithms); the output
space may sometimes be a constant (for example in the case of
string-matching algorithms).
• Algorithms that use significant amount of extra storage
space (sometimes, additional space as large as the input
– example: merge sort) are said to be out-of-place in
nature.
• Time-Space Complexity Tradeoffs of Sorting Algorithms:
– In-place sorting algorithms like Selection Sort, Bubble Sort, Insertion Sort
and Quick Sort have a worst-case time complexity of Θ(n2).
– On the other hand, Merge sort has a space-complexity of Θ(n), but has a
worst-case time complexity of Θ(nlogn).
Time and Space Complexity Analysis
of Recursive Sorting Algorithms
Time-complexity: Θ(nlogn)
Space-complexity: Θ(n)
• Merge Sort:
• Is the algorithm in-place?
• Answer: No, The algorithm requires an equal amount of additional
space as that of the original input array for each recursive call of the
algorithm. The two sub-arrays B and C are stored in a different
memory location and are not part of the original array A.
Time-Space Complexity of Heap Sort
• Heap sort is probably the best algorithm we have seen in this course
with respect to time and space complexity. It is an in-place algorithm
with all the heapify and element rearrangement operations conductible
in the input array itself and no additional space is needed.