Programming and Data Structure

Slides:



Advertisements
Similar presentations
Chapter 1 – Basic Concepts
Advertisements

Fall 2006CENG 7071 Algorithm Analysis. Fall 2006CENG 7072 Algorithmic Performance There are two aspects of algorithmic performance: Time Instructions.
Chapter 10 Algorithm Efficiency
Introduction to Analysis of Algorithms
Algorithm Analysis CS 201 Fundamental Structures of Computer Science.
Analysis of Algorithms 7/2/2015CS202 - Fundamentals of Computer Science II1.
Analysis of Algorithms COMP171 Fall Analysis of Algorithms / Slide 2 Introduction * What is Algorithm? n a clearly specified set of simple instructions.
Analysis of Algorithms Spring 2015CS202 - Fundamentals of Computer Science II1.
Algorithm Analysis (Big O)
Algorithm Analysis Dr. Bernard Chen Ph.D. University of Central Arkansas.
Chapter 6 Algorithm Analysis Bernard Chen Spring 2006.
1 Chapter 2 Program Performance – Part 2. 2 Step Counts Instead of accounting for the time spent on chosen operations, the step-count method accounts.
Chapter 2.6 Comparison of Algorithms modified from Clifford A. Shaffer and George Bebis.
Week 2 CS 361: Advanced Data Structures and Algorithms
1 Recursion Algorithm Analysis Standard Algorithms Chapter 7.
Algorithm Analysis. Algorithm An algorithm is a clearly specified set of instructions which, when followed, solves a problem. recipes directions for putting.
Analysis of Algorithms
CSS342: Algorithm Analysis1 Professor: Munehiro Fukuda.
Program Efficiency & Complexity Analysis. Algorithm Review An algorithm is a definite procedure for solving a problem in finite number of steps Algorithm.
Geoff Holmes and Bernhard Pfahringer COMP206-08S General Programming 2.
Algorithm Analysis Dr. Bernard Chen Ph.D. University of Central Arkansas Fall 2008.
CE 221 Data Structures and Algorithms Chapter 2: Algorithm Analysis - I Text: Read Weiss, §2.1 – Izmir University of Economics.
Algorithm Analysis Part of slides are borrowed from UST.
Chapter 2 Computational Complexity. Computational Complexity Compares growth of two functions Independent of constant multipliers and lower-order effects.
Algorithm Analysis Chapter 5. Algorithm An algorithm is a clearly specified set of instructions which, when followed, solves a problem. –recipes –directions.
CISC 235: Topic 1 Complexity of Iterative Algorithms.
Algorithm Analysis (Big O)
Complexity Analysis. 2 Complexity The complexity of an algorithm quantifies the resources needed as a function of the amount of input data size. The resource.
Algorithm Analysis. What is an algorithm ? A clearly specifiable set of instructions –to solve a problem Given a problem –decide that the algorithm is.
Vishnu Kotrajaras, PhD.1 Data Structures
1 Chapter 2 Algorithm Analysis All sections. 2 Complexity Analysis Measures efficiency (time and memory) of algorithms and programs –Can be used for the.
1 Chapter 2 Algorithm Analysis Reading: Chapter 2.
Complexity of Algorithms Fundamental Data Structures and Algorithms Ananda Guna January 13, 2005.
GC 211:Data Structures Week 2: Algorithm Analysis Tools Slides are borrowed from Mr. Mohammad Alqahtani.
Algorithmic Foundations COMP108 COMP108 Algorithmic Foundations Algorithm efficiency Prudence Wong.
Algorithmic Foundations COMP108 COMP108 Algorithmic Foundations Algorithm efficiency Prudence Wong
Data Structures I (CPCS-204) Week # 2: Algorithm Analysis tools Dr. Omar Batarfi Dr. Yahya Dahab Dr. Imtiaz Khan.
Algorithm Analysis 1.
Chapter 2 Algorithm Analysis
Introduction to Analysis of Algorithms
Analysis of Algorithms
Analysis of Algorithms
COP 3503 FALL 2012 Shayan Javed Lecture 15
GC 211:Data Structures Week 2: Algorithm Analysis Tools
COMP108 Algorithmic Foundations Algorithm efficiency
Analysis of Algorithms
Introduction to Algorithms
GC 211:Data Structures Algorithm Analysis Tools
Time Complexity Analysis Neil Tang 01/19/2010
Algorithm Analysis Neil Tang 01/22/2008
Algorithm Analysis (not included in any exams!)
GC 211:Data Structures Algorithm Analysis Tools
CSC 413/513: Intro to Algorithms
Introduction to Algorithms Analysis
Algorithm Efficiency Chapter 10.
CS 201 Fundamental Structures of Computer Science
Analysis of Algorithms
DS.A.1 Algorithm Analysis Chapter 2 Overview
Programming and Data Structure
Algorithm Analysis Bina Ramamurthy CSE116A,B.
Analyzing an Algorithm Computing the Order of Magnitude Big O Notation
CE 221 Data Structures and Algorithms
CE 221 Data Structures and Algorithms
Math/CSE 1019N: Discrete Mathematics for Computer Science Winter 2007
At the end of this session, learner will be able to:
At the end of this session, learner will be able to:
CS210- Lecture 2 Jun 2, 2005 Announcements Questions
Estimating Algorithm Performance
Analysis of Algorithms
Algorithms and data structures: basic definitions
Presentation transcript:

Programming and Data Structure Algorithm Analysis December 29, 2018 Programming and Data Structure

Programming and Data Structure What is an algorithm ? A clearly specifiable set of instructions to solve a problem December 29, 2018 Programming and Data Structure

Algorithm vs. Programs A computer program is one concrete implementation of an algorithm using a particular computer language The design phase should produce an algorithm The implementation phase should produce a program

Algorithm Performance It would be great if you could code up an algorithm and then run it with a bunch of input. Take a look at the clock and decide how well it ran A better way is to approximate or estimate the performance of an algorithm.

Complexity Analysis An objective way to evaluate the cost of an algorithm or code section. The cost is computed in terms of space or time, usually

Analysis of Algorithms How much resource is required ? Measures for efficiency Execution time  time complexity Memory space  space complexity Observation : The larger amount of input data an algorithm has, the larger amount of resource it requires. Complexities are functions of the amount of input data (input size). December 29, 2018 Programming and Data Structure

What do we use for a yardstick? The same algorithm will run at different speeds and will require different amounts of space when run on different computers, different programming languages, different compilers. But algorithms usually consume resources in some fashion that depends on the size of the problem they solve : n. December 29, 2018 Programming and Data Structure

Programming and Data Structure Sorting integers void sort (int A[], int N) { int i, j, x; for (i=1; i<N; i++) x = A[i]; for (j=i; j>0 && x<A[j-1]; j- -) A[j] = A[j-1]; A[j] = x; } December 29, 2018 Programming and Data Structure

Programming and Data Structure We run this sorting algorithm on two different computers, and note the time (in ms) for different sizes of input. December 29, 2018 Programming and Data Structure

Programming and Data Structure Contd. Home Computer : f1(n) = 0.0007772 n2 + 0.00305 n + 0.001 Desktop Computer : f2(n) = 0.0001724 n2 + 0.00040 n + 0.100 Both are quadratic function of n. The shape of the curve that expresses the running time as a function of the problem size stays the same. December 29, 2018 Programming and Data Structure

Programming and Data Structure Complexity classes The running time for different algorithms fall into different complexity classes. Each complexity class is characterized by a different family of curves. All curves in a given complexity class share the same basic shape. The O-notation is used for talking about the complexity classes of algorithms. December 29, 2018 Programming and Data Structure

Introducing the language of O-notation For the quadratic function f(n) = an2 + bn + c we will say that f(n) is O(n2). We focus on the dominant term, and ignore the lesser terms; then throw away the coefficient. December 29, 2018 Programming and Data Structure

Mathematical background T(N) = O(f(N)) if there are positive constants c and n0 such that T(N)  c f(N) when N  n0. Meaning : As N increases, T(N) grows no faster than f(N). The function T is eventually bounded by some multiple of f(N). f(N) gives an upper bound in the behavior of T(N). T(N) = (g(N)) if there are positive constants c and n0 such that T(N) c f(N) when N  n0. Meaning : As N increases, T(N) grows no slower than g(N) ; T(N) grows at least as fast as g(N).

Programming and Data Structure Contd. T(N) = (h(N)) if and only if T(N) = O (h(N)) and T(N) = (h(N)) Meaning : As N increases, T(N) grows as fast as h(N). T(N) = o(p(N)) if T(N) = O(p(N)) and T(N)  (p(N)) Meaning : As N increases, T(N) grows slower than p(N). lim nT(N)/p(N) = 0. December 29, 2018 Programming and Data Structure

Common growth rates

Programming and Data Structure Examples logen = O(n) n10 = o(2n) 3 n2 + 5n + 1 = (n2) December 29, 2018 Programming and Data Structure

Programming and Data Structure Concepts in Analysis Worst Case Average case (expected value) Operator count Why is the analysis of algorithms important ? Can advance on hardware overcome inefficiency of your algorithm ?  NO ! December 29, 2018 Programming and Data Structure

Programming and Data Structure Model of computation In a normal computer, instructions are executed sequentially. addition, multiplication, comparison, assignment, etc. all are assumed to take a single time unit. December 29, 2018 Programming and Data Structure

Running time of algorithms Assume speed S is 107 instructions per second. December 29, 2018 Programming and Data Structure

Maximum size solvable in one hour

Programming and Data Structure Observations There is a big difference between polynomial time complexity and exponential time complexity Hardware advances affect only efficient algorithms and do not help inefficient algorithms. December 29, 2018 Programming and Data Structure

Maximum subsequence sum problem Given (possibly negative) integers <A1 A2 . . . AN> find the maximum value of jk=i Ak . For convenience, the maximum subsequence sum is considered to be 0 if all the integers are negative. Example : For input <-2,11,-4,13,-5,2> the answer is 20 (A2 to A4) December 29, 2018 Programming and Data Structure

Programming and Data Structure Algorithm 1 int MaxSubSum (int A[], int N) { int thissum, maxsum, i,j,k; 1. maxsum = 0; 2. for (i=0; i<N; i++) 3. for (j=i; j<N; j++) { 4. thissum = 0; 5. for (k=i; k <= j; k++) 6. thissum += A[k]; 7. if (thissum > maxsum) 8. maxsum = thissum; } 9. return maxsum; December 29, 2018 Programming and Data Structure

Programming and Data Structure The loop at line 2 is of size N. The second loop has size N-i. The third loop has size j-i+1 Total : about N3 steps jk=i 1 = j-i+1 jk=i (j-i+1) = (N-i+1)(N-i)/2 N-1i=0 (N-i+1)(N-i)/2 = (N3 + 3N2 + 2N)/6 December 29, 2018 Programming and Data Structure

Improve the running time Remove the second for loop Observe : jk=i Ak = Aj + j-1k=i Ak December 29, 2018 Programming and Data Structure

Programming and Data Structure Algorithm 2 int MaxSubSum2 (int A[], int N) { int thissum, maxsum, i, j; 1. maxsum = 0; for (i=0; i<N; i++) 3. thissum = 0; for (j=i; j < N; j++) 5. thissum += A[j]; 6. if (thissum > maxsum) 7. maxsum = thissum; } 8. return maxsum; Complexity : O(N2) December 29, 2018 Programming and Data Structure

Search in a sorted array Given an integer X, and integers <A0 A1. . . AN-1> which are presorted and already in memory, find i such that Ai = X, or return i = -1 if X is not in the input. December 29, 2018 Programming and Data Structure

Programming and Data Structure Linear Search int search (int A[], int X, int N) { int i; for (i=0; i<N; i++) if (A[i] == X) return i; return -1; } Complexity : (N) December 29, 2018 Programming and Data Structure

Programming and Data Structure Binary Search int BinarySearch (int A[], int X, int N) { int low, mid, high; while (low <= high) { mid = (low+high)/2; if (A[mid] < X) low = mid+1; else if (A[mid] > X) high = mid-1; else return mid; } return -1; December 29, 2018 Programming and Data Structure

Programming and Data Structure Binary Search Illustrated possible positions for what we are looking for ruled out as a possible position for what we are looking for December 29, 2018 Programming and Data Structure

Analysis of binary search All the work done inside the loop takes O(1) time per iteration. Number of times the loop is executed : The loop starts with high -low = N-1 Finishes with high -low 1 Every time through the loop the value of high -low is at least halved from its previous value. is at most log2(N-1) + 2 = O(log N). December 29, 2018 Programming and Data Structure

Programming and Data Structure Sorting integers void sort (int A[], int N) { int i, j, x; for (i=1; i<N; i++) { x = A[i]; for (j=i; j>0 && x<A[j-1]; j--) A[j] = A[j-1]; A[j] = x; } T(N) = 1+2+ ... + N-1 = N(N-1)/2  (N2) December 29, 2018 Programming and Data Structure

Explosive Example int boom (int M, int X) { if (M==0) return H(X); return boom (M-1, Q(X)) + boom(M-1, R(X); }

Explosive Example int boom (int M, int X) { if (M==0) return H(X); return boom (M-1, Q(X)) + boom(M-1, R(X); } T(N) = 2T(N-1) + 1 = 2(2T(N-2)+1) + 1 . . . = 2(...(2.0+1)+1)...+1 = 0jM-1 2j =2M -1 M M

Programming and Data Structure Worst Case Analysis Suppose that all the cases fall in one of n cases: x1, x2, ... , xn ci denotes the cost for case xi. Worst case complexity = max{ci|1<=i<=n} Example : Sequential search on a table. There are n+1 cases Worst case time complexity = n December 29, 2018 Programming and Data Structure

Programming and Data Structure Average Case Analysis Suppose that all the cases fall in one of n cases: x1, x2, ... , xn ci denotes the cost for case xi. pi denotes the probability of xi. Average case complexity = ni=1 pi ci Example : Sequential search on a table (the key is in the table and every key is equally likely) There are n cases, each w.p. 1/n. Average case time complexity = ni=1 i / n = (n+1)/2 December 29, 2018 Programming and Data Structure