Algorithms in a Nutshell Session 1 Introduction 9:10 – 9:40
Outline • What is an algorithm? • An example: INSERTION SORT • Pattern format description • Mathematical notations Algorithms in a Nutshell (c) 2009, George T. Heineman 2
What is an Algorithm? • A deterministic sequence of operations for solving a problem given a specific input set • Deterministic – this means it always works, given the known constraints on the problem – Produces solution for all possible inputs • Input Set – the (often arbitrary) way in which a problem instance is represented Algorithms in a Nutshell (c) 2009, George T. Heineman 3
Problem : Sort a collection of strings eagle • Input cat ant – Collection of String S dog ball • Output – Ordered result • Assumptions ant ball – Complete ordering between cat dog any two elements of S eagle Algorithms in a Nutshell (c) 2009, George T. Heineman 4
Problem Instance Representations • Array of fixed structured content e ¬ c ¬ a e a g l a t n t ¬ ¬ ¬ ¬ ¬ • Array of pointers to content eagle cat ant • Compact representation e ¬ c ¬ a e a g l a t n t ¬ Algorithms in a Nutshell (c) 2009, George T. Heineman 5
Common Data Structures • Array Data structures provide various – N dimensional ways to representation • Linked List information – Doubly‐linked Note the glyphs used throughout the book will need • Stack to be consistent here as well • Queue Key operations for each data – Double‐ended queue structure to be discussed as – Priority queue needed • Binary Tree • Binary Heap Algorithms in a Nutshell (c) 2009, George T. Heineman 6
63 INSERTION SORT • Frame the problem – Insert each new element into proper location remaining elements in the collection to h g a f m be processed sorted part of element being considered next the collection Algorithm iteratively applies this key operation Algorithms in a Nutshell (c) 2009, George T. Heineman 7
INSERTION SORT • Occasionally all elements must be moved remaining elements in the collection to g h a f m be processed sorted part of element being considered next the collection Algorithms in a Nutshell (c) 2009, George T. Heineman 8
INSERTION SORT • Only need to move elements “higher” than the one being inserted remaining elements in the collection to a g h f m be processed sorted part of element being considered next the collection Algorithms in a Nutshell (c) 2009, George T. Heineman 9
INSERTION SORT • Sometimes the element to be inserted is greater than all existing elements – no swaps! remaining elements in the collection to a f g h m be processed sorted part of element being considered next the collection Algorithms in a Nutshell (c) 2009, George T. Heineman 10
INSERTION SORT • Sometimes the element to be inserted is greater than all existing elements – no swaps! DONE a f g h m sorted part of the collection Algorithms in a Nutshell (c) 2009, George T. Heineman 11
64 Algorithm Fact Sheet Name of the Concepts Algorithm INSERTION SORT Array insert (A, 6, “7”) sort (A) Small Pseudocod 1. for i=1 to n–1 do Example e 1 4 8 9 11 15 7 12 13 6 2. insert (A, i, A[i]) description end Already sorted 7 value insert (A, pos, value) 1. i = pos–1 Elements Insert into 2. while (i ≥ 0 and A[i] > value) then compared proper and bumped up 3. A[i+1] = A[i] spot 4. i = i–1 1 4 7 8 9 11 15 12 13 6 5. A[i+1]=value Sorted region extended by one end Algorithms in a Nutshell (c) 2009, George T. Heineman 12
sort (A) Array 64 INSERTION 1. for i=1 to n–1 do 2. insert (A, i, A[i]) SORT end insert (A, pos, value) 1. i = pos–1 2. while (i ≥ 0 and A[i] > value) then insert (A, 6, “7”) 3. A[i+1] = A[i] 4. i = i–1 1 4 8 9 11 15 7 12 13 6 5. A[i+1]=value end Already sorted value 7 Elements Insert into compared proper and bumped spot up 1 4 7 8 9 11151213 6 Sorted region extended by one Algorithms in a Nutshell (c) 2009, George T. Heineman 13
Code Check • Show actual running code – Handout – Debug example Algorithms in a Nutshell (c) 2009, George T. Heineman 14
Performance of INSERTION SORT • As sorting algorithms go, how efficient is INSERTION SORT? – Is it the fastest algorithm? [NO] – How does it compare with other algorithms? • Difficult to answer without a theoretic model – independent of programming language – Independent of computer hardware Algorithms in a Nutshell (c) 2009, George T. Heineman 15
Performance of Algorithms • Use standard “Big O” notation – Let T( n ) be time for algorithm to perform on an average problem instance of size n – How does T( n ) grow in proportion to increasing n? • Example Problem – Given n integers, find the largest integer – Expect that T(2n) ≅ 2* T(n) • Find performance family that most closely matches behavior of algorithm Algorithms in a Nutshell (c) 2009, George T. Heineman 16
Performance Analysis • Linear or O(n) – As problem size is multiplied by 2, the time to complete the problem “should be” multiplied by 2 – Holds true for any constant multiplicative factor • How to capture this concept? – Once n is “sufficiently large”, there is some constant c such that t ( n ) ≤ c * n – Not an estimate but a firm upper bound • In practice, “c” is never computed, though its existence is guaranteed – Depends upon hardware platform, language, etc… Algorithms in a Nutshell (c) 2009, George T. Heineman 17
Functional Families • You already know this concept from algebra – x 2 and 4x 2 –3*x+170 are both “quadratic” formulae – Distinctive shape – Highest exponent is most important – In “long run” they behave similarly Algorithms in a Nutshell (c) 2009, George T. Heineman 18
66 Performance of INSERTION SORT • Average Case – Given random permutation of n elements, each element is (on average) n /3 positions from proper location 3 1 2 2 0 Average = 8/5 =1.6 ≅ 5/3 h g a f m a f g h m Algorithms in a Nutshell (c) 2009, George T. Heineman 19
66 Performance of INSERTION SORT • insert(A, pos, value) is invoked n–1 times – On average, while loop invoked n /3 times • Quick estimate = ( n –1 )*( n /3) = ⅓ n 2 – n/3 – The critical factor is the highest exponent – ⅓ n 2 – n/3 is O(n 2 ) sort (A) • INSERTION SORT is not linear 1. for i=1 to n–1 do 2. insert (A, i, A[i]) – It is Quadratic end – As problem size is multiplied by insert (A, pos, value) 1. i = pos–1 k =2, the time to complete the 2. while (i ≥ 0 and A[i] > value) then problem is multiplied by k 2 =4 3. A[i+1] = A[i] 4. i = i–1 5. A[i+1]=value Algorithms in a Nutshell (c) 2009, George T. Heineman 20 end
18 Rating Performance of Algorithm • Best Case – Problem instances for which algorithm computes answer most efficiently • Average Case – Typical random problem instance. Identifies the expected performance of the algorithm • Worst Case – Unusual problem instances that force algorithm to work harder and be less efficient Algorithms in a Nutshell (c) 2009, George T. Heineman 21
Recommend
More recommend