Introduction to complexity Prof. Sin-Min Lee Department of Computer Science San Jose State University.

Slides:



Advertisements
Similar presentations
MATH 224 – Discrete Mathematics
Advertisements

Fundamentals of Python: From First Programs Through Data Structures
ACM/JETT Workshop - August 4-5, 2005 Big-O Performance Analysis Based on a presentation by Dr. Simon Garrett, The University of Wales, Aberystwyth:
Fall 2006CENG 7071 Algorithm Analysis. Fall 2006CENG 7072 Algorithmic Performance There are two aspects of algorithmic performance: Time Instructions.
Introduction to Analysis of Algorithms
Scott Grissom, copyright 2004 Chapter 5 Slide 1 Analysis of Algorithms (Ch 5) Chapter 5 focuses on: algorithm analysis searching algorithms sorting algorithms.
Analysis of Algorithms. Time and space To analyze an algorithm means: –developing a formula for predicting how fast an algorithm is, based on the size.
Analyzing algorithms & Asymptotic Notation BIO/CS 471 – Algorithms for Bioinformatics.
© 2006 Pearson Addison-Wesley. All rights reserved6-1 More on Recursion.
Algorithm Analysis CS 201 Fundamental Structures of Computer Science.
Algorithm Efficiency and Sorting
Analysis of Algorithms 7/2/2015CS202 - Fundamentals of Computer Science II1.
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.
Prof. S.M. Lee Department of Computer Science. Answer:
 2006 Pearson Education, Inc. All rights reserved Searching and Sorting.
Analysis of Algorithms Spring 2015CS202 - Fundamentals of Computer Science II1.
Abstract Data Types (ADTs) Data Structures The Java Collections API
Algorithm Analysis (Big O)
COMP s1 Computing 2 Complexity
Time Complexity Dr. Jicheng Fu Department of Computer Science University of Central Oklahoma.
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.
Analysis of Algorithm Lecture 3 Recurrence, control structure and few examples (Part 1) Huma Ayub (Assistant Professor) Department of Software Engineering.
Week 2 CS 361: Advanced Data Structures and Algorithms
Discrete Mathematics Algorithms. Introduction  An algorithm is a finite set of instructions with the following characteristics:  Precision: steps are.
Lecture 8. How to Form Recursive relations 1. Recap Asymptotic analysis helps to highlight the order of growth of functions to compare algorithms Common.
Prof. S.M. Lee Department of Computer Science.
Analysis CS 367 – Introduction to Data Structures.
1 Chapter 24 Developing Efficient Algorithms. 2 Executing Time Suppose two algorithms perform the same task such as search (linear search vs. binary search)
Introduction to complexity. 2 Analysis of Algorithms Why do we need to analyze algorithms? –To measure the performance –Comparison of different algorithms.
1 Recursion Algorithm Analysis Standard Algorithms Chapter 7.
Computer Science: A Structured Programming Approach Using C1 6-9 Recursion In general, programmers use two approaches to writing repetitive algorithms.
Chapter 12 Recursion, Complexity, and Searching and Sorting
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 19: Searching and Sorting Algorithms.
Analysis of Algorithms
Chapter 19: Searching and Sorting Algorithms
1 7.Algorithm Efficiency What to measure? Space utilization: amount of memory required  Time efficiency: amount of time required to process the data Depends.
© 2011 Pearson Addison-Wesley. All rights reserved 10 A-1 Chapter 10 Algorithm Efficiency 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.
Analyzing algorithms & Asymptotic Notation BIO/CS 471 – Algorithms for Bioinformatics.
1 7.Algorithm Efficiency What to measure? Space utilization: amount of memory required  Time efficiency: amount of time required to process the data.
Analysis of Algorithms CSCI Previous Evaluations of Programs Correctness – does the algorithm do what it is supposed to do? Generality – does it.
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.
Geoff Holmes and Bernhard Pfahringer COMP206-08S General Programming 2.
Computer Science and Software Engineering University of Wisconsin - Platteville 8. Comparison of Algorithms Yan Shi CS/SE 2630 Lecture Notes Part of this.
Algorithms and data structures: basic definitions An algorithm is a precise set of instructions for solving a particular task. A data structure is any.
Algorithm Analysis Part of slides are borrowed from UST.
Big-Oh Notation. Agenda  What is Big-Oh Notation?  Example  Guidelines  Theorems.
1/6/20161 CS 3343: Analysis of Algorithms Lecture 2: Asymptotic Notations.
27-Jan-16 Analysis of Algorithms. 2 Time and space To analyze an algorithm means: developing a formula for predicting how fast an algorithm is, based.
Complexity Analysis. 2 Complexity The complexity of an algorithm quantifies the resources needed as a function of the amount of input data size. The resource.
E.G.M. PetrakisAlgorithm Analysis1  Algorithms that are equally correct can vary in their utilization of computational resources  time and memory  a.
Searching Topics Sequential Search Binary Search.
CSC 212 – Data Structures Lecture 15: Big-Oh Notation.
Analysis of Algorithms Spring 2016CS202 - Fundamentals of Computer Science II1.
Chapter 15 Running Time Analysis. Topics Orders of Magnitude and Big-Oh Notation Running Time Analysis of Algorithms –Counting Statements –Evaluating.
1 7.Algorithm Efficiency These factors vary from one machine/compiler (platform) to another  Count the number of times instructions are executed So, measure.
Algorithmic Foundations COMP108 COMP108 Algorithmic Foundations Algorithm efficiency Prudence Wong
Algorithm Analysis 1.
Introduction to Analysis of Algorithms
Analysis of Algorithms
Introduction to Analysis of Algorithms
Introduction to complexity
Introduction to Analysis of Algorithms
Introduction to Algorithms
Algorithm Analysis CSE 2011 Winter September 2018.
Recursion "To understand recursion, one must first understand recursion." -Stephen Hawking.
Algorithm An algorithm is a finite set of steps required to solve a problem. An algorithm must have following properties: Input: An algorithm must have.
CS 201 Fundamental Structures of Computer Science
Presentation transcript:

Introduction to complexity Prof. Sin-Min Lee Department of Computer Science San Jose State University

An Introduction In the 1930’s before computers were used, mathematicians worked hard to formalize and study the concept of the algorithm. –But what exactly is an algorithm? An algorithm is a precise set of instructions that leads to a solution. In other words, an algorithm is a precisely stated method for solving a problem.

3 "Fundamentally, computer science is the a science of abstraction - creating the right model for thinking about a problem and devising the appropriate mechanizable techniques to solve it.” Alfred V. Aho, 1995

4

5

6

7 The subject was founded by Knuth (who coined the term "analysis of algorithms" in the mid-sixties) and is well illustrated by his monumental series, The Art of Computer Programming The field entertains close ties with a number of areas like discrete mathematics, combinatorial analysis, probability theory, analytic number theory, asymptotic analysis, complexity theory, and sometimes statistical physics. Analysis of Algorithms is a field in computer science whose overall goal is an understanding of the complexity of algorithms. While an extremely large amount of research is devoted to worst-case evaluations,

8

9 Donald Knuth Among the heros of computer science is the algorithm master, Donald Knuth. He "wrote the book" in the early 70’s with his several volumes entitled The Art of Computer Programming, Vol… You will notice that he is referenced in essentially every book on data structures or algorithms because of his comprehensive cataloging and explanations of data structures.

10

11

12

13 Data Structures and Algorithms A data structure defines the admissible atomic steps and a control structure determines how these steps are to be combined to yield the desired algorithm. This view is stated very succinctly in the well known slogan ``algorithm = data structure + control''.

14

15

16

17

18 Recursion Recursion is more than just a programming technique. It has two other uses in computer science and software engineering, namely: as a way of describing, defining, or specifying things. as a way of designing solutions to problems (divide and conquer).

19 Mathematical Examples factorial function factorial(0) = 1 factorial(n) = n * factorial(n-1) [for n>0] Let's compute factorial(3). factorial(3) = 3 * factorial(2) = 3 * ( 2 * factorial(1) ) = 3 * ( 2 * ( 1 * factorial(0) )) = 3 * ( 2 * ( 1 * 1 ) )) = 6

20 Fibonacci function: fibonacci(0) = 1 fibonacci(1) = 1 fibonacci(n) = fibonacci(n-1) + fibonacci(n-2) [for n>1] This definition is a little different than the previous ones because It has two base cases, not just one; in fact, you can have as many as you like. In the recursive case, there are two recursive calls, not just one. There can be as many as you like.

21 Recursion Recursion can be seen as building objects from objects that have set definitions. Recursion can also be seen in the opposite direction as objects that are defined from smaller and smaller parts. “Recursion is a different concept of circularity.”(Dr. Britt, Computing Concepts Magazine, March 97, pg.78)

22 Mathematical induction appears to have been known to the mathematicians of the Hellenistic times. More rigorous accounts of the process were provided several centuries later by F. Maurolico ( ) and B. Pascal ( ).

23 Complexity: a measure of the performance of an algorithm An algorithm’s performance depends on internal and external factors External Size of the input to the algorithm Speed of the computer on which it is run Quality of the compiler Internal The algorithm’s efficiency, in terms of: Time required to run Space (memory storage) required to run Complexity measures the internal factors (usually more interested in time than space)

Introduction to complexity Prof. Sin-Min Lee Department of Computer Science San Jose State University

25

26 Growth rates and big-O notation Growth rates capture the essence of an algorithm’s performance Big-O notation indicates the growth rate. It is the class of mathematical formula that best describes an algorithm’s performance, and is discovered by looking inside the algorithm Big-O is a function with parameter N, where N is usually the size of the input to the algorithm –For example, if an algorithm depending on the value n has performance an 2 + bn + c (for constants a, b, c) then we say the algorithm has performance O(N 2 ) For large N, the N 2 term dominates. Only the dominant term is included in big-O

27 Common growth rates

28 Growth rates Number of Inputs Time O(N 2 ) O(Nlog N)

29 Calculating the actual time taken by a program (example) A program takes 10ms to process one data item (i.e. to do one operation on the data item) How long would the program take to process 1000 data items, if time is proportional to: –log 10 N –N –N log 10 N –N 2 –N 3 (time for 1 item) x (big-O( ) time complexity of N items)

30 In some cases, it is important to consider the best, worst and/or average (or typical) performance of an algorithm: E.g., when sorting a list into order, if it is already in order then the algorithm may have very little work to do The worst-case analysis gives a bound for all possible input (and may be easier to calculate than the average case) Best, average, worst-case complexity – Worst, O(N) or o(N):  or > true function – Best, Ω(N):  true function – Typical, Θ(N):  true function * * These approximations are true only after N has passed some value

31 How do we calculate big-O? 1Loops 2Nested loops 3Consecutive statements 4If-then-else statements 5Logarithmic complexity Five guidelines for finding out the time complexity of a piece of code

32 Guideline 1: Loops The running time of a loop is, at most, the running time of the statements inside the loop (including tests) multiplied by the number of iterations. for (i=1; i<=n; i++) { m = m + 2; } constant time executed n times Total time = a constant c * n = cn = O(N)

33 Guideline 2: Nested loops Analyse inside out. Total running time is the product of the sizes of all the loops. for (i=1; i<=n; i++) { for (j=1; j<=n; j++) { k = k+1; } constant time outer loop executed n times inner loop executed n times Total time = c * n * n * = cn 2 = O(N 2 )

34 Guideline 3: Consecutive statements Add the time complexities of each statement. Total time = c 0 + c 1 n + c 2 n 2 = O(N 2 ) x = x +1; for (i=1; i<=n; i++) { m = m + 2; } for (i=1; i<=n; i++) { for (j=1; j<=n; j++) { k = k+1; } inner loop executed n times outer loop executed n times constant time executed n times constant time

35 Guideline 4: If-then-else statements Worst-case running time: the test, plus either the then part or the else part (whichever is the larger). if (depth( ) != otherStack.depth( ) ) { return false; } else { for (int n = 0; n < depth( ); n++) { if (!list[n].equals(otherStack.list[n])) return false; } then part: constant else part: (constant + constant) * n test: constant another if : constant + constant (no else part) Total time = c 0 + c 1 + (c 2 + c 3 ) * n = O(N)

36 Guideline 5: Logarithmic complexity An algorithm is O(log N) if it takes a constant time to cut the problem size by a fraction (usually by ½) Example algorithm (binary search): finding a word in a dictionary of n pages Look at the centre point in the dictionary Is word to left or right of centre? Repeat process with left or right part of dictionary until the word is found

37 Performance isn’t everything! There can be a tradeoff between: –Ease of understanding, writing and debugging –Efficient use of time and space So, maximum performance is not always desirable However, it is still useful to compare the performance of different algorithms, even if the optimal algorithm may not be adopted