Binary Search and Worst-Case Analysis

Similar documents
Binary Search and Worst-Case Analysis

Recursion: The Beginning

Merge Sort. Yufei Tao. Department of Computer Science and Engineering Chinese University of Hong Kong

The RAM Computation Model

Recursion: The Beginning

(Refer Slide Time: 00:50)

Searching, Sorting. Arizona State University 1

RAM with Randomization and Quick Sort

Cost of Your Programs

Dynamic Arrays and Amortized Analysis

Today. CISC101 Reminders & Notes. Searching in Python - Cont. Searching in Python. From last time

Topics Applications Most Common Methods Serial Search Binary Search Search by Hashing (next lecture) Run-Time Analysis Average-time analysis Time anal

Subset sum problem and dynamic programming

k-selection Yufei Tao Department of Computer Science and Engineering Chinese University of Hong Kong

CSCI2100B Data Structures Heaps

CSE 373: Data Structures and Algorithms

8/2/10. Looking for something COMP 10 EXPLORING COMPUTER SCIENCE. Where is the book Modern Interiors? Lecture 7 Searching and Sorting TODAY'S OUTLINE

Intro to Algorithms. Professor Kevin Gold

CSE 143. Two important problems. Searching and Sorting. Review: Linear Search. Review: Binary Search. Example. How Efficient Is Linear Search?

/633 Introduction to Algorithms Lecturer: Michael Dinitz Topic: Sorting lower bound and Linear-time sorting Date: 9/19/17

SORTING AND SEARCHING

ECE 122. Engineering Problem Solving Using Java

Clustering: Centroid-Based Partitioning

CSE 373: Data Structures and Algorithms

(Refer Slide Time: 1:27)

12/30/2013 S. NALINI,AP/CSE

Outline. Definition. 2 Height-Balance. 3 Searches. 4 Rotations. 5 Insertion. 6 Deletions. 7 Reference. 1 Every node is either red or black.

Priority Queues and Binary Heaps

Java How to Program, 9/e. Copyright by Pearson Education, Inc. All Rights Reserved.

Searching for Information. A Simple Method for Searching. Simple Searching. Class #21: Searching/Sorting I

Lecture Notes for Chapter 2: Getting Started

Lecture 6 Sorting and Searching

LECTURE 08 SEARCHING AND SORTING ARRAYS

Sorting Algorithms. Array Data is being arranged in ascending order using the bubble sort algorithm. #1 #2 #3 #4 #5 #6 #7

LECTURE 17. Array Searching and Sorting

Definition: A data structure is a way of organizing data in a computer so that it can be used efficiently.

Data Structures and Algorithms Dr. Naveen Garg Department of Computer Science and Engineering Indian Institute of Technology, Delhi.

Example Algorithms. CSE 2320 Algorithms and Data Structures Alexandra Stefan. University of Texas at Arlington. Last updated 9/7/2016

Hashing. Yufei Tao. Department of Computer Science and Engineering Chinese University of Hong Kong

8 Algorithms 8.1. Foundations of Computer Science Cengage Learning

Programming, Data Structures and Algorithms Prof. Hema Murthy Department of Computer Science and Engineering Indian Institute Technology, Madras

Algorithm for siftdown(int currentposition) while true (infinite loop) do if the currentposition has NO children then return

Algorithms. Chapter 8. Objectives After studying this chapter, students should be able to:

Suffix Trees and Arrays

Chapter 8 Algorithms 1

Binary Heaps in Dynamic Arrays

such a manner that we are able to understand, grasp and grapple with the problem at hand in a more organized fashion.

Binary Search to find item in sorted array

S1) It's another form of peak finder problem that we discussed in class, We exploit the idea used in binary search.

CS 112 Introduction to Computing II. Wayne Snyder Computer Science Department Boston University

UNIT 5B Binary Search

(Refer Slide Time: 01.26)

CSE373: Data Structures and Algorithms Lecture 4: Asymptotic Analysis. Aaron Bauer Winter 2014

Lecture Notes on Binary Search Trees

DATA STRUCTURES/UNIT 3

Lecture 15: Algorithms. AP Computer Science Principles

Outline. Introduction. 2 Proof of Correctness. 3 Final Notes. Precondition P 1 : Inputs include

Threaded Programming. Lecture 1: Concepts

IUSE Knowledge Test. 1. Demographic Questions. IUSE Knowledge Test. 2. Computational Thinking Knowledge Test

8.1. Chapter 8: Introduction to Search Algorithms. Linear Search. Linear Search. Linear Search - Example 8/23/2014. Introduction to Search Algorithms

/633 Introduction to Algorithms Lecturer: Michael Dinitz Topic: Priority Queues / Heaps Date: 9/27/17

10/5/2016. Comparing Algorithms. Analyzing Code ( worst case ) Example. Analyzing Code. Binary Search. Linear Search

Lecture 2: Sorting and the Big O. Wednesday, 16 September 2009

CSc 110, Spring 2017 Lecture 39: searching

Chapter 10 - Notes Applications of Arrays

LECTURE NOTES OF ALGORITHMS: DESIGN TECHNIQUES AND ANALYSIS

week3 Tommy MacWilliam Design GDB week3 Running Time Search Sorting Recursion Practice Problems September 26, 2011

Lecture 6: Divide-and-Conquer

Search Trees. Computer Science S-111 Harvard University David G. Sullivan, Ph.D. Binary Search Trees

9. Heap : Priority Queue

Comp 249 Programming Methodology Chapter 11 Recursion

CSC 273 Data Structures

Testing and Debugging

COMP 364: Computer Tools for Life Sciences

Comparison Sorts. Chapter 9.4, 12.1, 12.2

introduction to Programming in C Department of Computer Science and Engineering Lecture No. #40 Recursion Linear Recursion

Fundamentals of Computing and Digital Literacy. Sample. Marking Scheme

CSCI 3130: Formal Languages and Automata Theory Lecture 16 The Chinese University of Hong Kong, Fall 2010

Assignment 1. Stefano Guerra. October 11, The following observation follows directly from the definition of in order and pre order traversal:

Data Structures and Algorithms Dr. Naveen Garg Department of Computer Science and Engineering Indian Institute of Technology, Delhi

Lecture 15 Binary Search Trees

THE UNIVERSITY OF WESTERN AUSTRALIA

Introduction to Programming in C Department of Computer Science and Engineering

6. Asymptotics: The Big-O and Other Notations

4.1 COMPUTATIONAL THINKING AND PROBLEM-SOLVING

Algorithm Efficiency & Sorting. Algorithm efficiency Big-O notation Searching algorithms Sorting algorithms

Algorithm Analysis and Design

Reasoning and writing about algorithms: some tips

Searching. Algorithms and Data Structures. Marcin Sydow. Divide and Conquer. Searching. Positional Statistics. Summary

Chapter 3: The Efficiency of Algorithms. Invitation to Computer Science, C++ Version, Third Edition

Problem Solving through Programming In C Prof. Anupam Basu Department of Computer Science & Engineering Indian Institute of Technology, Kharagpur

Sorting Pearson Education, Inc. All rights reserved.

Symbolic Execution and Proof of Properties

4.2 Sorting and Searching. Section 4.2

C/C++ Programming Lecture 18 Name:

Name: Lirong TAN 1. (15 pts) (a) Define what is a shortest s-t path in a weighted, connected graph G.

CSE 214 Computer Science II Searching

Searching Algorithms/Time Analysis

CSE101: Design and Analysis of Algorithms. Ragesh Jaiswal, CSE, UCSD

Sorting: Overview/Questions

Transcription:

Department of Computer Science and Engineering Chinese University of Hong Kong

A significant part of computer science is devoted to understanding the power of the RAM model in solving specific problems. Every time we discuss a problem in this course, we will learn something new. Today s lecture is about the dictionary search problem. We will learn not only a fast algorithm for solving this problem, but also a method called worst-case analysis for measuring the quality of an algorithm.

The Dictionary Search Problem Problem Input: In the memory, a set S of n integers have been arranged in ascending order at the memory cells from address 1 to n. The value of n has been placed in Register 1 of the CPU. Another integer v has been placed in Register 2 of the CPU. Goal: Design an algorithm to determine whether v exists in S. Note that we have not specified how your algorithm should indicate the outcome. This is up to you. For example, you may store 0 in a certain register to signify no, and 1 for yes. We will refer to the value of n as the problem size.

A yes -input with n = 16 16 35... 5 9 12 17 26 28 35 38 41 47 52 68 69 72 83 88 A no -input with n = 16 16 36... 5 9 12 17 26 28 35 38 41 47 52 68 69 72 83 88

The First Algorithm Let n be Register 1, and v be Register 2. Simply read the memory cell of address i, for each i [1, n] in turn. If any of those cells equals v, return yes. Otherwise, return no. The above is a concise, but clear, description of the same algorithm as in the pseudocode of the next slide.

The First Algorithm in Pseudocode 1. Let n be register 1, and v be register 2 2. register i 1, register one 1 3. repeat 4. read into register x the memory cell at address i 5. if x = v then 6. return yes (by writing 1 to a register) 7. i i + one (effectively increasing i by 1) 8. until i > n 8. return no (by writing 0 to a register)

Running Time of the First Algorithm How much time does the algorithm require? The answer depends on the problem input. Here are two extreme cases: If v is the first element in S (i.e., the integer in the memory cell of address 1), the algorithm has running time 5. If we are given a no -input, then the algorithm has running time 4n + 3. In computer science, it is an art to design algorithms with performance guarantees. In our scenario, this amounts to the question: what is the largest running time on the worst input with n integers? This gives rise to an important notion in the next slide.

Worst-Case Running Time The worst-case cost (or worst-case time) of an algorithm under a problem size n, is defined to be the largest running time of the algorithm on all the (possibly an infinite number of) inputs of the same size n.

Example Our algorithm has worst-case time f 1 (n) = 4n + 3. In other words, no matter how you design the input set S of n integers, the algorithm always terminates with a cost at most 4n + 3. This is its performance guarantee on every n.

Next, we will see another algorithm with much better worst-case time, namely, the binary search algorithm.

Binary Search We will utilize the fact that S has been stored in ascending order. Let us compare v to the element x in the middle of S (i.e., the (n/2)-th). If v = x, we have found v, and thus, can terminate. If v < x, we can immediately forget about the second half of S. If v > x, forget about the first half. In the 2nd and 3rd cases, we have at most n/2 elements left. Then, repeat the trick on those elements!

Binary Search 16 35... 5 9 12 17 26 28 35 38 41 47 52 68 69 72 83 88 Conceptually discard the second half of S.

Binary Search 16 35... 5 9 12 17 26 28 35 Conceptually discard the first half of what is shown.

Binary Search 16 35... 26 28 35 Conceptually discard the first half of what is shown.

Binary Search 16 35... 35 Found.

Binary Search in Pseudocode 1. let n be register 1, and v be register 2 2. register left 1, right n 3. repeat 4. register mid (left + right)/2 5. if the memory cell at address mid = v then 6. return yes 7. else if the memory cell at address mid > v then 8. right = mid 1 9. else 10. left = mid + 1 11. until left > right 12. return no

Worst-Case Time of Binary Search Let us call the integers whose memory addresses are from left to right as active elements. Refer to Lines 3-10 as an iteration. Each iteration performs at most 7 atomic operations (try verifying this yourself).

Worst-Case Time of Binary Search How many iterations are there? After the first iteration, the number of active elements is at most n/2. After another, the number is at most n/4. In general, after i iterations, the number drops to at most n/2 i. Suppose that there are h iterations in total. It holds that h is the smallest integer satisfying (think: why?) which gives h = 1 + log 2 n. n 2 h < 1

Worst-Case Time of Binary Search (cont.) In each iteration we perform only a constant number of operations we will not analyze this constant precisely, except for pointing out a loose upper bound of 10. The worst-case time of binary search is at most f 2 (n) = 10(1 + log 2 n). When n is large, this running time is much lower than the time 4n + 3 of our first algorithm.

In this lecture, we have got a taste of what computer science is like. We are seldom satisfied with just finding an algorithm that can correctly solve a problem. Instead, our goal is to design an algorithm with a strong performance guarantee, i.e., you must prove that it runs fast even in the worst case.