RUNNING TIME 10.4 – 10.5 (P. 551 – 555). RUNNING TIME analysis of algorithms involves analyzing their effectiveness analysis of algorithms involves analyzing.

Slides:



Advertisements
Similar presentations
Discrete Structures CISC 2315
Advertisements

Algorithm Analysis.
Razdan with contribution from others 1 Algorithm Analysis What is the Big ‘O Bout? Anshuman Razdan Div of Computing.
Analysys & Complexity of Algorithms Big Oh Notation.
Chapter 1 – Basic Concepts
Fall 2006CENG 7071 Algorithm Analysis. Fall 2006CENG 7072 Algorithmic Performance There are two aspects of algorithmic performance: Time Instructions.
Cmpt-225 Algorithm Efficiency.
Data Structures and Algorithms1 Basics -- 2 From: Data Structures and Their Algorithms, by Harry R. Lewis and Larry Denenberg (Harvard University: Harper.
Analysis of Algorithms 7/2/2015CS202 - Fundamentals of Computer Science II1.
Analysis of Algorithm.
Elementary Data Structures and Algorithms
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)
Spring2012 Lecture#10 CSE 246 Data Structures and Algorithms.
COMP s1 Computing 2 Complexity
Asymptotic Notations Iterative Algorithms and their analysis
Time Complexity Dr. Jicheng Fu Department of Computer Science University of Central Oklahoma.
Algorithm Analysis & Complexity We saw that a linear search used n comparisons in the worst case (for an array of size n) and binary search had logn comparisons.
Analysis of Algorithm Lecture 3 Recurrence, control structure and few examples (Part 1) Huma Ayub (Assistant Professor) Department of Software Engineering.
Program Performance & Asymptotic Notations CSE, POSTECH.
Chapter 2.6 Comparison of Algorithms modified from Clifford A. Shaffer and George Bebis.
Week 2 CS 361: Advanced Data Structures and Algorithms
SEARCHING, SORTING, AND ASYMPTOTIC COMPLEXITY Lecture 12 CS2110 – Fall 2009.
1 Chapter 24 Developing Efficient Algorithms. 2 Executing Time Suppose two algorithms perform the same task such as search (linear search vs. binary search)
Basic Concepts 2014, Fall Pusan National University Ki-Joune Li.
Mathematics Review and Asymptotic Notation
CSC 201 Analysis and Design of Algorithms Lecture 04: CSC 201 Analysis and Design of Algorithms Lecture 04: Time complexity analysis in form of Big-Oh.
Iterative Algorithm Analysis & Asymptotic Notations
Analysis of Algorithms
CS 340Chapter 2: Algorithm Analysis1 Time Complexity The best, worst, and average-case complexities of a given algorithm are numerical functions of the.
SEARCHING (Linear/Binary). Searching Algorithms  method of locating a specific item of information in a larger collection of data.  two popular search.
Searching. RHS – SOC 2 Searching A magic trick: –Let a person secretly choose a random number between 1 and 1000 –Announce that you can guess the number.
Analysis of Algorithms These slides are a modified version of the slides used by Prof. Eltabakh in his offering of CS2223 in D term 2013.
Analysis of Algorithms CSCI Previous Evaluations of Programs Correctness – does the algorithm do what it is supposed to do? Generality – does it.
Asymptotic Notation (O, Ω, )
Program Efficiency & Complexity Analysis. Algorithm Review An algorithm is a definite procedure for solving a problem in finite number of steps Algorithm.
Chapter 5 Algorithms (2) Introduction to CS 1 st Semester, 2015 Sanghyun Park.
3.3 Complexity of Algorithms
Algorithmic Analysis Charl du Plessis and Robert Ketteringham.
1 Asymptotic Notations Iterative Algorithms and their analysis Asymptotic Notations –Big O,  Notations Review of Discrete Math –Summations –Logarithms.
General rules: Find big-O f(n) = k = O(1) f(n) = a k n k + a k-1 n k a 1 n 1 + a 0 = O(n k ) Other functions, try to find the dominant term according.
Algorithm Analysis (Big O)
27-Jan-16 Analysis of Algorithms. 2 Time and space To analyze an algorithm means: developing a formula for predicting how fast an algorithm is, based.
تصميم وتحليل الخوارزميات عال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.
Algorithm Analysis. What is an algorithm ? A clearly specifiable set of instructions –to solve a problem Given a problem –decide that the algorithm is.
Searching Topics Sequential Search Binary Search.
CISC220 Spring 2010 James Atlas Lecture 07: Big O Notation.
DS.A.1 Algorithm Analysis Chapter 2 Overview Definitions of Big-Oh and Other Notations Common Functions and Growth Rates Simple Model of Computation Worst.
Search Algorithms Written by J.J. Shepherd. Sequential Search Examines each element one at a time until the item searched for is found or not found Simplest.
Dale Roberts Department of Computer and Information Science, School of Science, IUPUI Dale Roberts, Lecturer Computer Science, IUPUI
Basic Concepts 2011, Fall Pusan National University Ki-Joune Li.
Complexity of Algorithms Fundamental Data Structures and Algorithms Ananda Guna January 13, 2005.
Analysis of Algorithms Spring 2016CS202 - Fundamentals of Computer Science II1.
Algorithm Analysis 1.
Mathematical Foundation
Introduction to Algorithms
David Kauchak CS52 – Spring 2015
CSS 342 Data Structures, Algorithms, and Discrete Mathematics I
Analysys & Complexity of Algorithms
Programming and Data Structure
Chapter 2.
Programming and Data Structure
Analyzing an Algorithm Computing the Order of Magnitude Big O Notation
CSE 2010: Algorithms and Data Structures Algorithms
Searching, Sorting, and Asymptotic Complexity
Searching, Sorting, and Asymptotic Complexity
Algorithms Analysis Algorithm efficiency can be measured in terms of:
At the end of this session, learner will be able to:
Presentation transcript:

RUNNING TIME 10.4 – 10.5 (P. 551 – 555)

RUNNING TIME analysis of algorithms involves analyzing their effectiveness analysis of algorithms involves analyzing their effectiveness need way of "guessing" how fast the algorithm will run without actually programming it and running it need way of "guessing" how fast the algorithm will run without actually programming it and running it to determine the speed, you must consider running time regardless of size of data set used for input to determine the speed, you must consider running time regardless of size of data set used for input this is known as time complexity, and we often want to consider the worst case this is known as time complexity, and we often want to consider the worst case

What is Big-O Notation goal is to identify the dominant factor in determining the execution time of our algorithm goal is to identify the dominant factor in determining the execution time of our algorithm in most cases, this is some function of the size of the input data, denoted by n in most cases, this is some function of the size of the input data, denoted by n goal then is to see how the execution time varies with size of data set goal then is to see how the execution time varies with size of data set we consider the steps of an algorithm may be: we consider the steps of an algorithm may be: 1) constant (will not change regardless of n) example: x[0] = initvalue; x[n-1] = initvalue; example: x[0] = initvalue; x[n-1] = initvalue; 2) a function of n (changes with input size) example: example: for (int i = 1; i < n; i++) for (int i = 1; i < n; i++) x[i] = x[i-1] * factor; x[i] = x[i-1] * factor; (number of steps executed changes with size of n)

BIG-O Notation So, How Does this Relate to Big-O? - Big-O is the notation used for analyzing the upper bound of an algorithm - in the above examples, you can easily count the steps, but this is not always the case (sometimes it is almost impossible to tell how many steps will be taken) - thus, we usually talk about best case, average case or worst cast; - Big-O most often refers to worst case

BIG-O Notation (Definition): f(n) = O(g(n)) if and only if there exists positive constants C and n 0 such that: f(n) = O(g(n)) if and only if there exists positive constants C and n 0 such that: f(n) = n 0 f(n) = n 0 - in other words: for all sufficiently large n, g(n) is an upper-bound for f for all sufficiently large n, g(n) is an upper-bound for f - Explanation: - we are trying to predict the "time" of the algorithm using a function of the input size (n); - in other words, trying to determine f(n) - Big-O tries to determine the upper-bound for f(n) using another function, g(n) - bottom line: Big-O is used for stating the upper bound of the running time of an algorithm; the upper-bound ignores constants since for presumable large n, constants get lost anyway

BIG-O NOTATION O(1): constant time; doesn't change with problem size O(1): constant time; doesn't change with problem size O(logn): logarithmic; very slow growing with problem size O(n): linear; increases at same rate as the problem size O(nlogn): More than linear, but not by much O(n 2 ): Quadratic; when size doubles, time quadruples O(n 3 ): Cubic; when size doubles, time increases eightfold O(2 n ): Exponential O(n!): factorial; HUGE increase in time! Steps for determining complexity: Steps for determining complexity: 1) Break the algorithm down into steps and analyze the complexity of each 1) Break the algorithm down into steps and analyze the complexity of each Example: with a loop, analyze the body first and see how many times it executes Example: with a loop, analyze the body first and see how many times it executes 2) Look for for-loops. They are easiest to analyze - give a clear upperbound 2) Look for for-loops. They are easiest to analyze - give a clear upperbound 3) Look for loops that operate over an entire data structure 3) Look for loops that operate over an entire data structure

EXAMPLES OF BIG-O Example 1: Example 1: for (int i = 0; i < n; i++) x[i] = 0; x[i] = 0; - Time Complexity: O(n) - running time is directly related to the size of n loop will run n times, - Example 2: Linear Search: int LinearSearch(int a[], int listSize, int item) { int LinearSearch(int a[], int listSize, int item) { for (int pos=0; pos < listSize; pos++) { for (int pos=0; pos < listSize; pos++) { if (a[pos] == item) if (a[pos] == item) return pos; return pos; } return -1; } return -1;} - Time Complexity: O(n)

EXAMPLES OF BIG-O ) Example for Binary Search: ) Example for Binary Search: int BinarySearch(int a[], int first, int last, int item) { if (last < First) if (last < First) return -1; return -1; else { else { int middle = (last + first) / 2; int middle = (last + first) / 2; if (item == a[middle]) if (item == a[middle]) return middle; return middle; else (if a[middle] > item) else (if a[middle] > item) return BinarySearch(a,first,middle-1,item); return BinarySearch(a,first,middle-1,item); else else return BinarySearch(a,middle+1,last,item); return BinarySearch(a,middle+1,last,item); }}

BIG-O OF BINARY SEARCH list is halved each time BS is called list is halved each time BS is called maximum number of comparisons: maximum number of comparisons: 1) if n = 1, algorithm invoked 2 times 1) if n = 1, algorithm invoked 2 times 2) if n > 1, algorithm invoked 2 m times 2) if n > 1, algorithm invoked 2 m times where m is size of sequence being searched where m is size of sequence being searched 3) thus, total number of invocations: 3) thus, total number of invocations: a n = 1 + a n/2 a n = 1 + a n/2 which is called the recurrance relation 4) Thus, by solving the recurrance relation, we get: 4) Thus, by solving the recurrance relation, we get: a 2k = 1 + a 2k-1 a 2k = 1 + a 2k-1 2 k-1 < n <= 2 k 2 k-1 < n <= 2 k k-1 < log n <= k k-1 < log n <= k so, Algorithm complexity: O(log n) so, Algorithm complexity: O(log n) [see discrete textbook for more details] [see discrete textbook for more details]

QUESTIONS? Read HASHING Section ( ) Read HASHING Section ( )