CSE115/ENGR160 Discrete Mathematics 03/10/11

Slides:



Advertisements
Similar presentations
College of Information Technology & Design
Advertisements

Problems and Their Classes
CSE115/ENGR160 Discrete Mathematics 03/01/12
Discrete Structures CISC 2315
CompSci 102 Discrete Math for Computer Science
CSE115/ENGR160 Discrete Mathematics 02/28/12
CSE115/ENGR160 Discrete Mathematics 02/24/11 Ming-Hsuan Yang UC Merced 1.
CSE115/ENGR160 Discrete Mathematics 03/03/11 Ming-Hsuan Yang UC Merced 1.
Complexity Analysis (Part I)
Analysis of Algorithms CS 477/677
Chapter 11: Limitations of Algorithmic Power
Chapter 11 Limitations of Algorithm Power Copyright © 2007 Pearson Addison-Wesley. All rights reserved.
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.
Algorithms Chapter 3 With Question/Answer Animations.
February 17, 2015Applied Discrete Mathematics Week 3: Algorithms 1 Double Summations Table 2 in 4 th Edition: Section th Edition: Section th.
1 Complexity Lecture Ref. Handout p
Chapter Complexity of Algorithms –Time Complexity –Understanding the complexity of Algorithms 1.
Chapter 3: The Fundamentals: Algorithms, the Integers, and Matrices
Chapter 11 Limitations of Algorithm Power. Lower Bounds Lower bound: an estimate on a minimum amount of work needed to solve a given problem Examples:
Computational Complexity Polynomial time O(n k ) input size n, k constant Tractable problems solvable in polynomial time(Opposite Intractable) Ex: sorting,
Discrete Mathematics Algorithms. Introduction  An algorithm is a finite set of instructions with the following characteristics:  Precision: steps are.
Discrete Structures Lecture 11: Algorithms Miss, Yanyan,Ji United International College Thanks to Professor Michael Hvidsten.
2.3 Functions A function is an assignment of each element of one set to a specific element of some other set. Synonymous terms: function, assignment, map.
Matt Schierholtz. Method for solving problems with finite steps Algorithm example: Error Check for problem Solve problem Must terminate.
Chapter 3 Sec 3.3 With Question/Answer Animations 1.
ดร.สุรศักดิ์ มังสิงห์ SPU, Computer Science Dept.
22C:19 Discrete Structures Algorithms and Complexity Fall 2014 Sukumar Ghosh.
Fall 2002CMSC Discrete Structures1 Enough Mathematical Appetizers! Let us look at something more interesting: Algorithms.
MCA-2012Data Structure1 Algorithms Rizwan Rehman CCS, DU.
1 Lower Bounds Lower bound: an estimate on a minimum amount of work needed to solve a given problem Examples: b number of comparisons needed to find the.
1 Algorithms CS/APMA 202 Rosen section 2.1 Aaron Bloomfield.
Chapter Algorithms 3.2 The Growth of Functions 3.3 Complexity of Algorithms 3.4 The Integers and Division 3.5 Primes and Greatest Common Divisors.
New Mexico Computer Science For All Algorithm Analysis Maureen Psaila-Dombrowski.
1 Algorithms CS 202 Epp section ??? Aaron Bloomfield.
CS 103 Discrete Structures Lecture 12
3.3 Complexity of Algorithms
Module #7: Algorithmic Complexity Rosen 5 th ed., §2.3.
The Fundamentals. Algorithms What is an algorithm? An algorithm is “a finite set of precise instructions for performing a computation or for solving.
Algorithm Analysis. What is an algorithm ? A clearly specifiable set of instructions –to solve a problem Given a problem –decide that the algorithm is.
1 Discrete Structures – CNS2300 Text Discrete Mathematics and Its Applications Kenneth H. Rosen (5 th Edition) Chapter 2 The Fundamentals: Algorithms,
Copyright © 2014 Curt Hill Algorithms From the Mathematical Perspective.
Copyright © 2014 Curt Hill Algorithm Analysis How Do We Determine the Complexity of Algorithms.
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.
Chapter 3 Chapter Summary  Algorithms o Example Algorithms searching for an element in a list sorting a list so its elements are in some prescribed.
Discrete Mathematics Chapter 2 The Fundamentals : Algorithms, the Integers, and Matrices. 大葉大學 資訊工程系 黃鈴玲.
Complexity Analysis (Part I)
CSE15 Discrete Mathematics 03/06/17
Growth of Functions & Algorithms
Applied Discrete Mathematics Week 2: Functions and Sequences
CSE15 Discrete Mathematics 03/13/17
Lecture – 2 on Data structures
Algorithm Analysis CSE 2011 Winter September 2018.
Enough Mathematical Appetizers!
Computation.
CS 2210 Discrete Structures Algorithms and Complexity
Module #7: Algorithmic Complexity
CS 2210 Discrete Structures Algorithms and Complexity
Applied Discrete Mathematics Week 6: Computation
Discrete Mathematics CS 2610
Chapter 11 Limitations of Algorithm Power
Discrete Mathematics and its Applications
Module #7: Algorithmic Complexity
Enough Mathematical Appetizers!
Enough Mathematical Appetizers!
Algorithms.
Discrete Mathematics CS 2610
Complexity Analysis (Part I)
Complexity Analysis (Part I)
CS 2210 Discrete Structures Algorithms and Complexity
Presentation transcript:

CSE115/ENGR160 Discrete Mathematics 03/10/11 Ming-Hsuan Yang UC Merced

3.3 Complexity of algorithms Produce correct answer Efficient Efficiency Execution time (time complexity) Memory (space complexity) Space complexity is related to data structure

Time complexity Expressed in terms of number of operations when the input has a particular size Not in terms of actual execution time The operations can be comparison of integers, the addition of integers, the multiplication of integers, the division of integers, or any other basic operation Worst case analysis

Example procedure max(a1, a2, …, an: integers) max := a1 for i:=2 to n if max < ai then max:=ai {max is the largest element} There are 2(n-1)+1=2n-1 comparisons, the time complexity is 𝛳(n) measured in terms of the number of comparisons

Example procedure linear search(x:integer, a1, a2, …, an: distinct integers) i := 1 while (i≤n and x≠ai) i:=i+1 if i < n then location:=n else location:=0 {location is the index of the term equal to x, or is 0 if x is not found} At most 2 comparisons per iteration, 2n+1 for the while loop and 1 more for if statement. At most 2n+2 comparisons are required

Binary search procedure binary search(x:integer, a1, a2, …, an: increasing integers) i:=1 (left endpoint of search interval) j:=1 (right end point of search interval) while (i<j) begin m:=⌞(i+j)/2⌟ if x>am then i:=m+1 else j:=m end if x=ai then location:=i else location:=0 {location is the index of the term equal to x, or is 0 if x is not found}

Time complexity of binary search For simplicity, assume n=2k,k=log2n At each iteration, 2 comparisons are used For example, 2 comparisons are used when the list has 2k-1 elements, 2 comparisons are used when the list has 2k-2, …, 2 comparisons are used when the list has 21 elements 1 comparison is ued when the list has 1 element, and 1 more comparison is used to determine this term is x Hence, at most 2k+2=2log2n +2 comparisons are required If n is not a power of 2, the list can be expanded to 2k+1, and it requires at most 2 log n+2 comparisons The time complexity is at most 𝛳(log n)

Average case complexity Usually more complicated than worst-case analysis For linear search, assume x is in the list If x is at 1st term, 3 comparisons are needed (1 to determine the end of list, 1 to compare x and 1st term, one outside the loop) If x is the 2nd term, 2 more comparisons are needed, so 5 comparisons are needed In general, if x is the i-th term, 2 comparisons are used at each of the i-th step of the loop, and 1 outside the loop, so 2i+1 comparisons are used On average , (3+5+7+…+2n+1)/n=(2(1+2+3+…n)+n)/n=n+2, which is 𝛳(n)

Complexity Assume x is in the list It is possible to do an average-case analysis when x may not be in the list Although we have counted the comparisons needed to determine whether we have reached the end of a loop, these comparisons are often not counted From this point, we will ignore such comparisons

Complexity of bubble sort procedure bubble sort(a1, a2, …, an: real numbers with n≥2) for i:=1 to n-1 for j:=1 to n-i if aj>aj+1 then interchange aj and aj+1 {a1, a2, …, an is in increasing order} When the i-th pass begins, the i-1 largest elements are guaranteed to be in the correct positions During this pass, n-i comparisons are used, Thus from 2nd to (n-1)-th steps, (n-1)+(n-2)+…+2+1=(n-1)n/2 comparisons are used Time complexity is always 𝛳(n2)

Insertion sort procedure insertion sort(a1, a2, …, an: real numbers with n≥2) i:=1 (left endpoint of search interval) j:=1 (right end point of search interval) for j:=2 to n begin i:=1 while aj>ai i:=i+1 m:=aj for k:=0 to j-i-1 aj-k:= aj-k-1 ai:= m end {a1 , a2, …, an are sorted}

Complexity of insertion sort Insert j-th element into the correct position among the first j-1 elements that have already been put in correct order Use a linear search successively In the worst case, j comparisons are required to insert the j-th element, thus 2+3+…+n=n(n+1)/2-1, and time complexity is 𝛳(n2)

Understanding complexity

Tractable A problem that is solvable by an algorithm with a polynomial worst-case complexity is called tractable Often the degree and coefficients are small Intractable problems may have low average-case time complexity, or can be solved with approximate solutions

Solvable problems Some problems are solvable using an algorithm Some problems are unsolvable, e.g., the halting problem Many solvable problems are believed that no algorithm with polynomial worst-case time complexity solves them, but that a solution, if known, can be checked in polynomial time

NP-complete problems NP: problems for which a solution can be checked in polynomial time NP (nondeterministic polynomial time) NP-complete problems: if any of these problems can be solved by a polynomial worst-case time algorithm, then all problems in the class NP can be solved by polynomial worst cast time algorithms

NP-complete problems The satisfiability problem is an NP-complete problem We can quickly verify that an assignment of truth values to the variables of a compound proposition makes it true But no polynomial time algorithm has been discovered It is generally accepted, though not proven, that no NP-complete problem can be solved in polynomial time

Scalability