DAST 2005 Tirgul 6 Heaps Induction. DAST 2005 Heaps A binary heap is a nearly complete binary tree stored in an array object In a max heap, the value.

Slides:



Advertisements
Similar presentations
Introduction to Algorithms Quicksort
Advertisements

22C:19 Discrete Structures Induction and Recursion Fall 2014 Sukumar Ghosh.
Discussion #33 Adjacency Matrices. Topics Adjacency matrix for a directed graph Reachability Algorithmic Complexity and Correctness –Big Oh –Proofs of.
Shellsort. Review: Insertion sort The outer loop of insertion sort is: for (outer = 1; outer < a.length; outer++) {...} The invariant is that all the.
CSE115/ENGR160 Discrete Mathematics 04/12/11 Ming-Hsuan Yang UC Merced 1.
Fall 2007CS 2251 Proof by Induction. Fall 2007CS 2252 Proof by Induction There are two forms of induction Standard Induction –Prove the theorem is true.
Simple Sorting Algorithms
Discussion #131/18 Discussion #13 Induction (the process of deriving generalities from particulars) Mathematical Induction (deductive reasoning over the.
Insertion Sorting Lecture 21. Insertion Sort Start from element 2 of list location 1 –In first iteration: Compare element 1 with all of its elements to.
Fall 2008 Insertion Sort – review of loop invariants.
4/17/2017 Section 3.6 Program Correctness ch3.6.
Proving correctness. Proof based on loop invariants  an assertion which is satisfied before each iteration of a loop  At termination the loop invariant.
Simple Sorting Algorithms. 2 Bubble sort Compare each element (except the last one) with its neighbor to the right If they are out of order, swap them.
Induction and recursion
Simple Sorting Algorithms. 2 Outline We are going to look at three simple sorting techniques: Bubble Sort, Selection Sort, and Insertion Sort We are going.
Induction and recursion
Inductive Proof (the process of deriving generalities from particulars) Mathematical Induction (reasoning over the natural numbers)
Reading and Writing Mathematical Proofs
CSNB143 – Discrete Structure Topic 5 – Induction Part I.
Section 1.8. Section Summary Proof by Cases Existence Proofs Constructive Nonconstructive Disproof by Counterexample Nonexistence Proofs Uniqueness Proofs.
Chapter 3 (Part 3): Mathematical Reasoning, Induction & Recursion  Recursive Algorithms (3.5)  Program Correctness (3.6)
Section 5.3. Section Summary Recursively Defined Functions Recursively Defined Sets and Structures Structural Induction.
Section 3.1: Proof Strategy Now that we have a fair amount of experience with proofs, we will start to prove more difficult theorems. Our experience so.
Foundations of Discrete Mathematics Chapters 5 By Dr. Dalia M. Gil, Ph.D.
MS 101: Algorithms Instructor Neelima Gupta
Reading and Writing Mathematical Proofs Spring 2015 Lecture 4: Beyond Basic Induction.
Data Structure Introduction.
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.
Copyright © Zeph Grunschlag, Induction Zeph Grunschlag.
Section 3.3: Mathematical Induction Mathematical induction is a proof technique that can be used to prove theorems of the form:  n  Z +,P(n) We have.
Lecture No. 04,05 Sorting.  A process that organizes a collection of data into either ascending or descending order.  Can be used as a first step for.
CS 103 Discrete Structures Lecture 13 Induction and Recursion (1)
Foundations of Discrete Mathematics Chapters 5 By Dr. Dalia M. Gil, Ph.D.
Inductive Proofs and Inductive Definitions Jim Skon.
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.
2IS80 Fundamentals of Informatics Fall 2015 Lecture 6: Sorting and Searching.
Copyright © Zeph Grunschlag, Induction Zeph Grunschlag.
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.
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.
Discrete Maths: Invariant/2 1 Discrete Maths Objectives – –to show the use of induction for proving properties of code involving loops use induction.
1 Computer Algorithms Tutorial 2 Mathematical Induction Some of these slides are courtesy of D. Plaisted et al, UNC and M. Nicolescu, UNR.
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 a particular.
Algorithmic Foundations COMP108 COMP108 Algorithmic Foundations Mathematical Induction Prudence Wong
Chapter 15 Running Time Analysis. Topics Orders of Magnitude and Big-Oh Notation Running Time Analysis of Algorithms –Counting Statements –Evaluating.
Help Session 3: Induction and Complexity Ezgi, Shenqi, Bran.
Section 1.7. Section Summary Mathematical Proofs Forms of Theorems Direct Proofs Indirect Proofs Proof of the Contrapositive Proof by Contradiction.
Section Recursion 2  Recursion – defining an object (or function, algorithm, etc.) in terms of itself.  Recursion can be used to define sequences.
Copyright © Zeph Grunschlag, Induction Zeph Grunschlag.
Chapter 5 1. Chapter Summary  Mathematical Induction  Strong Induction  Recursive Definitions  Structural Induction  Recursive Algorithms.
ALGORITHMS PROVING ALGORITHMS (PROGRAMS) CORRECT WITH AND WITHOUT INDUCTION.
DAST Tirgul 7.
Shellsort.
COMP108 Algorithmic Foundations Mathematical Induction
Math/CSE 1019C: Discrete Mathematics for Computer Science Fall 2012
Simple Sorting Algorithms
CS2210:0001Discrete Structures Induction and Recursion
Chapter 3 The Real Numbers.
CSE 311: Foundations of Computing
Selection sort Given an array of length n,
Multi-Way Search Trees
Mathematical Induction
Induction (Section 3.3).
Simple Sorting Algorithms
Induction Chapter
Simple Sorting Algorithms
Mathematical Induction
Simple Sorting Algorithms
COMP108 Algorithmic Foundations Mathematical Induction
Program Correctness an introduction.
Presentation transcript:

DAST 2005 Tirgul 6 Heaps Induction

DAST 2005 Heaps A binary heap is a nearly complete binary tree stored in an array object In a max heap, the value of each node ≥ that of its children (In a min heap, the value of each node ≤ that of its children) Since the height of a heap containing n elements is Θ(log(n)) the basic operations on a heap run in time that is proportional to the heaps height and thus take O(log(n))

DAST 2005 Heaps

DAST 2005 Heaps

DAST 2005 Mathematical Induction Mathematical induction is a method of mathematical proof typically used to establish that a given statement is true for all natural numbers, or for all members of an infinite sequence The simplest and most common form of mathematical induction proves that a statement holds for all natural numbers n and consists of two steps: 1.The basis: showing that the statement holds when n = 0. 2.The inductive step: showing that if the statement holds for n = k, it also holds for n = k + 1.

DAST 2005 Mathematical Induction This method works by: 1.First proving the statement is true for a starting value 2.Then proving that the process used to go from one value to the next is valid. If these are both proven, then any value can be obtained by performing the process repeatedly For example, suppose we have a long row of dominos standing, and we can be sure that: 1.The first domino will fall. 2.Whenever a domino falls, its next neighbor will also fall.  We can conclude that all dominos will fall.

DAST 2005 Mathematical Induction Suppose we wish to prove the following statement: Proof by induction: Check if it is true for n = 0 Assume the statement is true for n = m Adding m+1 to both sides gives:

DAST 2005 Mathematical Induction Note that it has not been proven as true: we made the assumption that S(m) is true, and from that assumption we derived S(m+1). Symbolically, we have shown that: However, by induction, we may now conclude that the statement S(n) holds for all natural numbers n. we have showed S(0) we have showed that S(0)→S(1) we have showed that S(1)→S(2) etc.

DAST 2005 Generalization A common generalization is proving that a claim is true for all n ≥ c 1.Showing that the statement holds when n = c 2.Showing that if the statement holds for n = m ≥ c, it also holds for n = m+1 This can be used for showing that n 2 ≥ 10n for all n ≥ 10 1.For n==10, n 2 = 10*10 = 10n 2.Assuming the statement holds for n = m ≥ 10, we get:

DAST 2005 Complete Induction Another form of mathematical induction is the complete-induction (also called strong-induction ) In complete induction, the inductive hypothesis, instead of simply is (we have a stronger inductive hypothesis, hence the name strong-induction ) The complete induction steps are therefore: 1.Showing that the statement holds for n = 0 (or n = c ) 2.Showing that if the statement holds for all c ≤ n ≤ m then the same statement also holds for n = m+1

DAST 2005 The game of Nin Rules: Two players throw a number of stones on the ground At each turn, a player removes one two or three stones The one to remove the last stones loses Proposition: The second player has a winning strategy iff the number of stones is 4k+1, otherwise the first player has a winning strategy Proof: Base case – there is only one stone, the second player wins ( 1=4k+1 ) Induction: Assume that P(n) is true for all 1≤n≤m and prove that P(n+1) is true as well

DAST 2005 The game of Nin We have four possible cases: 1.n+1 = 4k+1, We have already showed P(1) to be true, so we assume that n+1≥ 5. The first player can lift either one, two, or three stones, leaving either 4k,4(k−1)+3,4(k−1)+2 respectively. By the induction hypothesis, the person who plays next has a winning strategy. 2.n+1 = 4k, The first player can remove just three stones, leaving n = 4(k−1)+1. The strong induction hypothesis asserts that the second player loses. 3.n+1 = 4k+2, The first player can remove just one stone, leaving n = 4k + 1. The strong induction hypothesis asserts that the second player loses. 4.n+1 = 4k+3, The first player can remove two stones, leaving n = 4k + 1. The strong induction hypothesis asserts that the second player loses.

DAST 2005 Loop Invariants A loop invariant is statement that is true when a program enters a loop, remains true in each iteration of the body of the loop, and is still true when control exits the loop. Understanding loop invariants can help us analyze programs, check for errors, and derive programs from specifications. The loop invariant technique is a derived from the mathematical induction: 1.We first check that the statement is true when the loop is first entered 2.We then verify that if the invariant is true after n times around the loop, it also true after n+1 times

DAST 2005 Loop Invariants public static int factorial(int num) { int P = 1; int C = 0; while (C < num) { // P = C! (our loop invariant) C++; P *= C; } // P = C! (our loop invariant) return (P); } Our loop invariant is true when we first enter and last leave the loop

DAST 2005 Insertion sort The outer loop of insertion sort is: for (outer = 1; outer < a.length; outer++) {...} The invariant is that all the elements to the left of outer are sorted with respect to one another For all i < outer, j < outer, if i < j then a[i] <= a[j] This does not mean they are all in their final correct place; the remaining array elements may need to be inserted When we increase outer, a[outer-1] becomes to its left; we must keep the invariant true by inserting a[outer-1] into its proper place This means: 1.Finding the element’s proper place 2.Making room for the inserted element (by shifting over other elements) 3.Inserting the element

DAST 2005 Code for Insertion Sort public static void insertionSort(int[] array) int inner, outer; for(outer=1; outer 0 && array[inner-1] >= temp) { array[inner] = array[inner - 1]; inner--; } array[inner] = temp; // Invariant: For all i < outer, j < outer, if i < j then a[i] ≤ a[j] } }

DAST 2005 Insertion Sort - Loop Invariants The loop invariant is true before the loop is first executed If the loop invariant holds after n times around the loop, the inner loop makes sure we insert the n+1 element in place, therefore the loop invariant also true after n+1 times