Algorithms Analysis Section 3.3 of Rosen Fall 2008 CSCE 235 Introduction to Discrete Structures Course web-page: cse.unl.edu/~cse235 Questions:

Slides:



Advertisements
Similar presentations
Algorithm Analysis Input size Time I1 T1 I2 T2 …
Advertisements

Analysis of Computer Algorithms
MATH 224 – Discrete Mathematics
Analysis of Algorithms
HST 952 Computing for Biomedical Scientists Lecture 10.
Fundamentals of Python: From First Programs Through Data Structures
CMPT 225 Sorting Algorithms Algorithm Analysis: Big O Notation.
Computational Complexity 1. Time Complexity 2. Space Complexity.
What is an Algorithm? (And how do we analyze one?)
Fall 2006CENG 7071 Algorithm Analysis. Fall 2006CENG 7072 Algorithmic Performance There are two aspects of algorithmic performance: Time Instructions.
Chapter 2: Fundamentals of the Analysis of Algorithm Efficiency
CS 206 Introduction to Computer Science II 09 / 10 / 2008 Instructor: Michael Eckmann.
Introduction to Analysis of Algorithms
Recursion Sections 7.1 and 7.2 of Rosen Fall 2008 CSCE 235 Introduction to Discrete Structures Course web-page: cse.unl.edu/~cse235 Questions:
Complexity Analysis (Part I)
Chapter 2 Fundamentals of the Analysis of Algorithm Efficiency Copyright © 2007 Pearson Addison-Wesley. All rights reserved.
Algorithm Analysis CS 201 Fundamental Structures of Computer Science.
CSE115/ENGR160 Discrete Mathematics 03/10/11
Complexity (Running Time)
Concept of Basic Time Complexity Problem size (Input size) Time complexity analysis.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Chapter 23 Algorithm Efficiency.
Analysis of Algorithms 7/2/2015CS202 - Fundamentals of Computer Science II1.
Copyright © Cengage Learning. All rights reserved. CHAPTER 11 ANALYSIS OF ALGORITHM EFFICIENCY ANALYSIS OF ALGORITHM EFFICIENCY.
1 Section 2.3 Complexity of Algorithms. 2 Computational Complexity Measure of algorithm efficiency in terms of: –Time: how long it takes computer to solve.
Analysis of Algorithms Spring 2015CS202 - Fundamentals of Computer Science II1.
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.
Analysis of Algorithm Lecture 3 Recurrence, control structure and few examples (Part 1) Huma Ayub (Assistant Professor) Department of Software Engineering.
1 Chapter 24 Developing Efficient Algorithms. 2 Executing Time Suppose two algorithms perform the same task such as search (linear search vs. binary search)
{ CS203 Lecture 7 John Hurley Cal State LA. 2 Execution Time Suppose two algorithms perform the same task such as search (linear search vs. binary search)
Data Structures and Algorithms Lecture 5 and 6 Instructor: Quratulain Date: 15 th and 18 th September, 2009 Faculty of Computer Science, IBA.
Lecture 2 Computational Complexity
CS 3343: Analysis of Algorithms
Design and Analysis of Algorithms - Chapter 21 Analysis of Algorithms b Issues: CorrectnessCorrectness Time efficiencyTime efficiency Space efficiencySpace.
Analysis of Algorithms
Chapter 3 Sec 3.3 With Question/Answer Animations 1.
Lecture 4. RAM Model, Space and Time Complexity
CS 221 Analysis of Algorithms Instructor: Don McLaughlin.
© 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.
CSS342: Algorithm Analysis1 Professor: Munehiro Fukuda.
Analysis of Algorithms CSCI Previous Evaluations of Programs Correctness – does the algorithm do what it is supposed to do? Generality – does it.
1Computer Sciences Department. Book: Introduction to Algorithms, by: Thomas H. Cormen Charles E. Leiserson Ronald L. Rivest Clifford Stein Electronic:
CSC 211 Data Structures Lecture 13
Lecture 5 Jianjun Hu Department of Computer Science and Engineering University of South Carolina CSCE350 Algorithms and Data Structure.
Algorithm Analysis Data Structures and Algorithms (60-254)
Algorithmic Analysis Charl du Plessis and Robert Ketteringham.
1 Algorithms  Algorithms are simply a list of steps required to solve some particular problem  They are designed as abstractions of processes carried.
2-0 Copyright © 2007 Pearson Addison-Wesley. All rights reserved. A. Levitin “ Introduction to the Design & Analysis of Algorithms, ” 2 nd ed., Ch. 2 Theoretical.
Copyright © 2007 Pearson Addison-Wesley. All rights reserved. A. Levitin “ Introduction to the Design & Analysis of Algorithms, ” 2 nd ed., Ch. 1 Chapter.
A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 2 ©2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved.
Searching Topics Sequential Search Binary Search.
Algorithmics - Lecture 41 LECTURE 4: Analysis of Algorithms Efficiency (I)
Lecture 4 Jianjun Hu Department of Computer Science and Engineerintg University of South Carolina CSCE350 Algorithms and Data Structure.
BITS Pilani Pilani Campus Data Structure and Algorithms Design Dr. Maheswari Karthikeyan Lecture1.
Ch03-Algorithms 1. Algorithms What is an algorithm? An algorithm is a finite set of precise instructions for performing a computation or for solving a.
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.
LECTURE 2 : fundamentals of analysis of algorithm efficiency Introduction to design and analysis algorithm 1.
Algorithmic Foundations COMP108 COMP108 Algorithmic Foundations Algorithm efficiency Prudence Wong
Algorithm Analysis 1.
Theoretical analysis of time efficiency
Analysis of algorithms
Chapter 2 Fundamentals of the Analysis of Algorithm Efficiency
Algorithms Analysis Section 3.3 of Rosen Spring 2017
Fundamentals of the Analysis of Algorithm Efficiency
Analysis of algorithms
Algorithms Analysis Section 3.3 of Rosen Spring 2013
Algorithms Analysis Section 3.3 of Rosen Spring 2018
Presentation transcript:

Algorithms Analysis Section 3.3 of Rosen Fall 2008 CSCE 235 Introduction to Discrete Structures Course web-page: cse.unl.edu/~cse235 Questions:

Algorithm AnalysisCSCE 235, Fall Outline Introduction Input Size Order of Growth Intractability Worst, Best, and Average Cases Mathematical Analysis of Algorithms – 3 Examples Summation tools

Algorithm AnalysisCSCE 235, Fall Introduction How can we say that one algorithm performs better than another one? Quantify the resources needed to run it: – Time – Memory – I/O, disk access – Circuit, power, etc. Time is not merely CPU clock cycle – We want to study algorithms independent of implementations, platforms, and hardware. – We need an objective point of reference – For that we measure time by the number of operations as a function of the size of the input to the algorithm

Algorithm AnalysisCSCE 235, Fall Input Size For a given problem, we characterize the input size n appropriately – Sorting: The number of items to be sorted – Graphs: The number of vertices and/or edges – Matrix manipulation: The number of rows and colums – Numerical operations: the number of bits needed to represent a number The choice of an input size greatly depends on the elementary operation: the most relevant or important operation of an algorithm – Comparisons – Additions – Multiplications

Algorithm AnalysisCSCE 235, Fall Outline Introduction Input Size Order of Growth Intractability Worst, Best, and Average Cases Mathematical Analysis of Algorithms – 3 Examples Summation tools

Algorithm AnalysisCSCE 235, Fall Order of Growth Small input sizes can usually be computed instantaneously, thus we are most interested in how an algorithms performs as n  Indeed, for small values of n, most such functions will be very similar in running time. Only for sufficiently large n do differences in running time become apparent: As n  the differences become more and more stark

Algorithm AnalysisCSCE 235, Fall Intractability Problems that we can solve (today) only with exponential or super- exponential time algorithms are said to be (likely) intractable. That is, though they may be solved in a reasonable amount of time for small n, for large n, there is (likely) no hope for efficient execution. It may take millions or billions of years. Tractable problems are problems that have efficient (read: polynomial) algorithms to solve them. Polynomial order of magnitude usually means that there exists a polynomial p(n)=n k for some constant k that always bounds the order of growth. More on asymptotics in the next lecture Intractable problems may need to be solved using approximation or randomized algorithms (except for small size of input)

Algorithm AnalysisCSCE 235, Fall Worst, Best, and Average Case Some algorithms perform differently on various input of similar size. It is sometimes helpful to consider – The worst-case – The best-case – The average-case Performance of the algorithm. For example, say we want to search an array A of size n for a given value k – Worst-case: k  A, then we must check every item. Cost= n comparisons – Best-case: k is the first item in the array. Cost=1 comparison – Average-case: Probabilistic analysis

Algorithm AnalysisCSCE 235, Fall Average-Case: Example Since any worthwhile algorithm will be used quite extensively, the average running time is arguably the best measure of the performance of the algorithm (if the worst case is not frequently encountered). For searching an array and assuming that p is the probability of a successful search we have C average (n) = (p+2p+…+ip+…+np)/n + n(1-p) = (1+2+…+i+n) p/n+n(1-p) = (n(n+1)/2) p/n +n(1-p)=p(n+1)/2+n(1-p) – If p=0 (search fails), C average (n) =n – If p=1 (search succeeds), C average (n) =(n+1)/2  n/2 Intuitively, the algorithm must examine on average half of all the elements in A

Algorithm AnalysisCSCE 235, Fall Average-Case: Importance Average-case analysis of algorithms is important in a practical sense. Often C avg and C worst have the same order of magnitude and thus from a theoretical point of view, are no different from each other Practical implementations, however, require a real-world examination and empirical analysis

Algorithm AnalysisCSCE 235, Fall Outline Introduction Input Size Order of Growth Intractability Worst, Best, and Average Cases Mathematical Analysis of Algorithms – 3 Examples Summation tools

Algorithm AnalysisCSCE 235, Fall Mathematical Analysis of Algorithms After developing a pseudo-code for an algorithm, we wish to analyze its performance – as a function of the size of the input, n, – in terms of how many times the elementary operation is performed. Here is a general strategy 1.Decide on a parameter(s) for the input, n 2.Identify the basic operation 3.Evaluate if the elementary operation depends only on n 4.Set up a summation corresponding to the number of elementary operations 5.Simplify the equation to get as simple of a function f(n) as possible

Algorithm AnalysisCSCE 235, Fall Algorithm Analysis: Example 1 (1) UniqueElements Input: Integer array A of size n Output: True if all elements a  A are distinct 1. For i=1,…, (n-1) Do 2. For j=i+1,…,n Do 3. If a i =a j 4. Then Return false 5. End 6. End 7. End 8. Return true

Algorithm AnalysisCSCE 235, Fall Algorithm Analysis: Example 1 (2) For this algorithm, what is – The elementary operation? – Input size? – Does the elementary operation depend only on n? The outer for-loop runs n-1 times. More formally it contributes:  i=1 n-1 The inner for-loop depends on the outer for-loop and contributes:  j=i+1 n – Comparing a i and a j – n, size of A

Algorithm AnalysisCSCE 235, Fall Algorithm Analysis: Example 1 (3) We observe that the elementary operation is executes once in each iteration, thus we have C worst (n) =  i=1 n-1  j=i+1 n 1 = n(n-1)/2

Algorithm AnalysisCSCE 235, Fall Computing  i=1 n-1  j=i+1 n 1  j=i+1 n 1 = …+1 = n-(i+1)+1=n-i  i=1 n-1 (n-i)=  i=1 n-1 n-  i=1 n-1 i = n(n-1) -  i=1 n-1 i Computing  i=1 n-1 i – Check Table 2, page 157:  k=1 n k=n(n+1)/2 – Rewrite  i=1 n-1 I =  i=1 n i-n = n(n+1)/2 –n = n(n+1-2)/2=n(n-1)/2  i=1 n-1  j=i+1 n 1 = n(n-1)-n(n-1)/2 = n(n-1)/2

Algorithm AnalysisCSCE 235, Fall Algorithm Analysis: Example 2 (1) The parity of a bit string determines whether or not the number of 1s in it is even or odd. It is used as a simple form of error correction over communication networks

Algorithm AnalysisCSCE 235, Fall Algorithm Analysis: ParityChecking ParityChecking Input: An integer n in binary (as an array b[]) Output: 0 if parity is even, 1 otherwise 1.parity  0 2. While n>0 Do 3. If b[0]=1 Then 4. parity  parity +1 mod 2 5. RightShift (n) 6. End 7. End 8. Return parity

Algorithm AnalysisCSCE 235, Fall Algorithm Analysis: Example 2 (2) For this algorithm, what is – The elementary operation? – Input size, n? – Does the elementary operation depend only on n? The number of bits required to represent an integer n is  log n  The while-loop will be executed as many times as there are 1-bits in the binary representation. In the worst case we have a bit string of all 1s So the running time is simply log n

Algorithm AnalysisCSCE 235, Fall Algorithm Analysis: Example 3 (1) MyFunction Input: Integers n,m,p such that n>m>p Output: Some function f(n,m,p) 1.x  1 2. For i = 0,…,10 Do 3. For j = 0,…, n Do 4. For k= m/2, …, m Do 5. x  x  p 6. End 7. End 8. End 9. Return x

Algorithm AnalysisCSCE 235, Fall Algorithm Analysis: Example 3 (2) Outer for-loop: executes 11 times, but do not depend on input size 2 nd for-loop: executes n times 3 rd for-loop: executes m/2 times Thus, the cost is C(n,m,p)=11(n+1)(m/2+1) And we do NOT need to consider p

Algorithm AnalysisCSCE 235, Fall Outline Introduction Input Size Order of Growth Intractability Worst, Best, and Average Cases Mathematical Analysis of Algorithms – 3 Examples Summation tools

Algorithm AnalysisCSCE 235, Fall Summation Tools Table 2, Section 2.4 (page 157) has more summation rules, which will be You can always use Maple to evaluate and simplify complex expressions – But you should know how to do them by hand! To use Maple on cse you can use the command-line interface by typing maple Under Unix (gnome of KDE) or via xwindows interface, you can use the graphical version via xmaple Will try to demonstrate during the recitation.

Algorithm AnalysisCSCE 235, Fall Summation Tools: Maple > simplify(sum(i,i=0..n)); ½ n 2 + ½ n > Sum(Sum(j,j=i..n),i=0..n);  i=0 n (  j=i n j)

Algorithm AnalysisCSCE 235, Fall Summary Introduction Input Size Order of Growth Intractability Worst, Best, and Average Cases Mathematical Analysis of Algorithms – 3 Examples Summation tools