SlideShare a Scribd company logo
1
CSE 417: Algorithms and
Computational Complexity
Winter 2001
Lecture 4
Instructor: Paul Beame
TA: Gidon Shavit
2
Master Divide and Conquer
Recurrence
If T(n)=aT(n/b)+cnk for n>b then
if a>bk then T(n) is
if a<bk then T(n) is Q(nk)
if a=bk then T(n) is Q(nk log n)
Works even if it is n/b instead of n/b.
)
( log a
b
n
Q
3
Multiplying Matrices
n3 multiplications, n3-n2 additions

























44
43
42
41
34
33
32
31
24
23
22
21
14
13
12
11
44
43
42
41
34
33
32
31
24
23
22
21
14
13
12
11
b
b
b
b
b
b
b
b
b
b
b
b
b
b
b
b
a
a
a
a
a
a
a
a
a
a
a
a
a
a
a
a

















































44
44
34
43
24
42
14
41
42
44
32
43
22
42
12
41
41
44
31
43
21
42
11
41
44
34
34
33
24
32
14
31
42
34
32
33
22
32
12
31
41
34
31
33
21
32
11
31
44
24
34
23
24
22
14
21
42
24
32
23
22
22
12
21
41
24
31
23
21
22
11
21
44
14
34
13
24
12
14
11
42
14
32
13
22
12
12
11
41
14
31
13
21
12
11
11
b
a
b
a
b
a
b
a
b
a
b
a
b
a
b
a
b
a
b
a
b
a
b
a
b
a
b
a
b
a
b
a
b
a
b
a
b
a
b
a
b
a
b
a
b
a
b
a
b
a
b
a
b
a
b
a
b
a
b
a
b
a
b
a
b
a
b
a
b
a
b
a
b
a
b
a
b
a
b
a
b
a
b
a
b
a
b
a
b
a
b
a
b
a
b
a




4
Multiplying Matrices

























44
43
42
41
34
33
32
31
24
23
22
21
14
13
12
11
44
43
42
41
34
33
32
31
24
23
22
21
14
13
12
11
b
b
b
b
b
b
b
b
b
b
b
b
b
b
b
b
a
a
a
a
a
a
a
a
a
a
a
a
a
a
a
a

















































44
44
34
43
24
42
14
41
42
44
32
43
22
42
12
41
41
44
31
43
21
42
11
41
44
34
34
33
24
32
14
31
42
34
32
33
22
32
12
31
41
34
31
33
21
32
11
31
44
24
34
23
24
22
14
21
42
24
32
23
22
22
12
21
41
24
31
23
21
22
11
21
44
14
34
13
24
12
14
11
42
14
32
13
22
12
12
11
41
14
31
13
21
12
11
11
b
a
b
a
b
a
b
a
b
a
b
a
b
a
b
a
b
a
b
a
b
a
b
a
b
a
b
a
b
a
b
a
b
a
b
a
b
a
b
a
b
a
b
a
b
a
b
a
b
a
b
a
b
a
b
a
b
a
b
a
b
a
b
a
b
a
b
a
b
a
b
a
b
a
b
a
b
a
b
a
b
a
b
a
b
a
b
a
b
a
b
a
b
a
b
a




5
Multiplying Matrices

























44
43
42
41
34
33
32
31
24
23
22
21
14
13
12
11
44
43
42
41
34
33
32
31
24
23
22
21
14
13
12
11
b
b
b
b
b
b
b
b
b
b
b
b
b
b
b
b
a
a
a
a
a
a
a
a
a
a
a
a
a
a
a
a

















































44
44
34
43
24
42
14
41
42
44
32
43
22
42
12
41
41
44
31
43
21
42
11
41
44
34
34
33
24
32
14
31
42
34
32
33
22
32
12
31
41
34
31
33
21
32
11
31
44
24
34
23
24
22
14
21
42
24
32
23
22
22
12
21
41
24
31
23
21
22
11
21
44
14
34
13
24
12
14
11
42
14
32
13
22
12
12
11
41
14
31
13
21
12
11
11
b
a
b
a
b
a
b
a
b
a
b
a
b
a
b
a
b
a
b
a
b
a
b
a
b
a
b
a
b
a
b
a
b
a
b
a
b
a
b
a
b
a
b
a
b
a
b
a
b
a
b
a
b
a
b
a
b
a
b
a
b
a
b
a
b
a
b
a
b
a
b
a
b
a
b
a
b
a
b
a
b
a
b
a
b
a
b
a
b
a
b
a
b
a
b
a




6
Multiplying Matrices

























44
43
42
41
34
33
32
31
24
23
22
21
14
13
12
11
44
43
42
41
34
33
32
31
24
23
22
21
14
13
12
11
b
b
b
b
b
b
b
b
b
b
b
b
b
b
b
b
a
a
a
a
a
a
a
a
a
a
a
a
a
a
a
a

















































44
44
34
43
24
42
14
41
42
44
32
43
22
42
12
41
41
44
31
43
21
42
11
41
44
34
34
33
24
32
14
31
42
34
32
33
22
32
12
31
41
34
31
33
21
32
11
31
44
24
34
23
24
22
14
21
42
24
32
23
22
22
12
21
41
24
31
23
21
22
11
21
44
14
34
13
24
12
14
11
42
14
32
13
22
12
12
11
41
14
31
13
21
12
11
11
b
a
b
a
b
a
b
a
b
a
b
a
b
a
b
a
b
a
b
a
b
a
b
a
b
a
b
a
b
a
b
a
b
a
b
a
b
a
b
a
b
a
b
a
b
a
b
a
b
a
b
a
b
a
b
a
b
a
b
a
b
a
b
a
b
a
b
a
b
a
b
a
b
a
b
a
b
a
b
a
b
a
b
a
b
a
b
a
b
a
b
a
b
a
b
a




A11 A12
A21
A11B12+A12B22
A22
A11B11+A12B21
B11 B12
B21 B22
A21B12+A22B22
A21B11+A22B21
7
Multiplying Matrices
T(n)=8T(n/2)+4(n/2)2=8T(n/2)+n2
8>22 so T(n) is
A11 A12
A21
A11B12+A12B22
A22
A11B11+A12B21
B11 B12
B21 B22
A21B12+A22B22
A21B11+A22B21
=
)
(
)
(
)
( 3
log
log
n
n
n 8
a 2
b
Q

Q

Q
8
Strassen’s algorithm
Strassen’s algorithm
Multiply 2x2 matrices using 7 instead of 8
multiplications (and lots more than 4 additions)
T(n)=7 T(n/2)+cn2
7>22 so T(n) is Q(n ) which is O(n2.81)
Fastest algorithms theoretically use O(n2.376) time
not practical but Strassen’s is practical provided
calculations are exact and we stop recursion when
matrix has size about 100 (maybe 10)
log27
9
The algorithm
P1=A12(B11+B21) P2=A21(B12+B22)
P3=(A11 - A12)B11 P4=(A22 - A21)B22
P5=(A22 - A12)(B21 - B22)
P6=(A11 - A21)(B12 - B11)
P7= (A21 - A12)(B11+B22)
C11=P1+P3 C12=P2+P3+P6 - P7
C21= P1+P4+P5+P7 C22=P2+P4
10
Proving Master recurrence
T(n)=aT(n/b)+cnk
a
n
Problem size
n/b
n/b2
b
1
# probs
a2
a
1
ad
T(1)=c
11
Proving Master recurrence
T(n)=aT(n/b)+cnk
a
n
Problem size
n/b
n/b2
b
1
# probs
a2
a
1
ad
T(1)=c
12
Proving Master recurrence
T(n)=aT(n/b)+cnk
a
n
Problem size
n/b
n/b2
b
1
# probs
a2
a
1
ad
cost
cnk
T(1)=c
cank/bk
ca2nk/b2k
=cnk(a/bk)2
cnk(a/bk)d
=cad
13
Total Cost
Geometric series
ratio a/bk
d+1=logbn +1 terms
first term cnk, last term cad
If a/bk=1, all terms are equal T(n) is Q(nk log n)
If a/bk<1, first term is largest T(n) is Q(nk)
If a/bk>1, last term is largest
T(n) is Q(ad)=Q(a ) =Q(n )
(To see this take logb of both sides)
logbn logba
14
Quicksort
Quicksort(X,left,right)
if left < right
split=Partition(X,left,right)
Quicksort(X,left,split-1)
Quicksort(X,split+1,right)
15
Partition - two finger algorithm
Partition(X,left,right)
choose a random element to be a pivot and
pull it out of the array, say at left end
maintain two fingers starting at each end of
the array
slide them towards each other until you get a
pair of elements where right finger has
a smaller element and left finger has a
bigger one (when compared to pivot)
swap them and repeat until fingers meet
put the pivot element where they meet
16
Partition - two finger algorithm
Partition(X,left,right)
swap X[left],X[random(left,right)]
pivot  X[left]; L  left; R right
while L<R do
while (X[L]  pivot & L  right) do
L  L+1
while (X[R] > pivot & R  left) do
R  R-1
if L>R then swap X[L],X[R]
swap X[left],X[R]
return R
17
In practice
often choose pivot in fixed way as
middle element for small arrays
median of 1st, middle, and last for larger
arrays
median of 3 medians of 3 (9 elements in all)
for largest arrays
four finger algorithm is better
also maintain two groups at each end of
elements equal to the pivot
• swap them all into middle at the end of Partition
equal elements are bad cases for two fingers
18
Quicksort Analysis
Partition does n-1 comparisons on a list of
length n
pivot is compared to each other element
If pivot is ith largest then two subproblems
are of size i-1 and n-i
Pivot is equally likely to be any one of 1st
through nth largest
 








n
1
i
i)
T(n
1)
T(i
n
1
1
n
T(n)
19
Quicksort analysis
 
2)
1)(n
(n
2n
1
n
T(n)
2
n
1)
T(n
2n
2)T(n)
(n
1)
1)T(n
(n
2n
T(n)
2
nT(n)
-
1)
1)T(n
(n
T(n)
2
...
T(2)
2
T(1)
2
1)n
(n
1)
1)T(n
(n
1)
-
T(n
2
...
T(2)
2
T(1)
2
1)
-
n(n
nT(n)
n
1)
-
T(n
2
...
T(2)
2
T(1)
2
1
-
n
i)
T(n
1)
T(i
n
1
1
n
T(n)
n
1
i










































 

20
Quicksort analysis
n
n
1.38
T(n)
dx)
1/x
n
that
(Recall
n
38
1
n
2
2H
n
1
3
1
2
1
2(1
Q(n)
1
n
2
Q(n)
1)
Q(n
1
n
T(n)
Q(n)
Let
2
n
2
n
log
ln
ln
1
log
.
)
...





















More Related Content

Similar to lecture4.ppt

CS330-Lectures Statistics And Probability
CS330-Lectures Statistics And ProbabilityCS330-Lectures Statistics And Probability
CS330-Lectures Statistics And Probability
bryan111472
 
Design and Analysis of Algorithms - Divide and Conquer
Design and Analysis of Algorithms - Divide and ConquerDesign and Analysis of Algorithms - Divide and Conquer
Design and Analysis of Algorithms - Divide and Conquer
Seshu Chakravarthy
 
Quicksort analysis
Quicksort analysisQuicksort analysis
Quicksort analysis
Premjeet Roy
 
Sienna 4 divideandconquer
Sienna 4 divideandconquerSienna 4 divideandconquer
Sienna 4 divideandconquer
chidabdu
 
lecture 10
lecture 10lecture 10
lecture 10
sajinsc
 
lecture 3
lecture 3lecture 3
lecture 3
sajinsc
 
Sorting algorithums > Data Structures & Algorithums
Sorting algorithums  > Data Structures & AlgorithumsSorting algorithums  > Data Structures & Algorithums
Sorting algorithums > Data Structures & Algorithums
Ain-ul-Moiz Khawaja
 
Weatherwax cormen solutions
Weatherwax cormen solutionsWeatherwax cormen solutions
Weatherwax cormen solutions
kirankoushik
 

Similar to lecture4.ppt (20)

CS330-Lectures Statistics And Probability
CS330-Lectures Statistics And ProbabilityCS330-Lectures Statistics And Probability
CS330-Lectures Statistics And Probability
 
CSE680-07QuickSort.pptx
CSE680-07QuickSort.pptxCSE680-07QuickSort.pptx
CSE680-07QuickSort.pptx
 
Sorting
SortingSorting
Sorting
 
2.pptx
2.pptx2.pptx
2.pptx
 
pradeepbishtLecture13 div conq
pradeepbishtLecture13 div conqpradeepbishtLecture13 div conq
pradeepbishtLecture13 div conq
 
Design and Analysis of Algorithms - Divide and Conquer
Design and Analysis of Algorithms - Divide and ConquerDesign and Analysis of Algorithms - Divide and Conquer
Design and Analysis of Algorithms - Divide and Conquer
 
Sorting2
Sorting2Sorting2
Sorting2
 
Quicksort analysis
Quicksort analysisQuicksort analysis
Quicksort analysis
 
Sienna 4 divideandconquer
Sienna 4 divideandconquerSienna 4 divideandconquer
Sienna 4 divideandconquer
 
Admission in india 2015
Admission in india 2015Admission in india 2015
Admission in india 2015
 
Daa divide-n-conquer
Daa divide-n-conquerDaa divide-n-conquer
Daa divide-n-conquer
 
L2
L2L2
L2
 
lecture 10
lecture 10lecture 10
lecture 10
 
Recurrent Problem in Discrete Mathmatics.pptx
Recurrent Problem in Discrete Mathmatics.pptxRecurrent Problem in Discrete Mathmatics.pptx
Recurrent Problem in Discrete Mathmatics.pptx
 
lecture 3
lecture 3lecture 3
lecture 3
 
Arithmetic Progressions and the Construction of Doubly Even Magic Squares and...
Arithmetic Progressions and the Construction of Doubly Even Magic Squares and...Arithmetic Progressions and the Construction of Doubly Even Magic Squares and...
Arithmetic Progressions and the Construction of Doubly Even Magic Squares and...
 
07 Analysis of Algorithms: Order Statistics
07 Analysis of Algorithms: Order Statistics07 Analysis of Algorithms: Order Statistics
07 Analysis of Algorithms: Order Statistics
 
Sorting algorithums > Data Structures & Algorithums
Sorting algorithums  > Data Structures & AlgorithumsSorting algorithums  > Data Structures & Algorithums
Sorting algorithums > Data Structures & Algorithums
 
Master method
Master method Master method
Master method
 
Weatherwax cormen solutions
Weatherwax cormen solutionsWeatherwax cormen solutions
Weatherwax cormen solutions
 

Recently uploaded

Article writing on excessive use of internet.pptx
Article writing on excessive use of internet.pptxArticle writing on excessive use of internet.pptx
Article writing on excessive use of internet.pptx
abhinandnam9997
 
一比一原版UTS毕业证悉尼科技大学毕业证成绩单如何办理
一比一原版UTS毕业证悉尼科技大学毕业证成绩单如何办理一比一原版UTS毕业证悉尼科技大学毕业证成绩单如何办理
一比一原版UTS毕业证悉尼科技大学毕业证成绩单如何办理
aagad
 

Recently uploaded (12)

History+of+E-commerce+Development+in+China-www.cfye-commerce.shop
History+of+E-commerce+Development+in+China-www.cfye-commerce.shopHistory+of+E-commerce+Development+in+China-www.cfye-commerce.shop
History+of+E-commerce+Development+in+China-www.cfye-commerce.shop
 
Article writing on excessive use of internet.pptx
Article writing on excessive use of internet.pptxArticle writing on excessive use of internet.pptx
Article writing on excessive use of internet.pptx
 
一比一原版UTS毕业证悉尼科技大学毕业证成绩单如何办理
一比一原版UTS毕业证悉尼科技大学毕业证成绩单如何办理一比一原版UTS毕业证悉尼科技大学毕业证成绩单如何办理
一比一原版UTS毕业证悉尼科技大学毕业证成绩单如何办理
 
The AI Powered Organization-Intro to AI-LAN.pdf
The AI Powered Organization-Intro to AI-LAN.pdfThe AI Powered Organization-Intro to AI-LAN.pdf
The AI Powered Organization-Intro to AI-LAN.pdf
 
1.Wireless Communication System_Wireless communication is a broad term that i...
1.Wireless Communication System_Wireless communication is a broad term that i...1.Wireless Communication System_Wireless communication is a broad term that i...
1.Wireless Communication System_Wireless communication is a broad term that i...
 
The+Prospects+of+E-Commerce+in+China.pptx
The+Prospects+of+E-Commerce+in+China.pptxThe+Prospects+of+E-Commerce+in+China.pptx
The+Prospects+of+E-Commerce+in+China.pptx
 
ER(Entity Relationship) Diagram for online shopping - TAE
ER(Entity Relationship) Diagram for online shopping - TAEER(Entity Relationship) Diagram for online shopping - TAE
ER(Entity Relationship) Diagram for online shopping - TAE
 
The Best AI Powered Software - Intellivid AI Studio
The Best AI Powered Software - Intellivid AI StudioThe Best AI Powered Software - Intellivid AI Studio
The Best AI Powered Software - Intellivid AI Studio
 
The Use of AI in Indonesia Election 2024: A Case Study
The Use of AI in Indonesia Election 2024: A Case StudyThe Use of AI in Indonesia Election 2024: A Case Study
The Use of AI in Indonesia Election 2024: A Case Study
 
Stay Ahead with 2024's Top Web Design Trends
Stay Ahead with 2024's Top Web Design TrendsStay Ahead with 2024's Top Web Design Trends
Stay Ahead with 2024's Top Web Design Trends
 
How to Use Contact Form 7 Like a Pro.pptx
How to Use Contact Form 7 Like a Pro.pptxHow to Use Contact Form 7 Like a Pro.pptx
How to Use Contact Form 7 Like a Pro.pptx
 
Multi-cluster Kubernetes Networking- Patterns, Projects and Guidelines
Multi-cluster Kubernetes Networking- Patterns, Projects and GuidelinesMulti-cluster Kubernetes Networking- Patterns, Projects and Guidelines
Multi-cluster Kubernetes Networking- Patterns, Projects and Guidelines
 

lecture4.ppt