Introduction to Algorithms 2 nd edition by Cormen
Introduction to Algorithms (2 nd edition) by Cormen, Leiserson, Rivest & Stein Chapter 2: Getting Started (slides enhanced by N. Adlai A. De. Pano)
Overview o Aims to familiarize us with framework used throughout text o Examines alternate solutions to the sorting problem presented in Ch. 1 o Specify algorithms to solve problem o Argue for their correctness o Analyze running time, introducing notation for asymptotic behavior o Introduce divide-and-conquer algorithm technique
The Sorting Problem Input: A sequence of n numbers [a 1, a 2, … , an]. Output: A permutation or reordering [a'1, a'2, … , a'n ] of the input sequence such that a'1 a'2 … a'n. An instance of the Sorting Problem: Input: A sequence of 6 number [31, 41, 59, 26, 41, 58]. Expected output for given instance: Expected Output: The permutation of the input [26, 31, 41, 58 , 59].
Copyright © The Mc. Graw-Hill Companies, Inc. Permission required for reproduction or display. Insertion Sort The main idea …
Copyright © The Mc. Graw-Hill Companies, Inc. Permission required for reproduction or display. Insertion Sort (cont. )
Copyright © The Mc. Graw-Hill Companies, Inc. Permission required for reproduction or display. Insertion Sort (cont. )
Copyright © The Mc. Graw-Hill Companies, Inc. Permission required for reproduction or display. Insertion Sort (cont. ) The algorithm …
Loop Invariant o Property of A[1. . j 1] At the start of each iteration of the for loop of lines 1 8, the subarray A[1. . j 1] consists of the elements originally in A[1. . j 1] but in sorted order. o Need to establish the following re invariant: n Initialization: true prior to first iteration n Maintenance: if true before iteration, remains true after iteration n Termination: at loop termination, invariant implies correctness of algorithm
Analyzing Algorithms o Has come to mean predicting the resources that the algorithm requires o Usually computational time is resource of primary importance o Aims to identify best choice among several alternate algorithms o Requires an agreed-upon “model” of computation o Shall use a generic, one-processor, random -access machine (RAM) model of computation
Random-Access Machine o Instructions are executed one after another (no concurrency) o Admits commonly found instructions in “real” computers, data movement operations, control mechanism o Uses common data types (integer and float) o Other properties discussed as needed o Care must be taken since model of computation has great implications on resulting analysis
Analysis of Insertion Sort o Time resource requirement depends on input size o Input size depends on problem being studied; frequently, this is the number of items in the input o Running time: number of primitive operations or “steps” executed for an input o Assume constant amount of time for each line of pseudocode
Copyright © The Mc. Graw-Hill Companies, Inc. Permission required for reproduction or display. Analysis of Insertion Sort Time efficiency analysis …
Best Case Analysis o Least amount of (time) resource ever needed by algorithm o Achieved when incoming list is already sorted in increasing order o Inner loop is never iterated o Cost is given by: T(n) = c 1 n+c 2 (n 1)+c 4 (n 1)+c 5(n 1)+c 8(n 1) = (c 1+c 2+c 4+c 5+c 8)n (c 2+c 4+c 5+c 8) = an + b o Linear function of n
Worst Case Analysis o Greatest amount of (time) resource ever needed by algorithm o Achieved when incoming list is in reverse order o Inner loop is iterated the maximum number of times, i. e. , tj = j o Therefore, the cost will be: T(n) = c 1 n + c 2 (n 1)+c 4 (n 1) + c 5((n(n+1)/2) 1) + c 6(n(n 1)/2) + c 7(n(n 1)/2) + c 8(n 1) = ( c 5 /2 + c 6 /2 + c 7/2 ) n 2 + (c 1+c 2+c 4+c 5 /2 c 6 /2 c 7 /2 +c 8 ) n ( c 2 + c 4 + c 5 + c 8 ) = an 2 + bn + c o Quadratic function of n
Future Analyses o For the most part, subsequent analyses will focus on: n Worst-case running time o Upper bound on running time for any input n Average-case analysis o Expected running time over all inputs o Often, worst-case and average-case have the same “order of growth”
Order of Growth o Simplifying abstraction: interested in rate of growth or order of growth of the running time of the algorithm o Allows us to compare algorithms without worrying about implementation performance o Usually only highest order term without constant coefficient is taken o Uses “theta” notation n Best case of insertion sort is (n) n Worst case of insertion sort is (n 2)
Designing Algorithms o Several techniques/patterns for designing algorithms exist o Incremental approach: builds the solution one component at a time o Divide-and-conquer approach: breaks original problem into several smaller instances of the same problem n Results in recursive algorithms n Easy to analyze complexity using proven techniques
Divide-and-Conquer o Technique (or paradigm) involves: n “Divide” stage: Express problem in terms of several smaller subproblems n “Conquer” stage: Solve the smaller subproblems by applying solution recursively – smallest subproblems may be solved directly n “Combine” stage: Construct the solution to original problem from solutions of smaller subproblem
Merge Sort Strategy o Divide stage: Split the nelement sequence into two subsequences of n/2 elements each o Conquer stage: Recursively sort the two subsequences o Combine stage: Merge the two sorted subsequences into one sorted sequence (the solution) n (unsorted) n/2 (unsorted) MERGE SORT n/2 (sorted) MERGE n (sorted)
Copyright © The Mc. Graw-Hill Companies, Inc. Permission required for reproduction or display. Merging Sorted Sequences
Copyright © The Mc. Graw-Hill Companies, Inc. Permission required for reproduction or display. Merging Sorted Sequences • Combines the sorted (1) (n) (1) subarrays A[p. . q] and A[q+1. . r] into one sorted array A[p. . r] • Makes use of two working arrays L and R which initially hold copies of the two subarrays • Makes use of sentinel (n) value ( ) as last element to simplify logic
Merge Sort Algorithm (1) T(n/2) (n) T(n) = 2 T(n/2) + (n)
Copyright © The Mc. Graw-Hill Companies, Inc. Permission required for reproduction or display. Analysis of Merge Sort Analysis of recursive calls …
Copyright © The Mc. Graw-Hill Companies, Inc. Permission required for reproduction or display. Analysis of Merge Sort T(n) = cn(lg n + 1) = cnlg n + cn T(n) is (n lg n)
- Slides: 24