Analyzing algorithms & Asymptotic Notation BIO/CS 471 – Algorithms for Bioinformatics.

Slides:



Advertisements
Similar presentations
Analysis of Algorithms
Advertisements

Intro to Analysis of Algorithms. Algorithm “A sequence of unambiguous instructions for solving a problem, i.e., for obtaining a required output for any.
Razdan with contribution from others 1 Algorithm Analysis What is the Big ‘O Bout? Anshuman Razdan Div of Computing.
the fourth iteration of this loop is shown here
Chapter 3 Growth of Functions
20-Jun-15 Analysis of Algorithms II. 2 Basics Before we attempt to analyze an algorithm, we need to define two things: How we measure the size of the.
1 Data Structures A program solves a problem. A program solves a problem. A solution consists of: A solution consists of:  a way to organize the data.
CS3381 Des & Anal of Alg ( SemA) City Univ of HK / Dept of CS / Helena Wong 2. Analysis of Algorithms - 1 Analysis.
25 June 2015Comp 122, Spring 2004 Asymptotic Notation, Review of Functions & Summations.
Algorithm Analysis CS 201 Fundamental Structures of Computer Science.
Analysis of Algorithms 7/2/2015CS202 - Fundamentals of Computer Science II1.
Elementary Data Structures and Algorithms
Lecture 3: Algorithms and Complexity Study Chapter COMP 555 Bioalgorithms Fall 2014.
Introduction to Algorithm design and analysis
DATA STRUCTURES AND ALGORITHMS Lecture Notes 1 Prepared by İnanç TAHRALI.
Algorithm Cost Algorithm Complexity. Algorithm Cost.
CSE373: Data Structures and Algorithms Lecture 4: Asymptotic Analysis Aaron Bauer Winter 2014.
Asymptotic Notations Iterative Algorithms and their analysis
Algorithm Analysis. Algorithm Def An algorithm is a step-by-step procedure.
Time Complexity Dr. Jicheng Fu Department of Computer Science University of Central Oklahoma.
Introduction to complexity Prof. Sin-Min Lee Department of Computer Science San Jose State University.
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.
Week 2 CS 361: Advanced Data Structures and Algorithms
Lecture 8. How to Form Recursive relations 1. Recap Asymptotic analysis helps to highlight the order of growth of functions to compare algorithms Common.
For Wednesday Read Weiss chapter 3, sections 1-5. This should be largely review. If you’re struggling with the C++ aspects, you may refer to Savitch, chapter.
SEARCHING, SORTING, AND ASYMPTOTIC COMPLEXITY Lecture 12 CS2110 – Fall 2009.
Introduction to complexity. 2 Analysis of Algorithms Why do we need to analyze algorithms? –To measure the performance –Comparison of different algorithms.
Mathematics Review and Asymptotic Notation
CS 3343: Analysis of Algorithms
Iterative Algorithm Analysis & Asymptotic Notations
Algorithm Analysis An algorithm is a clearly specified set of simple instructions to be followed to solve a problem. Three questions for algorithm analysis.
What is an Algorithm? An algorithm is a sequence of instructions that one must perform in order to solve a well formulated problem.
Analysis of Algorithms
Chapter 19: Searching and Sorting Algorithms
2IL50 Data Structures Fall 2015 Lecture 2: Analysis of Algorithms.
1 COMP3040 Tutorial 1 Analysis of algorithms. 2 Outline Motivation Analysis of algorithms Examples Practice questions.
Analyzing algorithms & Asymptotic Notation BIO/CS 471 – Algorithms for Bioinformatics.
Coursenotes CS3114: Data Structures and Algorithms Clifford A. Shaffer Department of Computer Science Virginia Tech Copyright ©
Program Efficiency & Complexity Analysis. Algorithm Review An algorithm is a definite procedure for solving a problem in finite number of steps Algorithm.
Algorithm Analysis CS 400/600 – Data Structures. Algorithm Analysis2 Abstract Data Types Abstract Data Type (ADT): a definition for a data type solely.
Growth of Functions. 2 Analysis of Bubble Sort 3 Time Analysis – Best Case The array is already sorted – no swap operations are required.
Chapter 5 Algorithms (2) Introduction to CS 1 st Semester, 2015 Sanghyun Park.
Java Methods Big-O Analysis of Algorithms Object-Oriented Programming
Time Complexity of Algorithms (Asymptotic Notations)
DATA STRUCTURES AND ALGORITHMS Lecture Notes 2 Prepared by İnanç TAHRALI.
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.
Time Complexity. Solving a computational program Describing the general steps of the solution –Algorithm’s course Use abstract data types and pseudo code.
David Luebke 1 1/6/2016 CS 332: Algorithms Asymptotic Performance.
1/6/20161 CS 3343: Analysis of Algorithms Lecture 2: Asymptotic Notations.
Asymptotics and Recurrence Equations Prepared by John Reif, Ph.D. Analysis of Algorithms.
Analysis of Algorithms Asymptotic Performance. Review: Asymptotic Performance Asymptotic performance: How does algorithm behave as the problem size gets.
E.G.M. PetrakisAlgorithm Analysis1  Algorithms that are equally correct can vary in their utilization of computational resources  time and memory  a.
21-Feb-16 Analysis of Algorithms II. 2 Basics Before we attempt to analyze an algorithm, we need to define two things: How we measure the size of the.
BITS Pilani Pilani Campus Data Structure and Algorithms Design Dr. Maheswari Karthikeyan Lecture1.
1 Chapter 2 Algorithm Analysis Reading: Chapter 2.
Analysis of Algorithms Spring 2016CS202 - Fundamentals of Computer Science II1.
CSE 3358 NOTE SET 2 Data Structures and Algorithms 1.
Analysis of Algorithms
Introduction to Algorithms
Algorithm design and Analysis
Introduction to Algorithms Analysis
CS 201 Fundamental Structures of Computer Science
Analysis of Algorithms
G.PULLAIAH COLLEGE OF ENGINEERING AND TECHNOLOGY
At the end of this session, learner will be able to:
CS210- Lecture 2 Jun 2, 2005 Announcements Questions
Analysis of Algorithms
Presentation transcript:

Analyzing algorithms & Asymptotic Notation BIO/CS 471 – Algorithms for Bioinformatics

Analyzing Algorithms2 Why Sorting Algorithms?  Simple framework  Sorting & searching Without sorting, search is random  Finding information Sequence search Similarity identification Data structures & organization

Analyzing Algorithms3 Sorting algorithms  SelectionSort Looking at a “slot” = 1 operation Moving a value = 1 operation n items = (n+1)(n) operations n items = 2n memory positions 72946

Analyzing Algorithms4 The RAM model of computing  Linear, random access memory  READ/WRITE = one operation  Simple mathematical operations are also unit operations  Can only read one location at a time, by address  Registers …

Analyzing Algorithms5 “Naïve” Bubble Sort  Simplifying assumption: compare/swap = 1 operation  Each pass = (n-1) compare/swaps  n passes = (n)(n-1) compare/swaps  Space = n 72946

Analyzing Algorithms6 “Smart” Bubble Sort  MikeSort:  First pass (n-1) compare/swaps  Next pass (n-2) compare/swaps  n inputs: (n-1) + (n-2) + (n-3) … + 1 We need a mathematical tool to solve this

Analyzing Algorithms7 Series Sums  The arithmetic series: … + n =  Linearity:

Analyzing Algorithms8 Series Sums  … + n – 1 =  Example:

Analyzing Algorithms9 More Series  Geometric Series: 1 + x + x 2 + x 3 + … + x n  Example:

Analyzing Algorithms10 Telescoping Series  Consider the series:  Look at the terms:

Analyzing Algorithms11 Telescoping Series  In general:

Analyzing Algorithms12 The Harmonic Series

Analyzing Algorithms13 Time Complexity of MikeSort  “Smart” BubbleSort  n inputs: (n-1) + (n-2) + (n-3) …

Analyzing Algorithms14 Exact Analysis of Algorithms  To make it easy, we’ll ignore loop control structures, and worry about the code in the loops.  Each line of code will be considered one “operation”. for ($i=1; $i<=$n; $i++) { print $i; } for ($i=1; $i<=$n; $i++) { print $i; print “Hi there\n”. }

Analyzing Algorithms15 Exact analysis  $i = 1 $j =1, 2, 3, … n  $i = 2 $j = 1, 2, 3, … netc.  Total: n 2 operations for ($i=1; $i<=$n; $i++) { for ($j=1; $j<=$n; $j++) { print “$i, $j\n”; }

Analyzing Algorithms16 Exact Analysis of BubbleSort # $i is the pass number for ($i=0; $i<$n-1; $i++) { # $j is the current element looked at for ($j=0; $j<$n-1; $j++) { if ($array[$j] > $array[$j+1]) { swap($array[$j], $array[$j+1]); }  Best case: n 2  Worst case: 2n 2  Average case: 1.5(n 2 ) What if the array is often already sorted or nearly sorted??

Analyzing Algorithms17 Exact Analysis of MikeSort # $i is the pass number for ($i=1; $i<=$n-1; $i++) { # $j is the current element looked at for ($j=1; $j<=$n-$i; $j++) { if ($array[$j] > $array[$j+1]) { swap($array[$j], $array[$j+1]); }  Best case: = (n 2 – n)/2  Worst case: n 2 – n  Average case: 1.5((n 2 – n)/2) = (3n 2 – 3n)/2

Analyzing Algorithms18 Exact Analysis of MikeSort  Best case: = (n 2 – n)/2  Worst case: n 2 – n  Average case: 1.5((n 2 – n)/2) = (3n 2 – 3n)/2

Analyzing Algorithms19 Traveling Salesman Problem  n cities Traveling distance between each pair is given Find the circuit that includes all cities A C D G B E F

Analyzing Algorithms20 Is there a “real difference”?  10^1  10^2  10^3Number of students in the college of engineering  10^4 Number of students enrolled at Wright State University  10^6 Number of people in Dayton  10^8 Number of people in Ohio  10^10 Number of stars in the galaxy  10^20 Total number of all stars in the universe  10^80 Total number of particles in the universe  10^100 << Number of possible solutions to traveling salesman (100)  Traveling salesman (100) is computable but it is NOT feasible.

Analyzing Algorithms21 Growth of Functions

Analyzing Algorithms22 Is there a “real” difference?  Growth of functions

Analyzing Algorithms23 Introduction to Asymptotic Notation  We want to express the concept of “about”, but in a mathematically rigorous way  Limits are useful in proofs and performance analyses  Talk about input size: sequence align  notation:  (n 2 ) = “this function grows similarly to n 2 ”.  Big-O notation: O (n 2 ) = “this function grows at least as slowly as n 2 ”. Describes an upper bound.

Analyzing Algorithms24 Big-O  What does it mean? If f(n) = O(n 2 ), then:  f(n) can be larger than n 2 sometimes, but…  I can choose some constant c and some value n 0 such that for every value of n larger than n 0 : f(n) < cn 2  That is, for values larger than n 0, f(n) is never more than a constant multiplier greater than n 2  Or, in other words, f(n) does not grow more than a constant factor faster than n 2.

Analyzing Algorithms25 Visualization of O(g(n)) n0n0 cg(n) f(n)f(n)

Analyzing Algorithms26 Big-O

Analyzing Algorithms27 More Big-O  Prove that:  Let c = 21 and n 0 = 4  21n 2 > 20n 2 + 2n + 5 for all n > 4 n 2 > 2n + 5 for all n > 4 TRUE

Analyzing Algorithms28  -notation  Big-O is not a tight upper bound. In other words n = O(n 2 )  provides a tight bound

Analyzing Algorithms29 Visualization of  (g(n)) n0n0 c2g(n)c2g(n) f(n)f(n) c1g(n)c1g(n)

Analyzing Algorithms30 A Few More Examples  n = O(n 2 ) ≠  (n 2 )  200n 2 = O(n 2 ) =  (n 2 )  n 2.5 ≠ O(n 2 ) ≠  (n 2 )

Analyzing Algorithms31 Some Other Asymptotic Functions  Little o – A non-tight asymptotic upper bound n = o(n 2 ), n = O(n 2 ) 3n 2 ≠ o(n 2 ), 3n 2 = O(n 2 )  () – A lower bound Similar definition to Big-O n 2 =  (n)  () – A non-tight asymptotic lower bound  f(n) =  (n)  f(n) = O(n) and f(n) =  (n)

Analyzing Algorithms32 Visualization of Asymptotic Growth n0n0 O(f(n)) f(n)f(n)  (f(n))  (f(n)) o(f(n))  (f(n))

Analyzing Algorithms33 Analogy to Arithmetic Operators

Analyzing Algorithms34 Approaches to Solving Problems  Direct/iterative SelectionSort Can by analyzed using series sums  Divide and Conquer Recursion and Dynamic Programming Cut the problem in half MergeSort

Analyzing Algorithms35 Recursion  Computing factorials sub fact($n) { if ($n <= 1) { return(1); } else { $temp = $fact($n-1); $result = $temp + 1; return($result); } print(fact(4). “\n”); fib(5)

Analyzing Algorithms36 Fibonacci Numbers int fib(int N) { int prev, pprev; if (N == 1) { return 0; } else if (N == 2) { return 1; } else { prev = fib(N-1); pprev = fib(N-2); return prev + pprev; }

Analyzing Algorithms37 MergeSort  Let M n be the time to MergeSort n items M n = 2(M n-1 ) + n