Docsity

CSCE 350 Announcement: Presorting & Balanced Search Trees - Prof. C. Huang, Study notes of Computer Science

Toggle sidebar
bg1
1
CSCE 350:
Data Structures and Algorithms
Chin-Tser Huang
University of South Carolina
03/06/2009 2
Announcement
๎š„Reading assignment: Chapter 6.3
๎š„Homework 5 is assigned on March 6 and is
due on Monday, March 16 in class
๎š„Please download it from class website
๎š„Have your answers neatly typed
๎š„Explain your answer clearly and adequately by
showing the steps
๎š„Study guide for 2nd midterm exam and
Homework 4 solution are available on class
website
๎š„Happy spring break!
03/06/2009 3
Transform and Conquer
Solve problem by transforming into:
๎š„a more convenient instance of the same problem (
instance
simplification
)
๎š„presorting
๎š„Gaussian elimination
๎š„a different representation of the same instance (
representation
change
)
๎š„balanced search trees
๎š„heaps and heapsort
๎š„polynomial evaluation by Hornerโ€™s rule
๎š„Fast Fourier Transform
๎š„a different problem altogether (
problem reduction
)
๎š„reductions to graph problems
๎š„linear programming
03/06/2009 4
Instance Simplification Example:
Presorting
๎š„Solve instance of problem by transforming into
another simpler/easier instance of the same
problem
๎š„Presorting:
Many problems involving lists are easier when
list is sorted. For example,
๎š„computing the median (a special case of selection
problem)
๎š„element uniqueness
๎š„computing the mode
๎š„searching
03/06/2009 5
Presorting Example 1:
Selection Problem
๎š„Find the
k
th smallest element in A[1],โ€ฆA[
n
]. Special cases:
๎š„
minimum: k
= 1
๎š„
maximum: k
=
n
๎š„
median: k
= โŽก
n
/2โŽค
๎š„Presorting-based algorithm
๎š„sort list (e.g., using mergesort)
๎š„return A[
k
]
๎š„Partition-based algorithm (Variable-size-decrease and conquer):
๎š„pivot/split at A[
s
] using partitioning algorithm from quicksort
๎š„if
s
=
k
return A[
s
]
๎š„else if
s<k
repeat with sublist A[
s
+1],โ€ฆA[
n
].
๎š„
else if s>k
repeat with sublist A[1],โ€ฆA[
s
-1].
03/06/2009 6
Notes on Selection Problem
๎š„Presorting-based algorithm: โ„ฆ(
n
log
n
) + ฮ˜(1) = โ„ฆ(
n
log
n
)
๎š„Partition-based algorithm (Variable-size-decrease and
conquer):
๎š„worst case: T(
n
) =T(
n
-1) + (
n
+1) ร†ฮ˜(
n
2)
๎š„best case: ฮ˜(
n
)
๎š„average case: T(
n
) =T(
n
/2) + (
n
+1) ร†ฮ˜(
n
)
๎š„Extra benefit: also identifies the
k
smallest elements (not just
the
k
th)
๎š„Special cases of max, min: better, simpler linear
algorithm (brute force)
๎š„Conclusion: Presorting does
not
help in this case.
pf3
pf4

Partial preview of the text

Download CSCE 350 Announcement: Presorting & Balanced Search Trees - Prof. C. Huang and more Study notes Computer Science in PDF only on Docsity!

CSCE 350:

Data Structures and Algorithms

Chin-Tser Huang

[email protected]

University of South Carolina

03/06/2009 2

Announcement

ย„ Reading assignment: Chapter 6.

ย„ Homework 5 is assigned on March 6 and is

due on Monday, March 16 in class

ย„ Please download it from class website

ย„ Have your answers neatly typed

ย„ Explain your answer clearly and adequately by

showing the steps

ย„ Study guide for 2nd^ midterm exam and

Homework 4 solution are available on class

website

ย„ Happy spring break!

03/06/2009 3

Transform and Conquer

Solve problem by transforming into:

ย„ a more convenient instance of the same problem (instance

simplification)

ย„ presorting ย„ Gaussian elimination

ย„ a different representation of the same instance (representation

change)

ย„ balanced search trees ย„ heaps and heapsort ย„ polynomial evaluation by Hornerโ€™s rule ย„ Fast Fourier Transform

ย„ a different problem altogether (problem reduction)

ย„ reductions to graph problems ย„ linear programming 03/06/2009 4

Instance Simplification Example:

Presorting

ย„ Solve instance of problem by transforming into

another simpler/easier instance of the same

problem

ย„ Presorting:

Many problems involving lists are easier when

list is sorted. For example,

ย„ computing the median (a special case of selection

problem)

ย„ element uniqueness

ย„ computing the mode

ย„ searching

03/06/2009 5

Presorting Example 1:

Selection Problem

ย„ Find thekth^ smallest element in A[1],โ€ฆA[n]. Special cases:

ย„ minimum: k = 1 ย„ maximum: k =n ย„ median: k = โŽก n/2โŽค ย„ Presorting-based algorithm ย„ sort list (e.g., using mergesort) ย„ return A[k] ย„ Partition-based algorithm (Variable-size-decrease and conquer): ย„ pivot/split at A[s] using partitioning algorithm from quicksort ย„ ifs=k return A[s] ย„ else ifs<k repeat with sublist A[s+1],โ€ฆA[n]. ย„ else if s>k repeat with sublist A[1],โ€ฆA[s-1].

03/06/2009 6

Notes on Selection Problem

ย„ Presorting-based algorithm: ฮฉ(n logn) + ฮ˜(1) = ฮฉ(n logn)

ย„ Partition-based algorithm (Variable-size-decrease and

conquer):

ย„ worst case: T(n) =T(n-1) + (n+1) ร† ฮ˜(n^2 )

ย„ best case: ฮ˜(n)

ย„ average case: T(n) =T(n/2) + (n+1) ร† ฮ˜(n)

ย„ Extra benefit: also identifies thek smallest elements (not just

thek th)

ย„ Special cases of max, min: better, simpler linear

algorithm (brute force)

ย„ Conclusion: Presorting doesnot help in this case.

03/06/2009 7

Presorting Example 2:

Element Uniqueness Problem

ย„ Presorting-based algorithm:

ย„ use mergesort (optimal): ฮ˜(n logn)

ย„ scan array to find repeated adjacent

elements: ฮ˜(n)

ย„ Brute force algorithm: compare all pairs of

elements: ฮ˜(n^2 )

ย„ Conclusion: Presorting yields significant

improvement

ฮ˜ ฮ˜((n lognlognn))

03/06/2009 8

Presorting Example 3:

Computing A Mode

ย„ Amode is the value that occurs most often in a given

list of numbers

ย„ For example: the mode of [5, 1, 5, 7, 6, 5, 7] is 5

ย„ Brute-force technique: construct a list to record the

frequency of each distinct element

ย„ In each iteration, thei-th element is compared to the stored

distinct elements. If a matching is found, its frequency is incremented by 1. Otherwise, current element is added to the list as a distinct element

ย„ Worst case complexity ฮ˜ฮ˜((nn 22 ), when all the given), when all the givennn elements areelements are

distinctdistinct

03/06/2009 9

Computing A Mode With

Presorting Algorithm

modevalue

i i runlength

modefrequency runlengthmodevalue runvalue

runlength modefrequency

runlength runlength

i runlength n Ai runlength runvalue

runlength runvalue Ai

i n

modfrequency

i

A

PresortModeA n

return

if

while and

while do

ALGORITHM

1 [ ]

1 ; []

0 highestfrequencyseensofar

Sort thearray

([ 0 .. 1 ])

03/06/2009 10

Complexity ofPresortMode()

ย„ The dominating part is on the sorting ฮ˜(n logn)

ย„ The complexity of the outer while loop is linear

since each element will be visited once for

comparison

ย„ Therefore, the complexity ofpresortMode is ฮ˜(n

logn)

ย„ This is much more efficient than the brute-force

algorithm that needs ฮ˜ฮ˜((nn 22 ))

03/06/2009 11

Presorting Example 4:

Searching Problem

ย„ Brute-force search: worst case ฮ˜(n).

ย„ Presorted search T(n)=Tsort(n)+ Tsearch (n)

= ฮ˜(n logn)+ ฮ˜(logn) = ฮ˜(n logn)

ย„ Therefore, the presorted search is inferior to the brute-

force search

ย„ However, if we are going to repeat the searching in the

same list many times, presorted search may be more

efficient because the sorting need not be repeated

Binary search

03/06/2009 12

Taxonomy of Searching Algorithms

ย„ Elementary searching algorithms

ย„ sequential search ย„ binary search ย„ binary tree search

ย„ Balanced tree searching

ย„ AVL trees ย„ red-black trees ย„ multiway balanced trees (2-3 trees, 2-3-4 trees, B trees)

ย„ Hashing

ย„ separate chaining ย„ open addressing

03/06/2009 19

General Form: Double LR-rotation

03/06/2009 20

Notes on Rotations

ย„ Rotations can be done in constant time (steps)

ย„ Rotations not only guarantee that the resulting tree is

balanced, but also should preserve the basic property of

a binary search tree, i.e., all the keys in the left subtree

should be smaller than the root, the root should be

smaller than all the keys in the right subtree.

ย„ Height (h) of an AVL tree withn nodes is bounded by lgn โ‰ค h โ‰ค 1.4404 lg (n + 2) - 1. average: 1.01 lgn + 0.1 for largen

ย„ So the operations of searching and insertion in an AVL

tree take ฮ˜(logn) in the worst case

03/06/2009 21

Example: Construct an AVL Tree for

the List [5, 6, 8, 3, 2, 4, 7]

5

6

0

5

0 5

6

8

0

6

0

8

0 5

0

L (5)

6

1

5

1

3

0

8

0

6

2

5

2

3

1

2

0

8

0 >

R (5)

6

1

3

0

2

0

8

0

5

0

03/06/2009 22

Example: Construct an AVL Tree for

the List [5, 6, 8, 3, 2, 4, 7] (cont.)

6

2

3

2

0 5

1

4

0

8

0 >

LR (6)

5

0

3

0

2

0 4

0

6

8

0

5

3

0

2

0 4

0

6

8

1

7 0

>

RL (6)

5

0

3

0

2

0 4

0

7

0

8

0 6

0