MS 101: Algorithms Instructor Neelima Gupta

Slides:



Advertisements
Similar presentations
CSE Lecture 3 – Algorithms I
Advertisements

Hoare’s Correctness Triplets Dijkstra’s Predicate Transformers
MS 101: Algorithms Instructor Neelima Gupta
Selection and Insertion Sort Mrs. C. Furman October 1, 2008.
Chapter 2. Getting Started. Outline Familiarize you with the to think about the design and analysis of algorithms Familiarize you with the framework to.
Chapter 19: Searching and Sorting Algorithms
Insertion sort, Merge sort COMP171 Fall Sorting I / Slide 2 Insertion sort 1) Initially p = 1 2) Let the first p elements be sorted. 3) Insert the.
What is an Algorithm? (And how do we analyze one?)
Proving the Correctness of Algorithms Algorithm Design and Analysis Week 2
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 ’
Complexity Analysis (Part I)
Instruction count for statements Methods Examples
Searching Algorithms. Lecture Objectives Learn how to implement the sequential search algorithm Learn how to implement the binary search algorithm To.
Fall 2008 Insertion Sort – review of loop invariants.
B + -Trees (Part 1) COMP171. Slide 2 Main and secondary memories  Secondary storage device is much, much slower than the main RAM  Pages and blocks.
4/17/2017 Section 3.6 Program Correctness ch3.6.
What is an Algorithm? (And how do we analyze one?) COMP 122, Spring 04.
Copyright © Cengage Learning. All rights reserved. CHAPTER 11 ANALYSIS OF ALGORITHM EFFICIENCY ANALYSIS OF ALGORITHM EFFICIENCY.
Proofs, Recursion, and Analysis of Algorithms Mathematical Structures for Computer Science Chapter 2 Copyright © 2006 W.H. Freeman & Co.MSCS SlidesProofs,
Proving correctness. Proof based on loop invariants  an assertion which is satisfied before each iteration of a loop  At termination the loop invariant.
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.
Induction and recursion
Advance Data Structure 1 College Of Mathematic & Computer Sciences 1 Computer Sciences Department م. م علي عبد الكريم حبيب.
Instructor Neelima Gupta
Data Structures and Algorithms Lecture 5 and 6 Instructor: Quratulain Date: 15 th and 18 th September, 2009 Faculty of Computer Science, IBA.
Reading and Writing Mathematical Proofs
MCA 202: Discrete Mathematics Instructor Neelima Gupta
1 “Not all recursive solutions are better than iterative solutions…” “… recursion, however, can provide elegantly simple solutions to problems of great.
Chapter 1 Introduction. Goals Why the choice of algorithms is so critical when dealing with large inputs Basic mathematical background Review of Recursion.
1 Inference Rules and Proofs (Z); Program Specification and Verification Inference Rules and Proofs (Z); Program Specification and Verification.
Chapter 19: Searching and Sorting Algorithms
Jessie Zhao Course page: 1.
10/14/ Algorithms1 Algorithms - Ch2 - Sorting.
Lecture 1: Introduction and Overview CSCI 700 – Algorithms 1.
Merge sort, Insertion sort. Sorting I / Slide 2 Sorting * Selection sort (iterative, recursive?) * Bubble sort.
CSC 211 Data Structures Lecture 13
MS 101: Algorithms Instructor Neelima Gupta
MS 101: Algorithms Instructor Neelima Gupta
Reading and Writing Mathematical Proofs Spring 2015 Lecture 4: Beyond Basic Induction.
1 Section 2.1 Algorithms. 2 Algorithm A finite set of precise instructions for performing a computation or for solving a problem.
Recursive Algorithms &
Chapter 18: Searching and Sorting Algorithms. Objectives In this chapter, you will: Learn the various search algorithms Implement sequential and binary.
Searching & Sorting Programming 2. Searching Searching is the process of determining if a target item is present in a list of items, and locating it A.
Analysis of Algorithms CS 477/677 Instructor: Monica Nicolescu Lecture 3.
September 9, Algorithms and Data Structures Lecture II Simonas Šaltenis Nykredit Center for Database Research Aalborg University
More on Correctness. Prime Factorization Problem: Write a program that computes all the prime factors of a given number Solution (Idea): Factors are less.
Instructor Neelima Gupta Table of Contents Factor 2 algorithm for Bin Packing Factor 2 algorithm for Minimum Makespan Scheduling Reference:
COSC 3101NJ. Elder Announcements Midterms are marked Assignment 2: –Still analyzing.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 14 Searching and Sorting.
2IS80 Fundamentals of Informatics Fall 2015 Lecture 6: Sorting and Searching.
1 Ch. 2: Getting Started. 2 About this lecture Study a few simple algorithms for sorting – Insertion Sort – Selection Sort (Exercise) – Merge Sort Show.
CS 162 Intro to Programming II Insertion Sort 1. Assume the initial sequence a[0] a[1] … a[k] is already sorted k = 0 when the algorithm starts Insert.
Dr. Naveed Riaz Design and Analysis of Algorithms 1 1 Formal Methods in Software Engineering Lecture # 26.
Loop Invariants and Binary Search Chapter 4.4, 5.1.
CSC317 1 So far so good, but can we do better? Yes, cheaper by halves... orkbook/cheaperbyhalf.html.
 2004 SDU 1 Lecture5-Strongly Connected Components.
Lecture 2 What is a computational problem? What is an instance of a problem? What is an algorithm? How to guarantee that an algorithm is correct? What.
Chapter 3: Sorting and Searching Algorithms 3.1 Searching Algorithms.
1 Computer Algorithms Tutorial 2 Mathematical Induction Some of these slides are courtesy of D. Plaisted et al, UNC and M. Nicolescu, UNR.
Chapter 16: Searching, Sorting, and the vector Type.
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
ALGORITHMS PROVING ALGORITHMS (PROGRAMS) CORRECT WITH AND WITHOUT INDUCTION.
Analysis of Algorithms CS 477/677
Math/CSE 1019C: Discrete Mathematics for Computer Science Fall 2012
CS 3343: Analysis of Algorithms
Proving correctness.
Algorithms Take a look at the worksheet. What do we already know, and what will we have to learn in this term?
Program Correctness an introduction.
Presentation transcript:

MS 101: Algorithms Instructor Neelima Gupta

Table Of Contents Proving Correctness of Algorithms

Proving the correctness of Algorithm Sequential Search 1. index = 1; 2. While index ≤ n and L[index] ≠ x do index = index + 1; 3. if index > n then index = 0;

Defining the I/O of the algorithm Input : Given an array L containing n items (n ≥ 0) and given x, Output: –the sequential search algorithm terminates –with index = first occurrence of x in L, if found –and, index = 0 otherwise.

Loop Invariant Hypothesis: For 1 ≤ k ≤ n + 1, L(k): when the control reaches the test in line 2 for kth time –index = k and, –for 1 ≤ i ≤ k-1, L[i] ≠ x. Prove the above hypothesis by induction

Proving the Hypothesis by induction Base Case : H[1] is true vacuously. Let H(k) be true We will prove that H(k+1) is also true.

control reaches the test in line 2 for (k+1)th time only if L(k) ≠ x ………….….. (i) Also since H(k) is true 1 ≤ i ≤ k-1, L[i] ≠ x ………….. (ii) from (i) and (ii) we get, 1 ≤ i ≤ k, L[i] ≠ x ·˙· it holds for index = k+1 Thus by induction our hypothesis is true.

Correctness contd.. Suppose the test condition is executed exactly k times. i.e. body of the loop is executed k-1 times. –Case 1: k = n+1, by loop invariant hypothesis, index = n+ 1 and for 1 ≤ i ≤ n, L[i] ≠ x. Since index = n+ 1, line 3 sets index to 0 and by second condition above x is not in the array. So correct. –Case 2: k ≤ n, => index =k and loop terminated because L[k] = x. Thus index is the position of the first occurrence of x in the array. Hence the algorithm is correct in either case.

Binary Search Input : Given an array L containing n items (n ≥ 0) ordered such that L(1) <= L(2) <= L(3) <=… <=L(n) and given x, Output: The binary search algorithm terminates –with index = an occurrence of x in L, if found –and, index = 0 otherwise. Binary search can only be applied if the array to be searched is already sorted.

Search for the no

Search for the no

1.index_first = 1; 2.index_last=n; 3.While index_first<=index_last do 4.index_mid= floor((index_first+index_last)/2); 5.if L[index_mid]=x 6.Exit loop 7.if L[index_mid]>x 8.index_last=index_mid-1; 9.if L[index_mid]<x 10.index_first=index_mid+1; If index_first>index_last then 13.index_mid=0;

Proof of correctness The Loop Invariant Let r be the maximum number of times the loop starting from line 3 will run. Hypothesis: For 1 ≤ k ≤ r + 1, H(k): when the control reaches the test in line 3 for k th time –L[i] ≠ x for every i<first & for every i>last Prove the above hypothesis by induction

Correctness of the algorithm assuming the hypothesis (the loop invariant) Suppose the test condition is executed t times. –Case 1: first<=last. We exit from the loop because L[m t ]=x. Thus, mid is a position of occurrence of x as mid=m t. –Case 2: If first>last, we exit from the loop starting at line 3. Line 13 sets the value of mid to 0 since first>last. By loop invariant hypothesis, for i last, L[i] ≠ x i.e. x is not found in the array. The algorithm correctly returns 0 in mid. Hence assuming that the statement H(k) is correct the algorithm works correctly.

Proof of Induction Hypothesis Let f i and l i be the values of first and last when the test condition at line 3 is executed for the i th time with f 1 and l 1 being 1 and n respectively. Assume that the statement is true for H(k) We will prove that it is true for H(k+1) In the k th iteration either first was set to (f k + l k )/2 +1 i.e. f k+1 = (f k + l k )/2 +1 or last was set to (f k + l k )/2 -1 i.e l k+1 = (f k + l k )/2 -1

Proof of Induction Hypothesis contd.. Case 1: f k+1 > l k+1 ( only if f k = l k and L[(f k + l k )/2] ≠ x) By induction hypothesis L[i] ≠ x  i< f k L[i] ≠ x  i> l k (= f k ) And, also L[f k ] = L[(f k + l k )/2] ≠ x So x is not present, hence trivially L[i] ≠ x  i< f k+1 L[i] ≠ x  i> l k+1 Thus H(k+1) is true.

Proof of Induction Hypothesis contd.. Case 2: f k+1 <= l k+1 a:) when L[(f k + l k )/2] <x then f k+1 = (f k + l k )/2 +1  i< f k +1, L[i] ≤ L[(f k + l k )/2]<x (Why?)

Proof of Induction Hypothesis contd.. Case 2: f k+1 <= l k+1 a:) when L[(f k + l k )/2] <x then f k+1 = (f k + l k )/2 +1  i< f k +1, L[i] ≤ L[(f k + l k )/2]<x (input is sorted) i.e. L[i] ≠ x  i<= f k+1 – 1 or  I < f k+1 Also, by induction hypothesis L[i] ≠ x  i> l k (= l k+1 ) Thus H(k+1) is true.

Proof of Induction Hypothesis contd.. b:) When L[(f k + l k )/2] >x then l k+1 =(f k + l k )/2 -1  i> l k+1, L[i] ≥ L[(f k + l k )/2]>x (Why?) i.e. L[i] ≠ x  i> l k+1

Proof of Induction Hypothesis contd.. b:) When L[(f k + l k )/2] >x then l k+1 =(f k + l k )/2 -1  i> l k+1, L[i] ≥ L[(f k + l k )/2]>x (Why?) i.e. L[i] ≠ x  i> l k+1 Also, by induction hypothesis L[i] ≠ x  i< f k+1 (= f k ) Hence H(k+1) is true. This proves that our hypothesis is correct

Assignment 5 Show that Insertion sort works correctly by using Induction.

Up Next Introduction to some tools to designing algorithms through Sorting

The End