Chapter 1 Introduction. Goals Why the choice of algorithms is so critical when dealing with large inputs Basic mathematical background Review of Recursion.

Slides:



Advertisements
Similar presentations
Lecture 2 Based on Chapter 1, Weiss. Mathematical Foundation Series and summation: ……. N = N(N+1)/2 (arithmetic series) 1 + r+ r 2 + r 3 +………r.
Advertisements

22C:19 Discrete Structures Induction and Recursion Fall 2014 Sukumar Ghosh.
Proofs, Recursion and Analysis of Algorithms Mathematical Structures for Computer Science Chapter 2 Copyright © 2006 W.H. Freeman & Co.MSCS SlidesProofs,
Copyright © Cengage Learning. All rights reserved. CHAPTER 5 SEQUENCES, MATHEMATICAL INDUCTION, AND RECURSION SEQUENCES, MATHEMATICAL INDUCTION, AND RECURSION.
February 19, 2015Applied Discrete Mathematics Week 4: Number Theory 1 The Growth of Functions Question: If f(x) is O(x 2 ), is it also O(x 3 )? Yes. x.
The Fundamental Theorem of Arithmetic (2/12) Definition (which we all already know). A number greater than 1 is called prime if its only divisors are 1.
Number Theory and Cryptography
Basic properties of the integers
Induction and recursion
Proof Techniques and Recursion. Proof Techniques Proof by induction –Step 1: Prove the base case –Step 2: Inductive hypothesis, assume theorem is true.
Chapter 4 Properties of the integers: mathematical induction Yen-Liang Chen Dept of IM NCU.
1 Chapter 1: Introduction What you have learnt in Comp1220 or Comp1170? What will be taught in Comp 1200? - more Abstract Data Types -efficient algorithms.
Section Section Summary Recursive Algorithms Proving Recursive Algorithms Correct Recursion and Iteration (not yet included in overheads) Merge.
Mathematics of Cryptography Part I: Modular Arithmetic, Congruence,
Proofs, Recursion, and Analysis of Algorithms Mathematical Structures for Computer Science Chapter 2 Copyright © 2006 W.H. Freeman & Co.MSCS SlidesProofs,
Properties of the Integers: Mathematical Induction
Fall 2002CMSC Discrete Structures1 Let us get into… Number Theory.
BY MISS FARAH ADIBAH ADNAN IMK
Mathematics of Cryptography Part I: Modular Arithmetic, Congruence,
9/2/2015Discrete Structures1 Let us get into… Number Theory.
Methods of Proof & Proof Strategies
Sequences and Series (T) Students will know the form of an Arithmetic sequence.  Arithmetic Sequence: There exists a common difference (d) between each.
MATH 224 – Discrete Mathematics
1 CS 201 Data Structures & Algorithms Chapter 1 Introduction Text: Read Weiss, §1.1 – 1.3 Izmir University of Economics.
Mathematics Review Exponents Logarithms Series Modular arithmetic Proofs.
Chapter 2 - Mathematical Review Functions
Mathematical Induction. F(1) = 1; F(n+1) = F(n) + (2n+1) for n≥ F(n) n F(n) =n 2 for all n ≥ 1 Prove it!
1 CE 221 Data Structures & Algorithms Chapter 1 Introduction Text: Read Weiss, §1.1 – 1.3 Izmir University of Economics.
Advance Data Structure and Algorithm COSC600 Dr. Yanggon Kim Chapter 1.
CSC201 Analysis and Design of Algorithms Asst.Proof.Dr.Surasak Mungsing Oct-151 Lecture 2: Definition of algorithm and Mathematical.
Chapter 1 - Mathematical Review Functions Factorial - x!=1*2*3*…..*x Permutation - a rearrangement of a sequence Boolean variable - takes on 2 values –
Mathematical Background and Linked Lists. 2 Iterative Algorithm for Sum Find the sum of the first n integers stored in an array v : sum (v[], n) temp_sum.
Basic Mathematics Chapter 1 (1.2 and 1.3) Weiss. Recursion / Slide 2 Logarithms * Definition: if and only if * Theorem 1.1: n Proof: apply the definition.
Copyright © 2009 Pearson Education, Inc. Chapter 5 Section 1 - Slide 1 Chapter 1 Number Theory and the Real Number System.
Cpt S 223 – Advanced Data Structures Math Review 2
Chapter 1: Introduction
Foundations of Discrete Mathematics Chapters 5 By Dr. Dalia M. Gil, Ph.D.
October 3, 2001CSE 373, Autumn Mathematical Background 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 2 N +2 N = 2 N+1.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 223 – Advanced Data Structures Math Review 1.
1 CMSC 341 Math Review. 2 Exponents Identities (X A ) B = X AB X A * X B = X A+B X A / X B = X A-B X A + X B  X A+B.
1 2/21/2016 MATH 224 – Discrete Mathematics Sequences and Sums A sequence of the form ar 0, ar 1, ar 2, ar 3, ar 4, …, ar n, is called a geometric sequence.
CompSci 102 Discrete Math for Computer Science March 13, 2012 Prof. Rodger Slides modified from Rosen.
Section 1.7. Definitions A theorem is a statement that can be shown to be true using: definitions other theorems axioms (statements which are given as.
TCSS 342 Autumn 2004 Version TCSS 342 Data Structures & Algorithms Autumn 2004 Ed Hong.
1 Discrete Structures – CNS2300 Text Discrete Mathematics and Its Applications Kenneth H. Rosen (5 th Edition) Chapter 2 The Fundamentals: Algorithms,
Chapter 4 With Question/Answer Animations 1. Chapter Motivation Number theory is the part of mathematics devoted to the study of the integers and their.
Number Theory Lecture 1 Text book: Discrete Mathematics and its Applications, 7 th Edition.
Section Recursion 2  Recursion – defining an object (or function, algorithm, etc.) in terms of itself.  Recursion can be used to define sequences.
1 Recursive Definitions and Structural Induction CS/APMA 202 Rosen section 3.4 Aaron Bloomfield.
Fuw-Yi Yang1 Textbook: Introduction to Cryptography 2nd ed. By J.A. Buchmann Chap 1 Integers Department of Computer Science and Information Engineering,
Fall 2002CMSC Discrete Structures1 Chapter 3 Sequences Mathematical Induction Recursion Recursion.
Hubert Chan (Chapters 1.6, 1.7, 4.1)
MATH 224 – Discrete Mathematics
Chapter 1 Introduction Recursion
CS2210:0001Discrete Structures Induction and Recursion
Hubert Chan (Chapters 1.6, 1.7, 4.1)
Applied Discrete Mathematics Week 3: Algorithms
Applied Discrete Mathematics Week 4: Number Theory
Mathematical Induction Recursion
Chapter 5 Induction and Recursion
BaSIC Math Reviews.
CSE 373 Data Structures Lecture 5
Applied Discrete Mathematics Week 9: Integer Properties
CMSC 341 Math Review.
CE 221 Data Structures & Algorithms Chapter 1 Introduction
Applied Discrete Mathematics Week 10: Introduction to Counting
Clements MAΘ October 30th, 2014
Chapter 1 Introduction Recursion
Recursion.
Presentation transcript:

Chapter 1 Introduction

Goals Why the choice of algorithms is so critical when dealing with large inputs Basic mathematical background Review of Recursion Review of basic C++ concepts

Selection Problem Find the k th -largest number from a group of N numbers  How would you solve this? Algorithm 1:  Sort the N numbers and Pick the k th one  Easy to implement, but Sorting requires many comparisons Much work comparing elements that have no chance of being in position k  To sort N elements, need N log 2 (N) comparisons/swaps in general  Unsorted database with 10,000,000 elements and 1,000 swaps/sec will result in 2.8 hours to finish task

Another strategy Algorithm 2: (Better)  Sort first K elements in the array.  Insert elements (K+1) to N, discarding the smallest element each time.  Then pick the k th element N = 10,000,000 and K = 5,000,000 ? If K is about as large as N, little improvement A better algorithm can solve this in a second!

Word Puzzle Find all words, given a 2-D array and a word-list. Algorithm 1:  For each word in the word list, compare each combination of letters in the puzzle Algorithm 2:  For each combination of letters Check if the word is in the word-list What if the word-list is the entire dictionary?

Math Review: Exponents X A X B = X A+B X A  X B = X A-B (X A ) B = X AB X A + X A = 2X A  X 2A 2 N + 2 N = 22 N = 2 N N = 2 N+1 -1

Logarithms All logarithms are to the base 2, unless otherwise specified Definition:  log X A = B is equivalent to X B = A

Properties of logarithms Base-change:  log A B = ; A, B, C > 0, A  1 Proof: Let Z = log C A and Y = log A B  Then A = C Z and B = A Y  Implies B = (C Z ) Y = C ZY  Hence log C B = ZY = log C A log A B log C B log C A

Logarithms (contd.) log(AB)= log(A) + log(B); A, B > 0 Proof:  Let X = log(A) and Y = log(B)  Hence 2 X = A and 2 Y = B  Combining the above, AB = 2 X 2 Y = 2 X + Y  Implies, log(AB) = X + Y = log(A) + log(B)  Hence proved!

Logarithms (contd.) More results  log(A/B) = log(A) - log(B)  log(AB) = B log(A)  log(X) 0  log(1)=0, log(2)=1, log(1,024)=10,  log(1,048,576)=20

Geometric Series   2 i = 2 N i=0..N   A i = i=0..N  If 0 < A < 1,  A i  i=0..N  If N  ,  A i = i=0..N A N A

Arithmetic Series   i = i=0..N How about (3k-1) ? N (N+1) 2

Modular Arithmetic We say that A is congruent to B modulo N  Written as A  B (mod N)  If N divides (A-B) In other words, the remainder is the same if either A or B are divided by N  E.g. 81  61  1 (mod 10) Similarly,  A+C  B+C (mod N) and AD  BD (mod N)

Proof by Induction Given a theorem First prove a base case  Show the theorem is true for some small degenerate values Next assume an inductive hypothesis  Assume the theorem is true for all cases up to some limit k Then prove that the theorem holds for the next value (k+1)

Proof by Induction - example Fibonacci Series  F 0 = 1, F 1 = 1, F i = F i-1 + F i-2, for i > 1 Show that  F i < (5/3) i, for i  1 Base case: F 0 = 1 = F 1 < 5/3 Inductive Hypothesis  F i < (5/3) i, for i = 1, 2,..., k

Proof by Induction - example (contd.) Now prove that F k+1 < (5/3) k+1 From definition of Fibonacci Sequence F k+1 < F k + F k-1 Using inductive Hypothesis  F k+1 < (5/3) k + (5/3) k-1 < (5/3) k + (5/3) k-1 < (5/3) k+1 [ 3/5 + (3/5) 2 ] < (5/3) k+1 [24/25] < (5/3) k+1

Proof by Induction - Exercise   i = i=0..N N (N+1) 2 N (N+1)(2N+1) 6

Other types of proofs Proof by counter-example  The statement F k < k 2 in the Fibonacci series is false Proof: F 11 = 144 > 11 2 Proof by Contradiction  Initially assume that the theorem is false Then show that some known property would be false as well  Example: “There are an infinite number of prime numbers”  Proof: Assume the theorem is false Let P 1, P 2,..., P k be all the primes in increasing order N = P 1  P 2    P k + 1 is > P k and so it is not a prime. But it is also not divisible by any of the listed primes, contradicting the factorization of integers into primes. This is a counter-example

Another type of Proof “Proof by intimidation” Also known as “Proof by Authority” Proof:  Your prof. says the theorem is true

Recursion Recursive function  Function that is defined in terms of itself  E.g. f(0)=0 and f(x) = 2*f(x-1)+ x 2  Implementation in C or C++ 1.int f( int x ) 2.{ 3. if( x == 0) 4. return 0; 5. else 6. return 2 * f( x - 1) + x * x; 7. }  Recursive Call  Base Case

Recursion Examples 1.Evaluating f(x) = 2*f(x-1) + x*x  f(4) = 2*f(3) + 4*4 f(3) = 2*f(2) + 3*3 f(2) = 2*f(1) + 2*2  f(1) = 2*f(0) + 1*1  f(0) = 0  f(1) = 2*0 + 1*1 = 1 f(2) = 2*1 + 2*2 = 6 f(3) = 2*6 + 3*3 = 21  f(4) = 2*21 + 4*4 = 58 2.Looking up meaning of a word in dictionary  Where is the recursion?  What is the base case?

Ensuring recursive programs terminate What happens if you make the call f(-1) in the implementation in slide 19? Another non-terminating recursive function 1.int bad(int n) 2.{ 3. if(n == 0) 4. return 0; 5. else 6. return bad(n/3 + 1) + n - 1; 7.} What happens on call to bad(1)? And bad(2)? And bad(3)?

Ensuring recursive programs terminate (contd.) Two rules for writing correct recursive programs 1.Define a base case This is what terminates recursion 2.Making progress: At each step make progress towards the base case More rules for writing clean recursive programs 3.Design Rule Assume that all recursive calls work. 4.Compound Interest Rule: Never duplicate work by making same recursive call twice! E.g. Fibonacci series f(i) = f(i-1) + f(i-2). Why??

For next week… Reading Assignment  Sections 1.5, 1.6 and 1.7 Exercise problems  Problems 1.1 to 1.12 on pages 39 and 40  Not for submission/grading.