1 Data Structures CSCI 132, Spring 2014 Lecture23 Analyzing Search Algorithms.

Slides:



Advertisements
Similar presentations
22C:19 Discrete Structures Trees Spring 2014 Sukumar Ghosh.
Advertisements

Binary Searching.
1 Data Structures CSCI 132, Spring 2014 Lecture 37 Binary Search Trees II.
Searching Algorithms Finding what you are looking for.
Binary Search Trees1 Part-F1 Binary Search Trees   
Chapter 7 Chapter 7 SEARCHING`. Outline 1. Introduction, Notation 2. Sequential Search 3. Binary Search 4. Comparison Trees 5. Lower Bounds 6. Asymptotics:
Objectives Learn how to implement the sequential search algorithm Explore how to sort an array using the selection sort algorithm Learn how to implement.
CHAPTER 11 Searching. 2 Introduction Searching is the process of finding a target element among a group of items (the search pool), or determining that.
Searching Arrays Linear search Binary search small arrays
Data Structures Using C++ 2E Chapter 11 Binary Trees and B-Trees.
1 Data Structures CSCI 132, Spring 2014 Lecture 22 Searching.
Binary Trees Chapter 6.
1 Search Algorithms Sequential Search (Linear Search) Binary Search Rizwan Rehman Centre for Computer Studies Dibrugarh University.
Indexing. Goals: Store large files Support multiple search keys Support efficient insert, delete, and range queries.
B-Tree. B-Trees a specialized multi-way tree designed especially for use on disk In a B-tree each node may contain a large number of keys. The number.
Binary Tree. Binary Trees – An Informal Definition A binary tree is a tree in which no node can have more than two children Each node has 0, 1, or 2 children.
Chapter 19: Binary Trees. Objectives In this chapter, you will: – Learn about binary trees – Explore various binary tree traversal algorithms – Organize.
Searching Chapter 7. Objectives Introduce sequential search. – Calculate the computational complexity of a successful search. Introduce binary search.
Lecture 81 Data Structures, Algorithms & Complexity Tree Algorithms GRIFFITH COLLEGE DUBLIN.
Data Structures Using C++1 Search Algorithms Sequential Search (Linear Search) Binary Search.
Data Structures & Algorithms CHAPTER 4 Searching Ms. Manal Al-Asmari.
AL-HUSEEN BIN TALAL UNIVERSITY College of Engineering Department of Computer Engineering Algorithms and Data Structures Search Algorithms Course No.:
Lecture 10 Trees –Definiton of trees –Uses of trees –Operations on a tree.
CISC220 Fall 2009 James Atlas Lecture 13: Trees. Skip Lists.
1 Section 1.4 Graphs and Trees A graph is set of objects called vertices or nodes where some pairs of objects may be connected by edges. (A directed graph.
C++ Programming: From Problem Analysis to Program Design, Second Edition Chapter 19: Searching and Sorting.
Advance Data Structure 1 College Of Mathematic & Computer Sciences 1 Computer Sciences Department م. م علي عبد الكريم حبيب.
CIS3023: Programming Fundamentals for CIS Majors II Summer 2010 Ganesh Viswanathan Searching Course Lecture Slides 28 May 2010 “ Some things Man was never.
Searching. Linear (Sequential) Search Search an array or list by checking items one at a time. Linear search is usually very simple to implement, and.
Chapter 6 Binary Trees. 6.1 Trees, Binary Trees, and Binary Search Trees Linked lists usually are more flexible than arrays, but it is difficult to use.
Balanced search trees: 2-3 trees. 2-3 trees allow us to process ordered lists in more efficient way than binary trees with an ordering property. Recall.
CSC 211 Data Structures Lecture 13
CMSC 341 B- Trees D. Frey with apologies to Tom Anastasio.
Starting at Binary Trees
1 Data Structures CSCI 132, Spring 2014 Lecture 36 Binary Search Trees.
Trees  Linear access time of linked lists is prohibitive Does there exist any simple data structure for which the running time of most operations (search,
 Trees Data Structures Trees Data Structures  Trees Trees  Binary Search Trees Binary Search Trees  Binary Tree Implementation Binary Tree Implementation.
Data Structure Introduction.
Preview  Graph  Tree Binary Tree Binary Search Tree Binary Search Tree Property Binary Search Tree functions  In-order walk  Pre-order walk  Post-order.
Chapter 9 slide 1 Introduction to Search Algorithms Search: locate an item in a list (array, vector, table, etc.) of information Two algorithms (methods):
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.
1 Lower Bound on Comparison-based Search We have now covered lots of searching methods –Contiguous Data (Arrays) Sequential search Binary Search –Dynamic.
Chapter 4: Trees Part I: General Tree Concepts Mark Allen Weiss: Data Structures and Algorithm Analysis in Java.
Computer Science and Software Engineering University of Wisconsin - Platteville 10. Binary Search Tree Yan Shi CS/SE 2630 Lecture Notes Partially adopted.
Trees By P.Naga Srinivasu M.tech,(MBA). Basic Tree Concepts A tree consists of finite set of elements, called nodes, and a finite set of directed lines.
Discrete Mathematics Chapter 5 Trees.
DATA STRUCTURE BS(IT)3rd. Tree An Introduction By Yasir Mustafa Roll No. BS(IT) Bahauddin Zakariya University, Multan.
1 Chapter 13-2 Applied Arrays: Lists and Strings Dale/Weems.
Lecture 9COMPSCI.220.FS.T Lower Bound for Sorting Complexity Each algorithm that sorts by comparing only pairs of elements must use at least 
Copyright © 2012 Pearson Education, Inc. Chapter 20: Binary Trees.
Lecture 10COMPSCI.220.FS.T Binary Search Tree BST converts a static binary search into a dynamic binary search allowing to efficiently insert and.
Searching ( 搜索) 1. Introduction 2. Sequential Search 3. Binary Search 4. Comparison Trees.
Trees and Graphs CSE 2320 – Algorithms and Data Structures Vassilis Athitsos University of Texas at Arlington 1.
Chapter 6 – Trees. Notice that in a tree, there is exactly one path from the root to each node.
 Introduction to Search Algorithms  Linear Search  Binary Search 9-2.
8/3/2007CMSC 341 BTrees1 CMSC 341 B- Trees D. Frey with apologies to Tom Anastasio.
Kruse/Ryba Ch071 Object Oriented Data Structures Searching STL Vector Sequential Search Binary Search Comparison Trees.
Chapter 11. Chapter Summary  Introduction to trees (11.1)  Application of trees (11.2)  Tree traversal (11.3)  Spanning trees (11.4)
Searching Arrays Linear search Binary search small arrays
Trees ---- Soujanya.
CISC220 Fall 2009 James Atlas Lecture 13: Binary Trees.
Source Code for Data Structures and Algorithm Analysis in C (Second Edition) – by Weiss
Binary Search Tree Chapter 10.
Section 8.1 Trees.
Chapter 20: Binary Trees.
Chapter 21: Binary Trees.
Data Structures & Algorithms
Binary Trees.
General Tree Concepts Binary Trees
Data Structures Using C++ 2E
Presentation transcript:

1 Data Structures CSCI 132, Spring 2014 Lecture23 Analyzing Search Algorithms

2 Binary Search—Forgetful version Examines the element in the middle of the array. Is the middle element too small? Then start looking in second half of array. Otherwise: Begin looking in first half of the array. Repeat the process in the half of the list that should be examined next. Stop when there is only one item left, or when there is nowhere else to search. If the one item matches the target, the search was successful. Otherwise the item was not in the list.

3 Binary Search-- The forgetful version Error_code recursive_binary_1(const Ordered_list &the_list, const Key &target, int bottom, int top, int &position) { Record data; if (bottom < top) { // List has more than one entry. //we will work this out in class } else if (top < bottom) { return not_present; // List is empty. } else { // List has exactly one entry. position = bottom; the_list.retrieve(bottom, data); if (data == target) return success; else return not_present; }

4 Binary Search-- The forgetful version Error_code recursive_binary_1(const Ordered_list &the_list, const Key &target, int bottom, int top, int &position) { Record data; if (bottom < top) { // List has more than one entry. int mid = (bottom + top)/2; the_list.retrieve(mid, data); if (data < target) // Reduce to top half of list. return recursive_binary_1(the_list, target, mid + 1, top, position); else // Reduce to bottom half of list. return recursive_binary_1(the_list, target, bottom, mid, position); } else if (top < bottom) { return not_present; // List is empty. } else { // List has exactly one entry. position = bottom; the_list.retrieve(bottom, data); if (data == target) return success; else return not_present; }

5 Binary Search—recognizing equality Examines the element in the middle of the array. Is it the sought item? If so, stop searching. Is the middle element too small? Then start looking in second half of array. Is the middle element too large? Then begin looking in first half of the array. Repeat the process in the half of the list that should be examined next. Stop when item is found, or when there is nowhere else to look and it has not been located.

6 Binary Search-- Recognizing equality Error_code recursive_binary_2(const Ordered_list &the_list, const Key &target, int bottom, int top, int &position) { Record data; if (bottom <= top) { // List has more than one entry. //we will work this out in class } else { return not_present; // List is empty. }

7 Binary Search-- Recognizing equality Error_code recursive_binary_2(const Ordered_list &the_list, const Key &target, int bottom, int top, int &position) { Record data; if (bottom <= top) { // List has more than one entry. int mid = (bottom + top)/2; the_list.retrieve(mid, data); if (data == target) { position = mid; return success; } else if (data < target) // Reduce to top half of list. return recursive_binary_2(the_list, target, mid + 1, top, position); else // Reduce to bottom half of list. return recursive_binary_2(the_list, target, bottom, mid -1, position); } else { return not_present; // List is empty. }

8 Comparison Trees Tree for sequential search: root vertex (node) height level 0 level 1 level 2 parent child leaf

9 Comparison tree for binary search 1

10 Average number of comparisons for binary search 1 The number of comparisons = external path length External path length = Sum of the number of branches traversed in going from root to leaf once for each leaf. For n = 10, external path length = ? Answer: ((4*5) + (6*4))*2 = 88 Half the leaves are for successful searches. Half the leaves are for unsuccessful searches. Average number of comparisons for successful searches is? Answer: 0.5(88)/10 = 4.4 Average number of comparisons for unsuccessful searches is? Answer: 4.4

11 Comparison tree for binary search 2

12 Average number of comparisons for binary search 2 For successful searches--average number of comparisons is related to the internal path length. Internal path length = sum of the number of branches traversed from root to vertex for each non-leaf vertex. Internal path length for n = 10 is ? Answer: ( ) = 19 Number of vertices traversed on each path is one more than path length. Number of comparisons is 2 for each non-terminating vertex and 1 for each terminating vertex. Total comparisons = ? Answer: 2*( ) - 10 = 48 Average number of comparisons = ? Answer: 48/10 = 4.8 (worse than binary search 1)

13 Average number of comparisons for binary search 2 For unsuccessful searches--average number of comparisons is related to external path length. For binary search 2 tree, external path length = ? Answer: (5*3) + (6*4) = 39 Since there are 2 comparisons per vertex, and 11 possible unsuccessful terminations, then: Average number of comparisons = ? Answer: 2*39/11 = 7.1

14 In general, for lists of length n: SuccessfulUnsuccessful Binary Search 1 lg n + 1 lg n + 1 Binary Search 2 2 lg n lg n

15 Graphing the comparisons