1 CS 201 Data Structures & Algorithms Chapter 1 Introduction Text: Read Weiss, §1.1 – 1.3 Izmir University of Economics.

Slides:



Advertisements
Similar presentations
With examples from Number Theory
Advertisements

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.
22C:19 Discrete Structures Induction and Recursion Fall 2014 Sukumar Ghosh.
Copyright © Cengage Learning. All rights reserved. CHAPTER 5 SEQUENCES, MATHEMATICAL INDUCTION, AND RECURSION SEQUENCES, MATHEMATICAL INDUCTION, AND RECURSION.
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.
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.
Discrete Structures Chapter 2 Part B Mathematical Induction
Algorithm Analysis CS 201 Fundamental Structures of Computer Science.
Data Structures Review Session 1
Chapter 15 Recursive Algorithms. 2 Recursion Recursion is a programming technique in which a method can call itself to solve a problem A recursive definition.
Analysis of Algorithms 7/2/2015CS202 - Fundamentals of Computer Science II1.
Section Section Summary Recursive Algorithms Proving Recursive Algorithms Correct Recursion and Iteration (not yet included in overheads) Merge.
Induction and recursion
CHAPTER 7: SORTING & SEARCHING Introduction to Computer Science Using Ruby (c) Ophir Frieder at al 2012.
Induction and recursion
Lecture 8. How to Form Recursive relations 1. Recap Asymptotic analysis helps to highlight the order of growth of functions to compare algorithms Common.
Mathematics Review Exponents Logarithms Series Modular arithmetic Proofs.
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 Introduction to Abstract Mathematics Chapter 4: Sequences and Mathematical Induction Instructor: Hayk Melikya 4.1- Sequences. 4.2,
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.
Recursion l Powerful Tool l Useful in simplifying a problem (hides details of a problem) l The ability of a function to call itself l A recursive call.
Chapter 1 Introduction. Goals Why the choice of algorithms is so critical when dealing with large inputs Basic mathematical background Review of Recursion.
Chapter 3 (Part 3): Mathematical Reasoning, Induction & Recursion  Recursive Algorithms (3.5)  Program Correctness (3.6)
© by Kenneth H. Rosen, Discrete Mathematics & its Applications, Sixth Edition, Mc Graw-Hill, 2007 Chapter 4 (Part 3): Mathematical Reasoning, Induction.
Slides prepared by Rose Williams, Binghamton University ICS201 Lecture 19 : Recursion King Fahd University of Petroleum & Minerals College of Computer.
CSC201 Analysis and Design of Algorithms Asst.Proof.Dr.Surasak Mungsing Oct-151 Lecture 2: Definition of algorithm and Mathematical.
1 Sections 1.5 & 3.1 Methods of Proof / Proof Strategy.
1 Chapter 7: Sorting (Insertion Sort, Shellsort) CE 221 Data Structures and Algorithms Izmir University of Economics Text: Read Weiss, § 7.1 – 7.4.
Recursion. What is recursion? Rules of recursion Mathematical induction The Fibonacci sequence Summary Outline.
Introduction to Data Structures and Algorithms CS 110: Data Structures and Algorithms First Semester,
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.
Reading and Writing Mathematical Proofs Spring 2015 Lecture 4: Beyond Basic Induction.
Recursive Algorithms &
Data Structures and Algorithm Analysis Introduction Lecturer: Ligang Dong, egan Tel: , Office: SIEE Building.
Cpt S 223 – Advanced Data Structures Math Review 2
Chapter 1: Introduction
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.
Mathematical Induction Section 5.1. Climbing an Infinite Ladder Suppose we have an infinite ladder: 1.We can reach the first rung of the ladder. 2.If.
Copyright © Zeph Grunschlag, Induction Zeph Grunschlag.
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.
CompSci 102 Discrete Math for Computer Science March 13, 2012 Prof. Rodger Slides modified from Rosen.
Chapter 5. Section 5.1 Climbing an Infinite Ladder Suppose we have an infinite ladder: 1.We can reach the first rung of the ladder. 2.If we can reach.
Chapter 4 With Question/Answer Animations 1. Chapter Summary Divisibility and Modular Arithmetic - Sec 4.1 – Lecture 16 Integer Representations and Algorithms.
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.
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.
6/12/2016 Prepared by Dr.Saad Alabbad1 CS100 : Discrete Structures Proof Techniques(2) Mathematical Induction & Recursion Dr.Saad Alabbad Department of.
Section Recursion 2  Recursion – defining an object (or function, algorithm, etc.) in terms of itself.  Recursion can be used to define sequences.
Section Recursion  Recursion – defining an object (or function, algorithm, etc.) in terms of itself.  Recursion can be used to define sequences.
Mathematical Induction What it is? Why is it a legitimate proof method? How to use it?
Fall 2002CMSC Discrete Structures1 Chapter 3 Sequences Mathematical Induction Recursion Recursion.
Chapter 4 (Part 3): Mathematical Reasoning, Induction & Recursion
MA/CSSE 473 Day 05 Factors and Primes Recursive division algorithm.
Induction and recursion
Algorithm Analysis CSE 2011 Winter September 2018.
Mathematical Induction Recursion
Chapter 5 Induction and Recursion
Induction and recursion
CS 201 Fundamental Structures of Computer Science
Applied Discrete Mathematics Week 9: Integer Properties
CE 221 Data Structures & Algorithms Chapter 1 Introduction
Recursion.
Presentation transcript:

1 CS 201 Data Structures & Algorithms Chapter 1 Introduction Text: Read Weiss, §1.1 – 1.3 Izmir University of Economics

2 Course Policy Syllabus Grading Labs always in C++ (and/or C) programming language Each assignment starts and ends in the same Lab session. Late assignments will not be accepted. Study hard! Izmir University of Economics

3 Introduction See that how a program performs for reasonably large input is just as important as its performance on moderate amounts of input Summarize basic mathematical background needed Review recursion Izmir University of Economics

4 Motivating Examples: Selection Selection problem: you have a group of N numbers and would like to determine the k th largest. I: read them into an array. Sort them in a decreasing order. Return the k th element. II: read the first k elements into the array. Sort them in decreasing order. Next read the remaining elements one by one. If the new element read is smaller than the last, ignore it otherwise place in the correct spot in the array bumping one element out of the array. A simulation with a random file of 10 million elements and k = 5,000,000 shows that each requires several days of computer processing. Izmir University of Economics

5 Motivating Examples: Word Puzzles Solving a popular word puzzle: Input consists of a two dimensional array of letters and a list of words. The objective is to find the words laying horizontally, vertically or diagonally in either direction. I: for each word in the word list, check (row, column, orientation) II: for each ordered quadruple (row, column, orientation, number of characters), test whether the word is in the word list. {this, two, fat, that} Izmir University of Economics

6 Math Review - 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 !=X 2N Izmir University of Economics

7 Math Review – Logarithms I In computer science, all logarithms are to the base 2 unless specified otherwise. Definition 1.1. X A = B if log X B=A Theorem 1.1. log A B = log C B/log C A where A, B, C > 0, A != 1 Proof: Let X=log C B, Y=log C A, Z=log A B C X =B, C Y =A, A Z =B by Definition 1.1. B=C X =(C Y ) Z.Therefore, X=YZ Theorem 1.2. log AB = logA + logB where A, B > 0 Proof: X=logA, Y=logB, and Z=logAB, 2 X =A, 2 Y =B, and 2 Z =AB, 2 X 2 Y =AB=2 Z. Therefore, X+Y=Z Izmir University of Economics

Math Review – Logarithms II log A/B = logA – logB log(A B )=BlogA logX 0 log1 = 0, log2 = 1, log1024 = 10 8Izmir University of Economics

9 Math Review – Series I Geometric Series If 0 < A < 1, then and as N tends to , the sum approaches 1/(1-A) S=1+A+A 2 +A 3 +A 4... AS=A+A 2 +A 3 +A 4 +A 5... S-AS= 1 which implies S=1/(1-A) Izmir University of Economics

10 Math Review – Series II Arithmetic Series Izmir University of Economics

11 Math Review – Series III The error in the approximation tends to Euler’s constant  = Izmir University of Economics

12 Math Review – Modular Arithmetic A  B (mod N) means A is congruent to B modulo N, If N divides A-B (remainders are the same) Example:81  61  1 (mod 10) if A  B (mod N), then A + C  B + C (mod N) and AD  BD (mod N) Izmir University of Economics

13 The P Word – Proof by Induction There are various ways of proving statements in data structures analysis Proof by Induction: It has two standart parts: The first step is proving a base case. Establishing that a theorem is true for some small (usually degenerate) value(s). This step is almost always trivial. Next, an inductive hypothesis is assumed. Generally this means that the theorem is assumed to be true for all cases up to some limit k.Using the assumption, the theorem is then shown to be true for the next value, typically k+1. Izmir University of Economics

14 The P Word – Induction Example I Example: Prove that Fibonacci Numbers F 0 =1, F 1 =1, and F i =F i-1 +F i-2 for i > 1, satisfy F i < (5/3) i for i ≥ 1. Proof: Verify that the theorem is true for the trivial cases (base cases): F 1 =1 < (5/3) 1 and, F 2 = 2 < (5/3) 2. These prove the basis. We now assume that the theorem is true for i = 1, 2,..., k; this is the inductive hypothesis. To prove the theorem, we need to prove F k+1 <(5/3) k+1. Izmir University of Economics

15 Example: If N ≥ 1 then Proof: For the base case, the theorem is true when N = 1. For the inductive hypothesis, assume the theorem is true for 1 ≤ k ≤ N. Let’s try to prove that it is true for N + 1 The P Word – Induction Example II Izmir University of Economics

Proof by Counterexample: Best way for proving that a statement is false. Example: The statement F k ≤ k 2 is false. The easiest way to prove this is to compute F 11 = 144 > 11 2 = Proof by Counterexample Izmir University of Economics

Proof by Contradiction It proceeds by assuming that the theorem is false and showing that this assumption implies that some known property is false, and hence the original assumption is erroneous. Example: Prove that there is an infinite number of primes. Proof: Assume the theorem is false, so that there is some largest prime P k. Let P 1, P 2,..., P k be all the primes in order and consider N = P 1 P 2...P k + 1. Clearly, N > P k, so by assumption N can not be prime. However, none of P 1, P 2,..., P k divides N exactly, because remainders are all 1. This is a contradiction: numbers are either prime or a product of primes. Hence the original assumption is false implying that the theorem is true. 17Izmir University of Economics

18 A Brief Introduction to Recursion A function that is defined in terms of itself is called recursive. Not all mathematically recursive functions are correctly or efficiently implemented by recursion. Example: for all integers x ≥ 0 with f(0)=0 #include int F( int X ) { if( X == 0 ) /* base case */ return 0; else return 2 * F( X - 1 ) + X * X; } main( ) { printf( "F(4) = %d\n", F( 4 ) ); return 0; } If F is called with a value of 4, then 2*F(3)+4*4 will be required to be computed. Thus a call is made to find F(3). 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 base case. Recursive calls until a base case. F(-1)=? Automatic Bookkeeping Izmir University of Economics

Recursion - Bad #include int Bad( unsigned int N ) { if( N == 0 ) return 0; else return Bad( N / ) + N - 1; } main( ) { printf( "Bad is infinite recursion\n" ); return 0; } 19Izmir University of Economics Bad(0)=0, Bad(N)=Bad(N/3 + 1) + N – 1 To compute Bad(1), the computer will repeatedly make calls to Bad(1). Eventually, it will run out of space

Fundamental Rules of Recursion - I 1) Base cases: You must always have some base cases, which can be solved without recursion. 2) Making progress: For the cases to be solved recursively, the recursive call must always be to a case that makes progress toward a base case. Izmir University of Economics20

Recursion - Induction #include #define PrintDigit( Ch ) ( putchar( ( Ch ) + '0' ) ) /* Print nonnegative N */ void PrintOut( unsigned int N ) { if( N >= 10 ) PrintOut( N / 10 ); PrintDigit( N % 10 ); } main( ) { PrintOut( 1369 ); putchar( '\n' ); return 0; } 21Izmir University of Economics PrintOut(N) prints out positive integers. PrintDigit(Ch) take a single digit number as character and output it to the terminal Theorem: Recursive number-printing algorithm is correct for N ≥ 0. Proof (by induction): If N has one digit, then it is correct. Assume it works for all numbers of k or fewer digits. A number of k+1 digits is expressed by its first k digits followed by its least significant digit. By the inductive hypothesis, first part is printed correctly and then the last digit is appended.

3) Design rule: Assume that all the recursive calls work. This rule is important. It relieves you of the burden of thinking about the details of bookkeeping. 4) Compound interest rule: Never duplicate work by solving the same instance of a problem in separate calls. Hidden bookkeeping costs are mostly justifiable. However; It should never be used as a substitute for a simple loop. Izmir University of Economics22 Fundamental Rules of Recursion - II

C Style

Kth Fibonacci Numbers

kth Fibonacci Numbers int BinaryFib(k) { // Input: An integer k // Output: The kth Fibonacci number if (k <= 1) then return k ; else return BinaryFib(k-1)+BinaryFib(k-2); } Binary recursion

Recursion Calculate factorial (n!) of a positive integer: n! = n(n-1)(n-2)...(n-n-1), 0! = 1! = 1 int factorial(int n) { if (n <= 1) return 1; else return (n * factorial(n-1)); }

Homework Assignments 1.5, 1.7, 1.8.a, 1.8.b, 1.8.c, 1.9, and You are requested to study and solve the exercises. Note that these are for you to practice only. You are not to deliver the results to me. Izmir University of Economics27