Analysis of Algorithm.

Slides:



Advertisements
Similar presentations
CSE Lecture 3 – Algorithms I
Advertisements

HST 952 Computing for Biomedical Scientists Lecture 10.
Fall 2006CENG 7071 Algorithm Analysis. Fall 2006CENG 7072 Algorithmic Performance There are two aspects of algorithmic performance: Time Instructions.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved L17 (Chapter 23) Algorithm.
Complexity Analysis (Part I)
Cmpt-225 Algorithm Efficiency.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 23 Algorithm Efficiency.
Algorithm Analysis CS 201 Fundamental Structures of Computer Science.
Algorithm Efficiency and Sorting Bina Ramamurthy CSE116A,B.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Chapter 23 Algorithm Efficiency.
Main Index Contents 11 Main Index Contents Selection Sort Selection SortSelection Sort Selection Sort (3 slides) Selection Sort Alg. Selection Sort Alg.Selection.
Data Structure Algorithm Analysis TA: Abbas Sarraf
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.
 2006 Pearson Education, Inc. All rights reserved Searching and Sorting.
Analysis of Algorithms Spring 2015CS202 - Fundamentals of Computer Science II1.
Analysis of Algorithms Dilemma: you have two (or more) methods to solve problem, how to choose the BEST? One approach: implement each algorithm in C, test.
Abstract Data Types (ADTs) Data Structures The Java Collections API
COMP s1 Computing 2 Complexity
Asymptotic Notations Iterative Algorithms and their analysis
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Chapter 23 Algorithm Efficiency.
Time Complexity Dr. Jicheng Fu Department of Computer Science University of Central Oklahoma.
Week 2 CS 361: Advanced Data Structures and Algorithms
1 Sorting Algorithms (Basic) Search Algorithms BinaryInterpolation Big-O Notation Complexity Sorting, Searching, Recursion Intro to Algorithms Selection.
1 Chapter 24 Developing Efficient Algorithms. 2 Executing Time Suppose two algorithms perform the same task such as search (linear search vs. binary search)
{ CS203 Lecture 7 John Hurley Cal State LA. 2 Execution Time Suppose two algorithms perform the same task such as search (linear search vs. binary search)
Lecture 2 Computational Complexity
Iterative Algorithm Analysis & Asymptotic Notations
(C) 2010 Pearson Education, Inc. All rights reserved. Java How to Program, 8/e.
Design and Analysis of Algorithms - Chapter 21 Analysis of Algorithms b Issues: CorrectnessCorrectness Time efficiencyTime efficiency Space efficiencySpace.
Analysis of Algorithms
Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 22 Developing Efficient Algorithms.
Chapter 10 A Algorithm Efficiency. © 2004 Pearson Addison-Wesley. All rights reserved 10 A-2 Determining the Efficiency of Algorithms Analysis of algorithms.
Analysis of Algorithms Aaron Tan
SortingBigOh Sorting and "Big Oh" Adapted for ASFA from a presentation by: Barb Ericson Georgia Tech Aug 2007 ASFA AP Computer Science.
Program Efficiency & Complexity Analysis. Algorithm Review An algorithm is a definite procedure for solving a problem in finite number of steps Algorithm.
CSC 211 Data Structures Lecture 13
Lecture 5 Jianjun Hu Department of Computer Science and Engineering University of South Carolina CSCE350 Algorithms and Data Structure.
Data Structure Introduction.
© Copyright 2012 by Pearson Education, Inc. All Rights Reserved. 1 Chapter 16 Developing Efficient Algorithms.
Chapter 5 Algorithms (2) Introduction to CS 1 st Semester, 2015 Sanghyun Park.
Java Methods Big-O Analysis of Algorithms Object-Oriented Programming
Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 24 Developing Efficient Algorithms.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 23 Algorithm Efficiency.
تصميم وتحليل الخوارزميات عال311 Chapter 3 Growth of Functions
CS 150: Analysis of Algorithms. Goals for this Unit Begin a focus on data structures and algorithms Understand the nature of the performance of algorithms.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 14 Searching and Sorting.
C++ How to Program, 7/e © by Pearson Education, Inc. All Rights Reserved.
Searching Topics Sequential Search Binary Search.
Analysis of Algorithms Spring 2016CS202 - Fundamentals of Computer Science II1.
GC 211:Data Structures Week 2: Algorithm Analysis Tools Slides are borrowed from Mr. Mohammad Alqahtani.
Chapter 15 Running Time Analysis. Topics Orders of Magnitude and Big-Oh Notation Running Time Analysis of Algorithms –Counting Statements –Evaluating.
LECTURE 9 CS203. Execution Time Suppose two algorithms perform the same task such as search (linear search vs. binary search) and sorting (selection sort.
Data Structures I (CPCS-204) Week # 2: Algorithm Analysis tools Dr. Omar Batarfi Dr. Yahya Dahab Dr. Imtiaz Khan.
Algorithm Analysis 1.
CIS265/506 Cleveland State University – Prof. Victor Matos
Chapter 22 Developing Efficient Algorithms
Analysis of Algorithms
COP 3503 FALL 2012 Shayan Javed Lecture 15
Analysis of Algorithms
Chapter 7 Single-Dimensional Arrays
CS 201 Fundamental Structures of Computer Science
Algorithm Efficiency and Sorting
Analysis of Algorithms
CS2013 Lecture 5 John Hurley Cal State LA.
Algorithm Analysis Bina Ramamurthy CSE116A,B.
Chapter 23 Searching and Sorting
IST311 - CIS265/506 Cleveland State University – Prof. Victor Matos
Analysis of Algorithms
Presentation transcript:

Analysis of Algorithm

Reasons to analyze algorithms Performance prediction Compare the performance of different algorithm for the same task and provide some guarantees on how well they perform Understanding some theoretical bases for how algorithms perform This helps us to avoid performance bugs Clients get poor results because programmer did not understand performance characteristics of the algorithm CS2336: Computer Science II

CS2336: Computer Science II Running time How many time some operation has to be performed in order to get the computation done Suppose two algorithms perform the same task. Which one is better? First approach: implement them in Java and run the programs to get execution time Two problems for this approach: First, The execution time of a particular program is dependent on the system load. Second, the execution time is dependent on specific input. Consider linear search and binary search for example. If an element to be searched happens to be the first in the list, linear search will find the element quicker than binary search. First, there are many tasks running concurrently on a computer Second, CS2336: Computer Science II

Running time: Growth Rate Second approach: Growth Rate Analyze algorithms independent of computers and specific input Approximates the effect of a change on the size of the input We can see how fast the execution time increases as the input size increases you can compare two algorithms by examining their growth rates. CS2336: Computer Science II

Linear Search The linear search approach compares the key element, key, sequentially with each element in the array list. The method continues to do so until the key matches an element in the list or the list is exhausted without a match being found. If a match is made, the linear search returns the index of the element in the array that matches the key. If no match is found, the search returns -1.

Linear Search Animation Key List 3 6 4 1 9 7 3 2 8 3 6 4 1 9 7 3 2 8 3 6 4 1 9 7 3 2 8 3 6 4 1 9 7 3 2 8 3 6 4 1 9 7 3 2 8 3 6 4 1 9 7 3 2 8

CS2336: Computer Science II Big O Notation “Linear Search Algorithm” for (int i = 0; i < n; i++) { if (key == a[i]) { return i; // Found key, return index. } If the key is not in the array, it requires n comparisons for an array of size n If the key is in the array, it requires n/2 comparisons on average The algorithm’s execution time is proportional to the size of the array CS2336: Computer Science II

CS2336: Computer Science II Big O Notation If you double the size of the array, you will expect the number of comparisons to double. The algorithm grows at a linear rate. The growth rate has an order of magnitude of n. Big O notation to abbreviate for “order of magnitude.” The complexity of the linear search algorithm is O(n), pronounced as “order of n.” CS2336: Computer Science II

Best, Worst, and Average Cases For the same input size, an algorithm’s execution time may vary, depending on the input An input that results in the shortest execution time is called the best-case input An input that results in the longest execution time is called the worst-case input worst-case analysis is very useful. You can show that the algorithm will never be slower than the worst-case. Worst-case analysis is easier to obtain and is thus common. CS2336: Computer Science II

Ignoring Multiplicative Constants The linear search algorithm requires: n comparisons in the worst-case n/2 comparisons in the average-case both cases require O(n) time The multiplicative constant (1/2) can be omitted Algorithm analysis is focused on growth rate and multiplicative constants have no impact on growth rates The growth rate for n/2 or 100n is the same as n i.e., O(n) = O(n/2) = O(100n). CS2336: Computer Science II

Ignoring Non-Dominating Terms “Find max number” int max = a[0]; for (int i = 1; i < n; i++) if (a[i] > max) max = a[i]; Return max; It takes n-1 times of comparisons to find maximum number in a list of n elements The complexity of this algorithm is O(n) The Big O notation allows you to ignore the non-dominating part CS2336: Computer Science II

Useful Mathematic Summations CS2336: Computer Science II

Examples: Determining Big-O Repetition Sequence Selection Logarithm CS2336: Computer Science II

Repetition: Simple Loops for (i = 1; i <= n; i++) { k = k + 5; } executed n times constant time Time Complexity T(n) = (a constant c) * n = cn = O(n) Ignore multiplicative constants (e.g., “c”).

Repetition: Nested Loops for (i = 1; i <= n; i++) { for (j = 1; j <= n; j++) { k = k + i + j; } executed n times inner loop executed n times constant time Time Complexity T(n) = (a constant c) * n * n = cn2 = O(n2) Ignore multiplicative constants (e.g., “c”).

Repetition: Nested Loops for (i = 1; i <= n; i++) { for (j = 1; j <= i; j++) { k = k + i + j; } executed n times inner loop executed i times constant time Time Complexity T(n) = c + 2c + 3c + 4c + … + nc = cn(n+1)/2 = (c/2)n2 + (c/2)n = O(n2) Ignore non-dominating terms Ignore multiplicative constants

Repetition: Nested Loops for (i = 1; i <= n; i++) { for (j = 1; j <= 20; j++) { k = k + i + j; } executed n times inner loop executed 20 times constant time Time Complexity T(n) = 20 * c * n = O(n) Ignore multiplicative constants (e.g., 20*c)

Sequence for (j = 1; j <= 10; j++) { k = k + 4; executed } 10 times for (i = 1; i <= n; i++) { for (j = 1; j <= 20; j++) { k = k + i + j; } executed n times inner loop executed 20 times Time Complexity T(n) = c *10 + 20 * c * n = O(n)

T(n) = test time + worst-case (if, else) Selection O(n) if (list.contains(e)) { System.out.println(e); } else for (Object t: list) { System.out.println(t); Let n be list.size(). Executed n times. Time Complexity T(n) = test time + worst-case (if, else) = O(n) + O(n) = O(n)

Constant Time The Big O notation estimates the execution time of an algorithm in relation to the input size. If the time is not related to the input size, the algorithm is said to take constant time with the notation O(1). For example, a method that retrieves an element at a given index in an array takes constant time, because it does not grow as the size of the array increases.

CS2336: Computer Science II Computation of an result = 1; for (i = 1; i <= n; i++) { result *= a ; } O(n) i result 1 a 2 a2 3 a3 … k ak …. n an CS2336: Computer Science II

CS2336: Computer Science II Computation of an result = a; for (i = 1; i <= k; i++) { result = result * result ; } O(lg n) i result 1 a2^i a2 2 a4 3 a8 … k a2^k an n=2k => lg n = k If you square the input size, you only double the time for the algorithm CS2336: Computer Science II

Binary Search For binary search to work, the elements in the array must already be ordered. Without loss of generality, assume that the array is in ascending order. e.g., 2 4 7 10 11 45 50 59 60 66 69 70 79 The binary search first compares the key with the element in the middle of the array.

Consider the following three cases: Binary Search, cont. Consider the following three cases: If the key is less than the middle element, you only need to search the key in the first half of the array. If the key is equal to the middle element, the search ends with a match. If the key is greater than the middle element, you only need to search the key in the second half of the array.

animation Binary Search Key List 8 1 2 3 4 6 7 8 9 8 1 2 3 4 6 7 8 9 8 1 2 3 4 6 7 8 9

Binary Search, cont.

From Idea to Soluton /** Use binary search to find the key in the list */ public static int binarySearch(int[] list, int key) { int low = 0; int high = list.length - 1;   while (high >= low) { int mid = (low + high) / 2; if (key < list[mid]) high = mid - 1; else if (key == list[mid]) return mid; else low = mid + 1; } return -1 - low;

Logarithm: Analyzing Binary Search Each iteration in the algorithm contains a fixed number of operations, denoted by c. Let T(n) denote the time complexity for a binary search on a list of n elements. Without loss of generality, assume n is a power of 2 and k=logn. Since binary search eliminates half of the input after two comparisons T(n) = c + T(n/2) = c + c + T(n/4) = c + c + c + T(n/23) = 4c + T(n/24) = ck + T(n/2k)

Logarithmic Time An algorithm with the O(logn) time complexity is called a logarithmic algorithm. The base of the log is 2, but the base does not affect a logarithmic growth rate, so it can be omitted. The logarithmic algorithm grows slowly as the problem size increases. If you square the input size, you only double the time for the algorithm.

Quadratic Time An algorithm with the O(n2) time complexity is called a quadratic algorithm. The quadratic algorithm grows quickly as the problem size increases. If you double the input size, the time for the algorithm is quadrupled. Algorithms with a nested loop are often quadratic.

animation Insertion Sort The insertion sort algorithm sorts a list of values by repeatedly inserting an unsorted element into a sorted sublist until the whole list is sorted. int[] myList = {2, 9, 5, 4, 8, 1, 6}; // Unsorted 2 9 5 4 8 1 6 2 9 5 4 8 1 6 2 5 9 4 8 1 6 2 4 5 9 8 1 6 2 4 5 8 9 1 6 1 2 4 5 8 9 6 1 2 4 5 6 8 9

How to Insert? The insertion sort algorithm sorts a list of values by repeatedly inserting an unsorted element into a sorted sublist until the whole list is sorted.

CS2336: Computer Science II InsertionSort public static void insertionSort(int a[]) { n = a.length; for (int i = 1; i < n; i++) { int temp = a[i]; int j; for(j = i - 1; j >= 0 && temp < a[j]; j--) { a[j+1] = a[j]; } a[j+1] = temp; CS2336: Computer Science II

Analyzing Insertion Sort At the kth iteration, to insert an element to a array of size k, it may take k comparisons to find the insertion position, and k moves to insert the element. Let T(n) denote the complexity for insertion sort and c denote the total number of other operations such as assignments and additional comparisons in each iteration. So, Ignoring constants and smaller terms, the complexity of the insertion sort algorithm is O(n2).

Towers of Hanoi There are n disks labeled 1, 2, 3, . . ., n, and three towers labeled A, B, and C. No disk can be on top of a smaller disk at any time. All the disks are initially placed on tower A. Only one disk can be moved at a time, and it must be the top disk on the tower.

Towers of Hanoi, cont.

Solution to Towers of Hanoi The Towers of Hanoi problem can be decomposed into three subproblems.

Solution to Towers of Hanoi Move the first n - 1 disks from A to C with the assistance of tower B. Move disk n from A to B. Move n - 1 disks from C to B with the assistance of tower A.

Analyzing Towers of Hanoi Let T(n) denote the complexity for the algorithm that moves disks and c denote the constant time to move one disk, i.e., T(1) is c. So, 2kT(n-k) T(1) = C : n – k = 1 => k = n - 1 Exponential algorithms are not practical. If the disk move at rate of 1 disk per second then it would take 232 = 136 years to move 32 disks

Comparing Common Growth Functions Constant time Logarithmic time Linear time Log-linear time Quadratic time Cubic time Exponential time

CS2336: Computer Science II Questions? CS2336: Computer Science II