Sunawar Khan
Islamia University of Bahawalpur
Bahawalnagar Campus
Oct 2, 2018
Analysis o f Algorithm
‫خان‬ ‫سنور‬ Algorithm Analysis
Intro to Algorithm
Problem Solving and Flow Chart
Fundamental of Algorithm
How to Compute?
Time and Space Complexity
Contents
‫خان‬ ‫سنور‬ Algorithm Analysis
Divide and Conquer
The most well known algorithm design strategy:
1. Divide instance of problem into two or more smaller instances
2. Solve smaller instances recursively
3. Obtain solution to original (larger) instance by combining these
solutions
‫خان‬ ‫سنور‬ Algorithm Analysis
Divide-and-conquer Technique
subproblem 2
of size n/2
subproblem 1
of size n/2
a solution to
subproblem 1
a solution to
the original problem
a solution to
subproblem 2
a problem of size n
‫خان‬ ‫سنور‬ Algorithm Analysis
Divide and Conquer Examples
• Sorting: merge sort and quicksort
• Tree traversals
• Matrix multiplication-Strassen’ s algorithm
• Closest pair problem
• Convex hull-Quick Hull algorithm
‫خان‬ ‫سنور‬ Algorithm Analysis
Review Mergesort
‫خان‬ ‫سنور‬ Algorithm Analysis
Merge Sort
• Merge sort is based on the divide-and-conquer paradigm.
• Its worst-case running time has a lower order of growth than insertion
sort.
• we are dealing with subproblems, we state each subproblem as
sorting a subarray A[p .. r].
• Initially, p = 1 and r = n, but these values change as we recurse
through subproblems.
‫خان‬ ‫سنور‬ Algorithm Analysis
Mergesort
• Divide Step
• If a given array A has zero or one element, simply return; it is already sorted.
Otherwise, split A[p .. r] into two subarrays A[p .. q] and A[q + 1 .. r], each
containing about half of the elements of A[p .. r]. That is, q is the halfway
point of A[p .. r].
• Conquer Step
• Conquer by recursively sorting the two subarrays A[p .. q] and A[q + 1 .. r].
• Combine Step
• Combine the elements back in A[p .. r] by merging the two sorted
subarrays A[p .. q] and A[q + 1 .. r] into a sorted sequence. To accomplish this
step, we will define a procedure MERGE (A, p, q, r).
‫خان‬ ‫سنور‬ Algorithm Analysis
Pseudo Code - Mergesort
• Split array A[1..n] in two and make copies of each half in arrays
B[1.. n/2 ] and C[1.. n/2 ]
• Sort arrays B and C
• 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.
‫خان‬ ‫سنور‬ Algorithm Analysis
Using Divide and Conquer: Mergesort
• Mergesort Strategy
Sorted
Merge
Sorted Sorted
Sort recursively
by Mergesort
Sort recursively
by Mergesort
first last
(first + last)2
‫خان‬ ‫سنور‬ Algorithm Analysis
Basic Steps
Input: Array E and indices first and last, such that the elements E[i] are
defined for first  i  last.
Output: E[first], …, E[last] is a sorted rearrangement of the same elements
• To sort the entire sequence A[1 .. n], make the initial call to the procedure
MERGE-SORT (A, 1, n).
• MERGE-SORT (A, p, r)
1. IF p < r // Check for base case
2. THEN mid = FLOOR[(p + r)/2] // Divide step
3. MERGE (A, p, mid) // Conquer step.
4. MERGE (A, mid + 1, r) // Conquer step.
5. MERGE (A, p, mid, r) // Conquer step.
6. rerutn;
‫خان‬ ‫سنور‬ Algorithm Analysis
Example
• Example: Bottom-up view of the above procedure for n = 8.
‫خان‬ ‫سنور‬ Algorithm Analysis
Pseudo Code
• MERGE (A, p, q, r )
1. n1 ← q − p + 1
2. n2 ← r − q
3. Create arrays L[1 . . n1 + 1] and R[1 . . n2 + 1]
4. FOR i ← 1 TO n1
5. DO L[i] ← A[p + i − 1]
6. FOR j ← 1 TO n2
7. DO R[j] ← A[q + j ]
8. L[n1 + 1] ← ∞
9. R[n2 + 1] ← ∞
10. i ← 1
11. j ← 1
12. FOR k ← p TO r
13. DO IF L[i ] ≤ R[ j]
14. THEN A[k] ← L[i]
15. i ← i + 1
16. ELSE A[k] ← R[j]
17. j ← j + 1
‫خان‬ ‫سنور‬ Algorithm Analysis
Explanation
• The first part shows the arrays at the start of the "for k ← p to r"
loop, where A[p . . q] is copied into L[1 . . n1] and A[q + 1 . . r ] is
copied into R[1 . . n2].
• Succeeding parts show the situation at the start of successive
iterations.
• Entries in A with slashes have had their values copied to either L or R
and have not had a value copied back in yet. Entries in L and R with
slashes have been copied back into A.
• The last part shows that the subarrays are merged back into A[p . . r],
which is now sorted, and that only the sentinels (∞) are exposed in the
arrays L and R.]
‫خان‬ ‫سنور‬ Algorithm Analysis
Pictorial Explanation
‫خان‬ ‫سنور‬ Algorithm Analysis
Analyze Merge Sort
• For simplicity, assume that n is a power of 2 so that each divide step yields
two subproblems, both of size exactly n/2.
• The base case occurs when n = 1.
• When n ≥ 2, time for merge sort steps:
• Divide: Just compute q as the average of p and r, which takes constant
time i.e. Θ(1).
• Conquer: Recursively solve 2 subproblems, each of size n/2, which is
2T(n/2).
• Combine: MERGE on an n-element subarray takes Θ(n) time.
• Summed together they give a function that is linear in n, which is Θ(n).
Therefore, the recurrence for merge sort running time is
‫خان‬ ‫سنور‬ Algorithm Analysis
Solving the Merge Sort Recurrence
• By the master theorem in CLRS-Chapter 4 (page 73), we can show that
this recurrence has the solution
• T(n) = Θ(n lg n).
• Reminder: lg n stands for log2 n.
• Compared to insertion sort [Θ(n2) worst-case time], merge sort is
faster. Trading a factor of n for a factor of lg n is a good deal.
• On small inputs, insertion sort may be faster. But for large enough
inputs, merge sort will always be faster, because its running time
grows more slowly than insertion sorts.
‫خان‬ ‫سنور‬ Algorithm Analysis
Mergesort Complexity
•Mergesort always partitions the array equally.
•Thus, the recursive depth is always (lg n)
•The amount of work done at each level is (n)
•Intuitively, the complexity should be (n lg n)
•We have,
• T(n) =2T(n/2) + (n) for n>1, T(1)=0  (n lg n)
•The amount of extra memory used is (n)
‫خان‬ ‫سنور‬ Algorithm Analysis
Efficiency of Mergesort
•All cases have same efficiency: Θ( n log n)
•Space requirement: Θ( n ) (NOT in-place)
•Can be implemented without recursion (bottom-up)
•Even if not analyzing in detail, show the recurrence for
mergesort in worst case:
T(n) = 2 T(n/2) + (n-1)
worst case comparisons for merge
‫خان‬ ‫سنور‬ Algorithm Analysis
In-Class Exercise
• 5.1.9 Let A[0, n-1] be an array of n distinct real numbers. A pair (A[i],
A[j]) is said to be an inversion if these numbers are out of order, i.e.,
i<j but A[i]>A[j]. Design an O(nlogn) algorithm for counting the
number of inversions.
• Problem :
Apply Mergesort to sort the list E, X, A, M, P, L, E in alphabetical
order.
‫خان‬ ‫سنور‬ Algorithm Analysis
Quote of the Day
• The Best Way to Predict the Future is to Invent It.

Merge sort

  • 1.
    Sunawar Khan Islamia Universityof Bahawalpur Bahawalnagar Campus Oct 2, 2018 Analysis o f Algorithm
  • 2.
    ‫خان‬ ‫سنور‬ AlgorithmAnalysis Intro to Algorithm Problem Solving and Flow Chart Fundamental of Algorithm How to Compute? Time and Space Complexity Contents
  • 3.
    ‫خان‬ ‫سنور‬ AlgorithmAnalysis Divide and Conquer The most well known algorithm design strategy: 1. Divide instance of problem into two or more smaller instances 2. Solve smaller instances recursively 3. Obtain solution to original (larger) instance by combining these solutions
  • 4.
    ‫خان‬ ‫سنور‬ AlgorithmAnalysis Divide-and-conquer Technique subproblem 2 of size n/2 subproblem 1 of size n/2 a solution to subproblem 1 a solution to the original problem a solution to subproblem 2 a problem of size n
  • 5.
    ‫خان‬ ‫سنور‬ AlgorithmAnalysis Divide and Conquer Examples • Sorting: merge sort and quicksort • Tree traversals • Matrix multiplication-Strassen’ s algorithm • Closest pair problem • Convex hull-Quick Hull algorithm
  • 6.
    ‫خان‬ ‫سنور‬ AlgorithmAnalysis Review Mergesort
  • 7.
    ‫خان‬ ‫سنور‬ AlgorithmAnalysis Merge Sort • Merge sort is based on the divide-and-conquer paradigm. • Its worst-case running time has a lower order of growth than insertion sort. • we are dealing with subproblems, we state each subproblem as sorting a subarray A[p .. r]. • Initially, p = 1 and r = n, but these values change as we recurse through subproblems.
  • 8.
    ‫خان‬ ‫سنور‬ AlgorithmAnalysis Mergesort • Divide Step • If a given array A has zero or one element, simply return; it is already sorted. Otherwise, split A[p .. r] into two subarrays A[p .. q] and A[q + 1 .. r], each containing about half of the elements of A[p .. r]. That is, q is the halfway point of A[p .. r]. • Conquer Step • Conquer by recursively sorting the two subarrays A[p .. q] and A[q + 1 .. r]. • Combine Step • Combine the elements back in A[p .. r] by merging the two sorted subarrays A[p .. q] and A[q + 1 .. r] into a sorted sequence. To accomplish this step, we will define a procedure MERGE (A, p, q, r).
  • 9.
    ‫خان‬ ‫سنور‬ AlgorithmAnalysis Pseudo Code - Mergesort • Split array A[1..n] in two and make copies of each half in arrays B[1.. n/2 ] and C[1.. n/2 ] • Sort arrays B and C • 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.
  • 10.
    ‫خان‬ ‫سنور‬ AlgorithmAnalysis Using Divide and Conquer: Mergesort • Mergesort Strategy Sorted Merge Sorted Sorted Sort recursively by Mergesort Sort recursively by Mergesort first last (first + last)2
  • 11.
    ‫خان‬ ‫سنور‬ AlgorithmAnalysis Basic Steps Input: Array E and indices first and last, such that the elements E[i] are defined for first  i  last. Output: E[first], …, E[last] is a sorted rearrangement of the same elements • To sort the entire sequence A[1 .. n], make the initial call to the procedure MERGE-SORT (A, 1, n). • MERGE-SORT (A, p, r) 1. IF p < r // Check for base case 2. THEN mid = FLOOR[(p + r)/2] // Divide step 3. MERGE (A, p, mid) // Conquer step. 4. MERGE (A, mid + 1, r) // Conquer step. 5. MERGE (A, p, mid, r) // Conquer step. 6. rerutn;
  • 12.
    ‫خان‬ ‫سنور‬ AlgorithmAnalysis Example • Example: Bottom-up view of the above procedure for n = 8.
  • 13.
    ‫خان‬ ‫سنور‬ AlgorithmAnalysis Pseudo Code • MERGE (A, p, q, r ) 1. n1 ← q − p + 1 2. n2 ← r − q 3. Create arrays L[1 . . n1 + 1] and R[1 . . n2 + 1] 4. FOR i ← 1 TO n1 5. DO L[i] ← A[p + i − 1] 6. FOR j ← 1 TO n2 7. DO R[j] ← A[q + j ] 8. L[n1 + 1] ← ∞ 9. R[n2 + 1] ← ∞ 10. i ← 1 11. j ← 1 12. FOR k ← p TO r 13. DO IF L[i ] ≤ R[ j] 14. THEN A[k] ← L[i] 15. i ← i + 1 16. ELSE A[k] ← R[j] 17. j ← j + 1
  • 14.
    ‫خان‬ ‫سنور‬ AlgorithmAnalysis Explanation • The first part shows the arrays at the start of the "for k ← p to r" loop, where A[p . . q] is copied into L[1 . . n1] and A[q + 1 . . r ] is copied into R[1 . . n2]. • Succeeding parts show the situation at the start of successive iterations. • Entries in A with slashes have had their values copied to either L or R and have not had a value copied back in yet. Entries in L and R with slashes have been copied back into A. • The last part shows that the subarrays are merged back into A[p . . r], which is now sorted, and that only the sentinels (∞) are exposed in the arrays L and R.]
  • 15.
    ‫خان‬ ‫سنور‬ AlgorithmAnalysis Pictorial Explanation
  • 16.
    ‫خان‬ ‫سنور‬ AlgorithmAnalysis Analyze Merge Sort • For simplicity, assume that n is a power of 2 so that each divide step yields two subproblems, both of size exactly n/2. • The base case occurs when n = 1. • When n ≥ 2, time for merge sort steps: • Divide: Just compute q as the average of p and r, which takes constant time i.e. Θ(1). • Conquer: Recursively solve 2 subproblems, each of size n/2, which is 2T(n/2). • Combine: MERGE on an n-element subarray takes Θ(n) time. • Summed together they give a function that is linear in n, which is Θ(n). Therefore, the recurrence for merge sort running time is
  • 17.
    ‫خان‬ ‫سنور‬ AlgorithmAnalysis Solving the Merge Sort Recurrence • By the master theorem in CLRS-Chapter 4 (page 73), we can show that this recurrence has the solution • T(n) = Θ(n lg n). • Reminder: lg n stands for log2 n. • Compared to insertion sort [Θ(n2) worst-case time], merge sort is faster. Trading a factor of n for a factor of lg n is a good deal. • On small inputs, insertion sort may be faster. But for large enough inputs, merge sort will always be faster, because its running time grows more slowly than insertion sorts.
  • 18.
    ‫خان‬ ‫سنور‬ AlgorithmAnalysis Mergesort Complexity •Mergesort always partitions the array equally. •Thus, the recursive depth is always (lg n) •The amount of work done at each level is (n) •Intuitively, the complexity should be (n lg n) •We have, • T(n) =2T(n/2) + (n) for n>1, T(1)=0  (n lg n) •The amount of extra memory used is (n)
  • 19.
    ‫خان‬ ‫سنور‬ AlgorithmAnalysis Efficiency of Mergesort •All cases have same efficiency: Θ( n log n) •Space requirement: Θ( n ) (NOT in-place) •Can be implemented without recursion (bottom-up) •Even if not analyzing in detail, show the recurrence for mergesort in worst case: T(n) = 2 T(n/2) + (n-1) worst case comparisons for merge
  • 20.
    ‫خان‬ ‫سنور‬ AlgorithmAnalysis In-Class Exercise • 5.1.9 Let A[0, n-1] be an array of n distinct real numbers. A pair (A[i], A[j]) is said to be an inversion if these numbers are out of order, i.e., i<j but A[i]>A[j]. Design an O(nlogn) algorithm for counting the number of inversions. • Problem : Apply Mergesort to sort the list E, X, A, M, P, L, E in alphabetical order.
  • 21.
    ‫خان‬ ‫سنور‬ AlgorithmAnalysis Quote of the Day • The Best Way to Predict the Future is to Invent It.