Algorithm Analysis. Review of Terminology Some Consequences of Growth Rates. The book s big example: Detecting anagrams. There be math here!

Size: px
Start display at page:

Download "Algorithm Analysis. Review of Terminology Some Consequences of Growth Rates. The book s big example: Detecting anagrams. There be math here!"

Transcription

1 Algorithm Analysis Review of Terminology Some Consequences of Growth Rates There be math here! The book s big example: Detecting anagrams 4 different algorithms!

2 Big O Definition: T(N) = O(f(N)) if there exist positive constants c, n 0 such that: T(N) c f(n) for all N n 0 Idea: We are concerned with how the function grows when N is large. We are not picky about constant factors: coarse distinctions among functions Lingo: "T(N) grows no faster than f(n)." 2

3 Big O c, n 0 > 0 such that f(n) c g(n) when N n 0 f(n) grows no faster than g(n) for large N 3

4 Preferred big-o usage pick tightest bound. If f(n) = 5N, then: f(n) = O(N 5 ) f(n) = O(N 3 ) f(n) = O(N log N) f(n) = O(N) preferred ignore constant factors and low order terms T(N) = O(N), not T(N) = O(5N) T(N) = O(N 3 ), not T(N) = O(N 3 + N 2 + N log N) 4

5 Big-O of selected functions 5

6 An O(1) algorithm is constant time. The running time of such an algorithm is essentially independent of the input. Such algorithms are rare, since they cannot even read all of their input. An O(log b n) [for some b] algorithm is logarithmic time. We do not care what b is. An O(n) algorithm is linear time. Such algorithms are not rare. This is as fast as an algorithm can be and still read all of its input. An O(n log b n) [for some b] algorithm is log-linear time. This is about as slow as an algorithm can be and still be truly useful (scalable). An O(n 2 ) algorithm is quadratic time. These are usually too slow. An O(b n ) [for some b] algorithm is exponential time. These algorithms are much too slow to be useful. 6

7 Hierarchy of Big-O Functions, ranked in increasing order of growth: 1 log log n log n n n log n n 2 n 2 log n n n n! n n 7

8 Various growth rates T ( n) ( 1):Constant time T ( n) (log n): logarithmic time T ( n) ( n): linear time T ( n) ( nlog n):famous for sorting T ( n) ( n T ( n) ( n T ( n) (2 2 k n ): quadratic ): polynomial time ), T ( n) ( n n time ), T ( n) ( n!): exponential Practical for small values of n (e.g., n =10 or n = 20) time; 8

9 Program loop runtimes Suppose we have a list, mylist, with n elemenst: for x in mylist: // O(n) statement(s) The loop runtime grows linearly when compared to its maximum value n. 9

10 Program loop runtimes counter = 0 while counter < n: statement(s) counter += 1 What s the big-o? 10

11 Program loop runtimes counter = 0 while counter < n: statement(s) counter += 2 What s the big-o? 11

12 Program loop runtimes counter = 0 while counter < n*n: statement(s) counter += 1 What s the big-o? 12

13 Program loop runtimes Suppose we have a list, mylist, with n elemenst: for x in mylist: for y in mylist: statement(s) Nesting loops multiplies their runtimes. The outer loop is O(n). The inner loop is O(n). Multiply. The total big-o is O(n 2 ). 13

14 Program loop runtimes counter = 0 while counter < n: counter2 = 0 while counter2 < n: counter3 = 0 while counter3 < n: statements counter3 += 1 counter2 += 1 counter += 1 What s the big-o? 14

15 How does this translate to practical problems? If linear relationship, If input doubles, time doubles. If input triples, time triples. If quadratic relationship, If input doubles, time quadruples (2 2 ) If input triples, time increases ninefold (3 2 ) If cubic relationship, If input doubles, time increases eightfold If input triples, time increases twenty-sevenfold How about n log n?

16 Let s do some examples Suppose it takes 100 ms to process 1000 items in a list. How long will it take to process 10,000 items? It depends Is the algorithm Linear? Quadratic? Cubic? NlogN? 16

17 The linear solution Suppose it takes 100 ms to process 1000 items in a list. How long will it take to process 10,000 items? 100/1000 = t/10000 t = 1000 It will take 1,000 ms or 10 times longer. 17

18 The quadratic solution Suppose it takes 100 ms to process 1000 items in a list. How long will it take to process 10,000 items? 100/(1000) 2 = t/(10000) 2 100/ = t/ t = It will take 10,000 ms or 100 times longer. 18

19 The cubic solution Suppose it takes 100 ms to process 1000 items in a list. How long will it take to process 10,000 items? 100/(1000) 3 = t/(10000) 3 100/ = t/ t = 100,000 It will take 100,000 ms or 1000 times longer. 19

20 The log-linear solution Suppose it takes 100 ms to process 1000 items in a list. How long will it take to process 10,000 items? 100/1000log(1000) = t/10000log(10000) ~100/(1000*10) = t/(10000*13) ~1,300 It will take 1,300 ms. 20

21 The same problem, but with a different unknown variable Suppose it takes 100 ms to process 1000 items in a list. How many can we process if with have 400 ms? It depends Is the algorithm Linear? Quadratic? Cubic? NlogN? 21

22 The linear solution Suppose it takes 100 ms to process 1000 items in a list. How many can we process if with have 400 ms? 100/1000 = 400/n n = 4000 We can process 4000 items (or 4 times more) 22

23 The quadratic solution Suppose it takes 100 ms to process 1000 items in a list. How many can we process if with have 400 ms? 100/(1000) 2 = 400/(n) 2 100/ = 400/n 2 n 2 = 4,000,000 We can process 2,000 elements twice as many (or SQRT(4) as many). 23

24 The cubic solution Suppose it takes 100 ms to process 1000 items in a list. How many can we process if with have 400 ms? 100/(1000) 3 = 400/(n) 3 100/ = 400/n 3 n 3 = 4,000,000,000 We can process 1,587 items. 24

25 The log-linear solution Suppose it takes 100 ms to process 1000 items in a list. How many can we process if with have 400 ms? 100/1000log(1000) = 400/nlog(n) ~100/(1000*10) = 400/nlog(n) ~40,000 = nlogn (Use Wolfram Alpha) ~3,408 items 25

26 The Anagram Detection Example (2.2.2) What is an anagram? heart and earth python and typhon Our problem: Assume we have two words of equal length and all lower case How can we tell if they are anagrams? 26

27 The Book Gives Us 4 Solutions Checking Off: Listing 2.6 Sort and Compare Listing 2.7 Brute Force No Listing (darn it!) Count and Compare Listing

28 Checking Off For every character in the first string, make sure there is a corresponding character somewhere in the 2 nd string. When a character has been checked off, we want to mark it with the special value None. But strings are immutable, so the authors first convert the 2 nd string to a list 28

29 Here s the code: Pay attention to Big-O def anagramsolution1(s1,s2): alist = list(s2) pos1 = 0 stillok = True while pos1 < len(s1) and stillok: pos2 = 0 found = False while pos2 < len(alist) and not found: if s1[pos1] == alist[pos2]: found = True else: pos2 = pos2 + 1 if found: alist[pos2] = None else: stillok = False pos1 = pos1 + 1 return stillok 29

30 Big-O of Checking Off For every element of the first string (of length n), We will visit as many as n characters in the 2 nd string Each character in the 2 nd string must be marked In the worst case, finding the first character might take n compares. Finding the second character would take n 1. Finding the third would take n 2. Etc n n(n+1)/2 = (n^2 + n)/2 = O(n 2 ) 30

31 Look back at the code and see if it looks quadratic Notice the loop inside of the loop. What s the worst case for the big-o of each loop? Multiply nested loops. 31

32 Solution 2: Sort and Compare If the characters are sorted in each string, the end result must be equal strings! So make a list of each string, Sort them Compare the resulting lists. 32

33 Solution 2: Code def anagramsolution2(s1,s2): alist1 = list(s1) alist2 = list(s2) alist1.sort() alist2.sort() pos = 0 matches = True while pos < len(s1) and matches: if alist1[pos]==alist2[pos]: pos = pos + 1 else: matches = False return matches 33

34 Big O of Solution 2 Sorting takes nlogn time So the big O of this code looks like O(nlogn + nlogn + n) = O(2nlogn + n) O(nlogn) 34

35 Solution 3 (Brute Force) Take string1. Generate all of its anagrams. See if any of them are string2. Why didn t the book implement this? No good reason. I ll give a quick and dirty version on the next page. 35

36 Solution which hides all the dirty work from itertools import permutations def anagramsolution3(s1,s2): perms = [''.join(p) for p in permutations(s1)] for x in perms: if x == s2: return True return False 36

37 So why didn t the book show you the code? How many permutations are there of a string of length n? How many choices for the first character? The next? The next?. n*(n-1)*(n-2)* *3*2*1 N! Yikes. 37

38 Solution 4: Count and Compare Key insight: Each string must contain the same number of a s, b s, c s, and so on. So for each string, count up the occurrence of each of the 26 characters. Then at the end, see if those counts of the 26 characters are the same. 38

39 Solution 4 Code: Pay attention to big-o def anagramsolution4(s1,s2): c1 = [0]*26 c2 = [0]*26 for i in range(len(s1)): pos = ord(s1[i])-ord('a') c1[pos] = c1[pos] + 1 for i in range(len(s2)): pos = ord(s2[i])-ord('a') c2[pos] = c2[pos] + 1 j = 0 stillok = True while j<26 and stillok: if c1[j]==c2[j]: j = j + 1 else: stillok = False return stillok 39

40 Let s try out anagramsolution1 In listing_2_6, add some lines to set two varables, maybe a and b, to two strings like bat and tab. Run the anagram program on those two strings and print the result. 40

41 Using the Debugger in PyCharm Debugging is an important skill in programming. Often we debug by inserting print statements in our programs which works really well for limited debugging. But, for really in-depth debugging we can use debugging tools within good IDEs. 41

42 In PyCharm I ll show you how to set breakpoints There are also some YouTube videos if you can t remember what we do in lab Once breakpoints are set, use Run Debug. The code will stop at designated breakpoints. You can look at variable values You can step line by line, stepping into or over methods as you desire. 42

43 Let s also step through the other two programs First Sort and Compare. Then Count and Compare. 43

44 Problem 3 The book gives code for 3 solutions for determining whether two strings are anagrams of each other (Listing 2.6, Listing 2.7, and Listing 2.8). In this problem, you will experimentally determine the relative growth rates of each method by running them on strings of length 100 up to 1,000 (step 50) Here s a random method for generating a random string of length size: import string import random def randomstring(size): return ''.join(random.choice(string.ascii_lowercase) for _ in range(size)) 44

45 More on Problem 3 Each method will take the longest if it works on an anagram. Here s a way to rearrange a string randomly: def makeanagram(s): temp = s; t = '' while temp!= '': i = random.randrange(0, len(temp)) t += temp[i] temp = temp[:i] + temp[(i+1):] return t 45

46 More on Problem 3 o In a for-loop, vary n from 100 to 1000, step 50. Create a random string of length n. Call it s. Make an anagram of s by calling makeanagram. Call it t. Now time each of the three methods by passing in the parameters s and t. o Time how long each method to determine whether the string is an anagram. Make your output look something like this so that it is easy to graph: N Sol1 Sol2 Sol

47 More on Problem 3 o You should notice that Sol1 (Solution 1, Listing 2.6) takes much, much longer than the other two methods. o Now let s just look at the last two methods. We can run these for much larger sizes. Let s do another experiment without doing Listing 2.6. Just looking at the last two methods, time them for n = up to 100,000, step 5,000. Graph them. 47

What is an algorithm?

What is an algorithm? Reminders CS 142 Lecture 3 Analysis, ADTs & Objects Program 1 was assigned - Due on 1/27 by 11:55pm 2 Abstraction Measuring Algorithm Efficiency When you utilize the mylist.index(item) function you are

More information

PROGRAM EFFICIENCY & COMPLEXITY ANALYSIS

PROGRAM EFFICIENCY & COMPLEXITY ANALYSIS Lecture 03-04 PROGRAM EFFICIENCY & COMPLEXITY ANALYSIS By: Dr. Zahoor Jan 1 ALGORITHM DEFINITION A finite set of statements that guarantees an optimal solution in finite interval of time 2 GOOD ALGORITHMS?

More information

Algorithm Analysis. College of Computing & Information Technology King Abdulaziz University. CPCS-204 Data Structures I

Algorithm Analysis. College of Computing & Information Technology King Abdulaziz University. CPCS-204 Data Structures I Algorithm Analysis College of Computing & Information Technology King Abdulaziz University CPCS-204 Data Structures I Order Analysis Judging the Efficiency/Speed of an Algorithm Thus far, we ve looked

More information

CMPSCI 187: Programming With Data Structures. Lecture 5: Analysis of Algorithms Overview 16 September 2011

CMPSCI 187: Programming With Data Structures. Lecture 5: Analysis of Algorithms Overview 16 September 2011 CMPSCI 187: Programming With Data Structures Lecture 5: Analysis of Algorithms Overview 16 September 2011 Analysis of Algorithms Overview What is Analysis of Algorithms? L&C s Dishwashing Example Being

More information

Measuring algorithm efficiency

Measuring algorithm efficiency CMPT 225 Measuring algorithm efficiency Timing Counting Cost functions Cases Best case Average case Worst case Searching Sorting O Notation O notation's mathematical basis O notation classes and notations

More information

Data Structures Lecture 8

Data Structures Lecture 8 Fall 2017 Fang Yu Software Security Lab. Dept. Management Information Systems, National Chengchi University Data Structures Lecture 8 Recap What should you have learned? Basic java programming skills Object-oriented

More information

Algorithm efficiency can be measured in terms of: Time Space Other resources such as processors, network packets, etc.

Algorithm efficiency can be measured in terms of: Time Space Other resources such as processors, network packets, etc. Algorithms Analysis Algorithm efficiency can be measured in terms of: Time Space Other resources such as processors, network packets, etc. Algorithms analysis tends to focus on time: Techniques for measuring

More information

Checking for duplicates Maximum density Battling computers and algorithms Barometer Instructions Big O expressions. John Edgar 2

Checking for duplicates Maximum density Battling computers and algorithms Barometer Instructions Big O expressions. John Edgar 2 CMPT 125 Checking for duplicates Maximum density Battling computers and algorithms Barometer Instructions Big O expressions John Edgar 2 Write a function to determine if an array contains duplicates int

More information

Computer Algorithms. Introduction to Algorithm

Computer Algorithms. Introduction to Algorithm Computer Algorithms Introduction to Algorithm CISC 4080 Yanjun Li 1 What is Algorithm? An Algorithm is a sequence of well-defined computational steps that transform the input into the output. These steps

More information

Computer Science 210 Data Structures Siena College Fall Topic Notes: Complexity and Asymptotic Analysis

Computer Science 210 Data Structures Siena College Fall Topic Notes: Complexity and Asymptotic Analysis Computer Science 210 Data Structures Siena College Fall 2017 Topic Notes: Complexity and Asymptotic Analysis Consider the abstract data type, the Vector or ArrayList. This structure affords us the opportunity

More information

CSC148 Week 9. Larry Zhang

CSC148 Week 9. Larry Zhang CSC148 Week 9 Larry Zhang 1 Announcement 2 More Announcements Assignment 2 out. You may work in groups of 1-3 students. Test 2 on next Friday (Mar 16). No lecture on that day. The test starts at 5:30 The

More information

Algorithmic Analysis. Go go Big O(h)!

Algorithmic Analysis. Go go Big O(h)! Algorithmic Analysis Go go Big O(h)! 1 Corresponding Book Sections Pearson: Chapter 6, Sections 1-3 Data Structures: 4.1-4.2.5 2 What is an Algorithm? Informally, any well defined computational procedure

More information

Algorithm Analysis. Performance Factors

Algorithm Analysis. Performance Factors Algorithm Analysis How can we demonstrate that one algorithm is superior to another without being misled by any of the following problems: Special cases Every algorithm has certain inputs that allow it

More information

SEARCHING, SORTING, AND ASYMPTOTIC COMPLEXITY. Lecture 11 CS2110 Spring 2016

SEARCHING, SORTING, AND ASYMPTOTIC COMPLEXITY. Lecture 11 CS2110 Spring 2016 1 SEARCHING, SORTING, AND ASYMPTOTIC COMPLEXITY Lecture 11 CS2110 Spring 2016 Time spent on A2 2 Histogram: [inclusive:exclusive) [0:1): 0 [1:2): 24 ***** [2:3): 84 ***************** [3:4): 123 *************************

More information

CS/ENGRD 2110 Object-Oriented Programming and Data Structures Spring 2012 Thorsten Joachims. Lecture 10: Asymptotic Complexity and

CS/ENGRD 2110 Object-Oriented Programming and Data Structures Spring 2012 Thorsten Joachims. Lecture 10: Asymptotic Complexity and CS/ENGRD 2110 Object-Oriented Programming and Data Structures Spring 2012 Thorsten Joachims Lecture 10: Asymptotic Complexity and What Makes a Good Algorithm? Suppose you have two possible algorithms or

More information

CSE 146. Asymptotic Analysis Interview Question of the Day Homework 1 & Project 1 Work Session

CSE 146. Asymptotic Analysis Interview Question of the Day Homework 1 & Project 1 Work Session CSE 146 Asymptotic Analysis Interview Question of the Day Homework 1 & Project 1 Work Session Comparing Algorithms Rough Estimate Ignores Details Or really: independent of details What are some details

More information

Assignment 1 (concept): Solutions

Assignment 1 (concept): Solutions CS10b Data Structures and Algorithms Due: Thursday, January 0th Assignment 1 (concept): Solutions Note, throughout Exercises 1 to 4, n denotes the input size of a problem. 1. (10%) Rank the following functions

More information

Data structure and algorithm in Python

Data structure and algorithm in Python Data structure and algorithm in Python Algorithm Analysis Xiaoping Zhang School of Mathematics and Statistics, Wuhan University Table of contents 1. Experimental studies 2. The Seven Functions used in

More information

Algorithms A Look At Efficiency

Algorithms A Look At Efficiency Algorithms A Look At Efficiency 1B Big O Notation 15-121 Introduction to Data Structures, Carnegie Mellon University - CORTINA 1 Big O Instead of using the exact number of operations to express the complexity

More information

Analysis of Algorithms

Analysis of Algorithms Analysis of Algorithms Data Structures and Algorithms Acknowledgement: These slides are adapted from slides provided with Data Structures and Algorithms in C++ Goodrich, Tamassia and Mount (Wiley, 2004)

More information

CS171:Introduction to Computer Science II. Algorithm Analysis. Li Xiong

CS171:Introduction to Computer Science II. Algorithm Analysis. Li Xiong CS171:Introduction to Computer Science II Algorithm Analysis Li Xiong Announcement/Reminders Hw3 due Friday Quiz 2 on October 17, Wednesday (after Spring break, based on class poll) Linked List, Algorithm

More information

Outline. runtime of programs algorithm efficiency Big-O notation List interface Array lists

Outline. runtime of programs algorithm efficiency Big-O notation List interface Array lists Outline runtime of programs algorithm efficiency Big-O notation List interface Array lists Runtime of Programs compare the following two program fragments: int result = 1; int result = 1; for (int i=2;

More information

Algorithmic Complexity

Algorithmic Complexity Algorithmic Complexity Algorithmic Complexity "Algorithmic Complexity", also called "Running Time" or "Order of Growth", refers to the number of steps a program takes as a function of the size of its inputs.

More information

Algorithm Analysis. Gunnar Gotshalks. AlgAnalysis 1

Algorithm Analysis. Gunnar Gotshalks. AlgAnalysis 1 Algorithm Analysis AlgAnalysis 1 How Fast is an Algorithm? 1 Measure the running time» Run the program for many data types > Use System.currentTimeMillis to record the time Worst Time Average Best» Usually

More information

Algorithm Analysis. CENG 707 Data Structures and Algorithms

Algorithm Analysis. CENG 707 Data Structures and Algorithms Algorithm Analysis CENG 707 Data Structures and Algorithms 1 Algorithm An algorithm is a set of instructions to be followed to solve a problem. There can be more than one solution (more than one algorithm)

More information

CS:3330 (22c:31) Algorithms

CS:3330 (22c:31) Algorithms What s an Algorithm? CS:3330 (22c:31) Algorithms Introduction Computer Science is about problem solving using computers. Software is a solution to some problems. Algorithm is a design inside a software.

More information

Algorithm Performance. (the Big-O)

Algorithm Performance. (the Big-O) Algorithm Performance (the Big-O) Lecture 6 Today: Worst-case Behaviour Counting Operations Performance Considerations Time measurements Order Notation (the Big-O) Pessimistic Performance Measure Often

More information

TREES AND ORDERS OF GROWTH 7

TREES AND ORDERS OF GROWTH 7 TREES AND ORDERS OF GROWTH 7 COMPUTER SCIENCE 61A October 17, 2013 1 Trees In computer science, trees are recursive data structures that are widely used in various settings. This is a diagram of a simple

More information

Algorithm Analysis. This is based on Chapter 4 of the text.

Algorithm Analysis. This is based on Chapter 4 of the text. Algorithm Analysis This is based on Chapter 4 of the text. John and Mary have each developed new sorting algorithms. They are arguing over whose algorithm is better. John says "Mine runs in 0.5 seconds."

More information

CSCA48 Winter 2018 Week 10:Algorithm Analysis. Marzieh Ahmadzadeh, Nick Cheng University of Toronto Scarborough

CSCA48 Winter 2018 Week 10:Algorithm Analysis. Marzieh Ahmadzadeh, Nick Cheng University of Toronto Scarborough CSCA48 Winter 2018 Week 10:Algorithm Analysis Marzieh Ahmadzadeh, Nick Cheng University of Toronto Scarborough Algorithm Definition: Solving a problem step-by-step in finite amount of time. Analysis: How

More information

CS240 Fall Mike Lam, Professor. Algorithm Analysis

CS240 Fall Mike Lam, Professor. Algorithm Analysis CS240 Fall 2014 Mike Lam, Professor Algorithm Analysis HW1 Grades are Posted Grades were generally good Check my comments! Come talk to me if you have any questions PA1 is Due 9/17 @ noon Web-CAT submission

More information

Lecture 5: Running Time Evaluation

Lecture 5: Running Time Evaluation Lecture 5: Running Time Evaluation Worst-case and average-case performance Georgy Gimel farb COMPSCI 220 Algorithms and Data Structures 1 / 13 1 Time complexity 2 Time growth 3 Worst-case 4 Average-case

More information

CSE373: Data Structures and Algorithms Lecture 4: Asymptotic Analysis. Aaron Bauer Winter 2014

CSE373: Data Structures and Algorithms Lecture 4: Asymptotic Analysis. Aaron Bauer Winter 2014 CSE373: Data Structures and Algorithms Lecture 4: Asymptotic Analysis Aaron Bauer Winter 2014 Previously, on CSE 373 We want to analyze algorithms for efficiency (in time and space) And do so generally

More information

Elementary maths for GMT. Algorithm analysis Part I

Elementary maths for GMT. Algorithm analysis Part I Elementary maths for GMT Algorithm analysis Part I Algorithms An algorithm is a step-by-step procedure for solving a problem in a finite amount of time Most algorithms transform input objects into output

More information

UNIT 1 ANALYSIS OF ALGORITHMS

UNIT 1 ANALYSIS OF ALGORITHMS UNIT 1 ANALYSIS OF ALGORITHMS Analysis of Algorithms Structure Page Nos. 1.0 Introduction 7 1.1 Objectives 7 1.2 Mathematical Background 8 1.3 Process of Analysis 12 1.4 Calculation of Storage Complexity

More information

Computer Science Approach to problem solving

Computer Science Approach to problem solving Computer Science Approach to problem solving If my boss / supervisor / teacher formulates a problem to be solved urgently, can I write a program to efficiently solve this problem??? Polynomial-Time Brute

More information

Lecture Notes on Sorting

Lecture Notes on Sorting Lecture Notes on Sorting 15-122: Principles of Imperative Computation Frank Pfenning Lecture 4 September 2, 2010 1 Introduction Algorithms and data structures can be evaluated along a number of dimensions,

More information

Comparison of x with an entry in the array

Comparison of x with an entry in the array 1. Basic operations in algorithm An algorithm to solve a particular task employs some set of basic operations. When we estimate the amount of work done by an algorithm we usually do not consider all the

More information

Intro to Algorithms. Professor Kevin Gold

Intro to Algorithms. Professor Kevin Gold Intro to Algorithms Professor Kevin Gold What is an Algorithm? An algorithm is a procedure for producing outputs from inputs. A chocolate chip cookie recipe technically qualifies. An algorithm taught in

More information

Introduction to the Analysis of Algorithms. Algorithm

Introduction to the Analysis of Algorithms. Algorithm Introduction to the Analysis of Algorithms Based on the notes from David Fernandez-Baca Bryn Mawr College CS206 Intro to Data Structures Algorithm An algorithm is a strategy (well-defined computational

More information

CS 137 Part 7. Big-Oh Notation, Linear Searching and Basic Sorting Algorithms. November 10th, 2017

CS 137 Part 7. Big-Oh Notation, Linear Searching and Basic Sorting Algorithms. November 10th, 2017 CS 137 Part 7 Big-Oh Notation, Linear Searching and Basic Sorting Algorithms November 10th, 2017 Big-Oh Notation Up to this point, we ve been writing code without any consideration for optimization. There

More information

9/10/2018 Algorithms & Data Structures Analysis of Algorithms. Siyuan Jiang, Sept

9/10/2018 Algorithms & Data Structures Analysis of Algorithms. Siyuan Jiang, Sept 9/10/2018 Algorithms & Data Structures Analysis of Algorithms Siyuan Jiang, Sept. 2018 1 Email me if the office door is closed Siyuan Jiang, Sept. 2018 2 Grades have been emailed github.com/cosc311/assignment01-userid

More information

1 Introduction. 2 InsertionSort. 2.1 Correctness of InsertionSort

1 Introduction. 2 InsertionSort. 2.1 Correctness of InsertionSort CS 161, Lecture 2 MergeSort, Recurrences 101, and Asymptotic Analysis Scribes: Michael Kim (2015), Ofir Geri (2016), M. Wootters (2017) Date: September 27, 2017 Adapted From Virginia Williams lecture notes

More information

Recitation 9. Prelim Review

Recitation 9. Prelim Review Recitation 9 Prelim Review 1 Heaps 2 Review: Binary heap min heap 1 2 99 4 3 PriorityQueue Maintains max or min of collection (no duplicates) Follows heap order invariant at every level Always balanced!

More information

Computational thinking, problem-solving and programming:

Computational thinking, problem-solving and programming: Computational thinking, problem-solving and programming: Connecting computational thinking and program design IB Computer Science Content developed by Dartford Grammar School Computer Science Department

More information

asymptotic growth rate or order compare two functions, but ignore constant factors, small inputs

asymptotic growth rate or order compare two functions, but ignore constant factors, small inputs Big-Oh 1 asymptotic growth rate or order 2 compare two functions, but ignore constant factors, small inputs asymptotic growth rate or order 2 compare two functions, but ignore constant factors, small inputs

More information

Algorithm Efficiency, Big O Notation, and Javadoc

Algorithm Efficiency, Big O Notation, and Javadoc Algorithm Efficiency, Big O Notation, and Javadoc Algorithm Efficiency Big O Notation Role of Data Structures Javadoc Reading: o L&C 2.1-2.4 o http://algs4.cs.princeton.edu/14analysis o HTML Tutorial 1

More information

Extended Introduction to Computer Science CS1001.py Lecture 7: Basic Algorithms: Sorting, Merge; Time Complexity and the O( ) notation

Extended Introduction to Computer Science CS1001.py Lecture 7: Basic Algorithms: Sorting, Merge; Time Complexity and the O( ) notation Extended Introduction to Computer Science CS1001.py Lecture 7: Basic Algorithms: Sorting, Merge; Time Complexity and the O( ) notation Instructors: Daniel Deutch, Amir Rubinstein Teaching Assistants: Michal

More information

Chapter 2: Complexity Analysis

Chapter 2: Complexity Analysis Chapter 2: Complexity Analysis Objectives Looking ahead in this chapter, we ll consider: Computational and Asymptotic Complexity Big-O Notation Properties of the Big-O Notation Ω and Θ Notations Possible

More information

Algorithm Design and Recursion. Search and Sort Algorithms

Algorithm Design and Recursion. Search and Sort Algorithms Algorithm Design and Recursion Search and Sort Algorithms Objectives To understand the basic techniques for analyzing the efficiency of algorithms. To know what searching is and understand the algorithms

More information

How do we compare algorithms meaningfully? (i.e.) the same algorithm will 1) run at different speeds 2) require different amounts of space

How do we compare algorithms meaningfully? (i.e.) the same algorithm will 1) run at different speeds 2) require different amounts of space How do we compare algorithms meaningfully? (i.e.) the same algorithm will 1) run at different speeds 2) require different amounts of space when run on different computers! for (i = n-1; i > 0; i--) { maxposition

More information

Principles of Algorithm Analysis. Biostatistics 615/815

Principles of Algorithm Analysis. Biostatistics 615/815 Principles of Algorithm Analysis Biostatistics 615/815 Lecture 3 Snapshot of Incoming Class 25 Programming Languages 20 15 10 5 0 R C/C++ MatLab SAS Java Other Can you describe the QuickSort Algorithm?

More information

Analysis of Algorithms. CS 1037a Topic 13

Analysis of Algorithms. CS 1037a Topic 13 Analysis of Algorithms CS 1037a Topic 13 Overview Time complexity - exact count of operations T(n) as a function of input size n - complexity analysis using O(...) bounds - constant time, linear, logarithmic,

More information

csci 210: Data Structures Program Analysis

csci 210: Data Structures Program Analysis csci 210: Data Structures Program Analysis Summary Summary analysis of algorithms asymptotic analysis and notation big-o big-omega big-theta commonly used functions discrete math refresher Analysis of

More information

Announcements. Project 5 is on the street. Second part is essay questions for CoS teaming requirements.

Announcements. Project 5 is on the street. Second part is essay questions for CoS teaming requirements. Announcements Project 5 is on the street. Second part is essay questions for CoS teaming requirements. The first part you do as a team The CoS essay gets individually answered and has separate submission

More information

10/5/2016. Comparing Algorithms. Analyzing Code ( worst case ) Example. Analyzing Code. Binary Search. Linear Search

10/5/2016. Comparing Algorithms. Analyzing Code ( worst case ) Example. Analyzing Code. Binary Search. Linear Search 10/5/2016 CSE373: Data Structures and Algorithms Asymptotic Analysis (Big O,, and ) Steve Tanimoto Autumn 2016 This lecture material represents the work of multiple instructors at the University of Washington.

More information

SEARCHING, SORTING, AND ASYMPTOTIC COMPLEXITY

SEARCHING, SORTING, AND ASYMPTOTIC COMPLEXITY 1 A3 and Prelim 2 SEARCHING, SORTING, AND ASYMPTOTIC COMPLEXITY Lecture 11 CS2110 Fall 2016 Deadline for A3: tonight. Only two late days allowed (Wed-Thur) Prelim: Thursday evening. 74 conflicts! If you

More information

Math 4242 Polynomial Time algorithms, IndependentSet problem

Math 4242 Polynomial Time algorithms, IndependentSet problem Math 4242 Polynomial Time algorithms, IndependentSet problem Many of the algorithms we have looked at so far have a reasonable running time. Not all algorithms do. We make this idea more precise. Definition:

More information

LECTURE 9 Data Structures: A systematic way of organizing and accessing data. --No single data structure works well for ALL purposes.

LECTURE 9 Data Structures: A systematic way of organizing and accessing data. --No single data structure works well for ALL purposes. LECTURE 9 Data Structures: A systematic way of organizing and accessing data. --No single data structure works well for ALL purposes. Input Algorithm Output An algorithm is a step-by-step procedure for

More information

IT 4043 Data Structures and Algorithms

IT 4043 Data Structures and Algorithms IT 4043 Data Structures and Algorithms Budditha Hettige Department of Computer Science 1 Syllabus Introduction to DSA Abstract Data Types Arrays List Operation Using Arrays Recursion Stacks Queues Link

More information

Chapter 6 INTRODUCTION TO DATA STRUCTURES AND ALGORITHMS

Chapter 6 INTRODUCTION TO DATA STRUCTURES AND ALGORITHMS Chapter 6 INTRODUCTION TO DATA STRUCTURES AND ALGORITHMS 1 Reference books: The C Programming Language by Brian W. Kernighan and Dennis M. Ritchie Programming in C (3rd Edition) by Stephen G. Kochan. Data

More information

CS 231 Data Structures and Algorithms Fall Algorithm Analysis Lecture 16 October 10, Prof. Zadia Codabux

CS 231 Data Structures and Algorithms Fall Algorithm Analysis Lecture 16 October 10, Prof. Zadia Codabux CS 231 Data Structures and Algorithms Fall 2018 Algorithm Analysis Lecture 16 October 10, 2018 Prof. Zadia Codabux 1 Agenda Algorithm Analysis 2 Administrative No quiz this week 3 Algorithm Analysis 4

More information

Introduction to Programming I

Introduction to Programming I Still image from YouTube video P vs. NP and the Computational Complexity Zoo BBM 101 Introduction to Programming I Lecture #09 Development Strategies, Algorithmic Speed Erkut Erdem, Aykut Erdem & Aydın

More information

Sorting & Growth of Functions

Sorting & Growth of Functions Sorting & Growth of Functions CSci 588: Data Structures, Algorithms and Software Design Introduction to Algorithms, Cormen et al., Chapter 3 All material not from online sources or text copyright Travis

More information

MergeSort, Recurrences, Asymptotic Analysis Scribe: Michael P. Kim Date: September 28, 2016 Edited by Ofir Geri

MergeSort, Recurrences, Asymptotic Analysis Scribe: Michael P. Kim Date: September 28, 2016 Edited by Ofir Geri CS161, Lecture 2 MergeSort, Recurrences, Asymptotic Analysis Scribe: Michael P. Kim Date: September 28, 2016 Edited by Ofir Geri 1 Introduction Today, we will introduce a fundamental algorithm design paradigm,

More information

CSI33 Data Structures

CSI33 Data Structures Outline Department of Mathematics and Computer Science Bronx Community College August 31, 2015 Outline Outline 1 Chapter 1 Outline Textbook Data Structures and Algorithms Using Python and C++ David M.

More information

Analysis of Algorithms. CSE Data Structures April 10, 2002

Analysis of Algorithms. CSE Data Structures April 10, 2002 Analysis of Algorithms CSE 373 - Data Structures April 10, 2002 Readings and References Reading Chapter 2, Data Structures and Algorithm Analysis in C, Weiss Other References 10-Apr-02 CSE 373 - Data Structures

More information

Lecture 5 Sorting Arrays

Lecture 5 Sorting Arrays Lecture 5 Sorting Arrays 15-122: Principles of Imperative Computation (Spring 2018) Frank Pfenning, Rob Simmons We begin this lecture by discussing how to compare running times of functions in an abstract,

More information

Today s Outline. CSE 326: Data Structures Asymptotic Analysis. Analyzing Algorithms. Analyzing Algorithms: Why Bother? Hannah Takes a Break

Today s Outline. CSE 326: Data Structures Asymptotic Analysis. Analyzing Algorithms. Analyzing Algorithms: Why Bother? Hannah Takes a Break Today s Outline CSE 326: Data Structures How s the project going? Finish up stacks, queues, lists, and bears, oh my! Math review and runtime analysis Pretty pictures Asymptotic analysis Hannah Tang and

More information

For searching and sorting algorithms, this is particularly dependent on the number of data elements.

For searching and sorting algorithms, this is particularly dependent on the number of data elements. Looking up a phone number, accessing a website and checking the definition of a word in a dictionary all involve searching large amounts of data. Searching algorithms all accomplish the same goal finding

More information

MergeSort, Recurrences, Asymptotic Analysis Scribe: Michael P. Kim Date: April 1, 2015

MergeSort, Recurrences, Asymptotic Analysis Scribe: Michael P. Kim Date: April 1, 2015 CS161, Lecture 2 MergeSort, Recurrences, Asymptotic Analysis Scribe: Michael P. Kim Date: April 1, 2015 1 Introduction Today, we will introduce a fundamental algorithm design paradigm, Divide-And-Conquer,

More information

LECTURE NOTES OF ALGORITHMS: DESIGN TECHNIQUES AND ANALYSIS

LECTURE NOTES OF ALGORITHMS: DESIGN TECHNIQUES AND ANALYSIS Department of Computer Science University of Babylon LECTURE NOTES OF ALGORITHMS: DESIGN TECHNIQUES AND ANALYSIS By Faculty of Science for Women( SCIW), University of Babylon, Iraq Samaher@uobabylon.edu.iq

More information

Lecture. Algorithm Design and Recursion. Richard E Sarkis CSC 161: The Art of Programming

Lecture. Algorithm Design and Recursion. Richard E Sarkis CSC 161: The Art of Programming Lecture Algorithm Design and Recursion Richard E Sarkis CSC 161: The Art of Programming Class Administrivia Objectives To understand the basic techniques for analyzing the efficiency of algorithms To know

More information

The Limits of Sorting Divide-and-Conquer Comparison Sorts II

The Limits of Sorting Divide-and-Conquer Comparison Sorts II The Limits of Sorting Divide-and-Conquer Comparison Sorts II CS 311 Data Structures and Algorithms Lecture Slides Monday, October 12, 2009 Glenn G. Chappell Department of Computer Science University of

More information

Analysis of Algorithms & Big-O. CS16: Introduction to Algorithms & Data Structures Spring 2018

Analysis of Algorithms & Big-O. CS16: Introduction to Algorithms & Data Structures Spring 2018 Analysis of Algorithms & Big-O CS16: Introduction to Algorithms & Data Structures Spring 2018 Outline Running time Big-O Big-Ω and Big-Θ Analyzing Seamcarve Dynamic programming Fibonacci sequence 2 Algorithms

More information

CS 61A: Orders of Growth. 6 December 2016

CS 61A: Orders of Growth. 6 December 2016 CS 61A: Orders of Growth 6 December 2016 Orders of Growth: Boring Text Version An order of growth is a function describing how something scales (usually a resource; time or space) with respect to some

More information

Outline and Reading. Analysis of Algorithms 1

Outline and Reading. Analysis of Algorithms 1 Outline and Reading Algorithms Running time ( 3.1) Pseudo-code ( 3.2) Counting primitive operations ( 3.4) Asymptotic notation ( 3.4.1) Asymptotic analysis ( 3.4.2) Case study ( 3.4.3) Analysis of Algorithms

More information

Algorithm Efficiency and Big-O

Algorithm Efficiency and Big-O + Algorithm Efficiency and More List implementations + Algorithm Efficiency and Big-O Section 2.4 1 + Algorithm Efficiency and Big-O n Getting a precise measure of the performance of an algorithm is difficult

More information

ASYMPTOTIC COMPLEXITY

ASYMPTOTIC COMPLEXITY Simplicity is a great virtue but it requires hard work to achieve it and education to appreciate it. And to make matters worse: complexity sells better. - Edsger Dijkstra ASYMPTOTIC COMPLEXITY Lecture

More information

CS240 Fall Mike Lam, Professor. Algorithm Analysis

CS240 Fall Mike Lam, Professor. Algorithm Analysis CS240 Fall 2014 Mike Lam, Professor Algorithm Analysis Algorithm Analysis Motivation: what and why Mathematical functions Comparative & asymptotic analysis Big-O notation ("Big-Oh" in textbook) Analyzing

More information

Analysis of Algorithm. Chapter 2

Analysis of Algorithm. Chapter 2 Analysis of Algorithm Chapter 2 Outline Efficiency of algorithm Apriori of analysis Asymptotic notation The complexity of algorithm using Big-O notation Polynomial vs Exponential algorithm Average, best

More information

ASYMPTOTIC COMPLEXITY

ASYMPTOTIC COMPLEXITY Simplicity is a great virtue but it requires hard work to achieve it and education to appreciate it. And to make matters worse: complexity sells better. - Edsger Dijkstra ASYMPTOTIC COMPLEXITY Lecture

More information

RUNNING TIME ANALYSIS. Problem Solving with Computers-II

RUNNING TIME ANALYSIS. Problem Solving with Computers-II RUNNING TIME ANALYSIS Problem Solving with Computers-II Performance questions 4 How efficient is a particular algorithm? CPU time usage (Running time complexity) Memory usage Disk usage Network usage Why

More information

CS 3410 Ch 5 (DS*), 23 (IJP^)

CS 3410 Ch 5 (DS*), 23 (IJP^) CS 3410 Ch 5 (DS*), 23 (IJP^) *CS 1301 & 1302 text, Introduction to Java Programming, Liang, 7 th ed. ^CS 3410 text, Data Structures and Problem Solving Using Java, Weiss, 4 th edition Sections Pages Review

More information

Algorithm Analysis. Part I. Tyler Moore. Lecture 3. CSE 3353, SMU, Dallas, TX

Algorithm Analysis. Part I. Tyler Moore. Lecture 3. CSE 3353, SMU, Dallas, TX Algorithm Analysis Part I Tyler Moore CSE 5, SMU, Dallas, TX Lecture how many times do you have to turn the crank? Some slides created by or adapted from Dr. Kevin Wayne. For more information see http://www.cs.princeton.edu/~wayne/kleinberg-tardos.

More information

Week 12: Running Time and Performance

Week 12: Running Time and Performance Week 12: Running Time and Performance 1 Most of the problems you have written in this class run in a few seconds or less Some kinds of programs can take much longer: Chess algorithms (Deep Blue) Routing

More information

Introduction to Analysis of Algorithms

Introduction to Analysis of Algorithms Introduction to Analysis of Algorithms Analysis of Algorithms To determine how efficient an algorithm is we compute the amount of time that the algorithm needs to solve a problem. Given two algorithms

More information

Class Note #02. [Overall Information] [During the Lecture]

Class Note #02. [Overall Information] [During the Lecture] Class Note #02 Date: 01/11/2006 [Overall Information] In this class, after a few additional announcements, we study the worst-case running time of Insertion Sort. The asymptotic notation (also called,

More information

Module 07: Efficiency

Module 07: Efficiency Module 07: Efficiency Topics: Basic introduction to run-time efficiency Analyzing recursive code Analyzing iterative code 1 Consider the following two ways to calculate the maximum of a nonempty list.

More information

SEARCHING, SORTING, AND ASYMPTOTIC COMPLEXITY. Lecture 11 CS2110 Fall 2014

SEARCHING, SORTING, AND ASYMPTOTIC COMPLEXITY. Lecture 11 CS2110 Fall 2014 1 SEARCHING, SORTING, AND ASYMPTOTIC COMPLEXITY Lecture 11 CS2110 Fall 2014 Prelim 1 2 Thursday, 2 October. 5:30pm or 7:30pm. Olin 255 Review sheet is on the website. Everyone who had a conflict with the

More information

UNIT-1. Chapter 1(Introduction and overview) 1. Asymptotic Notations 2. One Dimensional array 3. Multi Dimensional array 4. Pointer arrays.

UNIT-1. Chapter 1(Introduction and overview) 1. Asymptotic Notations 2. One Dimensional array 3. Multi Dimensional array 4. Pointer arrays. UNIT-1 Chapter 1(Introduction and overview) 1. Asymptotic Notations 2. One Dimensional array 3. Multi Dimensional array 4. Pointer arrays. Chapter 2 (Linked lists) 1. Definition 2. Single linked list 3.

More information

Algorithm Analysis and Design

Algorithm Analysis and Design Algorithm Analysis and Design Dr. Truong Tuan Anh Faculty of Computer Science and Engineering Ho Chi Minh City University of Technology VNU- Ho Chi Minh City 1 References [1] Cormen, T. H., Leiserson,

More information

Lecture Notes for Chapter 2: Getting Started

Lecture Notes for Chapter 2: Getting Started Instant download and all chapters Instructor's Manual Introduction To Algorithms 2nd Edition Thomas H. Cormen, Clara Lee, Erica Lin https://testbankdata.com/download/instructors-manual-introduction-algorithms-2ndedition-thomas-h-cormen-clara-lee-erica-lin/

More information

ANS:

ANS: Math 15-Spring 17-Final Exam Solutions 1. Consider the following definition of the symbol. Definition. Let x and y be integers. Write x y if 5x + 7y = 11k for some integer k. (a) Show that 1 4, 2 8, and

More information

Programming II (CS300)

Programming II (CS300) 1 Programming II (CS300) Chapter 08 : Algorithm Analysis MOUNA KACEM mouna@cs.wisc.edu Fall 2018 Algorithm Analysis 2 Introduction Running Time Big-Oh Notation Keep in Mind Introduction Algorithm Analysis

More information

4.1 Real-world Measurement Versus Mathematical Models

4.1 Real-world Measurement Versus Mathematical Models Chapter 4 Complexity To analyze the correctness of a computer program, we reasoned about the flow of variable values throughout that program and verified logical properties using this information. In addition

More information

Extended Introduction to Computer Science CS1001.py

Extended Introduction to Computer Science CS1001.py Extended Introduction to Computer Science CS00.py Lecture 6: Basic algorithms: Search, Merge and Sort; Time Complexity Instructors: Benny Chor, Amir Rubinstein Teaching Assistants: Michal Kleinbort, Yael

More information

ORDERS OF GROWTH, DATA ABSTRACTION 4

ORDERS OF GROWTH, DATA ABSTRACTION 4 ORDERS OF GROWTH, DATA ABSTRACTION 4 COMPUTER SCIENCE 61A June 28, 2012 1 Orders of Growth When we talk about the efficiency of a procedure (at least for now), we are often interested in how much more

More information

Elementary maths for GMT. Algorithm analysis Part II

Elementary maths for GMT. Algorithm analysis Part II Elementary maths for GMT Algorithm analysis Part II Algorithms, Big-Oh and Big-Omega An algorithm has a O( ) and Ω( ) running time By default, we mean the worst case running time A worst case O running

More information

The Importance of Runtime Cost! Measuring Runtime Cost! How to Measure Runtime Cost?!

The Importance of Runtime Cost! Measuring Runtime Cost! How to Measure Runtime Cost?! The Importance of Runtime Cost Amazon claims that just an extra one tenth of a second on their response times will cost them 1% in sales" [http://highscalability.com/latency-everywhere-and-it-costs-you-sales-how-crush-it]

More information