Outline. 1 The Searching Problem. Unsorted Arrays. 3 Sorted Arrays. LinearSearch(k)

Similar documents
Loop Invariants and Binary Search

Analysis of Binary Search algorithm and Selection Sort algorithm

Lecture Notes on Linear Search

Computer Science 210: Data Structures. Searching

Binary Search Trees CMPSC 122

Zabin Visram Room CS115 CS126 Searching. Binary Search

Outline. Computer Science 331. Stack ADT. Definition of a Stack ADT. Stacks. Parenthesis Matching. Mike Jacobson

Section IV.1: Recursive Algorithms and Recursion Trees

CSE373: Data Structures and Algorithms Lecture 3: Math Review; Algorithm Analysis. Linda Shapiro Winter 2015

CSC 180 H1F Algorithm Runtime Analysis Lecture Notes Fall 2015

Outline. 1 Denitions. 2 Principles. 4 Implementation and Evaluation. 5 Debugging. 6 References

Data Structures and Algorithms Written Examination

6. Standard Algorithms

Converting a Number from Decimal to Binary

What Is Recursion? Recursion. Binary search example postponed to end of lecture

Sorting revisited. Build the binary search tree: O(n^2) Traverse the binary tree: O(n) Total: O(n^2) + O(n) = O(n^2)

CS 103X: Discrete Structures Homework Assignment 3 Solutions

Introduction to Algorithms March 10, 2004 Massachusetts Institute of Technology Professors Erik Demaine and Shafi Goldwasser Quiz 1.

Binary Heaps * * * * * * * / / \ / \ / \ / \ / \ * * * * * * * * * * * / / \ / \ / / \ / \ * * * * * * * * * *

CS/COE

Introduction to Programming System Design. CSCI 455x (4 Units)

The Union-Find Problem Kruskal s algorithm for finding an MST presented us with a problem in data-structure design. As we looked at each edge,

The Tower of Hanoi. Recursion Solution. Recursive Function. Time Complexity. Recursive Thinking. Why Recursion? n! = n* (n-1)!

CS473 - Algorithms I

Algorithm Analysis [2]: if-else statements, recursive algorithms. COSC 2011, Winter 2004, Section N Instructor: N. Vlajic

Binary Multiplication

Binary search algorithm

Lecture Notes on Binary Search Trees

Lecture 11: Tail Recursion; Continuations

Sequential Data Structures

Searching Algorithms

Class Overview. CSE 326: Data Structures. Goals. Goals. Data Structures. Goals. Introduction

recursion here it is in C++ power function cis15 advanced programming techniques, using c++ fall 2007 lecture # VI.1

CS104: Data Structures and Object-Oriented Design (Fall 2013) October 24, 2013: Priority Queues Scribes: CS 104 Teaching Team

Binary Heap Algorithms

Thomas Jefferson High School for Science and Technology Program of Studies Foundations of Computer Science. Unit of Study / Textbook Correlation

WESTMORELAND COUNTY PUBLIC SCHOOLS Integrated Instructional Pacing Guide and Checklist Computer Math

Full and Complete Binary Trees

Regular Expressions and Automata using Haskell

Outline BST Operations Worst case Average case Balancing AVL Red-black B-trees. Binary Search Trees. Lecturer: Georgy Gimel farb

Computing Concepts with Java Essentials

Algorithms and Data Structures

Data Structures. Algorithm Performance and Big O Analysis

Symbol Tables. Introduction

Why? A central concept in Computer Science. Algorithms are ubiquitous.

Software Verification and Testing. Lecture Notes: Temporal Logics

Data Structures Fibonacci Heaps, Amortized Analysis

Change Impact Analysis

Notes on Complexity Theory Last updated: August, Lecture 1

Course: Programming II - Abstract Data Types. The ADT Stack. A stack. The ADT Stack and Recursion Slide Number 1

GRID SEARCHING Novel way of Searching 2D Array

Binary Search Trees. Data in each node. Larger than the data in its left child Smaller than the data in its right child

Analysis of a Search Algorithm

MAT Program Verication: Exercises

CS473 - Algorithms I

Near Optimal Solutions

Chapter 13: Query Processing. Basic Steps in Query Processing

Questions 1 through 25 are worth 2 points each. Choose one best answer for each.

Analysis of Algorithms, I

HOMEWORK 5 SOLUTIONS. n!f n (1) lim. ln x n! + xn x. 1 = G n 1 (x). (2) k + 1 n. (n 1)!

Dynamic Programming Problem Set Partial Solution CMPSC 465

AP Computer Science AB Syllabus 1

Math 55: Discrete Mathematics

CSE 135: Introduction to Theory of Computation Decidability and Recognizability

The ADT Binary Search Tree

CSE 326, Data Structures. Sample Final Exam. Problem Max Points Score 1 14 (2x7) 2 18 (3x6) Total 92.

Functions Recursion. C++ functions. Declare/prototype. Define. Call. int myfunction (int ); int myfunction (int x){ int y = x*x; return y; }

Java 6 'th. Concepts INTERNATIONAL STUDENT VERSION. edition

AP Computer Science Java Mr. Clausen Program 9A, 9B

APP INVENTOR. Test Review

CompSci-61B, Data Structures Final Exam

Exam study sheet for CS2711. List of topics

Persistent Binary Search Trees

Solutions for Practice problems on proofs

Reading 13 : Finite State Automata and Regular Expressions

csci 210: Data Structures Recursion

Algorithms. Margaret M. Fleck. 18 October 2010

Lecture 3: Finding integer solutions to systems of linear equations

CSC148 Lecture 8. Algorithm Analysis Binary Search Sorting

Object-Oriented Design Lecture 4 CSU 370 Fall 2007 (Pucella) Tuesday, Sep 18, 2007

B-Trees. Algorithms and data structures for external memory as opposed to the main memory B-Trees. B -trees

Automata and Formal Languages

Cost Model: Work, Span and Parallelism. 1 The RAM model for sequential computation:

Learning Outcomes. COMP202 Complexity of Algorithms. Binary Search Trees and Other Search Trees

Factoring & Primality

A Note on Maximum Independent Sets in Rectangle Intersection Graphs

6.2 Permutations continued

Mathematical Induction. Lecture 10-11

Lecture 19: Introduction to NP-Completeness Steven Skiena. Department of Computer Science State University of New York Stony Brook, NY

Previous Lectures. B-Trees. External storage. Two types of memory. B-trees. Main principles

COMP 250 Fall 2012 lecture 2 binary representations Sept. 11, 2012

A Randomized Searching Algorithm and its Performance analysis with Binary Search and Linear Search Algorithms

2010/9/19. Binary number system. Binary numbers. Outline. Binary to decimal

Chapter Objectives. Chapter 9. Sequential Search. Search Algorithms. Search Algorithms. Binary Search

4.2 Sorting and Searching

Software Testing. Quality & Testing. Software Testing

Designing with Exceptions. CSE219, Computer Science III Stony Brook University

General Framework for an Iterative Solution of Ax b. Jacobi s Method

ON THE COMPLEXITY OF THE GAME OF SET.

Applied Algorithm Design Lecture 5

Transcription:

Outline Computer Science 331 Algorithms for Searching Mike Jacobson Department of Computer Science University of Calgary Lecture #21 1 The Searching Problem 2 3 Mike Jacobson (University of Calgary) Computer Science 331 Lecture #21 1 / 23 Mike Jacobson (University of Calgary) Computer Science 331 Lecture #21 2 / 23 The Searching Problem The Searching Problem Precondition: A: Array of length n, for some integer n 1, storing objects of some type k: An object of the type that might be found in A Postcondition: i: An integer such that 0 i < n and A[i] = k (The array A and key k were not changed.) Exceptions: KeyNotFoundException: Thrown if k is not found in A Idea: Compare A[0], A[1], A[2],... to k until either k is found, or we run out of entries to check LinearSearch(k) i = 0 while (i < n) and (A[i] k) do i = i + 1 end while if i < n then return i Mike Jacobson (University of Calgary) Computer Science 331 Lecture #21 3 / 23 Mike Jacobson (University of Calgary) Computer Science 331 Lecture #21 4 / 23

Example Partial Correctness 0 1 2 3 4 5 6 7 8 9 10 A: 43 30 6 18-3 49 2 21 29 35 23 Search for 18 in the array A : Return 3 i = 0 : A[0] = 43 18 i = 1 : A[1] = 30 18 i = 2 : A[2] = 6 18 i = 3 : A[3] = 18 Loop Invariant: If the loop body is exectuted j or more times, then after j executions of the loop body i = j 0 i n A[h] k for 0 h < i Proving the Loop Invariant: use induction on j Base Case (j = 0): before first execution of loop body we have i = 0 loop invariant holds (conditions on i, no values of h such that 0 h < 0) Mike Jacobson (University of Calgary) Computer Science 331 Lecture #21 5 / 23 Mike Jacobson (University of Calgary) Computer Science 331 Lecture #21 6 / 23 Partial Correctness (cont.) Partial Correctness (applying the loop invariant) Inductive hypothesis: assume that, if the loop iterates j times, then the loop invariant holds for i old = j Need to show that if the loop iterates a j + 1st time, then the loop invariant holds for i new = j + 1 : if true for iteration i old = j, then A[h] k for 0 h < i old if loop iterates, then A[i old ] k and i new = i old + 1 thus A[h] k for 0 h < i new because the loop iterated for i old = j, we have i old < n and i new n. Thus, the loop invariant holds for j + 1. When the loop test fails, the loop invariant holds and either i n or A[i] = k Case 1 (i n): loop invariant implies that A[h] k for 0 h < n, so k is not in A and KeyNotFoundException is thrown Case 2 (i < n): loop invariant implies that A[i] = k and i is returned Conclusion: postcondition is satisfied in either case, so linearsearch is paritally correct Mike Jacobson (University of Calgary) Computer Science 331 Lecture #21 7 / 23 Mike Jacobson (University of Calgary) Computer Science 331 Lecture #21 8 / 23

Termination and Efficiency New Problem: Searching in a Sorted Array Loop Variant: f (n, i) = n i Proving the Loop Variant: f (n, i) is a decreasing integer function because integer i increases after each loop body execution f (n, i) = 0 when i = n, loop terminates (worst case) when i n Application of Loop Variant: existence demonstrates termination worst-case number of iterations is f (n, 0) = n loop body runs in constant time, so worst-case runtime of LinearSearch is Θ(n) Precondition: A: Array of length n, for some integer n 1, storing objects of some ordered type New Requirement: A[i] A[i + 1] for 0 i < n 1 k: An object of the type that might be found in A Postcondition: i: An integer such that 0 i < n and A[i] = k (The array A and key k were not changed.) Exceptions: KeyNotFoundException: Thrown if k is not found in A Mike Jacobson (University of Calgary) Computer Science 331 Lecture #21 9 / 23 Mike Jacobson (University of Calgary) Computer Science 331 Lecture #21 10 / 23 Example Idea: compare A[0], A[1], A[2],... to k until either k is found or we see a value larger than k all future values will be larger than k as well! or we run out of entries to check LinearSearch(k) i = 0 while (i < n) and (A[i] < k) do i = i + 1 end while if (i < n) and (A[i] == k) then return i Mike Jacobson (University of Calgary) Computer Science 331 Lecture #21 11 / 23 0 1 2 3 4 5 6 7 8 9 10 A: -3 2 6 18 21 23 29 30 35 43 49 Search for 17 in the array A : i = 0 : A[0] = 3 < 17 i = 1 : A[1] = 2 < 17 i = 2 : A[2] = 6 < 17 i = 3 : A[3] = 18 17 Mike Jacobson (University of Calgary) Computer Science 331 Lecture #21 12 / 23

Partial Correctness Partial Correctness (cont.) Loop Invariant: If the loop body is exectuted j or more times, then after j executions of the loop body i = j 0 i n A[h] < k for 0 h < i Proving the Loop Invariant: use induction on j Base Case (j = 0): before first execution of loop body we have i = 0 loop invariant holds (conditions on i, no values of h such that 0 h < 0) Inductive hypothesis: assume that, if the loop iterates j times, then the loop invariant holds for i old = j Need to show that if the loop iterates a j + 1st time, then the loop invariant holds for i new = j + 1 : if true for iteration i old = j, then A[h] < k for 0 h < i old if loop iterates without terminating, A[i old ] < k and i new = i old + 1 thus A[h] < k for 0 h < i new because the loop iterated for i old = j, we have i old < n and i new n. Thus, the loop invariant holds for j + 1. Mike Jacobson (University of Calgary) Computer Science 331 Lecture #21 13 / 23 Mike Jacobson (University of Calgary) Computer Science 331 Lecture #21 14 / 23 Partial Correctness (applying the loop invariant) Termination and Efficiency When the loop test fails, the loop invariant holds and either i n or A[i] k Case 1 (i n): loop invariant implies that A[h] < k for 0 h < n, so k is not in A and KeyNotFoundException is thrown Case 2 (i < n and A[i] = k): key is found and i is returned Case 3 (i < n and A[i] > k): loop invariant implies that A[i] < k for 0 h < i, so k is not in A and KeyNotFoundException is thrown Conclusion: postcondition is satisfied in all cases, so linearsearch is paritally correct Loop Variant: f (n, i) = n i Proving the Loop Variant: same as before Application of Loop Variant: same as before (worst-case runtime is alsoθ(n)) Note: although the worst-case involves examining all elements of the array, fewer will be examined on average improves on unsorted case (all array elements must be examined to determine that k is not in the array) Mike Jacobson (University of Calgary) Computer Science 331 Lecture #21 15 / 23 Mike Jacobson (University of Calgary) Computer Science 331 Lecture #21 16 / 23

Specification of Requirements for Subroutine Calling Sequence: bsearch(low, high, k) Idea: suppose we compare k to A[i] if k > A[i] then k > A[h] for all h i. if k < A[i] then k < A[h] for all h i. Thus, comparing k to the middle of the array tells us a lot: can eliminate half of the array after the comparison binarysearch(k) return bsearch(0, n 1, k) Precondition: A, k: Same for for Searching in a Sorted Array low, high: Integers such that 0 low n and 1 high n 1 low high + 1 A[h] < k for 0 h < low A[h] > k for high < h n 1 Postcondition and Exceptions: Same as for Searching in a Sorted Array Mike Jacobson (University of Calgary) Computer Science 331 Lecture #21 17 / 23 Mike Jacobson (University of Calgary) Computer Science 331 Lecture #21 18 / 23 Pseudocode: The Subroutine Example bsearch(low, high, k) if low > high then mid = (low + high)/2 if (A[mid] > k) then return bsearch(low, mid 1, k) if (A[mid] < k) then return bsearch(mid + 1, high, k) return mid 0 1 2 3 4 5 6 7 8 9 10 A: -3 2 6 18 21 23 29 30 35 43 49 Search for 18 in the array A : Return 3 bsearch(0,10,18): mid = (0 + 10)/2 = 5, A[5] = 23 > 18 bsearch(0,4,18): mid = (0 + 4)/2 = 2, A[2] = 6 < 18 bsearch(3,4,18): mid = (3 + 4)/2 = 3, A[3] = 18 Mike Jacobson (University of Calgary) Computer Science 331 Lecture #21 19 / 23 Mike Jacobson (University of Calgary) Computer Science 331 Lecture #21 20 / 23

Partial Correctness Efficiency Assumptions bsearch is called with the precondition satisfied Calls to bsearch within the code behave as expected Case: low > high base case (no elements) throw KeyNotFoundException (correct) Case: low = high return mid(= low = high) if A[mid] = k (correct) otherwise recursive call with low > high (correct) Case: low < high return mid if A[mid] = k (correct) recursive call (correct by assumption) Mike Jacobson (University of Calgary) Computer Science 331 Lecture #21 21 / 23 Case: low high Θ(1) steps Case: low < high : Consider i = log 2 (high low + 1) Result of Function Call: i decreases by 1 (because new high low + 1 is less than half the old value) What Happens if i = 0 : high = low (algorithm terminates after at most one more iteration) Initial Value: i = log 2 n, because low = 0 and high = n 1 initially Conclusion: worst-case run time is Θ(log 2 n) (constant number of steps per iteration) Mike Jacobson (University of Calgary) Computer Science 331 Lecture #21 22 / 23 References Java.utils.Arrays package contains several implementations of binary search arrays with Object or generic entries, or entries of any basic type slightly different pre and postconditions than presented here Textbook: Section 7.1: design of recursive algorithms Section 7.3: discussion of recursive array search algorithms (linear and binary), Java implementation Mike Jacobson (University of Calgary) Computer Science 331 Lecture #21 23 / 23