What is an Algorithm And how do we
























- Slides: 24

What is an Algorithm? (And how do we analyze one? ) COMP 122, Spring 04

Algorithms w Informally, s A tool for solving a well-specified computational problem. Input Algorithm Output w Example: sorting input: A sequence of numbers. output: An ordered permutation of the input. issues: correctness, efficiency, storage, etc. Intro - 2 Comp 122,

Strengthening the Informal Definiton w An algorithm is a finite sequence of unambiguous instructions for solving a wellspecified computational problem. w Important Features: s s s Intro - 3 Finiteness. Definiteness. Input. Output. Effectiveness. Comp 122,

Algorithm – In Formal Terms… w In terms of mathematical models of computational platforms (general-purpose computers). w One definition – Turing Machine that always halts. w Other definitions are possible (e. g. Lambda Calculus. ) w Mathematical basis is necessary to answer questions such as: s Is a problem solvable? (Does an algorithm exist? ) s Complexity classes of problems. (Is an efficient algorithm possible? ) w Interested in learning more? s Take COMP 181 and/or David Harel’s book Algorithmics. Intro - 4 Comp 122,

Algorithm Analysis w Determining performance characteristics. (Predicting the resource requirements. ) s Time, memory, communication bandwidth etc. s Computation time (running time) is of primary concern. w Why analyze algorithms? s Choose the most efficient of several possible algorithms for the same problem. s Is the best possible running time for a problem reasonably finite for practical purposes? s Is the algorithm optimal (best in some sense)? – Is something better possible? Intro - 5 Comp 122,

Running Time w Run time expression should be machineindependent. s Use a model of computation or “hypothetical” computer. s Our choice – RAM model (most commonly-used). w Model should be s Simple. s Applicable. Intro - 6 Comp 122,

RAM Model w Generic single-processor model. w Supports simple constant-time instructions found in real computers. s Arithmetic (+, –, *, /, %, floor, ceiling). s Data Movement (load, store, copy). s Control (branch, subroutine call). w Run time (cost) is uniform (1 time unit) for all simple instructions. w Memory is unlimited. w Flat memory model – no hierarchy. w Access to a word of memory takes 1 time unit. w Sequential execution – no concurrent operations. Intro - 7 Comp 122,

Model of Computation w Should be simple, or even simplistic. s Assign uniform cost for all simple operations and memory accesses. (Not true in practice. ) s Question: Is this OK? w Should be widely applicable. s Can’t assume the model to support complex operations. Ex: No SORT instruction. s Size of a word of data is finite. s Why? Intro - 8 Comp 122,

Running Time – Definition w Call each simple instruction and access to a word of memory a “primitive operation” or “step. ” w Running time of an algorithm for a given input is s The number of steps executed by the algorithm on that input. w Often referred to as the complexity of the algorithm. Intro - 9 Comp 122,

Complexity and Input w Complexity of an algorithm generally depends on s Size of input. • Input size depends on the problem. – Examples: No. of items to be sorted. – No. of vertices and edges in a graph. s Other characteristics of the input data. • Are the items already sorted? • Are there cycles in the graph? Intro - 10 Comp 122,

Worst, Average, and Best-case Complexity w Worst-case Complexity s Maximum steps the algorithm takes for any possible input. s Most tractable measure. w Average-case Complexity s Average of the running times of all possible inputs. s Demands a definition of probability of each input, which is usually difficult to provide and to analyze. w Best-case Complexity s Minimum number of steps for any possible input. s Not a useful measure. Why? Intro - 11 Comp 122,

Pseudo-code Conventions w w w w w Read about pseudo-code in the text. pp 19 – 20. Indentation (for block structure). Value of loop counter variable upon loop termination. Conventions for compound data. Differs from syntax in common programming languages. Call by value not reference. Local variables. Error handling is omitted. Concerns of software engineering ignored. … Intro - 12 Comp 122,

INPUT: a sequence of n numbers, key to search for. OUTPUT: true if key occurs in the sequence, false otherwise. A Simple Example – Linear Search Linear. Search(A, key) cost times 2 while i ≤ n and A[i] != key 3 do i++ 4 if i n 5 then return true 6 else return false c 1 c 2 c 3 c 4 c 5 c 6 1 x x-1 1 1 i 1 x ranges between 1 and n+1. So, the running time ranges between c 1+ c 2+ c 4 + c 5 – best case and c 1+ c 2(n+1)+ c 3 n + c 4 + c 6 – worst case Intro - 13 Comp 122,

INPUT: a sequence of n numbers, key to search for. OUTPUT: true if key occurs in the sequence, false otherwise. A Simple Example – Linear Search Linear. Search(A, key) 1 i 1 2 while i ≤ n and A[i] != key 3 do i++ 4 if i n 5 then return true 6 else return false cost times 1 1 1 1 x x-1 1 Assign a cost of 1 to all statement executions. Now, the running time ranges between 1+ 1+ 1 = 4 – best case and 1+ (n+1)+ n + 1 = 2 n+4 – worst case Intro - 14 Comp 122,

INPUT: a sequence of n numbers, key to search for. OUTPUT: true if key occurs in the sequence, false otherwise. A Simple Example – Linear Search Linear. Search(A, key) 1 i 1 2 while i ≤ n and A[i] != key 3 do i++ 4 if i n 5 then return true 6 else return false cost times 1 1 1 1 x x-1 1 If we assume that we search for a random item in the list, on an average, Statements 2 and 3 will be executed n/2 times. Running times of other statements are independent of input. Hence, average-case complexity is 1+ n/2 + 1 = n+3 Intro - 15 Comp 122,

Order of growth w Principal interest is to determine s how running time grows with input size – Order of growth. s the running time for large inputs – Asymptotic complexity. w In determining the above, s Lower-order terms and coefficient of the highest-order term are insignificant. s Ex: In 7 n 5+6 n 3+n+10, which term dominates the running time for very large n? w Complexity of an algorithm is denoted by the highest-order term in the expression for running time. s Ex: Ο(n), Θ(1), Ω(n 2), etc. s Constant complexity when running time is independent of the input size – denoted Ο(1). s Linear Search: Best case Θ(1), Worst and Average cases: Θ(n). w More on Ο, Θ, and Ω in next class. Use Θ for the present. Intro - 16 Comp 122,

Comparison of Algorithms w Complexity function can be used to compare the performance of algorithms. w Algorithm A is more efficient than Algorithm B for solving a problem, if the complexity function of A is of lower order than that of B. w Examples: s Linear Search – (n) vs. Binary Search – (lg n) s Insertion Sort – (n 2) vs. Quick Sort – (n lg n) Intro - 17 Comp 122,

Comparisons of Algorithms w Multiplication s classical technique: O(nm) s divide-and-conquer: O(nmln 1. 5) ~ O(nm 0. 59) For operands of size 1000, takes 40 & 15 seconds respectively on a Cyber 835. w Sorting s insertion sort: (n 2) s merge sort: (n lg n) For 106 numbers, it took 5. 56 hrs on a supercomputer using machine language and 16. 67 min on a PC using C/C++. Intro - 18 Comp 122,

Why Order of Growth Matters? w Computer speeds double every two years, so why worry about algorithm speed? w When speed doubles, what happens to the amount of work you can do? w What about the demands of applications? Intro - 19 Comp 122,

Effect of Faster Machines No. of items sorted H/W Speed Comp. of Alg. 1 M* 2 M Ο(n 2) 1000 1414 O(n lgn) 62700 118600 * Gain 1. 414 1. 9 Million operations per second. • Higher gain with faster hardware for more efficient algorithm. • Results are more dramatic for more higher speeds. Intro - 20 Comp 122,

Correctness Proofs w Proving (beyond “any” doubt) that an algorithm is correct. s Prove that the algorithm produces correct output when it terminates. Partial Correctness. s Prove that the algorithm will necessarily terminate. Total Correctness. w Techniques s Proof by Construction. s Proof by Induction. s Proof by Contradiction. Intro - 21 Comp 122,

Loop Invariant w Logical expression with the following properties. s Holds true before the first iteration of the loop – Initialization. s If it is true before an iteration of the loop, it remains true before the next iteration – Maintenance. s When the loop terminates, the invariant ― along with the fact that the loop terminated ― gives a useful property that helps show that the loop is correct – Termination. w Similar to mathematical induction. s Are there differences? Intro - 22 Comp 122,

Correctness Proof of Linear Search w Use Loop Invariant for the while loop: s At the start of each iteration of the while loop, the search key is not in the subarray A[1. . i-1]. w. If the algm. terminates, then it produces correct result. w. Initialization. w. Maintenance. w. Termination. w. Argue that it terminates. Linear. Search(A, key) 1 i 1 2 while i ≤ n and A[i] != key 3 do i++ 4 if i n 5 then return true 6 else return false Intro - 23 Comp 122,

w Go through correctness proof of insertion sort in the text. Intro - 24 Comp 122,