lecture 10 jianjun hu department of computer science and engineering university of south carolina...

18
Lecture 10 Jianjun Hu Department of Computer Science and Engineering University of South Carolina 2009.9. CSCE350 Algorithms and Data Structure

Upload: magdalene-scott

Post on 15-Jan-2016

216 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Lecture 10 Jianjun Hu Department of Computer Science and Engineering University of South Carolina 2009.9. CSCE350 Algorithms and Data Structure

Lecture 10

Jianjun Hu

Department of Computer Science and Engineering

University of South Carolina

2009.9.

CSCE350 Algorithms and Data Structure

Page 2: Lecture 10 Jianjun Hu Department of Computer Science and Engineering University of South Carolina 2009.9. CSCE350 Algorithms and Data Structure

Outline

Divide and Conquer for algorithm design

Binary Tree traversal

Multiplication of numbers

Strassen’s Matrix Multiplication

Page 3: Lecture 10 Jianjun Hu Department of Computer Science and Engineering University of South Carolina 2009.9. CSCE350 Algorithms and Data Structure

Quicksort Example

5 3 1 9 8 2 4 7

l=3, r=3

l=5, r=7

s=6

(b)

l=2, r=1

l=2, r=3

s=2

l=0, r=0

l=0, r=3

s=1

l=0, r=7

s=4

l=7, r=7l=5, r=5

Page 4: Lecture 10 Jianjun Hu Department of Computer Science and Engineering University of South Carolina 2009.9. CSCE350 Algorithms and Data Structure

Example

Search for K=70

rmliter

rmliter

rmliter

value

index

,3#

2#

1#

9893858174705542393127143

1211109876543210

Page 5: Lecture 10 Jianjun Hu Department of Computer Science and Engineering University of South Carolina 2009.9. CSCE350 Algorithms and Data Structure

Time Efficiency

In the worst case, no key K exists in this array

Although this is an nonrecursive algorithm, we can see that the time efficiency can be analyzed using the recurrence relation

T(n)=T(n/2)+1 for n>1, T(1)=1

T(n) --- Θ(logn)

Exact solution:

Binary search is in fact not a typical example of divide and conquer because it does not solve two subproblems.

)1(log1log)( 22 nnnT

Page 6: Lecture 10 Jianjun Hu Department of Computer Science and Engineering University of South Carolina 2009.9. CSCE350 Algorithms and Data Structure

Binary Tree Traversals

A binary tree T is defined as a finite set of nodes that is either empty or consists of a root and two disjoint binary trees TL and TR called the left and right subtree of the root

Internal and external nodes: #ext_node = #internal_node+1

T TL R

Page 7: Lecture 10 Jianjun Hu Department of Computer Science and Engineering University of South Carolina 2009.9. CSCE350 Algorithms and Data Structure

Height of a binary tree

Input size n(T): # nodes in T, basic operation: “+”

Recurrence

A(n)=n why?

If the basic operation is the line to check whether a tree is empty A(n)=2n+1 why?

1)}(),(max{

1

)(

rL THeightTHeight

T

THeight

return else

return if

ALGORITHM

0)0(

0)(,1))(())(())((

A

TnTnATnATnA rL for

Page 8: Lecture 10 Jianjun Hu Department of Computer Science and Engineering University of South Carolina 2009.9. CSCE350 Algorithms and Data Structure

Traverse the binary tree

List all the nodes

Preorder traversal: root left subtree right subtree

Inorder traversal: left subtree root right subtree

Postorder traversal: left subtree right subtree root

What is the efficiency?

)(

)(

)(

r

L

root

Tpreorder

Tpreorder

T

T

Tpreorder

output

if

ALGORITHM

Page 9: Lecture 10 Jianjun Hu Department of Computer Science and Engineering University of South Carolina 2009.9. CSCE350 Algorithms and Data Structure

Large Integer Multiplication

Some applications, notably modern cryptology, require manipulation of integers that are over 100 decimal digits long

Such integers are too long to fit a single word of a computer

Therefore, they require special treatment

Consider the multiplication of two such long integers

If we use the classic pen-and-pencil algorithm to multiply two n-digit integers, we need n2 digit multiplications

Can we design a divide-and-conquer algorithm to solve this problem with better efficiency?

Page 10: Lecture 10 Jianjun Hu Department of Computer Science and Engineering University of South Carolina 2009.9. CSCE350 Algorithms and Data Structure

The Basic Idea

We want to calculate 23 x 14

Since

We have

Which includes four digit multiplications (n2)

But

Therefore, we only need three digit multiplications

0101 1041011410310223 and

012

0101

10)4*3(10)4*21*3(10)1*2(

)104101( *)103102(14*23

)4*3()1*2()41(*)32(

4*21*3

Page 11: Lecture 10 Jianjun Hu Department of Computer Science and Engineering University of South Carolina 2009.9. CSCE350 Algorithms and Data Structure

One Formula

Given a=a1a0 and b=b1b0, compute c=a*b

We have

That means only three digit multiplications are needed to multiply two 2-digit integers

)()(*)(

*

*

101010*

0201011

000

112

00

11

22

ccbbaac

bac

bac

cccbac

where

Page 12: Lecture 10 Jianjun Hu Department of Computer Science and Engineering University of South Carolina 2009.9. CSCE350 Algorithms and Data Structure

To Multiply Two n-digit integers

Assume n is even, write

Then

To calculate the involved three multiplications – recursion! Stops when n=1

02/

102/

1 1010 bbbaaa nn and

)()(*)(

*

*

101010*

0201011

000

112

00

2/12

ccbbaac

bac

bac

cccbac nn

where

Page 13: Lecture 10 Jianjun Hu Department of Computer Science and Engineering University of South Carolina 2009.9. CSCE350 Algorithms and Data Structure

Efficiency

The recurrence relation is

Solving it by backward substitution for n=2k yields

Therefore,

1)1(,1)2/(3)( TnnTnT for

kkkk

kkk

T

TTT

3)2(3

)2(3)2(3)2( 221

2585.13loglog 223)( nnnnT n

Page 14: Lecture 10 Jianjun Hu Department of Computer Science and Engineering University of South Carolina 2009.9. CSCE350 Algorithms and Data Structure

Strassen’s Matrix Multiplication

Brute-Force nxn matrix multiplication needs n3 number multiplications

For example, multiplying two 2x2 matrices needs 23=8 multiplications

1110

0100

1110

0100

1110

0100 *bb

bb

aa

aa

cc

cc

1111011011

1011001010

1101010001

1001000000

babac

babac

babac

babac

where

Page 15: Lecture 10 Jianjun Hu Department of Computer Science and Engineering University of South Carolina 2009.9. CSCE350 Algorithms and Data Structure

We can Reduce to 7 Multiplications

623142

537541

1110

0100

1110

0100

1110

0100 *

mmmmmm

mmmmmm

bb

bb

aa

aa

cc

cc

)(*)(

)(*)(

*)(

)(*

)(*

*)(

)(*)(

111011017

010000106

1101005

0010114

1101003

0011102

110011001

bbaam

bbaam

baam

bbam

bbam

baam

bbaam

where

Page 16: Lecture 10 Jianjun Hu Department of Computer Science and Engineering University of South Carolina 2009.9. CSCE350 Algorithms and Data Structure

Divide and Conquer

Partition the matrix into 4 submtrices with the size n/2xn/2

The above 7-multiplication 18 additions can be used here, but they are n/2xn/2 matrix multiplication and additions now

How to calculate the n/2xn/2 matrix multiplication?– recursion!

Stop condition, where the matrix size is 1x1.

Recurrence for efficiency analysis (based on # multiplication)

1110

0100

1110

0100

1110

0100 *BB

BB

AA

AA

CC

CC

1)1(,1)2/(7)( TnnTnT for

Page 17: Lecture 10 Jianjun Hu Department of Computer Science and Engineering University of South Carolina 2009.9. CSCE350 Algorithms and Data Structure

Solve the Recurrence

Solving it by backward substitution for n=2k yields

Therefore

Count the # of additions

which is the same as the complexity based on multiplication

kkkk

kkk

T

TTT

7)2(7

)2(7)2(7)2( 221

3807.27loglog 227)( nnnnT n

)()(

0)1(,1)2/(18)2/(7)(7log

2

2nnT

TnnnTnT

for

Page 18: Lecture 10 Jianjun Hu Department of Computer Science and Engineering University of South Carolina 2009.9. CSCE350 Algorithms and Data Structure

Many Improvements Along This Line

For example,

Coopersmith and Winograd

Getting closer to the theoretic lower bound

376.2n

2n