Applied Discrete Mathematics Week 2: Functions and Sequences

Slides:



Advertisements
Similar presentations
College of Information Technology & Design
Advertisements

College of Information Technology & Design
MATH 224 – Discrete Mathematics
CompSci 102 Discrete Math for Computer Science
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.
The Fundamentals: Algorithms, the Integers & Matrices.
February 17, 2015Applied Discrete Mathematics Week 3: Algorithms 1 Double Summations Table 2 in 4 th Edition: Section th Edition: Section th.
Chapter Complexity of Algorithms –Time Complexity –Understanding the complexity of Algorithms 1.
Chapter 3: The Fundamentals: Algorithms, the Integers, and Matrices
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.
DISCRETE MATHEMATICS I CHAPTER 11 Dr. Adam Anthony Spring 2011 Some material adapted from lecture notes provided by Dr. Chungsim Han and Dr. Sam Lomonaco.
February 12, 2015Applied Discrete Mathematics Week 2: Functions and Sequences 1Exercises Question 1: Given a set A = {x, y, z} and a set B = {1, 2, 3,
Analysis of Algorithms
Chapter 3 Sec 3.3 With Question/Answer Animations 1.
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.
Fall 2002CMSC Discrete Structures1 … and now for… Sequences.
Fall 2002CMSC Discrete Structures1 Enough Mathematical Appetizers! Let us look at something more interesting: Algorithms.
MCA-2012Data Structure1 Algorithms Rizwan Rehman CCS, DU.
CSC 211 Data Structures Lecture 13
1 Algorithms CS/APMA 202 Rosen section 2.1 Aaron Bloomfield.
Data Structure Introduction.
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.
1 Algorithms CS 202 Epp section ??? Aaron Bloomfield.
1 Section 2.1 Algorithms. 2 Algorithm A finite set of precise instructions for performing a computation or for solving a problem.
Sequences and Summations Section 2.4. Section Summary Sequences. – Examples: Geometric Progression, Arithmetic Progression Recurrence Relations – Example:
Counting Discrete Mathematics. Basic Counting Principles Counting problems are of the following kind: “How many different 8-letter passwords are there?”
ALGORITHMS.
1 Discrete Structures – CNS2300 Text Discrete Mathematics and Its Applications Kenneth H. Rosen (5 th Edition) Chapter 2 The Fundamentals: Algorithms,
1. Searching The basic characteristics of any searching algorithm is that searching should be efficient, it should have less number of computations involved.
Fall 2008/2009 I. Arwa Linjawi & I. Asma’a Ashenkity 11 Basic Structure : Sets, Functions, Sequences, and Sums Sequences and Summations.
3/16/20161 … and now for… Sequences. 3/16/20162 Sequences Sequences represent ordered lists of elements. A sequence is defined as a function from a subset.
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. 大葉大學 資訊工程系 黃鈴玲.
Fall 2002CMSC Discrete Structures1 Chapter 3 Sequences Mathematical Induction Recursion Recursion.
LECTURE 9 CS203. Execution Time Suppose two algorithms perform the same task such as search (linear search vs. binary search) and sorting (selection sort.
CSE15 Discrete Mathematics 03/06/17
Growth of Functions & Algorithms
Modeling with Recurrence Relations
Analysis of Algorithms
Analysis of Algorithms
Data Structures and Algorithms
Lecture – 2 on Data structures
Mathematical Induction Recursion
Enough Mathematical Appetizers!
Lecture 2: Introduction to Algorithms
Computation.
CS 2210 Discrete Structures Algorithms and Complexity
Algorithms Chapter 3 With Question/Answer Animations
Algorithm design and Analysis
Analysis Algorithms.
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.
Enough Mathematical Appetizers!
ICS 253: Discrete Structures I
Rosen 5th ed., §2.1 ~31 slides, ~1 lecture
Applied Discrete Mathematics Week 6: Computation
CMSC Discrete Structures
Rosen 5th ed., §2.1 ~31 slides, ~1 lecture
Counting Discrete Mathematics.
Applied Discrete Mathematics Week 7: Computation
Enough Mathematical Appetizers!
Properties of Functions
Discrete Mathematics and its Applications
Enough Mathematical Appetizers!
Algorithms.
Discrete Mathematics CS 2610
10.1 Sequences and Summation Notation
Analysis of Algorithms
Presentation transcript:

Applied Discrete Mathematics Week 2: Functions and Sequences … and now for… Sequences February 2, 2017 Applied Discrete Mathematics Week 2: Functions and Sequences

Applied Discrete Mathematics Week 2: Functions and Sequences Sequences represent ordered lists of elements. A sequence is defined as a function from a subset of N to a set S. We use the notation an to denote the image of the integer n. We call an a term of the sequence. Example: subset of N: 1 2 3 4 5 … S: 2 4 6 8 10 … February 2, 2017 Applied Discrete Mathematics Week 2: Functions and Sequences

Applied Discrete Mathematics Week 2: Functions and Sequences We use the notation {an} to describe a sequence. Important: Do not confuse this with the {} used in set notation. It is convenient to describe a sequence with an equation. For example, the sequence on the previous slide can be specified as {an}, where an = 2n. February 2, 2017 Applied Discrete Mathematics Week 2: Functions and Sequences

Applied Discrete Mathematics Week 2: Functions and Sequences The Equation Game What are the equations that describe the following sequences a1, a2, a3, … ? 1, 3, 5, 7, 9, … an = 2n - 1 -1, 1, -1, 1, -1, … an = (-1)n 2, 5, 10, 17, 26, … an = n2 + 1 0.25, 0.5, 0.75, 1, 1.25 … an = 0.25n 3, 9, 27, 81, 243, … an = 3n February 2, 2017 Applied Discrete Mathematics Week 2: Functions and Sequences

Applied Discrete Mathematics Week 2: Functions and Sequences Strings Finite sequences are also called strings, denoted by a1a2a3…an. The length of a string S is the number of terms that it consists of. The empty string contains no terms at all. It has length zero. February 2, 2017 Applied Discrete Mathematics Week 2: Functions and Sequences

Applied Discrete Mathematics Week 2: Functions and Sequences Summations What does stand for? It represents the sum am + am+1 + am+2 + … + an. The variable j is called the index of summation, running from its lower limit m to its upper limit n. We could as well have used any other letter to denote this index. February 2, 2017 Applied Discrete Mathematics Week 2: Functions and Sequences

Applied Discrete Mathematics Week 2: Functions and Sequences Summations How can we express the sum of the first 1000 terms of the sequence {an} with an=n2 for n = 1, 2, 3, … ? We write it as . What is the value of ? It is 1 + 2 + 3 + 4 + 5 + 6 = 21. What is the value of ? It is so much work to calculate this… February 2, 2017 Applied Discrete Mathematics Week 2: Functions and Sequences

Applied Discrete Mathematics Week 2: Functions and Sequences Summations It is said that Carl Friedrich Gauss came up with the following formula: When you have such a formula, the result of any summation can be calculated much more easily, for example: February 2, 2017 Applied Discrete Mathematics Week 2: Functions and Sequences

Applied Discrete Mathematics Week 2: Functions and Sequences Double Summations Corresponding to nested loops in C or Java, there is also double (or triple etc.) summation: Example: February 2, 2017 Applied Discrete Mathematics Week 2: Functions and Sequences

Applied Discrete Mathematics Week 2: Functions and Sequences Double Summations Table 2 in 4th Edition: Section 1.7 5th Edition: Section 3.2 6th and 7th Edition: Section 2.4 contains some very useful formulas for calculating sums. In the same Section, Exercises 15 and 17 (7th Edition: Exercises 31 and 33) make a nice homework. February 2, 2017 Applied Discrete Mathematics Week 2: Functions and Sequences

Enough Mathematical Appetizers! Let us look at something more interesting: Algorithms February 2, 2017 Applied Discrete Mathematics Week 2: Functions and Sequences

Applied Discrete Mathematics Week 2: Functions and Sequences Algorithms What is an algorithm? An algorithm is a finite set of precise instructions for performing a computation or for solving a problem. This is a rather vague definition. You will get to know a more precise and mathematically useful definition when you attend CS420 or CS620. But this one is good enough for now… February 2, 2017 Applied Discrete Mathematics Week 2: Functions and Sequences

Applied Discrete Mathematics Week 2: Functions and Sequences Algorithms Properties of algorithms: Input from a specified set, Output from a specified set (solution), Definiteness of every step in the computation, Correctness of output for every possible input, Finiteness of the number of calculation steps, Effectiveness of each calculation step and Generality for a class of problems. February 2, 2017 Applied Discrete Mathematics Week 2: Functions and Sequences

Applied Discrete Mathematics Week 2: Functions and Sequences Algorithm Examples We will use a pseudocode to specify algorithms, which slightly reminds us of Basic and Pascal. Example: an algorithm that finds the maximum element in a finite sequence procedure max(a1, a2, …, an: integers) max := a1 for i := 2 to n if max < ai then max := ai {max is the largest element} February 2, 2017 Applied Discrete Mathematics Week 2: Functions and Sequences

Applied Discrete Mathematics Week 2: Functions and Sequences Algorithm Examples Another example: a linear search algorithm, that is, an algorithm that linearly searches a sequence for a particular element. procedure linear_search(x: integer; a1, a2, …, an: integers) i := 1 while (i  n and x  ai) i := i + 1 if i  n then location := i else location := 0 {location is the subscript of the term that equals x, or is zero if x is not found} February 2, 2017 Applied Discrete Mathematics Week 2: Functions and Sequences

Applied Discrete Mathematics Week 2: Functions and Sequences Algorithm Examples If the terms in a sequence are ordered, a binary search algorithm is more efficient than linear search. The binary search algorithm iteratively restricts the relevant search interval until it closes in on the position of the element to be located. February 2, 2017 Applied Discrete Mathematics Week 2: Functions and Sequences

Algorithm Examples binary search for the letter ‘j’ search interval a c d f g h j l m o p r s u v x z center element February 2, 2017 Applied Discrete Mathematics Week 2: Functions and Sequences

Algorithm Examples binary search for the letter ‘j’ search interval a c d f g h j l m o p r s u v x z center element February 2, 2017 Applied Discrete Mathematics Week 2: Functions and Sequences

Algorithm Examples binary search for the letter ‘j’ search interval a c d f g h j l m o p r s u v x z center element February 2, 2017 Applied Discrete Mathematics Week 2: Functions and Sequences

Algorithm Examples binary search for the letter ‘j’ search interval a c d f g h j l m o p r s u v x z center element February 2, 2017 Applied Discrete Mathematics Week 2: Functions and Sequences

Algorithm Examples found ! binary search for the letter ‘j’ search interval a c d f g h j l m o p r s u v x z center element found ! February 2, 2017 Applied Discrete Mathematics Week 2: Functions and Sequences

Applied Discrete Mathematics Week 2: Functions and Sequences Algorithm Examples procedure binary_search(x: integer; a1, a2, …, an: integers) i := 1 {i is left endpoint of search interval} j := n {j is right endpoint 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 subscript of the term that equals x, or is zero if x is not found} February 2, 2017 Applied Discrete Mathematics Week 2: Functions and Sequences

Applied Discrete Mathematics Week 2: Functions and Sequences Algorithm Examples Obviously, on sorted sequences, binary search is more efficient than linear search. How can we analyze the efficiency of algorithms? We can measure the time (number of elementary computations) and space (number of memory cells) that the algorithm requires. These measures are called computational complexity and space complexity, respectively. February 2, 2017 Applied Discrete Mathematics Week 2: Functions and Sequences

Applied Discrete Mathematics Week 2: Functions and Sequences Complexity What is the time complexity of the linear search algorithm? We will determine the worst-case number of comparisons as a function of the number n of terms in the sequence. The worst case for the linear algorithm occurs when the element to be located is not included in the sequence. In that case, every item in the sequence is compared to the element to be located. February 2, 2017 Applied Discrete Mathematics Week 2: Functions and Sequences

Applied Discrete Mathematics Week 2: Functions and Sequences Algorithm Examples Here is the linear search algorithm again: procedure linear_search(x: integer; a1, a2, …, an: integers) i := 1 while (i  n and x  ai) i := i + 1 if i  n then location := i else location := 0 {location is the subscript of the term that equals x, or is zero if x is not found} February 2, 2017 Applied Discrete Mathematics Week 2: Functions and Sequences

Applied Discrete Mathematics Week 2: Functions and Sequences Complexity For n elements, the loop while (i  n and x  ai) i := i + 1 is processed n times, requiring 2n comparisons. When it is entered for the (n+1)th time, only the comparison i  n is executed and terminates the loop. Finally, the comparison if i  n then location := i is executed, so all in all we have a worst-case time complexity of 2n + 2. February 2, 2017 Applied Discrete Mathematics Week 2: Functions and Sequences

Reminder: Binary Search Algorithm procedure binary_search(x: integer; a1, a2, …, an: integers) i := 1 {i is left endpoint of search interval} j := n {j is right endpoint 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 subscript of the term that equals x, or is zero if x is not found} February 2, 2017 Applied Discrete Mathematics Week 2: Functions and Sequences

Applied Discrete Mathematics Week 2: Functions and Sequences Complexity What is the time complexity of the binary search algorithm? Again, we will determine the worst-case number of comparisons as a function of the number n of terms in the sequence. Let us assume there are n = 2k elements in the list, which means that k = log n. If n is not a power of 2, it can be considered part of a larger list, where 2k < n < 2k+1. February 2, 2017 Applied Discrete Mathematics Week 2: Functions and Sequences

Applied Discrete Mathematics Week 2: Functions and Sequences Complexity In the first cycle of the loop while (i < j) begin m := (i + j)/2 if x > am then i := m + 1 else j := m end the search interval is restricted to 2k-1 elements, using two comparisons. February 2, 2017 Applied Discrete Mathematics Week 2: Functions and Sequences

Applied Discrete Mathematics Week 2: Functions and Sequences Complexity In the second cycle, the search interval is restricted to 2k-2 elements, again using two comparisons. This is repeated until there is only one (20) element left in the search interval. At this point 2k comparisons have been conducted. February 2, 2017 Applied Discrete Mathematics Week 2: Functions and Sequences

Applied Discrete Mathematics Week 2: Functions and Sequences Complexity Then, the comparison while (i < j) exits the loop, and a final comparison if x = ai then location := i determines whether the element was found. Therefore, the overall time complexity of the binary search algorithm is 2k + 2 = 2 log n + 2. February 2, 2017 Applied Discrete Mathematics Week 2: Functions and Sequences

Applied Discrete Mathematics Week 2: Functions and Sequences Complexity In general, we are not so much interested in the time and space complexity for small inputs. For example, while the difference in time complexity between linear and binary search is meaningless for a sequence with n = 10, it is gigantic for n = 230. February 2, 2017 Applied Discrete Mathematics Week 2: Functions and Sequences

Applied Discrete Mathematics Week 2: Functions and Sequences Complexity For example, let us assume two algorithms A and B that solve the same class of problems. The time complexity of A is 5,000n, the one for B is 1.1n for an input with n elements. February 2, 2017 Applied Discrete Mathematics Week 2: Functions and Sequences

Applied Discrete Mathematics Week 2: Functions and Sequences Complexity Comparison: time complexity of algorithms A and B Input Size Algorithm A Algorithm B n 5,000n 1.1n 10 50,000 3 100 500,000 13,781 1,000 5,000,000 2.51041 1,000,000 5109 4.81041392 February 2, 2017 Applied Discrete Mathematics Week 2: Functions and Sequences

Applied Discrete Mathematics Week 2: Functions and Sequences Complexity This means that algorithm B cannot be used for large inputs, while running algorithm A is still feasible. So what is important is the growth of the complexity functions. The growth of time and space complexity with increasing input size n is a suitable measure for the comparison of algorithms. February 2, 2017 Applied Discrete Mathematics Week 2: Functions and Sequences