Lecture 9COMPSCI.220.FS.T - 20041 Lower Bound for Sorting Complexity Each algorithm that sorts by comparing only pairs of elements must use at least 

Slides:



Advertisements
Similar presentations
Introduction to Algorithms Quicksort
Advertisements

110/6/2014CSE Suprakash Datta datta[at]cse.yorku.ca CSE 3101: Introduction to the Design and Analysis of Algorithms.
Lower Bounds for Sorting, Searching and Selection
CSE Lecture 3 – Algorithms I
Heaps1 Part-D2 Heaps Heaps2 Recall Priority Queue ADT (§ 7.1.3) A priority queue stores a collection of entries Each entry is a pair (key, value)
© Neeraj Suri EU-NSF ICT March 2006 Dependable Embedded Systems & SW Group Introduction to Computer Science 2 Binary.
B+-Trees (PART 1) What is a B+ tree? Why B+ trees? Searching a B+ tree
© The McGraw-Hill Companies, Inc., Chapter 2 The Complexity of Algorithms and the Lower Bounds of Problems.
Binary Trees, Binary Search Trees COMP171 Fall 2006.
Sorting Chapter Sorting Consider list x 1, x 2, x 3, … x n We seek to arrange the elements of the list in order –Ascending or descending Some O(n.
2 -1 Chapter 2 The Complexity of Algorithms and the Lower Bounds of Problems.
Lists A list is a finite, ordered sequence of data items. Two Implementations –Arrays –Linked Lists.
Binary Search Trees1 Part-F1 Binary Search Trees   
C++ Programming: Program Design Including Data Structures, Third Edition Chapter 19: Heap Sort.
2 -1 Analysis of algorithms Best case: easiest Worst case Average case: hardest.
TTIT33 Algorithms and Optimization – Dalg Lecture 2 HT TTIT33 Algorithms and optimization Lecture 2 Algorithms Sorting [GT] 3.1.2, 11 [LD] ,
Objectives Learn how to implement the sequential search algorithm Explore how to sort an array using the selection sort algorithm Learn how to implement.
2 -1 Chapter 2 The Complexity of Algorithms and the Lower Bounds of Problems.
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.
E.G.M. Petrakissearching1 Searching  Find an element in a collection in the main memory or on the disk  collection: (K 1,I 1 ),(K 2,I 2 )…(K N,I N )
DAST 2005 Week 4 – Some Helpful Material Randomized Quick Sort & Lower bound & General remarks…
Priority Queues1 Part-D1 Priority Queues. Priority Queues2 Priority Queue ADT (§ 7.1.3) A priority queue stores a collection of entries Each entry is.
The Complexity of Algorithms and the Lower Bounds of Problems
Data Structures Using C++ 2E Chapter 11 Binary Trees and B-Trees.
Advanced Data Structures and Algorithms COSC-600 Lecture presentation-6.
Indexing. Goals: Store large files Support multiple search keys Support efficient insert, delete, and range queries.
CSCE 3110 Data Structures & Algorithm Analysis Binary Search Trees Reading: Chap. 4 (4.3) Weiss.
Chapter Tow Search Trees BY HUSSEIN SALIM QASIM WESAM HRBI FADHEEL CS 6310 ADVANCE DATA STRUCTURE AND ALGORITHM DR. ELISE DE DONCKER 1.
David Luebke 1 9/18/2015 CS 332: Algorithms Red-Black Trees.
Chapter 19: Binary Trees. Objectives In this chapter, you will: – Learn about binary trees – Explore various binary tree traversal algorithms – Organize.
DATA STRUCTURE & ALGORITHMS (BCS 1223) CHAPTER 8 : SEARCHING.
Advance Data Structure 1 College Of Mathematic & Computer Sciences 1 Computer Sciences Department م. م علي عبد الكريم حبيب.
CS 162 Intro to Programming II Searching 1. Data is stored in various structures – Typically it is organized on the type of data – Optimized for retrieval.
The Binary Heap. Binary Heap Looks similar to a binary search tree BUT all the values stored in the subtree rooted at a node are greater than or equal.
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.
Data Structure & Algorithm II.  Delete-min  Building a heap in O(n) time  Heap Sort.
The Lower Bounds of Problems
Sorting Fun1 Chapter 4: Sorting     29  9.
CSC 211 Data Structures Lecture 13
Starting at Binary Trees
Data Structures Using C++ 2E Chapter 9 Searching and Hashing Algorithms.
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,
1 Searching Searching in a sorted linked list takes linear time in the worst and average case. Searching in a sorted array takes logarithmic time in the.
1 Searching the dictionary ADT binary search binary search trees.
Data Structures and Algorithms TREE. Searching Sequential Searches Time is proportional to n We call this time complexity O(n) Pronounce this “big oh”
Lecture 8COMPSCI.220.FS.T Algorithm HeapSort J. W. J. Williams (1964): a special binary tree called heap to obtain an O(n log n) worst-case sorting.
Sorting Chapter Sorting Consider list x 1, x 2, x 3, … x n We seek to arrange the elements of the list in order –Ascending or descending Some O(n.
Lecture 11COMPSCI.220.FS.T Balancing an AVLTree Two mirror-symmetric pairs of cases to rebalance the tree if after the insertion of a new key to.
Chapter 18: Searching and Sorting Algorithms. Objectives In this chapter, you will: Learn the various search algorithms Implement sequential and binary.
1 Lower Bound on Comparison-based Search We have now covered lots of searching methods –Contiguous Data (Arrays) Sequential search Binary Search –Dynamic.
8 January Heap Sort CSE 2011 Winter Heap Sort Consider a priority queue with n items implemented by means of a heap  the space used is.
Data Structures Using C++
Red-Black Trees. Review: Binary Search Trees ● Binary Search Trees (BSTs) are an important data structure for dynamic sets ● In addition to satellite.
LIMITATIONS OF ALGORITHM POWER
Internal and External Sorting External Searching
Lecture 10COMPSCI.220.FS.T Binary Search Tree BST converts a static binary search into a dynamic binary search allowing to efficiently insert and.
1. Searching The basic characteristics of any searching algorithm is that searching should be efficient, it should have less number of computations involved.
CS6045: Advanced Algorithms Sorting Algorithms. Heap Data Structure A heap (nearly complete binary tree) can be stored as an array A –Root of tree is.
Chapter 4, Part II Sorting Algorithms. 2 Heap Details A heap is a tree structure where for each subtree the value stored at the root is larger than all.
BINARY TREES Objectives Define trees as data structures Define the terms associated with trees Discuss tree traversal algorithms Discuss a binary.
1 Data Structures CSCI 132, Spring 2014 Lecture23 Analyzing Search Algorithms.
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,
Binary Search Trees A binary search tree is a binary tree
Binary Search Tree Chapter 10.
(2,4) Trees 11/15/2018 9:25 AM Sorting Lower Bound Sorting Lower Bound.
Wednesday, April 18, 2018 Announcements… For Today…
(2,4) Trees 12/4/2018 1:20 PM Sorting Lower Bound Sorting Lower Bound.
(2,4) Trees 2/28/2019 3:21 AM Sorting Lower Bound Sorting Lower Bound.
Topic 5: Heap data structure heap sort Priority queue
Presentation transcript:

Lecture 9COMPSCI.220.FS.T Lower Bound for Sorting Complexity Each algorithm that sorts by comparing only pairs of elements must use at least  log 2 (n  )   n log 2 n n comparisons in the worst case (that is, for some “worst” input sequence) and in the average case. Stirling's approximation of the factorial ( n  ):

Lecture 9COMPSCI.220.FS.T Decision Tree for Sorting n Items Decision tree for n  3 : i  j - a comparison of a i and a j ijk - a sorted array (a i a j a k ) n  permutations  n  leaves Sorting in descending order of the numbers

Lecture 9COMPSCI.220.FS.T Decision Tree for Sorting n Items Decision tree for n  3 : an array a =  a 1, a 2, a 3 } Example: {35, 10, 17} –Comparison 1:2 (35  10)   left branch a 1  a 2 –Comparison 2:3 (10  17)  right branch a 2  a 3 –Comparison 1:3 (35  17)  left branch a 1  a 3 Sorted array 132  {a 1 =35, a 3 =17, a 2 =10}

Lecture 9COMPSCI.220.FS.T Decision Tree Decision tree of height h has L h ≤ 2 h leaves Mathematical induction: ∙h  1 : the tree of height 1 has L 1 ≤ 2 h leaves ∙h  1  h : let the tree of height h  1 have L h-1 ≤ 2 h-1 leaves; the tree of height h consists of a root and two subtrees at most of height h  1. Thus, L h = L h  1 + L h  1 ≤ 2 h  h  1  2 h

Lecture 9COMPSCI.220.FS.T Worst-Case Complexity of Sorting The lower bound for the least height h of a decision tree for sorting by pairwise comparisons which provides L h = 2 h  n  leaves is h  log 2 ( n  )  n log 2 n  1.44 n Thus, the worst-case complexity of the sorting is at least O(n log n)

Lecture 9COMPSCI.220.FS.T Average-Case Sorting Complexity Each n -element decision tree has an average height at least log (n  )  n log n Let H(D,k) be the sum of heights for all k leaves of a tree D and H(k) = min D H(D,k) denote the minimum sum of heights Math induction to prove that H(k)  k log k ∙k  1  Obviously, H(1)  0 ∙k  1  k  Let H(m)  m log m, m < k

Lecture 9COMPSCI.220.FS.T Average-Case Sorting Complexity The tree D with k leaves contains 2 subtrees, D 1 with m 1 < k leaves and D 2 with m 2 < k leaves just under the root ( m 1  m 2  k ): because the link to the root adds 1 to each leaf's height

Lecture 9COMPSCI.220.FS.T Average-Case Sorting Complexity

Lecture 9COMPSCI.220.FS.T Data Search Data record  Specific key Goal: to find all records with key s matching a given search key Purpose: –to access information in the record for processing, or –to update information in the record, or –to insert a new record or to delete the record

Lecture 9COMPSCI.220.FS.T Types of Search Static search : stored data is not changed –Given an integer search key X, return either the position of X in an array A of records or an indication that it is not present without altering the array A –If X occurs more than once, return any occurrence Dynamic search : the data may be inserted or deleted

Lecture 9COMPSCI.220.FS.T Sequential and Jump Search Sequential search is the only one for an unsorted array Successful / unsuccessful search: the O(n) worst-case and average-case complexity Jump search O(n 0.5 ) in a sorted array A of size n : T =  n  k  jumps of length k to the points B t = min{t  k, n} and the sequential search among k items in a t - th part such that B t  1 ≤ key ≤ B t  1; t = 1,…,T

Lecture 9COMPSCI.220.FS.T Jump Search O(n 0.5 ) Worst-case complexity:

Lecture 9COMPSCI.220.FS.T Binary Search O( log n) Ordered array: key 0 < key 1 < … < key n-1 Compare the search key with the record key i at the middle position i   (n  1)  2  –if key  key i, return i –if key  key i or key  key i, then it must be in the 1st or in the 2nd half of the array, respectively Apply the previous two steps to the chosen half of the array iteratively ( repeating halving principle )

Lecture 9COMPSCI.220.FS.T Implementation of Binary Search begin BinarySearch ( an integer array a of size n, a search key ) low ← 0; high ← n  1 while low ≤ high do middle ←  ( low + high ) / 2  if a [ middle ] < key then low ← middle + 1 else if a[ middle ] > key then high ← middle  1 else return middle end if end while return ItemNotFound end BinarySearch

Lecture 9COMPSCI.220.FS.T Binary search: detailed analysis

Lecture 9COMPSCI.220.FS.T Comparison structure: the binary (search) tree

Lecture 9COMPSCI.220.FS.T Worst-Case Complexity O(log n) of Binary Search Let n  2 k  1; k  1,2,…, then the binary tree is complete (each internal node has 2 children) The tree height is k  1 Each tree level l contains 2 l nodes for l  0 (the root), 1, …, k  2, k  1 (the leaves) l + 1 comparisons to find a key of level l The worst case : k = log 2 (n + 1) comparisons

Lecture 9COMPSCI.220.FS.T Average-Case Complexity O(log n) of Binary Search Let C i  l + 1 be the number of comparisons to find key i of level l ; i  0, …, n  1 ; l  0, …, k  1 Average number: There are 2 l nodes at the level l, so that: By math induction: S k  1  1+ (k  1) 2 k, so that