Lecture 1 - Introduction

# Lecture 1 - Introduction - Lecture #1 #1 Introduction Dr....

This preview shows page 1. Sign up to view the full content.

This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: Lecture #1 #1 Introduction Dr. Kh l d B hi Shaban D Khaled Bashir Sh b What is an algorithm? An algorithm is a sequence of unambiguous instructions for solving a problem, i.e., for obtaining a required output for any legitimate input in a finite amount of time. problem algorithm input "computer" Algorithmic solution output What is an algorithm? g Recipe, process, method, technique, procedure, routine,... with f ll i requirements: ith following i t Finiteness terminates after a finite number of steps 2. 3. 4. 4 5. 1. Definiteness rigorously and unambiguously specified Input valid inputs are clearly specified Output can be proved to produce the correct output given a valid input Effectiveness steps are sufficiently simple and basic Historical Perspective p Euclid's algorithm for finding the greatest common divisor (3rd century BC) Muhammad ibn Musa al-Khwarizmi 9th century almathematician http://en.wikipedia.org/wiki/Al-Khw%C4%81rizm%C4%AB http://en.wikipedia.org/wiki/Al-Kh %C4 81rizm%C4 h // iki di / iki/Al Khw%C %C4 i %C %C4 Why study algorithms? y y g Theoretical importance the core of computer science Practical importance A practitioner's toolkit of known algorithms Framework for designing and analyzing algorithms for new problems bl Two main issues related to algorithms g How to design algorithms g g How to analyze algorithm efficiency Algorithm design techniques/strategies Brute force Divide and conquer Decrease and conquer Transform and conquer Space and time tradeoffs Greedy approach Dynamic programming Iterative improvement Backtracking Branch and bound Analysis of algorithms y g How good is the algorithm? time efficiency space efficiency Does there exist a better algorithm? Important p p problem types yp sorting searching string processing graph problems combinatorial problems geometric problems numerical problems Fundamental data structures list array linked list string stack queue g p graph tree set and dictionary Example of computational p p p problem: sorting g Statement of problem: Input: A sequence of n numbers <a1, a2, ..., an> Output: A reordering of the input sequence <a1, a2, ..., an> so that ai aj whenever i < j Instance: The sequence <5, 3, 2, 8, 3> <5 Algorithms: Selection sort Insertion sort Merge sort (many others) Selection Sort Input: array a[1],...,a[n] a[1 Output: array a sorted in non-decreasing order nonAlgorithm: for i=1 to n swap a[i] with smallest of a[i],...a[n] p [ [ ], [ Some Well-known Computational WellProblems Sorting Searching Shortest paths in a graph Minimum spanning tree Primality testing Traveling salesman problem g p Knapsack problem Chess Towers of Hanoi Program termination Basic Issues Related to Algorithms g How to design algorithms How to express algorithms Proving correctness Efficiency Theoretical analysis Empirical analysis Optimality Analysis of algorithms y g Issues: correctness y time efficiency space efficiency optimality p y Approaches: theoretical analysis empirical analysis Empirical analysis of time efficiency p y y Select a specific (typical) sample of inputs Use physical unit of time (e.g., milliseconds) or Count actual number of basic operation's executions Analyze the empirical data y p Theoretical analysis of time efficiency y y Time efficiency is analyzed by determining the number of repetitions of the basic operation as a function of input size Basic operation: the operation that contributes most towards the running time of the algorithm input size T(n) copC(n) running time execution time for basic operation Number of times basic operation is executed Input size and basic operation examples Problem P bl Searching for key in a list of n items Multiplication of two matrices Checking primality of a given integer n Typical graph problem Input size measure I t i Number of list s items list's items, i.e. n Matrix dimensions or total number of elements Basic B i operation ti Key comparison Multiplication of two numbers Division Visiting a vertex or traversing an edge n'size = number of digits (in binary representation) #vertices and/or edges BestBest-case, average-case, worst-case averageworstFor some algorithms efficiency depends on form of input: Worst case: Cworst(n) maximum over inputs of size n p Best case: Cbest(n) minimum over inputs of size n p Average case: Cavg(n) "average" over inputs of size n g g p Number of times the basic operation will be executed on typical input NOT the average of worst and best case Expected number of basic operations considered as a random variable under some assumption about the probability distribution of all p possible inputs p Example: Sequential search p q Worst case: n Best case: 1 Average case? Types of formulas for basic operation's count Exact formula e.g., C(n) = n(n-1)/2 )/2 Formula indicating order of growth with specific multiplicative constant e.g., C(n) 0.5 n2 Formula indicating order of growth with unknown multiplicative constant e.g., C(n) cn2 Order of growth g Most important: Order of growth within a constant multiple as n Example: How much faster will algorithm run on computer that is twice as fast? How much longer does it take to solve problem of double input size? Values of some important functions as n Asymptotic order of g y p growth A way of comparing functions that ignores constant factors and small input sizes O(g(n)): class of functions f(n) that grow no faster than g(n) (g(n)): class of functions f(n) that grow at same rate as g(n) (g(n)): class of functions f(n) that grow at least as fast as g(n) BigBig-oh g BigBig-omega g g BigBig-theta g Establishing order of growth using the definition Definition: f(n) is in O(g(n)) if order of growth of f(n) order of growth of g(n) (within constant multiple), i.e., there exist positive constant c and non-negative integer nonn0 such that f(n) c g(n) for every n n0 Examples: 10n i O(n2) is 5n+20 is O(n) Orders of growth of some important functions All logarithmic functions loga n belong to the same class (log n) no matter what the logarithm's base a > 1 is hat All polynomials of the same degree k belong to the same class: aknk + ak-1nk-1 + ... + a0 (nk) Exponential functions an have different orders of growth for different a's order log n < order n (>0) < order an < order n! < order nn Basic asymptotic efficiency classes y p y 1 log n constant logarithmic linear n n log n g n2 n3 2n n-log-n g quadratic cubic exponential factorial n! Time efficiency of nonrecursive algorithms General Plan for Analysis Decide on parameter n indicating input size Identify algorithm's basic operation Determine worst, average, and best cases for input of size n Set up a sum for the number of times the basic operation is executed Simplify the sum using standard formulas and rules (see Appendix A) Useful summation formulas and rules liu1 = 1+1+...+1 = u - l + 1 +...+1 In particular, liu1 = n - 1 + 1 = n (n) 1in i = 1+2+...+n = n(n+1)/2 n2/2 (n2) )/2 1in i2 = 12+22+...+n2 = n(n+1)(2n+1)/6 n3/3 (n3) )(2 )/6 0in ai = 1 + a +...+ an = (an+1 - 1)/(a - 1) for any a 1 In particular, 0in 2i = 20 + 21 +...+ 2n = 2n+1 - 1 (2n ) (ai bi ) = ai bi cai = cai liuai = limai + m+1iuai ...
View Full Document

## This note was uploaded on 05/14/2011 for the course CMPS 323 taught by Professor Shaban during the Spring '11 term at Qatar University.

Ask a homework question - tutors are online