CE 221 Data Structures & Algorithms Chapter 1 Introduction

Slides:



Advertisements
Similar presentations
Copyright © Cengage Learning. All rights reserved. CHAPTER 5 SEQUENCES, MATHEMATICAL INDUCTION, AND RECURSION SEQUENCES, MATHEMATICAL INDUCTION, AND RECURSION.
Advertisements

Number Theory and Cryptography
Induction and recursion
1.  The set N = {1,2,3,4,……..} is known as natural numbers or the set of positive integers  The natural numbers are used mainly for :  counting  ordering.
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
Induction and recursion
BY MISS FARAH ADIBAH ADNAN IMK
Induction and recursion
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
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.
Chapter 1 Introduction. Goals Why the choice of algorithms is so critical when dealing with large inputs Basic mathematical background Review of Recursion.
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.
Recursion. What is recursion? Rules of recursion Mathematical induction The Fibonacci sequence Summary Outline.
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.
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.
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.
Chapter 15 Running Time Analysis. Topics Orders of Magnitude and Big-Oh Notation Running Time Analysis of Algorithms –Counting Statements –Evaluating.
MA/CSSE 473 Day 05 Factors and Primes Recursive division algorithm.
Fall 2002CMSC Discrete Structures1 Chapter 3 Sequences Mathematical Induction Recursion Recursion.
Chapter 5 1. Chapter Summary  Mathematical Induction  Strong Induction  Recursive Definitions  Structural Induction  Recursive Algorithms.
Proof And Strategies Chapter 2. Lecturer: Amani Mahajoub Omer Department of Computer Science and Software Engineering Discrete Structures Definition Discrete.
1 Proofs by Counterexample & Contradiction There are several ways to prove a theorem:  Counterexample: By providing an example of in which the theorem.
Hubert Chan (Chapters 1.6, 1.7, 4.1)
Recursion Powerful Tool
Direct Proof and Counterexample IV: Division into Cases and the Quotient-Remainder Theorem For each of the following values of n and d, find integers q.
Chapter 7: Sorting (Insertion Sort, Shellsort)
Recursion Version 1.0.
Chapter 4 (Part 3): Mathematical Reasoning, Induction & Recursion
MA/CSSE 473 Day 05 Factors and Primes Recursive division algorithm.
Math/CSE 1019C: Discrete Mathematics for Computer Science Fall 2012
Advanced Algorithms Analysis and Design
Handbook of Applied Cryptography - CH4, from 4.1~4.3
CSE373: Data Structures and Algorithms Lecture 3: Math Review; Algorithm Analysis Catie Baker Spring 2015.
MA/CSSE 473 Day 02 Some Numeric Algorithms and their Analysis
Chapter 3 The Real Numbers.
Induction and recursion
Algorithm Analysis CSE 2011 Winter September 2018.
Hubert Chan (Chapters 1.6, 1.7, 4.1)
CSE 20: Discrete Mathematics for Computer Science Prof. Shachar Lovett
Number Theory (Chapter 7)
Mathematical Induction Recursion
Chapter 5 Induction and Recursion
Proofs, Recursion and Analysis of Algorithms
CS 3343: Analysis of Algorithms
Induction and recursion
Data Structures Review Session
CS 201 Fundamental Structures of Computer Science
Applied Discrete Mathematics Week 9: Integer Properties
CSE 373: Data Structures & Algorithms
Elementary Number Theory & Proofs
CMSC 341 Math Review.
Mathematical Induction
Cryptography Lecture 16.
Algorithms and data structures: basic definitions
Presentation transcript:

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

Izmir University of Economics 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

Izmir University of Economics Introduction Mostly input size dominates the performance of an algorithm. Percieve 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 A short review of the recursion Izmir University of Economics

Motivating Examples: Selection Selection problem: you have a group of N numbers and would like to determine the kth largest. 1) Read them into an array. Sort them in decreasing order. Return the kth element. 2) 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 of these algorithms requires several days of computer processing. Izmir University of Economics

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. 1) for each word in the word list, check (row, column, orientation) 2) for each ordered fourfold (row, column, orientation, number of characters), test whether the word is in the word list. {this, two, fat, that} Izmir University of Economics

Math Review - Exponents XAXB = XA+B XA/XB = XA-B (XA)B = XAB XN+XN = 2XN !=X2N Izmir University of Economics

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

Math Review – Logarithms II log A/B = logA – logB log(AB)=BlogA logX < X for all X > 0 log1 = 0, log2 = 1, log1024 = 10 Izmir University of Economics

Izmir University of Economics 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+A2+A3+A4... AS=A+A2+A3+A4+A5... S-AS= 1 which implies S=1/(1-A) Izmir University of Economics

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

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

Math Review – Modular Arithmetic If two numbers b and c have the property that their difference b-c is integrally divisible by a number m (i.e., (b-c)/m is an integer), then b and c are said to be "congruent modulo m." The number m is called the modulus, and the statement "b is congruent to c (modulo m)" is written mathematically as If b-c is not integrally divisible by m, then it is said that "b is not congruent to c (modulo m)," which is written b≢c (mod m). 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

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

The P Word – Induction Example I Example: Prove that Fibonacci Numbers F0=1, F1=1, and Fi=Fi-1+ Fi-2 for i > 1, satisfy Fi < (5/3)i for i ≥ 1. Proof: Verify that the theorem is true for the trivial cases (base cases): F1=1 < (5/3)1 and, F2 = 2 < (5/3)2 = 3.66666 These prove the base case. 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 Fk+1<(5/3)k+1. Izmir University of Economics

The P Word – Induction Example II 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 Izmir University of Economics

Proof by Counterexample Proof by Counterexample: Best way for proving that a statement is false. Example: The statement Fk ≤ k2 is false. The easiest way to prove this is to compute F11 = 144 > 112 = 121 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 Pk. Let P1, P2, ..., Pk be all the primes in order and consider N = P1P2...Pk + 1. Clearly, N > Pk, so by assumption N can not be prime. However, none of P1, P2, ..., Pk 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. Izmir University of Economics

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 <stdio.h> public static int f( int X ) { if( X == 0 ) /* base case */ return 0; else return 2 * F( X - 1 ) + X * X; } 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

Izmir University of Economics Recursion - Bad 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 #include <stdio.h> public static int bad(int N ) { if( N == 0 ) return 0; else return Bad( N / 3 + 1 ) + N - 1; } Izmir University of Economics

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 Economics

Izmir University of Economics Recursion - Induction PrintOut(N) prints out positive integers. PrintDigit(Ch) take a single digit number as character and output it to the terminal /* Print nonnegative single digit given as a number N */ public static void PrintOut(int N ) { if( N >= 10 ) PrintOut( N / 10 ); PrintDigit( N % 10 ); } 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. Izmir University of Economics

Fundamental Rules of Recursion - II 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 Economics

Izmir University of Economics Homework Assignments 1.5, 1.7, 1.8.a, 1.8.b, 1.8.c, 1.9, and 1.12. 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 Economics