COMP s1 Computing 2 Complexity

Slides:



Advertisements
Similar presentations
Growth-rate Functions
Advertisements

MATH 224 – Discrete Mathematics
CSE Lecture 3 – Algorithms I
HST 952 Computing for Biomedical Scientists Lecture 10.
Razdan with contribution from others 1 Algorithm Analysis What is the Big ‘O Bout? Anshuman Razdan Div of Computing.
Chapter 1 – Basic Concepts
Fall 2006CENG 7071 Algorithm Analysis. Fall 2006CENG 7072 Algorithmic Performance There are two aspects of algorithmic performance: Time Instructions.
Chapter 10 Algorithm Efficiency
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved L17 (Chapter 23) Algorithm.
1 CSE1301 Computer Programming Lecture 31: List Processing (Search)
Complexity Analysis (Part I)
CS 307 Fundamentals of Computer Science 1 Asymptotic Analysis of Algorithms (how to evaluate your programming power tools) based on presentation material.
Cmpt-225 Algorithm Efficiency.
Algorithm Efficiency and Sorting Bina Ramamurthy CSE116A,B.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Chapter 23 Algorithm Efficiency.
Data Structure Algorithm Analysis TA: Abbas Sarraf
Analysis of Algorithm.
Elementary Data Structures and Algorithms
Analysis of Algorithms COMP171 Fall Analysis of Algorithms / Slide 2 Introduction * What is Algorithm? n a clearly specified set of simple instructions.
Analysis of Algorithms Spring 2015CS202 - Fundamentals of Computer Science II1.
Algorithm Analysis (Big O)
Asymptotic Notations Iterative Algorithms and their analysis
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Chapter 23 Algorithm Efficiency.
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.
Program Performance & Asymptotic Notations CSE, POSTECH.
Week 2 CS 361: Advanced Data Structures and Algorithms
1 Chapter 24 Developing Efficient Algorithms. 2 Executing Time Suppose two algorithms perform the same task such as search (linear search vs. binary search)
Mathematics Review and Asymptotic Notation
Unit III : Introduction To Data Structures and Analysis Of Algorithm 10/8/ Objective : 1.To understand primitive storage structures and types 2.To.
Analysis of Algorithms
© 2011 Pearson Addison-Wesley. All rights reserved 10 A-1 Chapter 10 Algorithm Efficiency and Sorting.
Searching. RHS – SOC 2 Searching A magic trick: –Let a person secretly choose a random number between 1 and 1000 –Announce that you can guess the number.
CSE1301 Computer Programming: Lecture 26 List Processing (Search)
C++ Programming: From Problem Analysis to Program Design, Second Edition Chapter 19: Searching and Sorting.
Analysis of Algorithms These slides are a modified version of the slides used by Prof. Eltabakh in his offering of CS2223 in D term 2013.
CMPT 438 Algorithms. Why Study Algorithms? Necessary in any computer programming problem ▫Improve algorithm efficiency: run faster, process more data,
Asymptotic Analysis-Ch. 3
Analysis of Algorithms CSCI Previous Evaluations of Programs Correctness – does the algorithm do what it is supposed to do? Generality – does it.
Algorithm Analysis (Algorithm Complexity). Correctness is Not Enough It isn’t sufficient that our algorithms perform the required tasks. We want them.
Program Efficiency & Complexity Analysis. Algorithm Review An algorithm is a definite procedure for solving a problem in finite number of steps Algorithm.
Data Structure Introduction.
Computer Science and Software Engineering University of Wisconsin - Platteville 8. Comparison of Algorithms Yan Shi CS/SE 2630 Lecture Notes Part of this.
Chapter 5 Algorithms (2) Introduction to CS 1 st Semester, 2015 Sanghyun Park.
RUNNING TIME 10.4 – 10.5 (P. 551 – 555). RUNNING TIME analysis of algorithms involves analyzing their effectiveness analysis of algorithms involves analyzing.
1/6/20161 CS 3343: Analysis of Algorithms Lecture 2: Asymptotic Notations.
Algorithm Analysis (Big O)
Algorithm Analysis. What is an algorithm ? A clearly specifiable set of instructions –to solve a problem Given a problem –decide that the algorithm is.
E.G.M. PetrakisAlgorithm Analysis1  Algorithms that are equally correct can vary in their utilization of computational resources  time and memory  a.
1. Searching The basic characteristics of any searching algorithm is that searching should be efficient, it should have less number of computations involved.
Searching Topics Sequential Search Binary Search.
Search Algorithms Written by J.J. Shepherd. Sequential Search Examines each element one at a time until the item searched for is found or not found Simplest.
Recursion COMP x1 Sedgewick Chapter 5. Recursive Functions problems can sometimes be expressed in terms of a simpler instance of the same problem.
Concepts of Algorithms CSC-244 Unit 3 and 4 Algorithm Growth Rates Shahid Iqbal Lone Computer College Qassim University K.S.A.
Analysis of Algorithms Spring 2016CS202 - Fundamentals of Computer Science II1.
GC 211:Data Structures Week 2: Algorithm Analysis Tools Slides are borrowed from Mr. Mohammad Alqahtani.
1 Algorithms Searching and Sorting Algorithm Efficiency.
Algorithmic Foundations COMP108 COMP108 Algorithmic Foundations Algorithm efficiency Prudence Wong.
CSE 3358 NOTE SET 2 Data Structures and Algorithms 1.
Algorithmic Foundations COMP108 COMP108 Algorithmic Foundations Algorithm efficiency Prudence Wong
Data Structures I (CPCS-204) Week # 2: Algorithm Analysis tools Dr. Omar Batarfi Dr. Yahya Dahab Dr. Imtiaz Khan.
Algorithm Analysis 1.
CMPT 438 Algorithms.
Analysis of Algorithms
GC 211:Data Structures Week 2: Algorithm Analysis Tools
Introduction to complexity
Introduction to Algorithms
GC 211:Data Structures Algorithm Analysis Tools
Teach A level Computing: Algorithms and Data Structures
GC 211:Data Structures Algorithm Analysis Tools
Algorithm Analysis Bina Ramamurthy CSE116A,B.
Presentation transcript:

COMP1927 15s1 Computing 2 Complexity Show them the video from Richard Buckland. Lecture 2. 44:00 Relate back to the big-O from linked lists Consider these situations;

Problems, Algorithms, Programs and Processes Problem: A problem that needs to be solved Algorithm: Well defined instructions for completing the problem Program: Implementation of the algorithm in a particular programming language Process: An instance of the program as it is being executed on a particular machine

Analysis of software What makes software "good"? returns expected result for all valid inputs behaves "sensibly" for non-valid inputs clear code, easy to maintain/modify interface is clear and consistent (API or GUI) returns results quickly (even for large inputs) Ie. It is efficient We may sometimes also be interested in other measures memory/disk space, network traffic, disk IO etc

Algorithm Efficiency The algorithm is by far the most important determinant of the efficiency of a program Small speed ups in terms of operating systems, compilers, computers and implementation details are irrelevant May give small speed ups but usually only by a small constant factor Show the telephone book example or dictionary example. Find big ones. How many times can we divide by?

Determining Algorithm Efficiency At the design stage Theoretical approach complexity theory After the testing stage Once it is implemented and correct you can empirically evaluate performance eg using the time command Show demo using the time command.

Timing Note we are not interested in the absolute time it takes to run. We are interested in the relative time it takes as the problem increases Absolute times differ on different machines and with different languages

Complexity Theory Example int linearSearch(int a[], int n, int key){ for indexes from 0 to n-1 if key equals current element array return current index return -1 } What is the worst case cost? When does this occur? How many comparisons between data instances were made? Comparison for int single CPU instruction.

Complexity Example How many times does each line run in the worst case? C0: line 2: For loop n+1 times C1: line 3: n comparisons C2: line 4: 0 times (worst case) C3: line 5: 1 time (worst case) Total: C0(n+1) + C1(n) + C3 = O(n) For an unsorted sequence that is the best we can do Eg. Planning to get to a job interview. Worst case in terms of traffic to guarantee you can get there in time.

Informal Definition of Big-O Notation We express complexity using big-O notation Represents the asymptotic worst case (unless stated otherwise) time complexity Big-O expressions do not have constants or low- order terms as when n gets larger these do not matter For example: For a problem of size n, if the cost of the worst case is 1.5n2 +3n +10 in Big-O notation would be O(n2) So if someone said minutes vs seconds vs days – estimate from your builder or in the waiting room.

Big O-notation Formal Definition The big O-notation is used to classify the work complexity of algorithms Definition: A function f(n) is said to be in (the set) O(g(n)) if there exist constants c and N0 such that f(n) < c * g(n) for all n > N0

Empirical Analysis Linear Search Use the ‘time’ command in linux. Run on different sized inputs time ./prog < input > /dev/null not interested in real-time interested in user-time What is the relationship between input size time Size of input(n) Time 100000 1000000 10000000 100000000 Practice graphing these things. Show how the scale can make a big difference – log scale. What if ran on strings? Graph it with scatter vs line.

Predicting Time If I know my algorithm is quadratic and I know that it takes 1.2 seconds to run on a data set of size 1000 Approximately how long would you expect to wait for a data set of size 2000? What about 10000? What about 100000? What about 1000000? What about 10000000? 1000: 1.2 2000: 2^2 = 4 4.8 10,000: 120 seconds 2 minutes 100,000: 1200 seconds 20 minutes 1000,000: 120000 seconds 2000 minutes or 33 hours 10,000,000: 3333 hours is 138 days 100,000,000: 333333 hours is 13888 days 38 years

Searching in a Sorted Array Given an array a of N elements, with a[i] <= a[j] for any pair of indices i,j, with i <= j < N, search for an element e in the array int a[N]; // array with N items int found = 0; int i = 0; while ((i < N) && (!found)){ found = (a[i] == e); i++; } Rewrite the code with better style If we are looking for the element 8 and we have 1 3 5 7 9 11 By the time we get to the 9 we can stop. Eg 8 < 9

Searching in a Sorted Array Given an array a of N elements, with a[i] <= a[j] for any pair of indices i,j, with i <= j < N, search for an element e in the array int a[N]; // array with N items int found = 0; int finished = 0; int i = 0; while ((i < N) && (!found) && (!finished)){ found = (a[i] == e); finished = (e < a[i]); i++; } Rewrite the code with better style If we are looking for the element 8 and we have 1 3 5 7 9 11 By the time we get to the 9 we can stop. Eg 8 < 9 exploit the fact that a is sorted

Searching in a Sorted Array How many steps are required to search an array of N elements Best case: TN = 1 Worst case: TN = N Average: TN = N/2 Still a linear algorithm, like searching in a unsorted array

Binary Search We start in the middle of the array: if a[N/2] == e, we found the element and we’re done and, if necessary, `split’ array in half to continue search if a[N/2] < e, continue search on a[0] to a[N/2 -1] if a[N/2] > e, continue search on a[N/2+1] to a[N-1] This algorithm is called binary search. 1 = 1 * 2 = 2/2 2 = 4/2 = 8/2 = 16/2 = 32/2 2^5 = 32 Log2 32 = 5 = log 10 32/log10 2 Log2 31 = 4.9

Binary Search See binary.c for implementation We maintain two indices, l and r, to denote leftmost and rightmost array index of current part of the array initially l=0 and r=N-1 iteration stops when: left and right index define an empty array, element not found Eg l > r a[(l+r)/2] holds the element we’re looking for if: a[(l+r)/2] is larger than element, continue search on left a[l]..a[(l+r)/2-1] else continue search on right a[(l+r)/2+1]..a[r]

Binary Search Searching in a Sorted Array with Binary Search How many comparisons do we need for an array of size N? Best case: TN = 1 Worst case: T1 = 1 TN = 1 + TN/2 TN = log2 N + 1 O(log n) Binary search is a logarithmic algorithm Tn = 1 + ( 1 + 1 + Tn/8) Tn/2 = 1 + Tn/4 Tn/4 = 1 + Tn/8 T(N) = T(N/4) + 2 = T(N/8) + 3 <= T(N/2^k) + k (assuming we round up)

Big-O Notation All constant functions are in O(1) All linear functions are in O(n) All logarithmic function are in the same class O(log(n)) O(log2(n)) = O(log3(n))= .... (since logb(a) * loga(n) = logb(n)) We say an algorithm is O(g(n)) if, for an input of size n, the algorithm requires T(n) steps, with T(n) in O(g(n)), and O(g(n)) minimal binary search is O(log(n)) linear search is O(n) We say a problem is O(g(n)) if the best algorithm is O(g(n)) finding the maximum in an unsorted sequence is O(n)

Common Categories O(1): constant - instructions in the program are executed a fixed number of times, independent of the size of the input O( log N): logarithmic - some divide & conquer algorithms with trivial splitting and combining operations O(N) : linear - every element of the input has to be processed, usually in a straight forward way O(N * log N): Divide &Conquer algorithms where splitting or combining operation is proportional to the input O(N2): quadratic. Algorithms which have to compare each input value with every other input value. Problematic for large input O(N3) : cubic, only feasible for very small problem sizes O( 2N): exponential, of almost no practical use 2^N when n is 20 is about 1 million – show some later. Hours, days, years, seconds, minutes

Complexity Matters n log n nlogn n^2 2^n 10 4 40 100 1024 7 700 10000 1000000 REALLY BIG 14 140000 100000000 100000 17 1700000 10000000000 20 20000000 1000000000000 2^100 has – for large n it matters a lot. Go through solution to reverse linked list and see O(n) what about doubly? O(1). Relate it back to searching? Etc.

Exercise What would be the time complexity of inserting an element at the beginning of a linked list an array What would be the time complexity of inserting an element at the end of

Exercise Explain to the person sitting next to you (or feel free to talk to yourself if you are sitting alone) What are the advantages of using linked lists over arrays What are the advantages of using arrays over linked lists