Data Structures CSCI 262, Spring 2004 Lecture24 Analyzing Search II - PowerPoint PPT Presentation

1 / 12
About This Presentation
Title:

Data Structures CSCI 262, Spring 2004 Lecture24 Analyzing Search II

Description:

The number of comparisons = external path length ... Notation: log2(x) = lg(x), log10(x) = log(x), loge (x) = ln(x) 20. 21. 22. 0. 1. 2. Level ... – PowerPoint PPT presentation

Number of Views:41
Avg rating:3.0/5.0
Slides: 13
Provided by: mathcsHo
Category:

less

Transcript and Presenter's Notes

Title: Data Structures CSCI 262, Spring 2004 Lecture24 Analyzing Search II


1
Data Structures CSCI 262, Spring
2004Lecture24Analyzing Search II
2
Comparison tree for binary search 1
3
Comparison tree for binary search 2
4
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 88) Half the leaves are for successful
searches. Half the leaves are for unsuccessful
searches. Average number of comparisons for
successful searches is 0.5(88)/10 4.4 Average
number of comparisons for unsuccessful searches
is also 4.4.
5
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 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 2(1910) - 10 48 Average number
of comparisons 48/10 4.8 (worse than binary
search 1)
6
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 39 Since there are 2 comparisons per
vertex, and 11 possible unsuccessful
terminations, then Average number of
comparisons 239/11 7.1
7
In general, for lists of length n
Successful Unsuccessful Binary Search 1 lg
n 1 lg n 1 Binary Search 2 2 lg n - 3
2 lg n
8
Graphing the comparisons
9
2-trees
2-trees Each non-leaf vertex has at most 2
children.
Number of Vertices
Level
20
0
21
1
22
2
Number of vertices at level t is at most 2t If
there are k vertices at level t, then t gt
log2(k) lg(k) Notation log2(x) lg(x),
log10(x) log(x), loge (x) ln(x)
10
Running time of Binary Search 1
For Binary Search 1, t lg k (Notation x
ceiling of x smallest positive integergt x
x floor of x largest positive integer lt x
) It can be shown that, for any search that
uses comparison of keys to find target, must make
lg k comparisons in the worst case, and lg k
comparisons in the average case. Therefore,
Binary Search 1 is optimal for a search based on
key comparisons.
11
Comparing Running Times
If f(n) describes the running time of some
algorithm with respect to the input size, n, we
would like to be able to compare f(n) with some
known function g(n) g(n) 1 Constant g(n)
log n Logarithmic g(n) n Linear g(n)
n2 Quadratic g(n) n3 Cubic g(n)
2n Exponential
12
Comparing f(n) with g(n)
To compare f(n) with g(n) as n gets large, we
take the limit of f(n)/g(n) as n approaches
infinity limn f(n)/g(n) 0 f(n) has
strictly smaller order of magnitude than
g(n). limn f(n)/g(n) finite, non-zero f(n)
is the same order of magnitude as
g(n) limn f(n)/g(n) f(n) has strictly
greater order of magnitude than g(n)
Write a Comment
User Comments (0)
About PowerShow.com