ECOM Discrete Mathematics

Similar documents
Announcements. CompSci 102 Discrete Math for Computer Science. Chap. 3.1 Algorithms. Specifying Algorithms

With Question/Answer Animations

Algorithms and Their Complexity

3. Algorithms. What matters? How fast do we solve the problem? How much computer resource do we need?

Discrete Mathematics CS October 17, 2006

COMP 182 Algorithmic Thinking. Algorithm Efficiency. Luay Nakhleh Computer Science Rice University

Announcements. CompSci 230 Discrete Math for Computer Science. The Growth of Functions. Section 3.2

The Growth of Functions (2A) Young Won Lim 4/6/18

Lecture 4: Best, Worst, and Average Case Complexity. Wednesday, 30 Sept 2009

Analysis of Algorithm Efficiency. Dr. Yingwu Zhu

Growth of Functions. As an example for an estimate of computation time, let us consider the sequential search algorithm.

CISC 235: Topic 1. Complexity of Iterative Algorithms

3 The Fundamentals:Algorithms, the Integers, and Matrices

Describe an algorithm for finding the smallest integer in a finite sequence of natural numbers.

Introduction. An Introduction to Algorithms and Data Structures

CSCE 222 Discrete Structures for Computing

Algorithms, Design and Analysis. Order of growth. Table 2.1. Big-oh. Asymptotic growth rate. Types of formulas for basic operation count

CS481: Bioinformatics Algorithms

Algorithms 2/6/2018. Algorithms. Enough Mathematical Appetizers! Algorithm Examples. Algorithms. Algorithm Examples. Algorithm Examples

Lecture 1: Asymptotics, Recurrences, Elementary Sorting

Lecture 2. Fundamentals of the Analysis of Algorithm Efficiency

Divide and Conquer Problem Solving Method

Algorithms: COMP3121/3821/9101/9801

Theory of Computation

Module 1: Analyzing the Efficiency of Algorithms

CS Analysis of Recursive Algorithms and Brute Force

Discrete Math Notes. Contents. William Farmer. April 8, Overview 3

Mat Week 6. Fall Mat Week 6. Algorithms. Properties. Examples. Searching. Sorting. Time Complexity. Example. Properties.

Student Responsibilities Week 6. Mat Properties of Algorithms. 3.1 Algorithms. Finding the Maximum Value in a Finite Sequence Pseudocode

COMPUTER ALGORITHMS. Athasit Surarerks.

CSC Design and Analysis of Algorithms. Lecture 1

Introduction to Computer Science Lecture 5: Algorithms

LECTURE NOTES ON DESIGN AND ANALYSIS OF ALGORITHMS

CSCE 750, Spring 2001 Notes 1 Page 1 An outline of the book 1. Analyzing algorithms 2. Data abstraction 3. Recursion and induction 4. Sorting 5. Selec

Lecture 6: Introducing Complexity

Data Structures and Algorithms

CS 310 Advanced Data Structures and Algorithms

Topic 17. Analysis of Algorithms

2. (25%) Suppose you have an array of 1234 records in which only a few are out of order and they are not very far from their correct positions. Which

csci 210: Data Structures Program Analysis

CS 4407 Algorithms Lecture 2: Iterative and Divide and Conquer Algorithms

Introduction to Algorithms

CSED233: Data Structures (2017F) Lecture4: Analysis of Algorithms

CS325: Analysis of Algorithms, Fall Final Exam

Introduction to Algorithms and Asymptotic analysis

Divide and Conquer CPE 349. Theresa Migler-VonDollen

Analysis of Algorithms

csci 210: Data Structures Program Analysis

P, NP, NP-Complete, and NPhard

Lecture 2. More Algorithm Analysis, Math and MCSS By: Sarah Buchanan

Math 391: Midterm 1.0 Spring 2016

CSE 20. Lecture 4: Introduction to Boolean algebra. CSE 20: Lecture4

Analysis of Algorithms

An analogy from Calculus: limits

Limitations of Algorithm Power

Asymptotic Analysis. Thomas A. Anastasio. January 7, 2004

CSE373: Data Structures and Algorithms Lecture 3: Math Review; Algorithm Analysis. Catie Baker Spring 2015

NATIONAL UNIVERSITY OF SINGAPORE CS3230 DESIGN AND ANALYSIS OF ALGORITHMS SEMESTER II: Time Allowed 2 Hours

COMP 555 Bioalgorithms. Fall Lecture 3: Algorithms and Complexity

Matrix Multiplication

CS 350 Algorithms and Complexity

Big O 2/14/13. Administrative. Does it terminate? David Kauchak cs302 Spring 2013

ECOM Discrete Mathematics

Analysis of Algorithms [Reading: CLRS 2.2, 3] Laura Toma, csci2200, Bowdoin College

Analysis of Algorithms

Module 1: Analyzing the Efficiency of Algorithms

Chapter 4. Greedy Algorithms. Slides by Kevin Wayne. Copyright 2005 Pearson-Addison Wesley. All rights reserved.

Algorithm. Executing the Max algorithm. Algorithm and Growth of Functions Benchaporn Jantarakongkul. (algorithm) ก ก. : ก {a i }=a 1,,a n a i N,

REVIEW QUESTIONS. Chapter 1: Foundations: Sets, Logic, and Algorithms

Quiz 1 Solutions. Problem 2. Asymptotics & Recurrences [20 points] (3 parts)

Input Decidable Language -- Program Halts on all Input Encoding of Input -- Natural Numbers Encoded in Binary or Decimal, Not Unary

MATH 363: Discrete Mathematics

Computational Complexity

Fall 2016 Test 1 with Solutions

Algorithms CMSC Homework set #1 due January 14, 2015

CS 350 Algorithms and Complexity

Advanced Counting Techniques. Chapter 8

CS 380 ALGORITHM DESIGN AND ANALYSIS

Asymptotic Analysis 1

Ch 01. Analysis of Algorithms

CS1210 Lecture 23 March 8, 2019

Assignment 4. CSci 3110: Introduction to Algorithms. Sample Solutions

data structures and algorithms lecture 2

Lecture 2: Divide and conquer and Dynamic programming

CSE548, AMS542: Analysis of Algorithms, Fall 2017 Date: Oct 26. Homework #2. ( Due: Nov 8 )

Induction and recursion. Chapter 5

CS 161: Design and Analysis of Algorithms

Algorithms Test 1. Question 1. (10 points) for (i = 1; i <= n; i++) { j = 1; while (j < n) {

Algorithm Design Strategies V

R ij = 2. Using all of these facts together, you can solve problem number 9.

Ch01. Analysis of Algorithms

NOTE: You have 2 hours, please plan your time. Problems are not ordered by difficulty.

Chapter Summary. Mathematical Induction Strong Induction Well-Ordering Recursive Definitions Structural Induction Recursive Algorithms

Searching Algorithms. CSE21 Winter 2017, Day 3 (B00), Day 2 (A00) January 13, 2017

Algorithm efficiency analysis

ICS 252 Introduction to Computer Design

Lecture 7: More Arithmetic and Fun With Primes

Fall 2017 November 10, Written Homework 5

Answer the following questions: Q1: ( 15 points) : A) Choose the correct answer of the following questions: نموذج اإلجابة

Asymptotic Notation. such that t(n) cf(n) for all n n 0. for some positive real constant c and integer threshold n 0

Transcription:

ECOM 2311- Discrete Mathematics Chapter # 3 : Algorithms Fall, 2013/2014 ECOM 2311- Discrete Mathematics - Ch.3 Dr. Musbah Shaat 1 / 41

Outline 1 Algorithms 2 The Growth of Functions 3 Complexity of Algorithms ECOM 2311- Discrete Mathematics - Ch.3 Dr. Musbah Shaat 2 / 41

Algorithms Introduction The term algorithm is a corruption of the name Al-Khowarizmi, ABU JA FAR MOHAMMED IBN MUSA AL-KHOWARIZMI. Al-Khowarizmi, an astronomer and mathematician, was a member of the House of Wisdom, an academy of scientists in Baghdad. The word algebra comes from al-jabr, part of the title of his book Kitab al-jabr w al muquabala. ECOM 2311- Discrete Mathematics - Ch.3 Dr. Musbah Shaat 3 / 41

An Algorithm Algorithms Introduction is a finite sequence of precise instructions for performing a computation or for solving a problem. Given a sequence of integers, find the largest one; Given a set, list all its subsets; Given a set of integers, put them in increasing order; Given a network, find the shortest path between two vertices. Solution Methodology Construct a model that translates the problem into a mathematical context. To complete the solution, a method is needed that will solve the general problem using the model. Ideally, what is required is a procedure that follows a sequence of steps that leads to the desired answer. ECOM 2311- Discrete Mathematics - Ch.3 Dr. Musbah Shaat 4 / 41

Algorithms Introduction Example: Describe an algorithm for finding the maximum (largest) value in a finite sequence of integers. Solution: We perform the following steps Set the temporary maximum equal to the first integer in the sequence. Compare the next integer in the sequence to the temporary maximum, and if it is larger than the temporary maximum, set the temporary maximum equal to this integer. Repeat the previous step if there are more integers in the sequence. Stop when there are no integers left in the sequence. The temporary maximum at this point is the largest integer in the sequence. An algorithm can also be described using a computer language. However, when that is done, only those instructions permitted in the language can be used. ECOM 2311- Discrete Mathematics - Ch.3 Dr. Musbah Shaat 5 / 41

Algorithms Introduction Pseudocode Pseudocode provides an intermediate step between an English language description of an algorithm and an implementation of this algorithm in a programming language. A pseudocode description of the algorithm for finding the maximum element in a finite sequence is as follows. ECOM 2311- Discrete Mathematics - Ch.3 Dr. Musbah Shaat 6 / 41

Algorithms Introduction PROPERTIES OF ALGORITHMS Input. An algorithm has input values from a specified set. Output. From each set of input values an algorithm produces output values from a specified set. The output values are the solution to the problem. Definiteness. The steps of an algorithm must be defined precisely. Correctness. An algorithm should produce the correct output values for each set of input values. Finiteness. An algorithm should produce the desired output after a finite (but perhaps large) number of steps for any input in the set. Effectiveness. It must be possible to perform each step of an algorithm exactly and in a finite amount of time. Generality. The procedure should be applicable for all problems of the desired form, not just for a particular set of input values. ECOM 2311- Discrete Mathematics - Ch.3 Dr. Musbah Shaat 7 / 41

Algorithms Searching Algorithms Problem: locate an element x in a list of distinct elements a 1, a 2,, a n, or determine that it is not in the list. Solution: the location of the term in the list that equals x (that is, i is the solution if x = ai ) and is 0 if x is not in the list. Procedures of the Linear Search (sequential search): Compare x and a 1. When x = a 1, the solution is the location of a 1, namely, 1. When x a 1, compare x with a 2. If x = a 2, the solution is the location of a 2, namely, 2. When x a 2, compare x with a 3. Continue this process, comparing x successively with each term of the list until a match is found, where the solution is the location of that term, unless no match occurs. If the entire list has been searched without locating x, the solution is 0. ECOM 2311- Discrete Mathematics - Ch.3 Dr. Musbah Shaat 8 / 41

Algorithms Searching Algorithms The pseudocode for the linear search algorithm is displayed as Algorithm 2. ECOM 2311- Discrete Mathematics - Ch.3 Dr. Musbah Shaat 9 / 41

Algorithms Searching Algorithms THE BINARY SEARCH: can be used when the list has terms occurring in order of increasing size (for instance: if the terms are numbers, they are listed from smallest to largest; if they are words, they are listed in alphabetic order). Procedures of the Binary search algorithm: Compare the element to be located to the middle term of the list. The list is then split into two smaller sublists of the same size, or where one of these smaller lists has one fewer term than the other. The search continues by restricting the search to the appropriate sublist based on the comparison of the element to be located and the middle term. The binary search algorithm is much more efficient than the linear search algorithm. ECOM 2311- Discrete Mathematics - Ch.3 Dr. Musbah Shaat 10 / 41

Algorithms Searching Algorithms Example: Search 19 in the list 1 2 3 5 6 7 8 10 12 13 15 16 18 19 20 22 Split the list into 2 subsets with 8 terms each {1 2 3 5 6 7 8 10} {12 13 15 16 18 19 20 22} Compare 19 with the largest element of the first set 19 > 10 search 19 in the second set Split the second subset into 2 smaller subsets {12 13 15 16} {18 19 20 22} Compare 19 with 16 19 > 16 search 19 in the second set Split the second subset as: { 18 19} {20 22} Compare 19 > 19 is false search 19 in {18 19} Split the subset as : {18} {19} Since 19 > 18 search restricted to the second list. Finally 19 is located at the 14th element of the original list ECOM 2311- Discrete Mathematics - Ch.3 Dr. Musbah Shaat 11 / 41

Algorithms Searching Algorithms The pseudocode for the binary search algorithm is displayed as Algorithm 3. ECOM 2311- Discrete Mathematics - Ch.3 Dr. Musbah Shaat 12 / 41

Algorithms Sorting Sorting Problem: putting these elements into a list in which the elements are in increasing order. For instance, sorting the list 7, 2, 1, 4, 5, 9 produces the list 1, 2, 4, 5, 7, 9. Sorting the list d, h, c, a, f (using alphabetical order) produces the list a, c, d, f, h. The bubble sort: is one of the simplest sorting algorithms. It puts a list into increasing order by successively comparing adjacent elements, interchanging them if they are in the wrong order. Procedures of the bubble sort algorithm: Perform the basic operation, that is, interchanging a larger element with a smaller one following it, starting at the beginning of the list, for a full pass. Iterate this procedure until the sort is complete. ECOM 2311- Discrete Mathematics - Ch.3 Dr. Musbah Shaat 13 / 41

Algorithms Sorting Example: Use the bubble sort to put 3, 2, 4, 1, 5 into increasing order. ECOM 2311- Discrete Mathematics - Ch.3 Dr. Musbah Shaat 14 / 41

Algorithms Sorting The insertion sort: is a simple sorting algorithm, but it is usually not the most efficient. Procedures of the insertion sort algorithm: The insertion sort begins with the second element and compares this second element with the first element. The insertion sort inserts it before the first element if it does not exceed the first element and after the first element if it exceeds the first element. At this point, the first two elements are in the correct order. The third element is then compared with the first element, and if it is larger than the first element, it is compared with the second element; it is inserted into the correct position among the first three elements. In general, in the jth step of the insertion sort, the jth element of the list is inserted into the correct position in the list of the previously sorted j - 1 elements. ECOM 2311- Discrete Mathematics - Ch.3 Dr. Musbah Shaat 15 / 41

Algorithms Sorting Example: Use the insertion sort to put 3, 2, 4, 1, 5 into increasing order. First, compare 2 and 3. As 2 is lower, we get 2,3,4,1,5. Insert the third element 4 in the comparison. Because 2 < 4 and 3 < 4, 4 remains in the third place to get 2,3,4,1,5. To find the place of 1, we put it in the 1st place because 1 < 2 to get 1,2,3,4,5 Compare the last element 5 to the rest of the elements successively, we put it in the last order as 5 > 4. ECOM 2311- Discrete Mathematics - Ch.3 Dr. Musbah Shaat 16 / 41

Algorithms Greedy Algorithms Greedy Algorithm: goals to solve optimization problems by finding a solution to the given problem that either minimizes or maximizes the value of some parameter. The algorithm make what seems to be the best choice at each step. Finding a route between two cities with smallest total mileage Determining a way to encode messages using the fewest bits possible Finding a set of fiber links between network nodes using the least amount of fiber. You have to prove that a greedy algorithm always finds an optimal solution. (we call the algorithm greedy whether or not it finds an optimal solution.) To do this, we either prove that the solution is optimal or we show that there is a counterexample where the algorithm yields a nonoptimal solution. ECOM 2311- Discrete Mathematics - Ch.3 Dr. Musbah Shaat 17 / 41

Algorithms Greedy Algorithms Example: Consider the problem of making n cents (67 cents for example) change with quarters [25 cents], dimes [10 cents], nickels [5 cents] and pennies [1 cent], and using the least total number of coins. Select a quarter, leaving 42 cents. Select a second quarter, leaving 17 cents. Select a dime, leaving 7 cents. Select a nickel, leaving 2 cents. Select a penny, leaving 1 cent Select a penny. Remark: if we have only quarters, dimes and pennies the change for 30 cents would be made using 6 coins = 1 quarter + 5 pennies. Whereas a better solution is equal to 3 coins = 3 dimes! ECOM 2311- Discrete Mathematics - Ch.3 Dr. Musbah Shaat 18 / 41

Algorithms Greedy Algorithms Example: Suppose we have a group of proposed talks with preset start and end times. Devise a greedy algorithm to schedule as many of these talks as possible in a lecture hall, under the assumptions that once a talk starts, it continues until it ends, no two talks can proceed at the same time, and a talk can begin at the same time another one ends. Assume that talk j begins at time s j (where s stands for start) and ends at time e j (where e stands for end). ECOM 2311- Discrete Mathematics - Ch.3 Dr. Musbah Shaat 19 / 41

Algorithms Greedy Algorithms Homework [due 29th of Oct]. From the text book, Section 3.1, page 202 Questions: Q2,Q10, Q14, Q20, Q34, and Q38. ECOM 2311- Discrete Mathematics - Ch.3 Dr. Musbah Shaat 20 / 41

The Growth of Functions Big-O Notation Big-O Notation Let f and g be functions from the set of integers or the set of real numbers to the set of real numbers.we say that f (x) is O(g(x)) if there are constants C and k such that f (x) C g(x) whenever x > k. [This is read as f (x) is big-oh of g(x). ] C and k are called witnesses to the relationship f (x) is O(g(x)). Approach to work with the definition of big O Notation Select a value of k for which the size of f (x) can be readily estimated when x > k. Find a value of C for which f (x) C g(x) for x > k. ECOM 2311- Discrete Mathematics - Ch.3 Dr. Musbah Shaat 21 / 41

The Growth of Functions Big-O Notation Example: Show that f (x) = x 2 + 2x + 1 is O(x 2 ). Choose k = 1 x > 1. When x > 1 x 2 + 2x + 1 x 2 + 2x 2 + x 2 = 4x 2. Consequently, we can take C = 4 and k = 1 as witnesses to show that f (x) is O(x 2 ). [What if k=2?] Note that it is not necessary to use absolute values here because all functions in these equalities are positive when x is positive. ECOM 2311- Discrete Mathematics - Ch.3 Dr. Musbah Shaat 22 / 41

The Growth of Functions Big-O Notation Observe that in the relationship f (x) is O(x 2 ), x 2 can be replaced by any function with larger values than x 2. For example, f (x) is O(x 3 ), f (x) is O(x 2 + x + 7), and so on. Note that in Example 1 we have two functions, f (x) = x 2 + 2x + 1 and g(x) = x 2, such that f (x) is O(g(x)) and g(x) is O(f (x)). We say that two functions f (x) and g(x) that satisfy both of these big-o relationships are of the same order. It is acceptable to write f (x) O(g(x)) because O(g(x)) represents the set of functions that are O(g(x)). When big-o notation is used, the function g in the relationship f (x) is O(g(x)) is chosen to be as small as possible. In subsequent discussions, we will almost always deal with functions that take on only positive values. All references to absolute values can be dropped when working with big-o estimates for such functions. ECOM 2311- Discrete Mathematics - Ch.3 Dr. Musbah Shaat 23 / 41

The Growth of Functions Big-O Notation Example: Show that f (x) = 7x 2 is O(x 3 ). Choose k = 7 x > 7. Multiplying both sides of x > 7 by x 2, we have 7x 2 < x 3. Consequently, we can take C = 1 and k = 7 as witnesses to establish the relationship 7x 2 is O(x 3 ). Alternatively, when x > 1, we have 7x 2 < 7x 3, so that C = 7 and k = 1 are also witnesses to the relationship 7x 2 is O(x 3 ). Big-O Estimates for Some Important Functions Let f (x) = a n x n + a n 1 x n 1 + + a 1 x + a 0, where a 0, a 1,, a n 1, a n are real numbers. Then f (x) is O(x n ). ECOM 2311- Discrete Mathematics - Ch.3 Dr. Musbah Shaat 24 / 41

The Growth of Functions Big-O Estimates for Some Important Functions Example: How can big-o notation be used to estimate the sum of the first n positive integers?. Because each of the integers in the sum of the first n positive integers does not exceed n, it follows that 1 + 2 + + n n + n + + n = n.n = n 2. From this inequality it follows that 1 + 2 + 3 + + n is O(n 2 ), taking C = 1 and k = 1 as witnesses. Example: Give big-o estimates for the factorial function and the logarithm of the factorial function. A big-o estimate for n! can be obtained by noting that each term in the product does not exceed n. n! = 1.2.3 n n.n.n. n = n n From this inequality it follows that n! is O(n n ), taking C = 1 and k = 1 as witnesses. ECOM 2311- Discrete Mathematics - Ch.3 Dr. Musbah Shaat 25 / 41

The Growth of Functions Big-O Estimates for Some Important Functions Taking logarithms of both sides of the inequality established for n!, we obtain log n! log n n = n log n. This implies that log n! is O(n log n), again taking C = 1 and k = 1 as witnesses. Example: use the inequality n < 2 n to show that log n is O(n).. n < 2 n log n < n From this inequality it follows that log n is O(n), taking C = 1 and k = 1 as witnesses. Big-O notation is used to estimate the number of operations needed to solve a problem using a specified procedure or algorithm. The functions used in these estimates often include the following: 1, log n, n, n log n, n 2, 2 n, n! ECOM 2311- Discrete Mathematics - Ch.3 Dr. Musbah Shaat 26 / 41

The Growth of Functions Big-O Estimates for Some Important Functions ECOM 2311- Discrete Mathematics - Ch.3 Dr. Musbah Shaat 27 / 41

The Growth of Functions The Growth of Combinations of Functions THEOREM 2 Suppose that f 1 (x) is O(g 1 (x)) and that f 2 (x) is O(g 2 (x)). Then (f 1 + f 2 )(x) is O(max( g 1 (x), g 2 (x) )). COROLLARY 1 Suppose that f 1 (x) and f 2 (x) are both O(g(x)). Then (f 1 + f 2 )(x) is O(g(x)). THEOREM 3 Suppose that f 1 (x) is O(g 1 (x)) and f 2 (x) is O(g 2 (x)). Then (f 1 f 2 )(x) is O(g 1 (x)g 2 (x)). ECOM 2311- Discrete Mathematics - Ch.3 Dr. Musbah Shaat 28 / 41

The Growth of Functions Big-O Estimates for Some Important Functions Example: Give a big-o estimate for f (n) = 3n log(n!) + (n 2 + 3) log n, where n is a positive integer.. For the first part 3n log(n!), we have two subparts 3n and log(n!). As we just mentioned, log(n!) is O(n log n) while 3n is O(n). The product of the two subparts implies that 3n log(n!) is O(n 2 log n). For the second part (n 2 + 3) log n, we have two subparts (n 2 + 3) and log n. As we mentioned, log(n) is O(n) while n 2 + 3 is O(n 2 ). The product of the two subparts implies that (n 2 + 3) log n is O(n 2 log n). Therefore, the two big-o estimates for the products shows that f (n) = 3n log(n!) + (n 2 + 3) log n is O(n 2 log n). ECOM 2311- Discrete Mathematics - Ch.3 Dr. Musbah Shaat 29 / 41

The Growth of Functions Big-Omega and Big-Theta Notation Let f and g be functions from the set of integers or the set of real numbers to the set of real numbers.we say that f (x) is Ω(g(x)) if there are positive constants C and k such that f (x) C g(x) whenever x > k. [This is read as f (x) is big-omega of g(x). ] There is a strong connection between big-o and big-omega notation. In particular, f (x) is Ω(g(x)) if and only if g(x) is O(f (x)). The function f (x) = 8x 3 + 5x 2 + 7 is Ω(g(x)), where g(x) is the function g(x) = x 3. This is easy to see because f (x) = 8x 3 + 5x 2 + 7 8x 3 for all positive real numbers x. This is equivalent to saying that g(x) = x 3 is O(8x 3 + 5x 2 + 7) ECOM 2311- Discrete Mathematics - Ch.3 Dr. Musbah Shaat 30 / 41

The Growth of Functions Big-Omega and Big-Theta Notation Let f and g be functions from the set of integers or the set of real numbers to the set of real numbers. We say that f (x) is Θ(g(x)) if f (x) is O(g(x)) and f (x) is Ω(g(x)). When f (x) is Θ(g(x)) we say that f is big-theta of g(x), that f (x) and g(x) are of the same order. Example: Show that 3x 2 + 8x log x is Θ(x 2 ). Because 0 8x log x 8x 2, it follows that 3x 2 + 8x log x 11x 2 for x > 1. Consequently, 3x 2 + 8x log x is O(x 2 ). Clearly, x 2 is O(3x 2 + 8x log x). Consequently, 3x 2 + 8x log x is Θ(x 2 ). Homework [due 29th of Oct]. From the text book, Section 3.2, page 216 Questions: Q2,Q4, Q15, Q26, and Q30 [Parts: a,d]. ECOM 2311- Discrete Mathematics - Ch.3 Dr. Musbah Shaat 31 / 41

Complexity of Algorithms Time Complexity How can the efficiency [computational complexity] of an algorithm be analyzed? The time used by a computer to solve a problem using the algorithm [time complexity]. The amount of computer memory required to implement the algorithm [space complexity]. The time complexity of an algorithm can be expressed in terms of the number of operations used by the algorithm when the input has a particular size. The operations used to measure time complexity can be the comparison of integers, the addition of integers, the multiplication of integers, the division of integers, or any other basic operation. Time complexity is described in terms of the number of operations required instead of actual computer time because of the difference in time needed for different computers to perform basic operations. ECOM 2311- Discrete Mathematics - Ch.3 Dr. Musbah Shaat 32 / 41

Complexity of Algorithms Time Complexity Describe the time complexity of Algorithm 1 of Section 3.1 for finding the maximum element in a finite set of integers.? Exactly 2(n - 1) + 1 = 2n - 1 comparisons are used. Hence, the algorithm has time complexity O(n), measured in terms of the number of comparisons used. ECOM 2311- Discrete Mathematics - Ch.3 Dr. Musbah Shaat 33 / 41

Complexity of Algorithms Time Complexity Describe the time complexity of Algorithm 2 of Section 3.1 for the linear search algorithm.? If x = a i 2i + 1 comparisons are used. The most comparisons, 2n + 2, are required when the element is not in the list [Worst case]. Hence, a linear search requires O(n) comparisons in the worst case, because 2n + 2 is O(n). ECOM 2311- Discrete Mathematics - Ch.3 Dr. Musbah Shaat 34 / 41

Complexity of Algorithms Time Complexity Describe the time complexity of Algorithm 3 of Section 3.1 for the binary search algorithm.? Assume that there are n = 2 k elements in the list k = log n. Each step we have i < j and x > a m comparisons. At every stage we have 2 k x elements where x is the number of the stage up to k x = 0. Hence, at most 2k + 2 = 2 log n + 2 comparisons are required which is O(log n) ECOM 2311- Discrete Mathematics - Ch.3 Dr. Musbah Shaat 35 / 41

Complexity of Algorithms Time Complexity AVERAGE-CASE COMPLEXITY The average number of operations used to solve the problem over all possible inputs of a given size is found in this type of analysis [See example 4 page 221]. 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 on we will ignore such comparisons. What is the worst-case complexity of the bubble sort in terms of the number of comparisons made? We need n-1 passes. In the i th pass, there are n i comparisons. Hence, the number of comparisons is n 1 (n 1)n i=1 (n i) = 2 as n k=1 k = n(n+1) 2 Consequently, it has O(n 2 ) complexity. ECOM 2311- Discrete Mathematics - Ch.3 Dr. Musbah Shaat 36 / 41

Complexity of Algorithms Time Complexity What is the worst-case complexity of the insertion sort in terms of the number of comparisons made? In the worst case, j comparisons are required to insert the j th element into the correct position. j 1 with the ordered elements and one with itself to stop. Therefore, the number of comparisons is 2 + 3 + + n = n (n+1)n i=2 (i) = 2 1 as n k=1 k = n(n+1) 2 Consequently, it has O(n 2 ) complexity. The most efficient sorting algorithms can sort n items in O(n log n) ECOM time, 2311- as Discrete we Mathematics will show- Ch.3 later. Dr. Musbah Shaat 37 / 41

Complexity of Algorithms Complexity of Matrix Multiplication How many additions of integers and multiplications of integers are used by Algorithm 1 to multiply two n n matrices with integer entries? There are n 2 entries in the product of A and B. To find each entry requires a total of n multiplications and n - 1 additions. Hence, a total of n 3 multiplications and n 2 (n 1) additions are used. Multiplying two n n matrices directly from the definition requires O(n 3 ) multiplications and additions. ECOM 2311- Discrete Mathematics - Ch.3 Dr. Musbah Shaat 38 / 41

Complexity of Algorithms Complexity of Matrix Multiplication Example: In which order should the matrices A 1, A 2, and A 3 where A 1 is 30 20, A 2 is 20 40, and A 3 is 40 10, all with integer entries- be multiplied to use the least number of multiplications of integers? There are two possible ways to compute A 1, A 2, A 3. These are A 1 (A 2 A 3 ) and (A 1 A 2 )A 3. If A 2 and A 3 are first multiplied, a total of 20 40 10 = 8000 multiplications of integers are used to obtain the 20 10 matrix A 2 A 3. Then, to multiply A 1 and A 2 A 3 requires 30 20 10 = 6000 multiplications. Hence, a total of 8000 + 6000 = 14, 000 multiplications. if A 1 and A 2 are first multiplied, then 30 20 40 = 24, 000 multiplications are used to obtain the 30 40 matrix A 1 A 2. Then, to multiply A 1 A 2 and A 3 requires 30 40 10 = 12, 000 multiplications. Hence, a total of 24, 000 + 12, 000 = 36, 000 multiplications. ECOM 2311- Discrete Mathematics - Ch.3 Dr. Musbah Shaat 39 / 41

Complexity of Algorithms Complexity of Matrix Multiplication Brute-force algorithm, a problem is solved in the most straightforward manner based on the statement of the problem and the definitions of terms. Brute-force algorithms are designed to solve problems without regard to the computing resources required. ECOM 2311- Discrete Mathematics - Ch.3 Dr. Musbah Shaat 40 / 41

Homework [due 5th of Nov]. From the text book, Section 3.3, page 229 Questions: Q2, Q3, Q8 and Q22[Parts a,b]. End of Chapter # 3