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.

Slides:



Advertisements
Similar presentations
Divide and Conquer (Merge Sort)
Advertisements

EECS 311: Chapter 2 Notes Chris Riesbeck EECS Northwestern.
22C:19 Discrete Structures Induction and Recursion Spring 2014 Sukumar Ghosh.
22C:19 Discrete Structures Induction and Recursion Fall 2014 Sukumar Ghosh.
Comp 122, Spring 2004 Binary Search Trees. btrees - 2 Comp 122, Spring 2004 Binary Trees  Recursive definition 1.An empty tree is a binary tree 2.A node.
Recursive Definitions and Structural Induction
Analysis of Algorithms
Comp 122, Spring 2004 Divide and Conquer (Merge Sort)
11 Computer Algorithms Lecture 6 Recurrence Ch. 4 (till Master Theorem) Some of these slides are courtesy of D. Plaisted et al, UNC and M. Nicolescu, UNR.
CSE332: Data Abstractions Lecture 2: Math Review; Algorithm Analysis Tyler Robison Summer
CSE332: Data Abstractions Lecture 2: Math Review; Algorithm Analysis Dan Grossman Spring 2010.
Proof Techniques and Recursion. Proof Techniques Proof by induction –Step 1: Prove the base case –Step 2: Inductive hypothesis, assume theorem is true.
DAST 2005 Week 4 – Some Helpful Material Randomized Quick Sort & Lower bound & General remarks…
Recurrences Part 3. Recursive Algorithms Recurrences are useful for analyzing recursive algorithms Recurrence – an equation or inequality that describes.
22C:19 Discrete Math Induction and Recursion Fall 2011 Sukumar Ghosh.
Discrete Mathematics CS 2610 March 26, 2009 Skip: structural induction generalized induction Skip section 4.5.
Lecture 9. Arithmetic and geometric series and mathematical induction
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.
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!
Discrete Maths Objective to introduce mathematical induction through examples , Semester 2, Mathematical Induction 1.
Analyzing Recursive Algorithms A recursive algorithm can often be described by a recurrence equation that describes the overall runtime on a problem of.
CS 3343: Analysis of Algorithms
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.
Project 2 due … Project 2 due … Project 2 Project 2.
Fundamentals CSE 373 Data Structures Lecture 5. 12/26/03Fundamentals - Lecture 52 Mathematical Background Today, we will review: ›Logs and exponents ›Series.
Divide & Conquer  Themes  Reasoning about code (correctness and cost)  recursion, induction, and recurrence relations  Divide and Conquer  Examples.
Week 12 - Wednesday.  What did we talk about last time?  Asymptotic notation.
Discrete Maths: Induction/1 1 Discrete Maths Objective – –to introduce mathematical induction through examples , Semester
Induction Proof. Well-ordering A set S is well ordered if every subset has a least element. [0, 1] is not well ordered since (0,1] has no least element.
Analysis of Algorithms CS 477/677 Instructor: Monica Nicolescu Lecture 9.
CSC401 – Analysis of Algorithms Lecture Notes 2 Asymptotic Analysis Objectives: Mathematics foundation for algorithm analysis Amortization analysis techniques.
Recursion Algorithm : Design & Analysis [3]. In the last class… Asymptotic growth rate The Sets ,  and  Complexity Class An Example: Maximum Subsequence.
CSE373: Data Structures and Algorithms Lecture 3: Math Review; Algorithm Analysis Nicki Dell Spring 2014.
Analysis of Algorithms CS 477/677 Instructor: Monica Nicolescu Lecture 3.
Cpt S 223 – Advanced Data Structures Math Review 2
Chapter 1: Introduction
1 Recursive Definitions and Structural Induction CS 202 Epp section ??? Aaron Bloomfield.
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.
CSC 413/513: Intro to Algorithms Introduction Proof By Induction Asymptotic notation.
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.
Divide & Conquer Themes –Reasoning about code (correctness and cost) –iterative code, loop invariants, and sums –recursion, induction, and recurrence relations.
TCSS 342 Autumn 2004 Version TCSS 342 Data Structures & Algorithms Autumn 2004 Ed Hong.
Mathematical Induction 1. 2 Suppose we have a sequence of propositions which we would like to prove: P (0), P (1), P (2), P (3), P (4), … P (n), … We.
CSE373: Data Structures and Algorithms Lecture 3: Math Review; Algorithm Analysis Linda Shapiro Winter 2015.
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.
Advanced Algorithms Analysis and Design By Dr. Nazir Ahmad Zafar Dr Nazir A. Zafar Advanced Algorithms Analysis and Design.
CSE373: Data Structures and Algorithms Lecture 2: Math Review; Algorithm Analysis Kevin Quinn Fall 2015.
MATH 224 – Discrete Mathematics
Analysis of Algorithms CS 477/677
DAST Tirgul 2.
CSE373: Data Structures and Algorithms Lecture 3: Math Review; Algorithm Analysis Catie Baker Spring 2015.
CS 3343: Analysis of Algorithms
CS2210:0001Discrete Structures Induction and Recursion
CS 3343: Analysis of Algorithms
Introduction to Algorithms Analysis
CSE373: Data Structures and Algorithms Lecture 2: Math Review; Algorithm Analysis Dan Grossman Fall 2013.
CSE 373 Data Structures Lecture 5
Applied Discrete Mathematics Week 9: Integer Properties
CSE 373: Data Structures & Algorithms
Mathematical Induction
Mathematical Background 2
Chapter 11: Further Topics in Algebra
Math/CSE 1019: Discrete Mathematics for Computer Science Fall 2011
11.4 Mathematical Induction
Presentation transcript:

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  0 for i  0 to n – 1 do temp_sum  temp_sum + v[i] return temp_sum

3 Programming Using Recursion Write a recursive function that solves the same problem: sum (v[], n) if (n = 0) then return 0 else return (v[n-1] + sum (v, n-1))

4 Review: Induction Suppose S(c) is true for a fixed constant c Often c = 0 S(k) implies S(k+1) for all k >= c Then S(n) is true for all n >= c

5 Proof By Induction Claim: S(n) is true for all n >= c Basis: Show S(n) is true for n = c Inductive hypothesis: Assume S(n) is true for n = k Step: Show that S(n) is then true for n = k + 1

6 Induction Example: Geometric Closed Form Prove: x 0 + x 1 + … + x n = (x n+1 – 1) / (x – 1) for all x  1 Basis: show that x 0 = (x ) / (x - 1): x 0 = 1 = (x 1 – 1) / (x – 1) Inductive hypothesis: assume that x 0 + x 1 + … + x k = (x k+1 – 1) / (x – 1)

7 Induction Example: Geometric Closed Form Step (show true for n+1): x 0 + x 1 + … + x k+1 = (x 0 + x 1 + … + x k ) + x k+1 = (by hypothesis) (x k+1 – 1) / (x – 1) + x k+1 = (x k+1 – 1 + x k+1  (x – 1)) / (x – 1) = (x k+1 – 1 + x k+2 – x k+1 ) / (x – 1) = (x k+2 – 1) / (x – 1)

8 Proving Program Correctness Using Induction Basis Step: sum (v, 0) = 0 Inductive Hypothesis (n = k): Assume that sum (v, k) correctly returns the sum of the first k elements of v, i.e. v[0] + v[1] + … + v[k-1] Inductive Step (n = k + 1): sum (v, n) returns v[k] + sum (v, k) which is the sum of the first k+1 elements of v

9 Powers of 2 Many of the numbers we use will be powers of 2 Binary numbers (base 2) are easily represented in digital computers Each "bit" is a 0 or a =1, 2 1 =2, 2 2 =4, 2 4 =16, 2 8 =256, … An n-bit wide field can hold 2 n positive integers: 0  k  2 n -1

10 Unsigned Binary Numbers Each bit represents a power of 2 For unsigned numbers: The minimum value is 0 The maximum value is 2 n -1, where n is the number of bits So, for example 5 bits => 32 possible values (2 5 ) 10 bits => 1024 possible values (2 10 )

11 Binary and Decimal 2 0 =1 2 1 =2 2 2 =4 2 3 =8 2 4 =162 5 = =642 7 = =256 Decimal

12 Logs and Exponents Definition: log 2 x = y means x = 2 y The log of x in base 2, is the value of y that gives x = 2 y 8 = 2 3, so log 2 8 = = 2 16, so log = 16 Notice that log 2 x tells you how many bits are needed to hold x values 8 bits hold 256 numbers: 0 to 255 (2 8 -1) log = 8

13 2 x and log 2 x – for Small x ’ s

14 2 x and log 2 x – for Large x ’ s

15 Floor and Ceiling – Floor function: the largest integer < X – Ceiling function: the smallest integer > X

16 Floor and Ceiling Properties: More examples:

17 Example: log 2 x and Tree Height n items in an almost full binary tree, the tree height (length of longest path) is  log 2 n 

18 Properties of logs Usually, we work in log base 2 In base 2: a = 2 log 2 a log 2 a n = n · log 2 a Similarly, in any base b: a = b log b a log b a n = n · log b a

19 Properties of logs Claim: log a · b = log a + log b Proof: a = 2 log 2 a and b = 2 log 2 b a · b = 2 log 2 a · 2 log 2 b = 2 log 2 a+log 2 b Therefore: log 2 a · b = log 2 a + log 2 b Note: log a · b  log a · log b

20 Other log Properties log a/b = log a – log b Special case: log 2 1/a = – log 2 a Base change: log a n = log b n / log b a log log X 0 log log X = Y means log X grows slower than X Called a “ sub-linear ” function

21 Log Base Change Any base x log is equivalent to base 2 log within a constant factor Example: log 10 n = log 2 n / log 2 10 log 2 10 = 10/3 Therefore: log 10 n = 0.3  log 2 n

22 Monotonic Functions A function is called monotonically increasing if for all x, y such that x > y: f(x) > f(y) Example: f(x) = x A function is called monotonically non- decreasing if for all x, y such that x > y: f(x) ≥ f(y) Example: f(x) = 5

23 Monotonic Functions A function is called monotonically decreasing if for all x, y such that x > y: f(x) < f(y) Example: f(x) = -x A function is called monotonically non- increasing if for all x, y such that x > y: f(x) ≤ f(y)

24 Monotonic Functions monotonically non-decreasing monotonically non-increasing neither

25 Monotonic Functions – Examples f(x) = x 2 Decreasing for x 0, therefore does not fit any definition Monotonous non-decreasing

26 Arithmetic Series The sum is S(1) = 1 S(2) = = 3 S(3) = = 6

27 Algorithm Analysis Consider the following program segment: x  0 for i  1 to n do for j  1 to i do x  x + 1 What is the value of x at the end?

28 Analyzing the Loop Total number of times x is incremented: The running time of the program is proportional to n(n+1)/2 for all n O(n 2 )

29 Geometric Series General geometric series: Common special case, for x = 2:

30 Infinite Geometric Series for |x| < 1 When |x| < 1, we can compute the sum of an infinite geometric series: Example, x = ½: