DATA STRUCTURES AND ALGORITHMS Lecture Notes 2 Prepared by İnanç TAHRALI.

Slides:



Advertisements
Similar presentations
MATH 224 – Discrete Mathematics
Advertisements

Intro to Analysis of Algorithms. Algorithm “A sequence of unambiguous instructions for solving a problem, i.e., for obtaining a required output for any.
Lecture: Algorithmic complexity
CSE 373: Data Structures and Algorithms Lecture 5: Math Review/Asymptotic Analysis III 1.
Fall 2006CENG 7071 Algorithm Analysis. Fall 2006CENG 7072 Algorithmic Performance There are two aspects of algorithmic performance: Time Instructions.
Chapter 2: Algorithm Analysis
Introduction to Analysis of Algorithms
Analyzing algorithms & Asymptotic Notation BIO/CS 471 – Algorithms for Bioinformatics.
Algorithm Analysis. Math Review – 1.2 Exponents –X A X B = X A+B –X A /X B =X A-B –(X A ) B = X AB –X N +X N = 2X N ≠ X 2N –2 N+ 2 N = 2 N+1 Logarithms.
Algorithm Analysis CS 201 Fundamental Structures of Computer Science.
 Last lesson  Arrays for implementing collection classes  Performance analysis (review)  Today  Performance analysis  Logarithm.
Analysis of Algorithms 7/2/2015CS202 - Fundamentals of Computer Science II1.
Cpt S 223 – Advanced Data Structures
Analysis of Algorithms COMP171 Fall Analysis of Algorithms / Slide 2 Introduction * What is Algorithm? n a clearly specified set of simple instructions.
DATA STRUCTURES AND ALGORITHMS Lecture Notes 1 Prepared by İnanç TAHRALI.
Asymptotic Notations Iterative Algorithms and their analysis
1 Chapter 2 Program Performance – Part 2. 2 Step Counts Instead of accounting for the time spent on chosen operations, the step-count method accounts.
Algorithm Analysis. Algorithm Def An algorithm is a step-by-step procedure.
Time Complexity Dr. Jicheng Fu Department of Computer Science University of Central Oklahoma.
Lecture 8. How to Form Recursive relations 1. Recap Asymptotic analysis helps to highlight the order of growth of functions to compare algorithms Common.
For Wednesday Read Weiss chapter 3, sections 1-5. This should be largely review. If you’re struggling with the C++ aspects, you may refer to Savitch, chapter.
Vishnu Kotrajaras, PhD.1 Data Structures. Vishnu Kotrajaras, PhD.2 Introduction Why study data structure?  Can understand more code.  Can choose a correct.
Algorithm Analysis. Algorithm An algorithm is a clearly specified set of instructions which, when followed, solves a problem. recipes directions for putting.
Algorithm Efficiency CS 110: Data Structures and Algorithms First Semester,
Iterative Algorithm Analysis & Asymptotic Notations
1 7.Algorithm Efficiency What to measure? Space utilization: amount of memory required  Time efficiency: amount of time required to process the data Depends.
CS 221 Analysis of Algorithms Instructor: Don McLaughlin.
Analyzing algorithms & Asymptotic Notation BIO/CS 471 – Algorithms for Bioinformatics.
Asymptotic Analysis-Ch. 3
Fundamentals CSE 373 Data Structures Lecture 5. 12/26/03Fundamentals - Lecture 52 Mathematical Background Today, we will review: ›Logs and exponents ›Series.
Algorithms Growth of Functions. Some Notation NNatural numbers RReal numbers N + Positive natural numbers R + Positive real numbers R * Non-negative real.
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.
Algorithm Efficiency There are often many approaches (algorithms) to solve a problem. How do we choose between them? At the heart of a computer program.
Geoff Holmes and Bernhard Pfahringer COMP206-08S General Programming 2.
Chapter 5 Algorithms (2) Introduction to CS 1 st Semester, 2015 Sanghyun Park.
1 Asymptotic Notations Iterative Algorithms and their analysis Asymptotic Notations –Big O,  Notations Review of Discrete Math –Summations –Logarithms.
CE 221 Data Structures and Algorithms Chapter 2: Algorithm Analysis - I Text: Read Weiss, §2.1 – Izmir University of Economics.
Algorithm Analysis Part of slides are borrowed from UST.
Chapter 2 Computational Complexity. Computational Complexity Compares growth of two functions Independent of constant multipliers and lower-order effects.
Algorithm Analysis Chapter 5. Algorithm An algorithm is a clearly specified set of instructions which, when followed, solves a problem. –recipes –directions.
1/6/20161 CS 3343: Analysis of Algorithms Lecture 2: Asymptotic Notations.
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.
Vishnu Kotrajaras, PhD.1 Data Structures
1 Chapter 2 Algorithm Analysis All sections. 2 Complexity Analysis Measures efficiency (time and memory) of algorithms and programs –Can be used for the.
1 Chapter 2 Algorithm Analysis Reading: Chapter 2.
Algorithm Analysis 1.
Chapter 2 Algorithm Analysis
Introduction to Algorithms
Algorithm Analysis CSE 2011 Winter September 2018.
CS 3343: Analysis of Algorithms
Time Complexity Analysis Neil Tang 01/19/2010
Algorithm Analysis Neil Tang 01/22/2008
Algorithm Analysis (not included in any exams!)
Algorithm design and Analysis
Introduction to Algorithms Analysis
BIG-OH AND OTHER NOTATIONS IN ALGORITHM ANALYSIS
CS 201 Fundamental Structures of Computer Science
Programming and Data Structure
Chapter 2.
Programming and Data Structure
CSE 373 Data Structures Lecture 5
CE 221 Data Structures and Algorithms
CE 221 Data Structures and Algorithms
At the end of this session, learner will be able to:
CS210- Lecture 2 Jun 2, 2005 Announcements Questions
Estimating Algorithm Performance
Presentation transcript:

DATA STRUCTURES AND ALGORITHMS Lecture Notes 2 Prepared by İnanç TAHRALI

2 Recapture Asymptotic Notations O Notation Ω Notation Θ Notation o Notation

3 Big Oh Notation (O) Provides an “upper bound” for the function f Definition : T(N) = O (f(N)) if there are positive constants c and n 0 such that T(N) ≤ c f(N) when N ≥ n 0 T(N) grows no faster than f(N) growth rate of T(N) is less than or equal to growth rate of f(N) for large N f(N) is an upper bound on T(N) not fully correct !

4 Omega Notation (Ω) Definition : T(N) = Ω (f(N)) if there are positive constants c and n 0 such that T(N) ≥ c f(N) when N≥ n 0 T(N) grows no slower than f(N) growth rate of T(N) is greater than or equal to growth rate of f(N) for large N f(N) is a lower bound on T(N) not fully correct !

5 Theta Notation (θ) Definition : T(N) = θ (h(N)) if and only if T(N) = O(h(N)) and T(N) = Ω(h(N)) T(N) grows as fast as h(N) growth rate of T(N) and h(N) are equal for large N h(N) is a tight bound on T(N) not fully correct !

6 Little o Notation (o) Definition : T(N) = o(p(N)) if T(N) = O(p(N)) and T(N)≠θ(p(N)) f(N) grows strictly faster than T(N) growth rate of T(N) is less than the growth rate of f(N) for large N f(N) is an upperbound on T(N) (but not tight) not fully correct !

7 ROAD MAP Model What to Analyze ? Running Time Analysis General Rules Recursive Calls Maximum Subsequence Sum Problem Binary Search Experimentally checking analysis

8 MODEL A formal framework for analysis (simplify the real computers) There are many models Automata Turing Machine RAM

9 MODEL We use RAM model (normal computer) addition multiplicationtake a unit time comparison assignment fixed size word(32 bit) no complicated operation supported requires an algorithm (algorithm may not take unit time)

10 What to Analyze ? Running Time (most important !) Required memory space Run-time complexity is effected by compiler usually effects the constants & computer lower order terms algorithm use asymptotic notation

11 Running Time Analysis Emprical  after implementation Theoritical  before implementation If there are many algorithms ideas, we need to evaluate them without implementation We will use 0-notation drop constants ignore lower order terms

12 Running Time Analysis Example: int sum (int N) { int i, partialsum;  does not count partialsum = 0;  1 for (i=1; i<=N; i++)  1+(N+1)+N partialsum+=i*i*i;  4 N return partialsum;  1 } = 6N + 4 = θ(N)

13 ROAD MAP Model What to Analyze ? Running Time Analysis General Rules Recursive Calls Maximum Subsequence Sum Problem Binary Search Experimentally checking analysis

14 GENERAL RULES RULE 1 : For Loops The running time of a for loop is at most the running time of the statements in the for loop times the number of iterations int i, a = 0; for (i=0; i<n; i++) { print i; a=a+i; } return i; T(n) = θ(n)

15 GENERAL RULES RULE 2 : Nested Loops Analyze nested loops inside out Example : for (int i=1;i<=q;i++) { for (int j=1;j<=r;j++) k++; } θ(r) θ( q) T(n) = θ(r*q)

16 GENERAL RULES RULE 3 : Consequtive Statements Add the running times for … θ(N) …; for … θ(N 2 ) …;

17 GENERAL RULES RULE 4 : If / Else if (condition)T 3 (n) S1;T 1 (n) T(n) else S2;T 2 (n) Running time is never more than the running time of the test plus larger of the running times of S1 and S2 (may overestimate but never underestimates) T(n) ≤ T 3 (n) + max (T 1 (n), T 2 (n))

18 Types of complexition

19 GENERAL RULES RULE 4 : If / Else if (condition) T 3 (n) S1; T 1 (n) T(n) else S2; T 2 (n) T w (n) = T 3 (n) + max (T 1 (n), T 2 (n)) T b (n) = T 3 (n) + min (T 1 (n), T 2 (n)) T av (n) = p(T)T 1 (n) + p(F)T 2 (n) + T 3 (n) p(T)  p (condition = True) p(F)  p (condition = False)

20 GENERAL RULES Example : if (condition) T 3 (n) = θ(n) S1; T 1 (n) = θ(n 2 ) T(n) else S2; T 2 (n) = θ(n) T w (n) = T 3 (n) + max (T 1 (n), T 2 (n)) = θ(n 2 ) T b (n) = T 3 (n) + min (T 1 (n), T 2 (n)) = θ(n) if p(T) = p(F) = ½ T av (n) = p(T)T 1 (n) + p(F)T 2 (n) + T 3 (n) = θ(n 2 ) T(n) = O (n 2 ) = Ω (n)

21 ROAD MAP Model What to Analyze ? Running Time Analysis General Rules Recursive Calls Maximum Subsequence Sum Problem Binary Search Experimentally checking analysis

22 RECURSIVE CALLS Example 1: Algorithm for computing factorial int factorial (int n) { if (n<=1) return 1; else return n*factorial(n-1); } T(n)= cost of evaluation of factorial of n T(n) = 4 + T(n-1) T(1) = 1 1 for multiplication + 1 for substraction + cost of evaluation of factorial(n-1)

23 RECURSIVE CALLS T(n) = 4 + T(n-1) T(n) = T(n-2) T(n) = T(n-3). T(n) = k*4 + T(n-k)k= n-1 => T(n) = (n-1)*4 + T(n-(n-1)) T(n) = (n-1)*4 + T(1) T(n) = (n-1)*4 + 1 T(n) = θ (n)

24 RECURSIVE CALLS Example 2: Algorithm for fibonacci series Fib (int N) { if (N<=1) return 1; else return Fib(N-1)+Fib(N-2); } T(N) = T(N-1) + T(N-2) + 2 => T(N) = O(2 n )(by induction)

25 ROAD MAP Model What to Analyze ? Running Time Analysis General Rules Recursive Calls Maximum Subsequence Sum Problem Binary Search Experimentally checking analysis

26 MAXIMUM SUBSEQUENCE SUM PROBLEM Given (possibly negative) integers A 1, A 2,…, A N, find max 1 ≤ i ≤ j ≤ N A[2,4]  sum = 20 A[2,5]  sum = 15 There are many algorithms to solve this problem !

27 MAXIMUM SUBSEQUENCE SUM PROBLEM for small N all algorithms are fast for large N O(N) is the best

28 MAXIMUM SUBSEQUENCE SUM PROBLEM 1) Try all possibilities exhaustively for each i (0 to N-1) for each j (i to N-1) compute the sum of subsequence for (i to j) check if it is maximum T(N) = O(N 3 )

29 Algorithm 1: © Mark Allen Weiss, Data Structures and Algorithm Analysis in C

30 MAXIMUM SUBSEQUENCE SUM PROBLEM 2) How to compute sum of a subsequence We can use previous subsequence sum to calculate current one in O(1) time T(N) = O(N 2 )

31 Algorithm 2: © Mark Allen Weiss, Data Structures and Algorithm Analysis in C

32 MAXIMUM SUBSEQUENCE SUM PROBLEM 3) Compeletely different algorithm ? Divide and Conquer Strategy Maximum subsequence can be in L solved recursively in R in the middle, in both sides largest sum in L ending with middle element + largest sum in R begining with middle element

33 © Mark Allen Weiss, Data Structures and Algorithm Analysis in C

34 Algorithm 4: © Mark Allen Weiss, Data Structures and Algorithm Analysis in C

35 ROAD MAP Model What to Analyze ? Running Time Analysis General Rules Recursive Calls Maximum Subsequence Sum Problem Binary Search Experimentally checking analysis

36 Binary Search © Mark Allen Weiss, Data Structures and Algorithm Analysis in C

37 ROAD MAP Model What to Analyze ? Running Time Analysis General Rules Recursive Calls Maximum Subsequence Sum Problem Binary Search Checking your analysis experimentally

38 Checking Your Analysis Experimentally Implement your algorithm, measure the running time of your implementation check with theoretical results When N doubles (2x) linear => 2x quadratic => 4x cubic => 8x what about logarithm ? O(N) O(N logN) not easy to see the difference !

39 Checking Your Analysis Experimentally T t (N) = theoretical running time T e (N) = experimental running time Let T t (N) = O(f(N)) compute if converges to a constant f(N) is a tight bound if converges to zero not tight bound (overestimate) if diverges underestimate

40 Checking Your Analysis Experimentally NTtTt TeTe T e / T t

41 Checking Your Analysis Experimentally