Complexity, General. Standard approach: count the number of primitive operations executed.

Similar documents
EE 368. Week 6 (Notes)

Searching in General

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

Advanced Java Concepts Unit 3: Stacks and Queues

CSE 373 Spring Midterm. Friday April 21st

First Semester - Question Bank Department of Computer Science Advanced Data Structures and Algorithms...

Algorithm Analysis. Performance Factors

csci 210: Data Structures Stacks and Queues

Stacks, Queues (cont d)

Name CPTR246 Spring '17 (100 total points) Exam 3

MULTIMEDIA COLLEGE JALAN GURNEY KIRI KUALA LUMPUR

Revision Statement while return growth rate asymptotic notation complexity Compare algorithms Linear search Binary search Preconditions: sorted,

Choice of C++ as Language

Data Structures Question Bank Multiple Choice

STACKS. A stack is defined in terms of its behavior. The common operations associated with a stack are as follows:

CSE 143 SAMPLE MIDTERM

Stack ADT. ! push(x) puts the element x on top of the stack! pop removes the topmost element from the stack.

Lecture No.04. Data Structures

1. Stack Implementation Using 1D Array

MID TERM MEGA FILE SOLVED BY VU HELPER Which one of the following statement is NOT correct.

PROGRAM EFFICIENCY & COMPLEXITY ANALYSIS

CH ALGORITHM ANALYSIS CH6. STACKS, QUEUES, AND DEQUES

namibia UniVERSITY OF SCIEnCE AnD TECHnOLOGY

Recitation 9. Prelim Review

Computer Science 1 Ah

Summer Final Exam Review Session August 5, 2009

Basic Data Structures

JAVA OPERATORS GENERAL

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

Section 05: Midterm Review

FORTH SEMESTER DIPLOMA EXAMINATION IN ENGINEERING/ TECHNOLIGY- MARCH, 2012 DATA STRUCTURE (Common to CT and IF) [Time: 3 hours

Data Structures and Algorithms, Winter term 2018 Practice Assignment 3

-The Hacker's Dictionary. Friedrich L. Bauer German computer scientist who proposed "stack method of expression evaluation" in 1955.

Basic Data Structures 1 / 24

Analysis of Algorithms 1 / 18

DATA STRUCTURE : A MCQ QUESTION SET Code : RBMCQ0305

Introduction to the Analysis of Algorithms. Algorithm

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

1) What is the primary purpose of template functions? 2) Suppose bag is a template class, what is the syntax for declaring a bag b of integers?

1 P a g e A r y a n C o l l e g e \ B S c _ I T \ C \

ASYMPTOTIC COMPLEXITY

McGill University Faculty of Science School of Computer Science. MIDTERM EXAMINATION - Sample solutions. COMP-250: Introduction to Computer Science

Remember to also pactice: Homework, quizzes, class examples, slides, reading materials.

Recursion. Example R1

Jump Statements. The keyword break and continue are often used in repetition structures to provide additional controls.

CS 314 Exam 2 Spring

1. Attempt any three of the following: 15

The Stack ADT. Stacks. The Stack ADT. The Stack ADT. Set of objects in which the location an item is inserted and deleted is prespecified.

Computer Science E-22 Practice Final Exam

Jump Statements. The keyword break and continue are often used in repetition structures to provide additional controls.

Sorting. Bubble Sort. Selection Sort

Draw a diagram of an empty circular queue and describe it to the reader.

CSE 332 Spring 2013: Midterm Exam (closed book, closed notes, no calculators)

March 13/2003 Jayakanth Srinivasan,

FINALTERM EXAMINATION Fall 2009 CS301- Data Structures Question No: 1 ( Marks: 1 ) - Please choose one The data of the problem is of 2GB and the hard

University of Palestine. Mid Exam Total Grade: 100

CSE 143 SAMPLE MIDTERM SOLUTION

CS171 Midterm Exam. October 29, Name:

ECE 242 Fall 13 Exam I Profs. Wolf and Tessier

Abstract Data Types. Stack. January 26, 2018 Cinda Heeren / Geoffrey Tien 1

MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION (Autonomous) (ISO/IEC Certified)

CSC 222: Computer Programming II. Spring 2005

from inheritance onwards but no GUI programming expect to see an inheritance question, recursion questions, data structure questions

Algorithm Analysis. Spring Semester 2007 Programming and Data Structure 1

MIDTERM EXAMINATION Spring 2010 CS301- Data Structures

University of Palestine. Final Exam 2 nd semester 2014/2015 Total Grade: 50

Sorting. Popular algorithms: Many algorithms for sorting in parallel also exist.

CS 261 Data Structures. Big-Oh Analysis: A Review

Stacks, Queues and Hierarchical Collections

ECE250: Algorithms and Data Structures Midterm Review

Arrays and Linked Lists

CSE 143. Complexity Analysis. Program Efficiency. Constant Time Statements. Big Oh notation. Analyzing Loops. Constant Time Statements (2) CSE 143 1

Section 05: Solutions

(Refer Slide Time: 1:27)

DC104 DATA STRUCTURE JUNE Q.2 a. If you are using C language to implement the heterogeneous linked list, what pointer type will you use?

infix expressions (review)

Data Structures CSCI C343, Fall 2016 [B]

Building Java Programs

Basic Data Structures (Version 7) Name:

CMPS 12A - Winter 2002 Final Exam A March 16, Name: ID:

Data Structures Lecture 3 Order Notation and Recursion

DO NOT. UNIVERSITY OF CALIFORNIA Department of Electrical Engineering and Computer Sciences Computer Science Division. P. N.

Computer Science Foundation Exam

Analysis of Algorithms. 5-Dec-16

Write a program to implement stack or any other data structure in Java ASSIGNMENT NO 15

SOLUTIONS. COMP103 Introduction to Data Structures and Algorithms

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


CSCI-1200 Data Structures Spring 2018 Lecture 7 Order Notation & Basic Recursion

9/26/2018 Data Structure & Algorithm. Assignment04: 3 parts Quiz: recursion, insertionsort, trees Basic concept: Linked-List Priority queues Heaps

Recursion: Factorial (1) Recursion. Recursion: Principle. Recursion: Factorial (2) Recall the formal definition of calculating the n factorial:

ESC101 : Fundamental of Computing

ASYMPTOTIC COMPLEXITY

The Arithmetic Operators. Unary Operators. Relational Operators. Examples of use of ++ and

The Arithmetic Operators

Advanced Java Concepts Unit 5: Trees. Notes and Exercises

Total Score /1 /20 /41 /15 /23 Grader

Big-O-ology. Jim Royer January 16, 2019 CIS 675. CIS 675 Big-O-ology 1/ 19

Stacks. Access to other items in the stack is not allowed A LIFO (Last In First Out) data structure

Recursion Chapter 3.5

Transcription:

Complexity, General Allmänt Find a function T(n), which behaves as the time it takes to execute the program for input of size n. Standard approach: count the number of primitive operations executed. Standard approximations: we are not interested of the precise count, only of the increase ratio as a function of the size n. For this we use the concept of Big O. LAD, complexity 1

Example Example Compute the sum of the numbers from 1 to n int sum = 0; ( 1 op) for ( int i = 1; i <= n; i++ ) ( 1 op, 1op, 2 op) sum =+ i; ( 2 op ) Gives the time function: T(n) = 1 + 1 + (n + 1) 1 + n (2 + 2) = 3 + 5 n LAD, complexity 2

Definition of Big O Big O Let g be a function whose domain and co-domain are the non-negative integer values. A function f is said to be of O(g) if for some pair of non-negative constants C and K, it holds: ( n K) f(n) C g(n) LAD, complexity 3

Common Big O functions Big O O(1) : The constant complexity O( 2 log n) : The logaritmic complexity O(n) : The linear complexity O(n 2 ) : The quadratic complexity O(n 3 ) : The cubic complexity O(2 n ) : The exponential complexity and combinations of the functions above, e.g. O(n 2 log n) LAD, complexity 4

Example above Big O Take the summation above for which we have f(n) == T(n) = 3 + 5 n. Let g be the function g(n) = n. Choose C = 6 and K = 3 then we have that: f(n) C g(n), for all n > K. i.e. f is of O(g), or f O(g) LAD, complexity 5

Example double loop Big O Consider the loop-statement: for ( int i = 0; i < n; i++ ) for (int j = 0; j < n; j++ ) // k primitive operations We find the complexity function by setting the work of the k primitive operations to one and use double summation: n n 1 = n n = n n O( n 2 ) i=0 j=0 i=0 LAD, complexity 6

Example: double loop Big O Now change the inner loop: for ( int i = 0; i < n; i++ ) for (int j = i; j < n; j++ ) // k primitive operations and we get n 1 i=0 n 1 j=i 1 = n 1 i=0 n i = n i=1 i = n (n + 1) 2 O( n 2 ) LAD, complexity 7

Example squareroot Big O public static int squareroot( int n ) { int i = 1, j = n+1; while ( i + 1!= j ) { int k = ( i + j ) / 2; if ( k*k <= n ) i = k; else j = k; } return i; } // squareroot LAD, complexity 8

Complexity of squareroot Big O The complexity varies depending on the size of the argument (n) We get the time function: T(n) = 1 + 2 + 2 + noofloops (2 + 3 + 2 + 1) or shorter T(n) = 5 + 8 noofloops Since the interval (i.. j )is decreased to half its size each loop, the number of loops becomes 2 log n why we draw the conclusion T(n) O( 2 log n ) LAD, complexity 9

Program squareroot Big O public static int noofloops; public static int squareroot( int n ) { while ( i + 1!= j ) { noofloops++;... }... } // squareroot public static void main(string[] args ) { int[] a = { 1, 8, 32, 128, 1024, 2048, 10000 }; for( int n : a ) { noofloops = 0; System.out.println( n + "\t" + squareroot(n) + "\t" + noofloops } LAD, complexity 10

Run squareroot Big O sed:bjerner:[~/dvds/program]$ java SquareRoot 1 1 1 8 2 3 32 5 5 128 11 7 1024 32 10 2048 45 11 10000 100 14 LAD, complexity 11

Algorithm Insertion sort Given an array a to be sorted. let i = smallest index + 1 while i highest index. if a[i] < a[i 1] let temp = a[i], a[i] = a[i 1] and j = i 1 while j > smallest index and a[j 1] > temp let a[j] = a[j 1] decrease j by 1 let a[j] = temp increase a[i] by 1 LAD, complexity 12

Correctness Predicates Insertion sort Loop invariant: The segment to the left of i in a is sorted. Loop condition: i is less than or equal to the highest index for a. Loop conclusion: The whole array is sorted. LAD, complexity 13

Java code Insertion sort public static void insertionsort( int[] a ) { for ( int i = 1; i < a.length; i++ ) if ( a[i] < a[i-1] ) { int tmp = a[i]; a[i] = a[i-1]; int j = i-1; while ( j > 0 && a[j-1] > temp ) { a[j] = a[j-1]; j--; } a[j] = temp; } }\\ insertionsort LAD, complexity 14

Complexity Insertion sort In analysing the comlexity of programs depending on probabilities, we analyse: the best case and the worst case. if they have different Big O complexity, we also analyse the: the average case LAD, complexity 15

Complexity Insertion sort The best case: Its obviously when the array is already sorted. Only the if-condition is computed. If size n = a.length, we get: n 1 i=0 1 = n O(n), i.e. linear The worst case: When the array is sorted reversely. Gives: n 1 i=0 i j=1 1 = n 1 i=0 i = n (n 1) 2 O( n 2 ), i.e. quadratic LAD, complexity 16

Complexity Insertion sort The average case: Assume that the inner loop only goes halfways, then we get: n 1 i=0 i 2 j=1 1 =... = N (N 1) 4 O(n 2 ), i.e. still quadratic LAD, complexity 17

Methods on stacks Stacks Historically, a stack is an object on which we can perform following methods: push(element) : void, puts an element onto the stack. pop() : void, takes the last pushed element away from the stack, i.e. in FILO order (or in LIFO order) top() : element, gives the last pushed element, but let it stay in the stack isempty : boolean: gives true if the stack is empty, otherwise false Sometimes size() : integer occurs. Cmp: with a plate holder in a restaurant. LAD, complexity 18

Exemple, pre-, in-, and post-fix Stacks In order to place a binary operator with respect to its operandes, there are three possibilities: preorder, i.e. first, + op 1 op 2 inorder, i.e. between, op 1 + op 2 postorder, i.e. last, op 1 op 2 + LAD, complexity 19

Exemple A, expressions of different forms Stacks We want to take the product of a and b and add it to the product of c and d preorder : + a b c d inorder : a b + c d postorder : a b c d + Note: For inorder we have to use priorty rules in order to perform the operations in right order! LAD, complexity 20

Exemple B, expressions of different forms Stacks Now, in stead we want to take the sum of a and b and multiply it with the sum of c and d preorder : + a b + c d inorder : (a + b) (c + d) postorder : a b + c d + Note: For inorder we have to use paranteses in order to perform the operations in right order! LAD, complexity 21

Compute expressions on postfix form Stacks Given a sequens of symbols, in postfix form, as an iterator it and a stack st As long as it not empty let sym be next symbol in it if sym is an operand put sym onto st else: if the operator is + Get the two uppermost operands from st, add them and put the result onto st else: Get the two uppermost operands from st, multiply them and put the result onto st The result is now on top of the stack st! LAD, complexity 22

Compute expressions on postfix form, Java Stacks Note: In most implementations pop gives the removed object as a result. Iterator<String> it =...; Stack<Integer> st = new Stack<Integer>(); while ( it.hasnext() ) { String sym = it.next(); switch ( sym.charat(0) ) { case + : st.push( st.pop() + st.pop() ); break; case * : st.push( st.pop() * st.pop() ); break; default : push( Integer.parsInt( sym ) ); } } LAD, complexity 23

Stack implementation Stacks May be implemented efficiantly either by array or as a linked structure. An array is allocated with expected maximum size of the stack, if possible, and a field keeping track of the stack top. All operations will then be of O(1). For the linked structure, a singly linked structure, is apropiate, since all operations only concern the first element in the list. Here also, the operations are of O(1). LAD, complexity 24