1 Lower Bound on Comparison-based Search We have now covered lots of searching methods –Contiguous Data (Arrays) Sequential search Binary Search –Dynamic.

Slides:



Advertisements
Similar presentations
CPSC 411 Design and Analysis of Algorithms Set 2: Sorting Lower Bound Prof. Jennifer Welch Spring 2011 CPSC 411, Spring 2011: Set 2 1.
Advertisements

Introduction to Algorithms Quicksort
110/6/2014CSE Suprakash Datta datta[at]cse.yorku.ca CSE 3101: Introduction to the Design and Analysis of Algorithms.
Binary Searching.
Discrete Structure Li Tak Sing( 李德成 ) Lectures
AA Trees another alternative to AVL trees. Balanced Binary Search Trees A Binary Search Tree (BST) of N nodes is balanced if height is in O(log N) A balanced.
CS 171: Introduction to Computer Science II
Chapter 19: Searching and Sorting Algorithms
© 2004 Goodrich, Tamassia Sorting Lower Bound1. © 2004 Goodrich, Tamassia Sorting Lower Bound2 Comparison-Based Sorting (§ 10.3) Many sorting algorithms.
AVL-Trees (Part 1) COMP171. AVL Trees / Slide 2 * Data, a set of elements * Data structure, a structured set of elements, linear, tree, graph, … * Linear:
Lecture 5: Linear Time Sorting Shang-Hua Teng. Sorting Input: Array A[1...n], of elements in arbitrary order; array size n Output: Array A[1...n] of the.
Lists A list is a finite, ordered sequence of data items. Two Implementations –Arrays –Linked Lists.
© 2004 Goodrich, Tamassia Sorting Lower Bound1. © 2004 Goodrich, Tamassia Sorting Lower Bound2 Comparison-Based Sorting (§ 10.3) Many sorting algorithms.
CPSC 411, Fall 2008: Set 2 1 CPSC 411 Design and Analysis of Algorithms Set 2: Sorting Lower Bound Prof. Jennifer Welch Fall 2008.
CPSC 411, Fall 2008: Set 2 1 CPSC 311 Analysis of Algorithms Sorting Lower Bound Prof. Jennifer Welch Fall 2009.
Lecture 5: Master Theorem and Linear Time Sorting
Sorting Lower Bound Andreas Klappenecker based on slides by Prof. Welch 1.
Analysis of Algorithms CS 477/677
DAST 2005 Week 4 – Some Helpful Material Randomized Quick Sort & Lower bound & General remarks…
Data Structures Using C++ 2E Chapter 11 Binary Trees and B-Trees.
1 Section 9.2 Tree Applications. 2 Binary Search Trees Goal is implementation of an efficient searching algorithm Binary Search Tree: –binary tree in.
Sorting Lower Bound1. 2 Comparison-Based Sorting (§ 4.4) Many sorting algorithms are comparison based. They sort by making comparisons between pairs of.
Lower Bounds for Comparison-Based Sorting Algorithms (Ch. 8)
Asymptotic Notations Iterative Algorithms and their analysis
CPT: Search/ Computer Programming Techniques Semester 1, 1998 Objectives of these slides: –to discuss searching: its implementation,
Iterative Algorithm Analysis & Asymptotic Notations
C++ Programming: Program Design Including Data Structures, Third Edition Chapter 19: Searching and Sorting Algorithms.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 19: Searching and Sorting Algorithms.
Chapter 19: Searching and Sorting Algorithms
SEARCHING (Linear/Binary). Searching Algorithms  method of locating a specific item of information in a larger collection of data.  two popular search.
Advance Data Structure 1 College Of Mathematic & Computer Sciences 1 Computer Sciences Department م. م علي عبد الكريم حبيب.
Searching Dr. Jose Annunziato. Linear Search Linear search iterates over an array sequentially searching for a matching element int linearSearch(int haystack[],
CIS3023: Programming Fundamentals for CIS Majors II Summer 2010 Ganesh Viswanathan Searching Course Lecture Slides 28 May 2010 “ Some things Man was never.
Sorting Algorithms 2. Quicksort General Quicksort Algorithm: Select an element from the array to be the pivot Select an element from the array to be the.
Analysis of Algorithms CS 477/677
Data Structures Using C++ 2E Chapter 10 Sorting Algorithms.
CSC 211 Data Structures Lecture 13
Data Structures Using C++ 2E Chapter 9 Searching and Hashing Algorithms.
Search Trees Chapter   . Outline  Binary Search Trees  AVL Trees  Splay Trees.
AVL Trees. AVL Node Structure The AVL node structure follows the same structure as the binary search tree, with the addition of a term to store the.
Chapter 18: Searching and Sorting Algorithms. Objectives In this chapter, you will: Learn the various search algorithms Implement sequential and binary.
Princeton University COS 423 Theory of Algorithms Spring 2001 Kevin Wayne Average Case Analysis.
1 Asymptotic Notations Iterative Algorithms and their analysis Asymptotic Notations –Big O,  Notations Review of Discrete Math –Summations –Logarithms.
Data Structures Using C++1 Chapter 10 Sorting Algorithms.
Trees 2: Section 4.2 and 4.3 Binary trees. Binary Trees Definition: A binary tree is a rooted tree in which no vertex has more than two children
Chapter 4: Trees Part I: General Tree Concepts Mark Allen Weiss: Data Structures and Algorithm Analysis in Java.
Asymptotic Behavior Algorithm : Design & Analysis [2]
Binary Search Trees (BSTs) 18 February Binary Search Tree (BST) An important special kind of binary tree is the BST Each node stores some information.
Week 10 - Friday.  What did we talk about last time?  Graph representations  Adjacency matrix  Adjacency lists  Depth first search.
LIMITATIONS OF ALGORITHM POWER
Lecture 9COMPSCI.220.FS.T Lower Bound for Sorting Complexity Each algorithm that sorts by comparing only pairs of elements must use at least 
Data Structures Using Java1 Chapter 9 Sorting Algorithms.
Searching ( 搜索) 1. Introduction 2. Sequential Search 3. Binary Search 4. Comparison Trees.
Sorting Lower Bounds n Beating Them. Recap Divide and Conquer –Know how to break a problem into smaller problems, such that –Given a solution to the smaller.
Sorting & Searching Geletaw S (MSC, MCITP). Objectives At the end of this session the students should be able to: – Design and implement the following.
1 Data Structures CSCI 132, Spring 2014 Lecture23 Analyzing Search Algorithms.
 Introduction to Search Algorithms  Linear Search  Binary Search 9-2.
1 compares each element of the array with the search key. works well for small arrays or for unsorted arrays works for any table slow can put more commonly.
1 Section 5.1 Analyzing Algorithms Let P be a problem and A an algorithm to solve P. The running time of A can be analyzed by counting the number of certain.
Decision Trees DEFINITION: DECISION TREE A decision tree is a tree in which the internal nodes represent actions, the arcs represent outcomes of an action,
AA Trees.
Data Structures I (CPCS-204)
Multiway Search Trees Data may not fit into main memory
Binary Search Trees A binary search tree is a binary tree
Decision trees Polynomial-Time
CPSC 411 Design and Analysis of Algorithms
Data Structures Using C++
B+ Trees What are B+ Trees used for What is a B Tree What is a B+ Tree
Binary Tree and General Tree
B+ Trees What are B+ Trees used for What is a B Tree What is a B+ Tree
Presentation transcript:

1 Lower Bound on Comparison-based Search We have now covered lots of searching methods –Contiguous Data (Arrays) Sequential search Binary Search –Dynamic Data (Linked Lists and Trees) Sequential search on linked lists Search Trees: BST, AVL tree, Splay Tree, B Trees, … Question: –How fast can a searching algorithm be made using only comparison of keys? –That is, what’s the lower bound on comparison- based searching?

2 Comparison (Decision) Trees A comparison tree (also called decision tree or search tree) of an algorithm is obtained by tracing through the actions of the algorithm Represent each key comparison by a vertex of the tree, which we denote by a circle Put the key against which we are comparing the target inside the vertex Branches (lines) drawn down a vertex represent possible outcomes of the comparison and are labeled accordingly

3 Comparison (Decision) Trees When the algorithm terminates, we put either – F (for failure) OR –The location where the target is found at the end of the appropriate branch, which we call a leaf and denote by a square

4 Sequential Search – Flashback // Return the index of the array containing the key or –1 if key not found // int LinearSearch(int A[], int noOfKeys, int key){ for (int k=0; k < noOfKeys; k++){ if (A[k] == key) return k; // Key found. Return the index of the array } //end-for return –1; // Key not found }

5 Comparison Tree for Sequential Search A[0] A[1] A[2] A[3] A[4] A[N] F = = = = = = The # of steps for the algorithm to terminate in a particular search is the # of internal (circular) vertices traversed going from the top of the tree down the appropriate path to a leaf Height = N Means that in the worst case the algorithm will take O(N) steps to terminate. So its running time is O(N)

6 Binary Search - Flashback // Return the index of the array containing the key or –1 if key not found int BinarySearch(int A[], int noOfKeys, int key){ left = 0; right = noOfKeys-1; while (left <= right){ int middle = (left+right)/2; // Index of the key to test against if (A[middle] == key) return middle; // Key found. Return the index else if (key < A[middle]) right = middle – 1; // Eliminate the right side else left = middle+1; // Eliminate the left side } //end-while return –1; // Key not found } //end-BinarySearch

7 Comparison Tree for Binary Search A[3]A[1]A[5]A[0]A[2] A[1] A[0] F F A[3] A[2] F F A[4]A[6] A[5] A[4] F F A[6] F F A: left right Height = log 2 (7) = 3 = = = = = = = < < < < < < < > > > >> > >

8 Comparison Tree for Binary Search A[middle] = Target less than key in the middle: Search (left, right = middle-1) Target greater than key in the middle: Search (left=middle+1, right) Since we divide the data set in half with each comparison, the maximum height of the comparison tree will be log 2 N. So the running time of binary search is O(log 2 N). Height = log 2 (N) < >

9 Lower Bound on Comparison-based Algorithms Theorem: –Suppose that an algorithm uses ONLY comparison of keys to solve a problem. –If there are “n” possible outcomes, then the algorithm must take AT LEAST log 2 N comparison of keys in the worst case to solve the problem.

10 Lower Bound on Comparison-based Algorithms – Informal Proof O1 O2 C O3 O4 C O5 O6 C O n-1 OnOn C C O5 O6 C ………………… C C ……………………………………………. C …………………. Height = log 2 (N) Notice that the comparison tree that would result in the smallest height is a complete binary tree. The height of a complete binary tree of N leaves is log 2 N This completes the informal proof.

11 Lower Bound on Comparison Based Search Let’s take comparison-based searching problem: –Search for a key in a data set consisting of N keys using only comparison of keys –How many possible outcomes? N + 1: N successes, 1 failure –Then, the running time of ANY comparison-based search algorithm is (log 2 N)