Math/CSE 1019C: Discrete Mathematics for Computer Science Fall 2012

Slides:



Advertisements
Similar presentations
Hoare’s Correctness Triplets Dijkstra’s Predicate Transformers
Advertisements

What is an Algorithm? (And how do we analyze one?)
CSE115/ENGR160 Discrete Mathematics 04/12/11 Ming-Hsuan Yang UC Merced 1.
Sorting. Input: A sequence of n numbers a 1, …, a n Output: A reordering a 1 ’, …, a n ’, such that a 1 ’ < … < a n ’
CS421 - Course Information Website Syllabus Schedule The Book:
CS3381 Des & Anal of Alg ( SemA) City Univ of HK / Dept of CS / Helena Wong 2. Analysis of Algorithms - 1 Analysis.
Copyright © 2006 The McGraw-Hill Companies, Inc. Programming Languages 2nd edition Tucker and Noonan Chapter 18 Program Correctness To treat programming.
Fall 2008 Insertion Sort – review of loop invariants.
4/17/2017 Section 3.6 Program Correctness ch3.6.
CS Main Questions Given that the computer is the Great Symbol Manipulator, there are three main questions in the field of computer science: What kinds.
Analysis of Algorithms CS 477/677
Proving correctness. Proof based on loop invariants  an assertion which is satisfied before each iteration of a loop  At termination the loop invariant.
Lecture 4 Discrete Mathematics Harper Langston. Algorithms Algorithm is step-by-step method for performing some action Cost of statements execution –Simple.
CS 202, Spring 2003 Fundamental Structures of Computer Science II Bilkent University1 Sorting CS 202 – Fundamental Structures of Computer Science II Bilkent.
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.
Reading and Writing Mathematical Proofs
1 Program Correctness CIS 375 Bruce R. Maxim UM-Dearborn.
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.
1 Inference Rules and Proofs (Z); Program Specification and Verification Inference Rules and Proofs (Z); Program Specification and Verification.
Jessie Zhao Course page: 1.
BY Lecturer: Aisha Dawood.  an algorithm is any well-defined computational procedure that takes some value, or set of values, as input and produces.
MS 101: Algorithms Instructor Neelima Gupta
Introduction to Data Structures and Algorithms CS 110: Data Structures and Algorithms First Semester,
Introduction to Problem Solving. Steps in Programming A Very Simplified Picture –Problem Definition & Analysis – High Level Strategy for a solution –Arriving.
Reading and Writing Mathematical Proofs Spring 2015 Lecture 4: Beyond Basic Induction.
September 17, 2001 Algorithms and Data Structures Lecture II Simonas Šaltenis Nykredit Center for Database Research Aalborg University
Recursive Algorithms &
Reasoning about programs March CSE 403, Winter 2011, Brun.
September 9, Algorithms and Data Structures Lecture II Simonas Šaltenis Nykredit Center for Database Research Aalborg University
Algorithms 1.Notion of an algorithm 2.Properties of an algorithm 3.The GCD algorithm 4.Correctness of the GCD algorithm 5.Termination of the GCD algorithm.
Time Complexity. Solving a computational program Describing the general steps of the solution –Algorithm’s course Use abstract data types and pseudo code.
13 Aug 2013 Program Verification. Proofs about Programs Why make you study logic? Why make you do proofs? Because we want to prove properties of programs.
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.
1 Ch. 2: Getting Started. 2 About this lecture Study a few simple algorithms for sorting – Insertion Sort – Selection Sort (Exercise) – Merge Sort Show.
Loop Invariants and Binary Search Chapter 4.4, 5.1.
1 Computer Algorithms Tutorial 2 Mathematical Induction Some of these slides are courtesy of D. Plaisted et al, UNC and M. Nicolescu, UNR.
5-5 Indirect Proof. Indirect Reasoning: all possibilities are considered and then all but one are proved false. The remaining possibility must be true.
Chapter 15 Running Time Analysis. Topics Orders of Magnitude and Big-Oh Notation Running Time Analysis of Algorithms –Counting Statements –Evaluating.
September 18, Algorithms and Data Structures Lecture II Simonas Šaltenis Aalborg University
Hubert Chan (Chapters 1.6, 1.7, 4.1)
Chapter 4 (Part 3): Mathematical Reasoning, Induction & Recursion
CSE15 Discrete Mathematics 04/12/17
CSE15 Discrete Mathematics 03/13/17
Data Structures and Algorithms
Reasoning About Code.
Reasoning about code CSE 331 University of Washington.
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
CS 583 Fall 2006 Analysis of Algorithms
Enough Mathematical Appetizers!
CS 3343: Analysis of Algorithms
Computation.
Analysis of Algorithms CS 477/677
CS 3343: Analysis of Algorithms
Proving correctness.
Introduction to Algorithms Analysis
Programming Languages 2nd edition Tucker and Noonan
Applied Discrete Mathematics Week 6: Computation
Algorithms: the big picture
Enough Mathematical Appetizers!
Ch. 2: Getting Started.
Math/CSE 1019N: Discrete Mathematics for Computer Science Winter 2007
Analysis of Algorithms
CS 583 Analysis of Algorithms
Programming Languages 2nd edition Tucker and Noonan
Discrete Mathematics CS 2610
Algorithms and Data Structures Lecture II
Program Correctness an introduction.
Presentation transcript:

Math/CSE 1019C: Discrete Mathematics for Computer Science Fall 2012 Jessie Zhao jessie@cse.yorku.ca Course page: http://www.cse.yorku.ca/course/1019

Introduction to Algorithms Why algorithms? Consider the problems as special cases of general problems. Searching for an element in any given list Sorting any given list, so its elements are in increasing/decreasing order What is an algorithm? For this course: detailed pseudocode, or a detailed set of steps

Algorithm (topics) Design of Algorithms (for simple problems) Analysis of algorithms – Is it correct? Loop invariants – Is it “good”? Efficiency – Is there a better algorithm? Lower bounds

Can we do it without using tmp ? Problem: Swapping two numbers in memory INPUT: x=a, y=b OUTPUT: x and y such that x=b and y=a. tmp = x; x = y; y = tmp; Can we do it without using tmp ? x = x+y; y = x-y; x= x-y; Why does this work? Does it always work?

Problem: How do you find the max of n numbers (stored in array A?) INPUT: A[1..n] - an array of integers OUTPUT: an element m of A such that A[j] ≤ m, 1 ≤ j ≤ length(A) Find-max (A) 1. max←A[1] 2. for j ← 2 to n 3. do if (max < A[j]) 4. max ← A[j] 5. return max

Reasoning (formally) about algorithms 1. I/O specs: Needed for correctness proofs, performance analysis. INPUT: A[1..n] - an array of integers OUTPUT: an element m of A such that A[j] ≤ m, 1 ≤ j ≤ length(A) 2. CORRECTNESS: The algorithm satisfies the output specs for EVERY valid input 3. ANALYSIS: Compute the running time, the space requirements, number of cache misses, disk accesses, network accesses,….

Correctness proofs for conditional statements If (condition), do (S) (p∧condition){S}q (p∧˥condition)→q ∴ p{If (condition), do (S)}q Note: p{S}q means whenever p is true for the input values of S and S terminates, then q is true for the output values of S.

Correctness proofs for conditional statements If (condition), do (S₁); else do (S₂) (p∧condition){S₁}q (p∧˥condition){S₂}q ∴ p {If (condition), do (S₁); else do (S₂)} q

Example: partial algorithm for Find-max p: T q: max≥A[j] Example: If x<0 then abs:= -x else abs:=x q: abs=|x|

Correctness proofs of algorithms Prove that for any valid Input, the output of Find-max satisfies the output condition. Proof by contradiction: Suppose the algorithm is incorrect. Then for some input A, Case 1: max is not an element of A. max is initialized to and assigned to elements of A – (a) is impossible. Case 2: (∃ j | max < A[j]). After the jth iteration of the for-loop (lines 2 – 4), max ≥ A[j]. From lines 3,4, max only increases. Therefore, upon termination, max ≥ A[j], which contradicts (b).

∴ p{while condition do S} (˥condition ∧ p) Correctness proofs using loop invariants while (condition), do (S) Loop invariant An assertion that remains true each time S is executed. p is a loop invariant if (p∧condition){S}p is true. p is true before S is executed. q and ˥condition are true after termination. (p∧condition){S}p ∴ p{while condition do S} (˥condition ∧ p)

Loop invariant proofs Prove that for any valid Input, the output of Find-max satisfies the output condition. Proof by loop invariants: Loop invariant: I(j): At the beginning of iteration j of the loop, max contains the maximum of A[1,..,j-1]. Proof: True for j=2. Assume that the loop invariant holds for the j iteration, So at the beginning of iteration k, max = maximum of A[1,..,j-1].

Loop invariant proofs For the (j+1)th iteration Case 1: A[j] is the maximum of A[1,…,j]. In lines 3, 4, max is set to A[j]. Case 2: A[j] is not the maximum of A[1,…,j]. So the maximum of A[1,…, j] is in A[1,…,j-1]. By our assumption max already has this value and by lines 3-4 max is unchanged in this iteration.

Loop invariant proofs STRATEGY: We proved that the invariant holds at the beginning of iteration j for each j used by Find-max. Upon termination, j = length(A)+1. (WHY?) The invariant holds, and so max contains the maximum of A[1..n]

Loop invariant proofs Advantages: Structured proof technique Rather than reason about the whole algorithm, reason about SINGLE iterations of SINGLE loops. Structured proof technique Usually prove loop invariant via Mathematical Induction.