Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
75 views

Data Structures and Algorithms - L6

This document discusses lower bounds on sorting algorithms. It states that the best worst-case sorting algorithm so far has a time complexity of O(N log N). It then proves a lower bound of Ω(N log N) for comparison-based sorting, showing that no algorithm can sort faster than this using only comparisons. It introduces decision trees to model sorting algorithms and proves that any sorting algorithm requires a decision tree of height Ω(N log N) due to the number of possible permutations. This establishes that comparison-based sorting requires Ω(N log N) time.

Uploaded by

vidula
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
75 views

Data Structures and Algorithms - L6

This document discusses lower bounds on sorting algorithms. It states that the best worst-case sorting algorithm so far has a time complexity of O(N log N). It then proves a lower bound of Ω(N log N) for comparison-based sorting, showing that no algorithm can sort faster than this using only comparisons. It introduces decision trees to model sorting algorithms and proves that any sorting algorithm requires a decision tree of height Ω(N log N) due to the number of possible permutations. This establishes that comparison-based sorting requires Ω(N log N) time.

Uploaded by

vidula
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 17

Data Structures and Algorithms

Dr. N. L. Bhanu Murthy


BITS Pilani Computer Science & Information Systems Department
Pilani Campus
Lower Bound on Sorting Algorithms (comp based)

 What is the best we can do on comparison based sorting?

 Best worst-case sorting algorithm (so far) is O(N log N)

 Can we do better (or) Can we prove a lower bound on the


sorting problem, independent of the algorithm?

 For comparison sorting, we can show lower bound of Ω(N log N)

BITS Pilani, Pilani Campus


Decision Tree for Sorting Algorithms

A decision tree is a binary tree where:


– Each node
• lists all left-out open possibilities (for deciding)
– Path of each node
• represents a decided sorted prefix of elements
– Each branch
• represents an outcome of a particular comparison
– Each leaf
• represents a particular ordering of the original array elements

BITS Pilani, Pilani Campus


Root = all open
possibilities IF a<b:
possible
A decision tree to sort
three elements {a,b,c}
(assuming no
duplicates)
Height =
all remaining (lg n!)
open
possibilities IF a<c:
possible

Worst-case n! leaves
evaluation path in this tree
for any algorithm 4 4
BITS Pilani, Pilani Campus
Decision-tree example
Sort a1, a2, …, an 1:2

2:3 1:3

123 1:3 213 2:3

132 312 231 321

Each internal node is labeled i:j for i, j {1, 2,…, n}.


• The left subtree shows subsequent comparisons if ai  aj.
• The right subtree shows subsequent comparisons if ai  aj.
BITS Pilani, Pilani Campus
Decision-tree example
Sort a1, a2, a3 9, 4, 6 
1:2 94
2:3 1:3

123 1:3 213 2:3

132 312 231 321

Each internal node is labeled i:j for i, j {1, 2,…, n}.


• The left subtree shows subsequent comparisons if ai  aj.
• The right subtree shows subsequent comparisons if ai  aj.
BITS Pilani, Pilani Campus
Decision-tree example
Sort a1, a2, a3 9, 4, 6 
1:2

2:3 1:3 96


123 1:3 213 2:3

132 312 231 321

Each internal node is labeled i:j for i, j {1, 2,…, n}.


• The left subtree shows subsequent comparisons if ai  aj.
• The right subtree shows subsequent comparisons if ai  aj.
BITS Pilani, Pilani Campus
Decision-tree example
Sort a1, a2, a3 9, 4, 6  1:2

2:3 1:3

123 1:3 213


4  6 2:3
132 312 231 321

Each internal node is labeled i:j for i, j {1, 2,…, n}.


• The left subtree shows subsequent comparisons if ai  aj.
• The right subtree shows subsequent comparisons if ai  aj.
BITS Pilani, Pilani Campus
Decision-tree example
Sort a1, a2, a3 9, 4, 6 
1:2

2:3 1:3

123 1:3 213 2:3

132 312 231 321


469
Each leaf contains a permutation , ,…, (n) to
indicate that the ordering a(1) a(2)    a(n) has been
established.
BITS Pilani, Pilani Campus
Decision Tree for Sorting Algorithms

 The logic of any sorting algorithm that uses comparisons can be


represented by a decision tree

 In the worst case, the number of comparisons used by the


algorithm equals the HEIGHT OF THE DECISION TREE

 In the average case, the number of comparisons is the average


of the depths of all leaves

 There are N! different orderings of N elements

BITS Pilani, Pilani Campus


Lower bound for decision-tree sorting

Theorem. Any decision tree that can sort n elements must


have height (n lg n) .
Proof. The tree must contain  n! leaves, since there are n!
possible permutations.

A height-h binary tree has  2h leaves.

Thus, n!  2h .
h  lg(n!) (lg is mono. increasing)
 lg ((n/e)n) (Stirling’s formula)
= n lg n – n lg e
= (n lg n) .
since n lg n – n lg e n lg n for all n >= n0 (take n0 to be
100) BITS Pilani, Pilani Campus
Problem Complexity

if

if

BITS Pilani, Pilani Campus


Optimal Algorithm

Corollary. Merge sort and Heap Sort are asymptotically


optimal comparison sorting algorithms.

BITS Pilani, Pilani Campus


BITS Pilani, Pilani Campus
Comparing P and Q

BITS Pilani, Pilani Campus


Comparing P and Q

BITS Pilani, Pilani Campus


Thank You!!

BITS Pilani, Pilani Campus

You might also like