Binary Search and Worst-Case Analysis

Similar documents
Binary Search and Worst-Case Analysis

Recursion: The Beginning

RAM with Randomization and Quick Sort

Priority Queues and Binary Heaps

Recursion: The Beginning

Binary Heaps in Dynamic Arrays

(Refer Slide Time: 00:50)

Suffix Trees and Arrays

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

The RAM Computation Model

Dynamic Arrays and Amortized Analysis

Multidimensional Divide and Conquer 2 Spatial Joins

ECE 122. Engineering Problem Solving Using Java

Searching, Sorting. Arizona State University 1

More Counting Sort and Sorting-by-Key

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

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

The B-Tree. Yufei Tao. ITEE University of Queensland. INFS4205/7205, Uni of Queensland

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

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

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

Subset sum problem and dynamic programming

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

CSE 373: Data Structures and Algorithms

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

Intro to Algorithms. Professor Kevin Gold

SORTING AND SEARCHING

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

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

High Dimensional Indexing by Clustering

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

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

Lecture Notes for Chapter 2: Getting Started

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

Reasoning and writing about algorithms: some tips

Binary Search APRIL 25 TH, 2014

CSE 373: Data Structures and Algorithms

(Refer Slide Time: 1:27)

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

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

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

LECTURE 08 SEARCHING AND SORTING ARRAYS

Lecture 6: Divide-and-Conquer

LECTURE 17. Array Searching and Sorting

Comp 249 Programming Methodology Chapter 11 Recursion

CSC 273 Data Structures

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

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

CSE 101. Algorithm Design and Analysis Miles Jones Office 4208 CSE Building Lecture 17: Divide and Conquer Design examples.

8 Algorithms 8.1. Foundations of Computer Science Cengage Learning

Fundamentals of Computing and Digital Literacy. Sample. Marking Scheme

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

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

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

Lecture 6 Sorting and Searching

Introduction to Programming in C Department of Computer Science and Engineering

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

C/C++ Programming Lecture 18 Name:

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

Sorting: Overview/Questions

Lecture Notes on Binary Search Trees

UNIT 5B Binary Search

Chapter 8 Algorithms 1

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

Searching Algorithms/Time Analysis

Lecture 15: Algorithms. AP Computer Science Principles

Goal of the course: The goal is to learn to design and analyze an algorithm. More specifically, you will learn:

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

Lecture 3.4: Recursive Algorithms

6/4/12. Recursive void Methods. Chapter 11. Vertical Numbers. Vertical Numbers. Vertical Numbers. Algorithm for Vertical Numbers

Finding Strongly Connected Components

There are functions to handle strings, so we will see the notion of functions itself in little a detail later. (Refer Slide Time: 00:12)

Data Structures. Alice E. Fischer. Lecture 4, Fall Alice E. Fischer Data Structures L4... 1/19 Lecture 4, Fall / 19

Notes slides from before lecture. CSE 21, Winter 2017, Section A00. Lecture 3 Notes. Class URL:

Recursion. John Ross Wallrabenstein

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

LECTURE NOTES OF ALGORITHMS: DESIGN TECHNIQUES AND ANALYSIS

Lecture 15 Binary Search Trees

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

CSCI 136 Data Structures & Advanced Programming. Lecture 12 Fall 2018 Profs Bill & Jon

Module 10. Recursion. Adapted from Absolute Java, Rose Williams, Binghamton University

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

Chapter 10 - Notes Applications of Arrays

CSE101-lec#19. Array searching and sorting techniques. Created By: Amanpreet Kaur & Sanjeev Kumar SME (CSE) LPU. LPU CSE101 C Programming

Testing and Debugging

CSc 110, Spring 2017 Lecture 39: searching

Symbolic Execution and Proof of Properties

n 1 x i = xn 1 x 1. i= (2n 1) = n 2.

Extended Introduction to Computer Science CS1001.py. Lecture 5 Part A: Integer Representation (in Binary and other bases)

4.1 COMPUTATIONAL THINKING AND PROBLEM-SOLVING

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

ΗΥ345 Operating Systems. Recitation 2 Memory Management - Solutions -

DATA STRUCTURES/UNIT 3

Clustering: Centroid-Based Partitioning

A New Algorithm for Multiple Key Interpolation Search in Uniform List of Numbers

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

Algorithm Analysis. Carlos Moreno uwaterloo.ca EIT Θ(n) Ω(log n) Θ(1)

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

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

Unit-2 Divide and conquer 2016

Transcription:

Yufei Tao ITEE University of Queensland

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. while i n 4. read into register x the memory cell at address i 5. if x = v then 6. return yes 7. i i + one (effectively increasing i by 1) 8. return no

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 6. If we are given a no -input, then the algorithm has running time 4n + 4. 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 infinite) distinct inputs of the same size n.

Example Our algorithm has worst-case time f 1 (n) = 4n + 4. 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 + 4. 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. while left right 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. 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 6 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 (think: why?) which gives h log 2 n. n 2 h 1 It thus follows that the worst-case time of binary search is at most f 2 (n) = 2 + 6 log 2 n. This is a performance guarantee that holds on all values of n.

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.