Introduction to Algorithmic Complexity. D. Thiebaut CSC212 Fall 2014

Similar documents
Lecture 2. Fundamentals of the Analysis of Algorithm Efficiency

Big O 2/14/13. Administrative. Does it terminate? David Kauchak cs302 Spring 2013

Analysis of Algorithms

The Time Complexity of an Algorithm

3.1 Asymptotic notation

CS 61B Asymptotic Analysis Fall 2017

CS473 - Algorithms I

Lecture 2. More Algorithm Analysis, Math and MCSS By: Sarah Buchanan

The Time Complexity of an Algorithm

Data Structures and Algorithms. Asymptotic notation

Computational Complexity

More Asymptotic Analysis Spring 2018 Discussion 8: March 6, 2018

Analysis of Algorithms

Agenda. We ve discussed

Ch01. Analysis of Algorithms

Lecture 2: Asymptotic Notation CSCI Algorithms I

Asymptotic Analysis of Algorithms. Chapter 4

Module 1: Analyzing the Efficiency of Algorithms

Data Structures and Algorithms CSE 465

Problem Set 1 Solutions

Growth of Functions (CLRS 2.3,3)

CS Non-recursive and Recursive Algorithm Analysis

CS 4407 Algorithms Lecture 3: Iterative and Divide and Conquer Algorithms

CSE 101. Algorithm Design and Analysis Miles Jones Office 4208 CSE Building Lecture 1: Introduction

COMP 555 Bioalgorithms. Fall Lecture 3: Algorithms and Complexity

Data Structures and Algorithms Running time and growth functions January 18, 2018

Big O (Asymptotic Upper Bound)

Programming, Data Structures and Algorithms Prof. Hema Murthy Department of Computer Science and Engineering Indian Institute Technology, Madras

Grade 11/12 Math Circles Fall Nov. 5 Recurrences, Part 2

Module 1: Analyzing the Efficiency of Algorithms

Asymptotic Running Time of Algorithms

Introduction to Computer Science Lecture 5: Algorithms

MA008/MIIZ01 Design and Analysis of Algorithms Lecture Notes 2

CS 4407 Algorithms Lecture 2: Growth Functions

COMP Analysis of Algorithms & Data Structures

Running Time Evaluation

COMP Analysis of Algorithms & Data Structures

Written Homework #1: Analysis of Algorithms

CS173 Running Time and Big-O. Tandy Warnow

COMP 9024, Class notes, 11s2, Class 1

Data Structures and Algorithms Chapter 2

Asymptotic Analysis 1

Introduction to Algorithms and Asymptotic analysis

Ch 01. Analysis of Algorithms

with the size of the input in the limit, as the size of the misused.

CS 4407 Algorithms Lecture 2: Iterative and Divide and Conquer Algorithms

5 + 9(10) + 3(100) + 0(1000) + 2(10000) =

Measuring Goodness of an Algorithm. Asymptotic Analysis of Algorithms. Measuring Efficiency of an Algorithm. Algorithm and Data Structure

Lecture 10: Big-Oh. Doina Precup With many thanks to Prakash Panagaden and Mathieu Blanchette. January 27, 2014

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

CSC236 Week 4. Larry Zhang

CIS 121 Data Structures and Algorithms with Java Spring Big-Oh Notation Monday, January 22/Tuesday, January 23

CSC Design and Analysis of Algorithms. Lecture 1

Foundations II: Data Structures and Algorithms

Lecture 1: Asymptotic Complexity. 1 These slides include material originally prepared by Dr.Ron Cytron, Dr. Jeremy Buhler, and Dr. Steve Cole.

Analysis of Multithreaded Algorithms

When we use asymptotic notation within an expression, the asymptotic notation is shorthand for an unspecified function satisfying the relation:

Time complexity analysis

Algorithms, CSE, OSU. Introduction, complexity of algorithms, asymptotic growth of functions. Instructor: Anastasios Sidiropoulos

EECS 477: Introduction to algorithms. Lecture 5

CISC 235: Topic 1. Complexity of Iterative Algorithms

Review Of Topics. Review: Induction

Big-O Notation and Complexity Analysis

Solving Recurrences. Lecture 23 CS2110 Fall 2011

Fundamentals of Programming. Efficiency of algorithms November 5, 2017

Algorithms Design & Analysis. Analysis of Algorithm

Analysis of Algorithms

CS-140 Fall 2017 Test 1 Version Practice Practice for Nov. 20, Name:

i=1 i B[i] B[i] + A[i, j]; c n for j n downto i + 1 do c n i=1 (n i) C[i] C[i] + A[i, j]; c n

On my honor I affirm that I have neither given nor received inappropriate aid in the completion of this exercise.

CSED233: Data Structures (2017F) Lecture4: Analysis of Algorithms

Collection Family Tree

DNHI Homework 2 Solutions Recursion

COMPUTER ALGORITHMS. Athasit Surarerks.

Analysis of Algorithm Efficiency. Dr. Yingwu Zhu

What is Performance Analysis?

INTRODUCTION 1.1 ALGORITHM

Cpt S 223. School of EECS, WSU

CSE332: Data Abstrac0ons Sec%on 2. HyeIn Kim Winter 2013

Mathematical Background. Unsigned binary numbers. Powers of 2. Logs and exponents. Mathematical Background. Today, we will review:

Md Momin Al Aziz. Analysis of Algorithms. Asymptotic Notations 3 COMP Computer Science University of Manitoba

Analysis of Algorithms

Practical Session #3 - Recursions

CS F-01 Algorithm Analysis 1

Asymptotic Notation. such that t(n) cf(n) for all n n 0. for some positive real constant c and integer threshold n 0

CS Analysis of Recursive Algorithms and Brute Force

Asymptotic Analysis. Slides by Carl Kingsford. Jan. 27, AD Chapter 2

When we use asymptotic notation within an expression, the asymptotic notation is shorthand for an unspecified function satisfying the relation:

ALGORITHMS AND COMPLEXITY THEORY

Define Efficiency. 2: Analysis. Efficiency. Measuring efficiency. CSE 417: Algorithms and Computational Complexity. Winter 2007 Larry Ruzzo

Omega notation. Transitivity etc.

Asymptotic Analysis Cont'd

Topic 17. Analysis of Algorithms

Principles of Algorithm Analysis

Analysis of Algorithms [Reading: CLRS 2.2, 3] Laura Toma, csci2200, Bowdoin College

Defining Efficiency. 2: Analysis. Efficiency. Measuring efficiency. CSE 421: Intro Algorithms. Summer 2007 Larry Ruzzo

Asymptotic Analysis. Thomas A. Anastasio. January 7, 2004

Analysis of Algorithms

Lecture 4: Stacks and Queues

Asymptotic Algorithm Analysis & Sorting

Transcription:

Introduction to Algorithmic Complexity D. Thiebaut CSC212 Fall 2014

http://youtu.be/p0tlbl5lrj8 Case Study: Fibonacci

public class RecurseFib {! private static long computefibrecursively( int n ) { if ( n<= 1 ) return 1; return computefibrecursively( n-1 ) + computefibrecursively( n-2 ); }! private static long computefibiteratively( int n ) { if ( n<=1 ) return 1; long[] fibs = new long[ n+1 ]; fibs[0] = fibs[1] = 1; for ( int i=2; i<=n; i++ ) fibs[i] = fibs[i-1]+fibs[i-2]; return fibs[ fibs.length - 1 ]; } public static void main(string[] args) { } } for ( int N = 0; N < 100; N++ ) { long start = System.currentTimeMillis(); long Fib = computefibrecursively( N ); //long Fib = computefibiteratively( N ); long end = System.currentTimeMillis(); System.out.println( String.format( "Fib(%d) = %d, %1.2f msec", N, Fib, 1.0*(end-start) ) ); }

Fib(0) = 1, 0.00 msec Fib(1) = 1, 0.00 msec Fib(2) = 2, 0.00 msec Fib(3) = 3, 0.00 msec Fib(4) = 5, 0.00 msec Fib(5) = 8, 0.00 msec Fib(6) = 13, 0.00 msec Fib(7) = 21, 0.00 msec Fib(8) = 34, 0.00 msec Fib(9) = 55, 0.00 msec Fib(10) = 89, 0.00 msec Fib(11) = 144, 0.00 msec Fib(12) = 233, 0.00 msec Fib(13) = 377, 0.00 msec Fib(14) = 610, 0.00 msec Fib(15) = 987, 0.00 msec Fib(16) = 1597, 1.00 msec Fib(17) = 2584, 0.00 msec Fib(18) = 4181, 1.00 msec Fib(19) = 6765, 1.00 msec Fib(20) = 10946, 1.00 msec Fib(21) = 17711, 0.00 msec Fib(22) = 28657, 0.00 msec Fib(23) = 46368, 0.00 msec Fib(24) = 75025, 0.00 msec Fib(25) = 121393, 0.00 msec Fib(26) = 196418, 0.00 msec Fib(27) = 317811, 1.00 msec Fib(28) = 514229, 1.00 msec Fib(29) = 832040, 2.00 msec Fib(30) = 1346269, 3.00 msec Fib(31) = 2178309, 5.00 msec Fib(32) = 3524578, 8.00 msec Fib(33) = 5702887, 13.00 msec Fib(34) = 9227465, 23.00 msec Fib(35) = 14930352, 35.00 msec Fib(36) = 24157817, 57.00 msec Fib(37) = 39088169, 92.00 msec Fib(38) = 63245986, 150.00 msec Fib(39) = 102334155, 242.00 msec Fib(40) = 165580141, 388.00 msec Fib(41) = 267914296, 626.00 msec Fib(42) = 433494437, 1011.00 msec Fib(43) = 701408733, 1651.00 msec Fib(44) = 1134903170, 2711.00 msec Fib(45) = 1836311903, 4384.00 msec computefibrecursively()

time (ms) computefibrecursively() Nth Fibonacci Term

Fib(0) = 1, 0.00 msec Fib(1) = 1, 0.00 msec Fib(2) = 2, 0.00 msec Fib(3) = 3, 0.00 msec Fib(4) = 5, 0.00 msec Fib(5) = 8, 0.00 msec Fib(6) = 13, 0.00 msec Fib(7) = 21, 0.00 msec Fib(8) = 34, 0.00 msec Fib(9) = 55, 0.00 msec Fib(10) = 89, 0.00 msec Fib(11) = 144, 0.00 msec Fib(12) = 233, 0.00 msec Fib(13) = 377, 0.00 msec Fib(14) = 610, 0.00 msec Fib(15) = 987, 0.00 msec Fib(16) = 1597, 0.00 msec Fib(17) = 2584, 0.00 msec Fib(18) = 4181, 0.00 msec Fib(19) = 6765, 0.00 msec Fib(20) = 10946, 0.00 msec Fib(21) = 17711, 0.00 msec Fib(22) = 28657, 0.00 msec Fib(23) = 46368, 0.00 msec Fib(24) = 75025, 0.00 msec Fib(25) = 121393, 0.00 msec Fib(26) = 196418, 0.00 msec Fib(27) = 317811, 0.00 msec Fib(28) = 514229, 0.00 msec Fib(29) = 832040, 0.00 msec Fib(30) = 1346269, 0.00 msec Fib(31) = 2178309, 0.00 msec Fib(32) = 3524578, 0.00 msec Fib(33) = 5702887, 0.00 msec Fib(34) = 9227465, 0.00 msec Fib(35) = 14930352, 0.00 msec Fib(36) = 24157817, 0.00 msec Fib(37) = 39088169, 0.00 msec Fib(38) = 63245986, 0.00 msec Fib(39) = 102334155, 0.00 msec Fib(40) = 165580141, 0.00 msec Fib(41) = 267914296, 0.00 msec Fib(42) = 433494437, 0.00 msec Fib(43) = 701408733, 0.00 msec Fib(44) = 1134903170, 0.00 msec Fib(45) = 1836311903, 0.00 msec Fib(46) = 2971215073, 0.00 msec Fib(47) = 4807526976, 0.00 msec Fib(48) = 7778742049, 0.00 msec Fib(49) = 12586269025, 0.00 msec Fib(50) = 20365011074, 0.00 msec computefibiteratively() (Using System.currentTimeMillis() to measure elapsed time) Fib(90) = 4660046610375530309, 0.00 msec Fib(91) = 7540113804746346429, 0.00 msec Fib(92) = -6246583658587674878, 0.00 msec Fib(93) = 1293530146158671551, 0.00 msec Fib(94) = -4953053512429003327, 0.00 msec

Fib(0) = 1, 0.00 msec Fib(1) = 1, 0.00 msec Fib(2) = 2, 0.00 msec Fib(3) = 3, 0.00 msec Fib(4) = 5, 0.00 msec Fib(5) = 8, 0.00 msec Fib(6) = 13, 0.00 msec Fib(7) = 21, 0.00 msec Fib(8) = 34, 0.00 msec Fib(9) = 55, 0.00 msec Fib(10) = 89, 0.00 msec Fib(11) = 144, 0.00 msec Fib(12) = 233, 0.00 msec Fib(13) = 377, 0.00 msec Fib(14) = 610, 0.00 msec Fib(15) = 987, 0.00 msec Fib(16) = 1597, 0.00 msec Fib(17) = 2584, 0.00 msec Fib(18) = 4181, 0.00 msec Fib(19) = 6765, 0.00 msec Fib(20) = 10946, 0.00 msec Fib(21) = 17711, 0.00 msec Fib(22) = 28657, 0.00 msec Fib(23) = 46368, 0.00 msec Fib(24) = 75025, 0.00 msec Fib(25) = 121393, 0.00 msec Fib(26) = 196418, 0.00 msec Fib(27) = 317811, 0.00 msec Fib(28) = 514229, 0.00 msec Fib(29) = 832040, 0.00 msec Fib(30) = 1346269, 0.00 msec Fib(31) = 2178309, 0.00 msec Fib(32) = 3524578, 0.00 msec Fib(33) = 5702887, 0.00 msec Fib(34) = 9227465, 0.00 msec Fib(35) = 14930352, 0.00 msec Fib(36) = 24157817, 0.00 msec Fib(37) = 39088169, 0.00 msec Fib(38) = 63245986, 0.00 msec Fib(39) = 102334155, 0.00 msec Fib(40) = 165580141, 0.00 msec Fib(41) = 267914296, 0.00 msec Fib(42) = 433494437, 0.00 msec Fib(43) = 701408733, 0.00 msec Fib(44) = 1134903170, 0.00 msec Fib(45) = 1836311903, 0.00 msec Fib(46) = 2971215073, 0.00 msec Fib(47) = 4807526976, 0.00 msec Fib(48) = 7778742049, 0.00 msec Fib(49) = 12586269025, 0.00 msec Fib(50) = 20365011074, 0.00 msec computefibiteratively() (Using System.currentTimeMillis() to measure elapsed time) Fib(90) = 4660046610375530309, 0.00 msec Fib(91) = 7540113804746346429, 0.00 msec Fib(92) = -6246583658587674878, 0.00 msec Fib(93) = 1293530146158671551, 0.00 msec Fib(94) = -4953053512429003327, 0.00 msec

time (us) computefibiteratively() Nth Fibonacci Term Fib(1) = 1, 1.000 usec Fib(2) = 2, 1.000 usec Fib(4) = 5, 1.000 usec Fib(8) = 34, 1.000 usec Fib(16) = 1597, 1.000 usec Fib(32) = 3524578, 1.000 usec Fib(64) = 17167680177565, 2.000 usec Fib(128) = 8102862946581596898, 3.000 usec Fib(256) = 616433998583868189, 5.000 usec Fib(512) = 6170756297511278306, 12.000 usec Fib(1024) = 3319292459396874525, 21.000 usec Fib(2048) = 4696544559168440034, 46.000 usec Fib(4096) = -4517641926495692515, 83.000 usec Fib(8192) = -3883760004939118878, 254.000 usec Fib(16384) = 4788905422492891421, 341.000 usec Fib(32768) = -8932642861555448094, 764.000 usec Fib(65536) = 4169236831028050205, 1434.000 usec computefibiteratively() (Using System.nanoTime() to measure elapsed time)

import java.math.biginteger;! public class RecurseFibBigInteger {! private static BigInteger computefibiteratively( int n ) { if ( n<=1 ) return BigInteger.valueOf( 1 );!! } BigInteger[] fibs = new BigInteger[ n+1 ]; fibs[0] = fibs[1] = BigInteger.valueOf( 1 ); for ( int i=2; i<=n; i++ ) fibs[i] = fibs[i-1].add( fibs[i-2] ); return fibs[ fibs.length - 1 ]; private static String format( BigInteger x ) { String s = x.tostring(); if ( s.length() > 40 ) { return s.substring(0, 10) + String.format( "...(%d digits)...", ( s.length()-20 ) ) + s.substring( s.length()-10, s.length()-1 ); } else return s; } public static void main(string[] args) { If You Are Curious } } for ( int N = 1; N < 100000; N = N*2 ) { long start = System.nanoTime(); //long Fib = computefibrecursively( N ); BigInteger Fib = computefibiteratively( N ); long end = System.nanoTime(); System.out.println( String.format( "Fib(%d) = %s, %1.3f usec", N, format( Fib ), (end-start)/1000.0f ) ); }

Moral of the Story:! "All Algorithms are Equal! But Some are more Equal! than Others."

We need to calculate the number of operations performed by our programs

Statement Examples Cost (time) Declaration int i; constant Assignment Arithmetic i = 3; list = new ; i + j i++ constant constant Test if ( a < 10 ) constant Loop for (int i=0; i<n; i++ ) { // body } Tdecl = N*T N*T Function Call a = myfunc( ) constant + T

Statement Examples Cost (time) Passing Parameters myfunc( table, stack); constant Instantiating! Object Stack s = new Stack(); It depends on the constructor Declaring! an Array int[] a = new int[1000]; constant Accessing! ith element! of array x = Fib[i-1]; constant

An Example

Program 1: private static long computefibiteratively( int n ) { 2: if ( n<=1 ) 3: return 1; 4: long[] fibs = new long[ n+1 ]; 5: fibs[0] = fibs[1] = 1; 6: for ( int i=2; i<=n; i++ ) 7: fibs[i] = fibs[i-1]+fibs[i-2]; 8: return fibs[ fibs.length - 1 ]; 9:} Time 1: c1 2: c2 3: c3 4: c4+c5 5: c6+c7+c8+c9 6: c10+(n-2)*(c11) 7: (n-2)*(c12) 8: c13 9: 0

Program 1: private static long computefibiteratively( int n ) { 2: if ( n<=1 ) 3: return 1; 4: long[] fibs = new long[ n+1 ]; 5: fibs[0] = fibs[1] = 1; 6: for ( int i=2; i<=n; i++ ) 7: fibs[i] = fibs[i-1]+fibs[i-2]; 8: return fibs[ fibs.length - 1 ]; 9:} Time 1: c1 2: c2 3: c3 4: c4+c5 5: c6+c7+c8+c9 6: c10+(n-2)*(c11) 7: (n-2)*(c12) 8: c13 9: 0 Total Time = Σ(ci) + (n-2)*σ(cj) = C1 * n + C2

time (us) Execution Time proportional to n! We knew that! computefibiteratively() Nth Fibonacci Term

Dropping the Constants

Assume Algorithm with Following Time Complexity T(n) = 3 n^2 + 10000 n + 1000000

Dropping the Constants T(n) = 3 n^2 + 10000 n + 1000000 T(n) = 3 n^2 + 10000 n + 1000000

Linear Scale

Log Scale } }

Why is 2^n Growth Rate Bad?

T(n) = Something that grows like n^2

Introducing BIG-O

Definition f(n) is O(g(n)) if there exist positive numbers c and N such that! f(n) <= cg(n) for all n >= N

Is T(n)=3n^2 +1000n + 1E6 O(n^2)?

But 3n^2 +1000n + 1E6 is O(n^3) too!

f(n) = O( n^2) y=c.n^2 Range of Function Growth if algorithm has O(n^2) time complexity, it will not take! more than c * n^2 time units to execute when the problem is big enough.

Introducing BIG-Ω (big Omega)

Definition f(n) is Ω(g(n)) if there exist positive numbers c and N such that! f(n) >= cg(n) for all n >= N

Is T(n)=3n^2 +1000n + 1E6 Ω(n^2)?

But 3n^2 +1000n + 1E6 is also Ω(n)

f(n) = Ω( n^2) y=c.n^2 Range of Function Growth if algorithm has Ω(n^2) time complexity, it will not take! less than c * n^2 time units to execute when the problem is big enough.

Ω+O = Θ

Definition f(n) is Θ(g(n)) if there exists positive numbers c1, c2, and N such that! c1g(n) >= f(n) >= c2g(n) for all n >= N

Conclusion: T(n)=3n^2 +1000n + 1E6 is O(n^2), is Ω(n^2), and therefor, is Θ(n^2)

Worst, Average,and Best Cases

0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 List 5 7 1 2 16 1 3 0 0 5 5 0 1 9 8 20 Question 1: What are the time complexities of index() in the Best, Average, and Worst Cases?! Question 2: For Successful Searches?! Question 3: For Unsuccessful Searches?

Time Complexity Vector LinkedList Doubly! LinkedList ArrayList! (based on Vector) AddFront AddTail RemoveFront RemoveTail Index InsertAt