Analysis of Algorithms: Time & Space

Slides:



Advertisements
Similar presentations
Growth-rate Functions
Advertisements

Analysis of Algorithms: time & space Dr. Jeyakesavan Veerasamy The University of Texas at Dallas, USA.
Arrays Dr. Jey Veerasamy July 31 st – August 23 rd 9:30 am to 12 noon 1.
Complexity Theory  Complexity theory is a problem can be solved? Given: ◦ Limited resources: processor time and memory space.
Analysys & Complexity of Algorithms Big Oh Notation.
Fall 2006CENG 7071 Algorithm Analysis. Fall 2006CENG 7072 Algorithmic Performance There are two aspects of algorithmic performance: Time Instructions.
CS 206 Introduction to Computer Science II 09 / 10 / 2008 Instructor: Michael Eckmann.
1 CSE1301 Computer Programming Lecture 31: List Processing (Search)
Object (Data and Algorithm) Analysis Cmput Lecture 5 Department of Computing Science University of Alberta ©Duane Szafron 1999 Some code in this.
Concept of Basic Time Complexity Problem size (Input size) Time complexity analysis.
CS 206 Introduction to Computer Science II 01 / 28 / 2009 Instructor: Michael Eckmann.
Analysis of Algorithms CPS212 Gordon College. Measuring the efficiency of algorithms There are 2 algorithms: algo1 and algo2 that produce the same results.
Abstract Data Types (ADTs) Data Structures The Java Collections API
Algorithm Analysis (Big O)
Analysis of Algorithms
CSE1301 Computer Programming: Lecture 26 List Processing (Search)
Big Oh Algorithms are compared to each other by expressing their efficiency in big-oh notation Big O notation is used in Computer Science to describe the.
Analysis of Algorithms CSCI Previous Evaluations of Programs Correctness – does the algorithm do what it is supposed to do? Generality – does it.
Analysis of Algorithms [ Section 4.1 ] Examples of functions important in CS: the constant function:f(n) =
Big Oh Data Structures and Algorithms CS 244 Brent M. Dingle, Ph.D. Department of Mathematics, Statistics, and Computer Science University of Wisconsin.
Computer Science and Software Engineering University of Wisconsin - Platteville 8. Comparison of Algorithms Yan Shi CS/SE 2630 Lecture Notes Part of this.
Big Oh – part 2 CS 244 This presentation requires Sound Enabled Brent M. Dingle, Ph.D. Game Design and Development Program Department of Mathematics,
1 5. Abstract Data Structures & Algorithms 5.6 Algorithm Evaluation.
Foundations of Algorithms, Fourth Edition
Algorithm Analysis (Big O)
1 Algorithm Analysis. 2 Question Suppose you have two programs that will sort a list of student records and allow you to search for student information.
Searching Topics Sequential Search Binary Search.
CISC220 Spring 2010 James Atlas Lecture 07: Big O Notation.
BITS Pilani Pilani Campus Data Structure and Algorithms Design Dr. Maheswari Karthikeyan Lecture1.
John Levine, Computer and Information Sciences, Strathclyde University 1 Algorithms and Complexity 2: Complexity Notation.
1 Algorithms Searching and Sorting Algorithm Efficiency.
1 5. Abstract Data Structures & Algorithms 5.6 Algorithm Evaluation.
Algorithm Analysis 1.
Introduction to Data Structures
Introduction to Analysis of Algorithms
Analysis of Algorithms
Searching – Linear and Binary Searches
Algorithmic Efficency
Introduction to Analysis of Algorithms
Introduction to complexity
Asymptotic Notations Algorithms perform f(n) basic operations to accomplish task Identify that function Identify size of problem (n) Count number of operations.
Data structure – is the scheme of organizing related information.
Introduction to Analysis of Algorithms
Lesson Objectives Aims Understand the following: The big O notation.
Complexity Analysis.
Lecture 2: Implementing ADTs
Building Java Programs
CSE 143 Lecture 5 Binary search; complexity reading:
CSE 143 Lecture 5 More Stacks and Queues; Complexity (Big-Oh)
What is CS 253 about? Contrary to the wide spread belief that the #1 job of computers is to perform calculations (which is why the are called “computers”),
Asymptotic Notations Algorithms perform f(n) basic operations to accomplish task Identify that function Identify size of problem (n) Count number of operations.
Big O Notation.
CSC 205 Java Programming II
Lecture 5: complexity reading:
Analysys & Complexity of Algorithms
Programming and Data Structure
CSE 143 Lecture 8 More Stacks and Queues; Complexity (Big-Oh)
Programming and Data Structure
CSE 2010: Algorithms and Data Structures Algorithms
Building Java Programs
Introduction to Data Structures
Asymptotic Notations Algorithms perform f(n) basic operations to accomplish task Identify that function Identify size of problem (n) Count number of operations.
CSE 143 Lecture 8 More Stacks and Queues; Complexity (Big-Oh)
8. Comparison of Algorithms
Building Java Programs
slides created by Ethan Apter
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
Algorithms and data structures: basic definitions
CMPT 225 Lecture 6 – Review of Complexity Analysis using the Big O notation + Comparing List ADT class implementations.
Data Structures & Programming
Presentation transcript:

Analysis of Algorithms: Time & Space Salim Arfaoui SJCNY-Brooklyn

What does ‘Space Complexity’ mean? Space Complexity: The term Space Complexity is misused for Auxiliary Space at many places. Auxiliary Space is the extra space or temporary space used by an algorithm. Space Complexity of an algorithm is total space taken by the algorithm with respect to the input size. Space complexity includes both Auxiliary space and space used by input.

Program running time When is the running time (waiting time for user) noticeable/important?

Program running time – Why? When is the running time (waiting time for user) noticeable/important? web search database search real-time systems with time constraints

Factors that determine running time of a program

Factors that determine running time of a program problem size: n basic algorithm / actual processing memory access speed CPU/processor speed # of processors? compiler/linker optimization?

Time Complexity measure of algorithm efficiency has a big impact on running time. Big-O notation is used. To deal with n items, time complexity can be O(1), O(log n), O(n), O(n log n), O(n2), O(n3), O(2n), even O(nn).

Coding example #1 for ( i=0 ; i<n ; i++ ) m += i;

Coding example #2 for ( i=0 ; i<n ; i++ ) for( j=0 ; j<n ; j++ ) sum[i] += entry[i][j];

Coding example #3 for ( i=0 ; i<n ; i++ ) for( j=0 ; j<i ; j++ ) m += j;

Coding example #4 i = 1; while (i < n) { tot += i; i = i * 2; }

Example #4: equivalent # of steps? i = n; while (i > 0) { tot += i; i = i / 2; }

Coding example #5 for ( i=0 ; i<n ; i++ ) for( j=0 ; j<n ; j++ ) for( k=0 ; k<n ; k++ ) sum[i][j] += entry[i][j][k];

Coding example #6 for ( i=0 ; i<n ; i++ ) for( j=0 ; j<n ; j++ ) sum[i] += entry[i][j][0]; for( k=0 ; k<n ; k++ ) sum[i] += entry[i][0][k];

Coding example #7 for ( i=0 ; i<n ; i++ ) for( j=0 ; j< sqrt(n) ; j++ ) m += j;

Coding example #8 for ( i=0 ; i<n ; i++ ) for( j=0 ; j< sqrt(995) ; j++ ) m += j;

Coding example #8 : Equivalent code for ( i=0 ; i<n ; i++ ) { m += j; … m += j; // 31 times }

Coding example #9 int total(int n) main() for( i=0 ; i < n; i++)   subtotal += i; main()  for ( i=0 ; i<n ; i++ )   tot += total(i);

Coding example #9: Equivalent code for ( i=0 ; i<n ; i++ ) { subtotal = 0; for( j=0 ; j < i; j++) subtotal += j; tot += subtotal; }

Important Functions Often appear in algorithm analysis: Constant  1 Logarithmic  log n Linear  n N-Log-N  n log n Quadratic  n2 Cubic  n3 Exponential  2n

Important Functions Growth Rates log(n) nlog(n) n2 n3 2n 8 3 24 64 512 256 16 4 4096 65536 32 5 160 1024 32768 4.3x109 6 384 262144 1.8x1019 128 7 896 16384 2097152 3.4x1038 2048 16777218 1.2x1077

Growth Rates Illustration Running Time in ms (10-3 of sec) Maximum Problem Size (n) 1000 ms 60000 ms 36*105 m (1 second) (1 minute) (1 hour) n 1000 60,000 3,600,000 n2 32 245 1,897 2n 10 16 22

Practical Examples

Example #1: carry n items from one room to another room

Example #1: carry n items from one room to another room How many operations? n pick-ups, n forward moves, n drops and n reverse moves  4 n operations 4n operations = c. n = O(c. n) = O(n) Similarly, any program that reads n inputs from the user will have minimum time complexity O(n).

Example #2: Sorting patients records in Doctor Office What is the time complexity of Sort?

Example #2: Sorting patients records in Doctor Office What is the time complexity of Sort? Binary Sort algorithm at work O(log n) Sequential Sort? O(n)

Example #3: Store manager gives gifts to first 10 customers There are n customers in the queue. Manager brings one gift at a time.

Example #3: Store manager gives gifts to first 10 customers There are n customers in the queue. Manager brings one gift at a time. Time complexity = O(c. 10) = O(1) Manager will take exactly same time irrespective of the line length.

Example #4: Thief visits a Doctor with Back Pain

Example #4: Thief visits a Doctor with Back Pain Doctor asks a few questions: Is there a lot of stress on the job? Do you carry heavy weight?

Example #4: Thief visits a Doctor with Back Pain Doctor asks a few questions: Is there a lot of stress on the job? Do you carry heavy weight? Doctor says: Never carry > 50 kgs

Space complexity