Algorithmic complexity: Speed of algorithms

Slides:



Advertisements
Similar presentations
Growth-rate Functions
Advertisements

MATH 224 – Discrete Mathematics
Fundamentals of Python: From First Programs Through Data Structures
Algorithmic Complexity Nelson Padua-Perez Bill Pugh Department of Computer Science University of Maryland, College Park.
Introduction to Analysis of Algorithms
Lists in Python.
Complexity Analysis Chapter 1.
Today  Table/List operations  Parallel Arrays  Efficiency and Big ‘O’  Searching.
Analysis of Algorithms
CMPT 438 Algorithms. Why Study Algorithms? Necessary in any computer programming problem ▫Improve algorithm efficiency: run faster, process more data,
Analysis of Algorithms CSCI Previous Evaluations of Programs Correctness – does the algorithm do what it is supposed to do? Generality – does it.
Program Efficiency & Complexity Analysis. Algorithm Review An algorithm is a definite procedure for solving a problem in finite number of steps Algorithm.
Announcements Course evaluation Your opinion matters! Attendance grades Will be posted prior to the final Project 5 grades Will be posted prior to the.
OCR Computing GCSE © Hodder Education 2013 Slide 1 OCR GCSE Computing Python programming 8: Fun with strings.
Sorting.
Algorithm Analysis (Big O)
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.
Searching Topics Sequential Search Binary Search.
1 Chapter 2 Algorithm Analysis Reading: Chapter 2.
CSC 108H: Introduction to Computer Programming Summer 2012 Marek Janicki.
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.
Algorithmic complexity: Speed of algorithms
Algorithm Analysis 1.
Complexity Analysis (Part I)
CMPT 438 Algorithms.
Design and Analysis of Algorithms
Sorting and "Big Oh" ASFA AP Computer Science A SortingBigOh.
CSC 427: Data Structures and Algorithm Analysis
Analysis of Algorithms
Introduction to complexity
GC 211:Data Structures Algorithm Analysis Tools
Sorting by Tammy Bailey
Algorithm Analysis CSE 2011 Winter September 2018.
Teach A level Computing: Algorithms and Data Structures
Algorithmic complexity: Speed of algorithms
10.3 Bubble Sort Chapter 10 - Sorting.
Big-Oh and Execution Time: A Review
Algorithm Analysis (not included in any exams!)
Algorithm design and Analysis
Chapter 12: Analysis of Algorithms
Algorithm An algorithm is a finite set of steps required to solve a problem. An algorithm must have following properties: Input: An algorithm must have.
CISC101 Reminders Slides have changed from those posted last night…
Winter 2018 CISC101 12/1/2018 CISC101 Reminders
GC 211:Data Structures Algorithm Analysis Tools
CS190/295 Programming in Python for Life Sciences: Lecture 6
Algorithm Efficiency Chapter 10.
Big O Notation.
Winter 2018 CISC101 12/2/2018 CISC101 Reminders
Comparing Algorithms Unit 1.2.
CS 201 Fundamental Structures of Computer Science
Algorithmic complexity: Speed of algorithms
Searching, Sorting, and Asymptotic Complexity
CSE 373 Data Structures and Algorithms
Lecture 11 Algorithm Design
CSC 427: Data Structures and Algorithm Analysis
Ch. 2: Getting Started.
CHAPTER 4: Lists, Tuples and Dictionaries
Algorithmic complexity: Speed of algorithms
slides created by Ethan Apter
Python Review
Sum this up for me Let’s write a method to calculate the sum from 1 to some n public static int sum1(int n) { int sum = 0; for (int i = 1; i
Analysis of Algorithms
Algorithmic complexity: Speed of algorithms
Winter 2019 CISC101 5/30/2019 CISC101 Reminders
CSE 373: Data Structures and Algorithms
Complexity Analysis (Part I)
Algorithm Course Algorithms Lecture 3 Sorting Algorithm-1
Complexity Analysis (Part I)
Introduction to Computer Science
Presentation transcript:

Algorithmic complexity: Speed of algorithms UW CSE 160 Winter 2016

How fast does your program run? Usually, this does not matter Correctness is more important than speed Computer time is much cheaper than human time The cost of your program depends on: Time to write and verify it High cost: salaries Time to run it Low cost: electricity An inefficient program may give you results faster

Sometimes, speed does matter Programs that need to run in real time E.g. will by car crash into the car in front of me? Very large datasets Even inefficient algorithms usually run quickly enough on a small dataset Example large data set: Google: 67 billion pages indexed (2014) 5.7 billion searches per day (2014) Number of pages searched per day?? http://www.statisticbrain.com/total-number-of-pages-indexed-by-google/

Program Performance We’ll discuss two things a programmer can do to improve program performance: Good Coding Practices Good Algorithm Choice

Good Coding Practices (1) Minimize amount of work inside of loops y = 500 for i in range(n): z = expensive_function() x = 5.0 * y / 2.0 + z lst.append(x + i) Move computations that WILL NOT CHANGE outside/above the loop whenever possible. Move computations that WILL NOT CHANGE outside/above the loop whenever possible.

Good Coding Practices (2) Minimize amount of work inside of loops for i in friends_of_friends(n): for j in friends_of_friends(n): # do stuff with i and j Move computations that WILL NOT CHANGE outside/above the loop whenever possible. Move computations that WILL NOT CHANGE outside/above the loop whenever possible.

Good Coding Practices (3) Avoid iterating over data multiple times when possible for base in nucleotides: if base == 'A': # code here if base == 'C': if base == 'T': if base == 'G': for base in nucleotides:     if base == 'A':         # code here        elif base == 'C':     elif base == 'T':     elif base == 'G': Even without the loop, it is more efficient to use the if elif elif than multiple if statements (Potentially fewer cases will be checked with the elif option vs. the if option where all four options will always be checked.) Even without the loop, it is more efficient to use the if elif elif than multiple if statements (Potentially fewer cases will be checked with the elif option vs. the if option where all four options will always be checked.)

Good Coding Practices (4) Expensive operations: Reading files Writing files Printing to the screen Try to open the file once and read in all the data you need into a data structure. Accessing the data structure will be MUCH faster than reading the file a second time.

Testing and Developing your Program Test your program on a SMALL input file. This will allow you to calculate expected results by hand to check for correctness But it can also make your development process easier if you have to wait a shorter time for your program to run

Good Algorithm Choice Good choice of algorithm can have a much bigger impact on performance than the good coding practices mentioned. However good coding practices can be applied fairly easily Trying to come up with a better algorithm can be a (fun!) challenge Remember: Correctness is more important than speed!!

How to compare two algorithms? Implement them both in Python Run them and time them

A Better Way to Compare Two Algorithms Hardware? Count number of “operations” something independent of speed of processor Properties of data set? (e.g. almost sorted, all one value, reverse sorted order) Pick the worst possible data set: gives you an upper bound on how long the algorithm will take Also it can be hard to decide on what is and “average” data set Size of data set? Describe running time of algorithm as a function of data set size

Asymptotic Analysis Comparing “Orders of Growth” This approach works when problem size is large When problem size is small, “constant factors” matter A few common Orders of Growth: Example: Constant O(1) integer + integer Linear O(n) iterating through a list Quadratic O(n2) iterating through a grid

Which Function Grows Faster? n3 + 2n2 vs. 100n2 + 1000 n n

Running Times of Python Operations Constant Time operations: O(1) Basic Math on numbers (+ - * /) Indexing into a sequence (eg. list, string, tuple) or dictionary E.g. myList[3] = 25 List operations: append, pop(at end of list) Sequence operation: len Dictionary operation: in Set operations: in, add, remove, len Linear Time operations: O(n) for loop traversing an entire sequence or dictionary Built in functions: sum, min, max, slicing a sequence Sequence operations: in, index, count Dictionary operations: keys(), values(), items() Set operations: &, |, - String concatenation (linear in length of strings) Note: These are general guidelines, may vary, or may have a more costly worst case. Built in functions (e.g. sum, max, min, sort) are often faster than implementing them yourself.

Example: Processing pairs def make_pairs(list1, list2): """Return a list of pairs. Each pair is made of corresponding elements of list1 and list2. list1 and list2 must be of the same length.""" … assert make_pairs([100, 200, 300], [101, 201, 301]) == [[100, 101], [200, 201], [300, 301]] 2 nested loops vs. 1 loop Quadratic (n2) vs. linear (n) time

Example: Searching Any list vs. a sorted list def search(value, lst): """Return index of value in list lst. The value must be in the list.""" … Any list vs. a sorted list Linear (n) vs. logarithmic (log n) time

Example: Sorting selection sort vs. quicksort def sort(lst): """Return a sorted version of the list lst. The input list is not modified.""" … assert sort([3, 1, 4, 1, 5, 9, 2, 6, 5]) == [1, 1, 2, 3, 4, 5, 5, 6, 9] selection sort vs. quicksort 2 nested loops vs. recursive decomposition time: quadratic (n2) vs. log-linear (n log n) time Note: Calling built in sorting methods sort or sorted in Python has O(n log n) time