SlideShare a Scribd company logo
1
CSE 417: Algorithms and
Computational Complexity
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

PPT
lecture4.ppt
PPT
Introduction to basic algorithm knowledge.ppt
PDF
Divide and conquer
PPTX
Introduction to Algorithms
PPTX
09 QUICK SORT Design and Analysis of algorithms
PPTX
Divide and Conquer
PPT
s4_quick_sort.ppt
PPTX
Divide and Conquer - Part 1
lecture4.ppt
Introduction to basic algorithm knowledge.ppt
Divide and conquer
Introduction to Algorithms
09 QUICK SORT Design and Analysis of algorithms
Divide and Conquer
s4_quick_sort.ppt
Divide and Conquer - Part 1

Similar to Algorithms and computational complexity.ppt (20)

PPTX
Quick sort.pptx
PPT
DAA-Divide and Conquer methodology, DAA 2024
PPT
MergesortQuickSort.ppt
PPT
presentation_mergesortquicksort_1458716068_193111.ppt
PDF
Sienna 4 divideandconquer
PPTX
CSE680-07QuickSort.pptx
PPTX
Data Structure and algorithms for software
PPTX
Divide and Conquer in DAA concept. For B Tech CSE
PPT
Algorithm.ppt
PDF
Daa notes 2
PPTX
Merge sort and quick sort
PPTX
Quick sort
PDF
module2_dIVIDEncONQUER_2022.pdf
PDF
Skiena algorithm 2007 lecture08 quicksort
PPTX
Beginning direct3d gameprogrammingmath02_logarithm_20160324_jintaeks
PPT
Ch04n
PPT
Data Structure Sorting
PPT
Quick Sort
Quick sort.pptx
DAA-Divide and Conquer methodology, DAA 2024
MergesortQuickSort.ppt
presentation_mergesortquicksort_1458716068_193111.ppt
Sienna 4 divideandconquer
CSE680-07QuickSort.pptx
Data Structure and algorithms for software
Divide and Conquer in DAA concept. For B Tech CSE
Algorithm.ppt
Daa notes 2
Merge sort and quick sort
Quick sort
module2_dIVIDEncONQUER_2022.pdf
Skiena algorithm 2007 lecture08 quicksort
Beginning direct3d gameprogrammingmath02_logarithm_20160324_jintaeks
Ch04n
Data Structure Sorting
Quick Sort
Ad

More from moh2020 (13)

PPT
Cours résumé sur l'algèbre mathématique.ppt
PPT
introduction aux réseaux industriels de terrain
PPTX
dynamic_capabilities_of_a_smart_city.pptx
PPT
The Introduction to Neural Networks.ppt
PPT
electronica-basica_i.ppt
PPT
Kommunikation.ppt
PPT
5942709.ppt
PPT
10250506.ppt
PPT
11861939.ppt
PPT
bus Can
PDF
MAS_Diaporama.pdf
PPT
3695812.ppt
PDF
sujet-et-corrige-bac-sti-genie-electronique-septembre-2008-physique-appliquee...
Cours résumé sur l'algèbre mathématique.ppt
introduction aux réseaux industriels de terrain
dynamic_capabilities_of_a_smart_city.pptx
The Introduction to Neural Networks.ppt
electronica-basica_i.ppt
Kommunikation.ppt
5942709.ppt
10250506.ppt
11861939.ppt
bus Can
MAS_Diaporama.pdf
3695812.ppt
sujet-et-corrige-bac-sti-genie-electronique-septembre-2008-physique-appliquee...
Ad

Recently uploaded (20)

PDF
keyrequirementskkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkk
PPT
Project quality management in manufacturing
PPTX
Construction Project Organization Group 2.pptx
PDF
SM_6th-Sem__Cse_Internet-of-Things.pdf IOT
PDF
Unit I ESSENTIAL OF DIGITAL MARKETING.pdf
PDF
Mitigating Risks through Effective Management for Enhancing Organizational Pe...
PDF
Enhancing Cyber Defense Against Zero-Day Attacks using Ensemble Neural Networks
PDF
Well-logging-methods_new................
PDF
Model Code of Practice - Construction Work - 21102022 .pdf
PPTX
FINAL REVIEW FOR COPD DIANOSIS FOR PULMONARY DISEASE.pptx
PDF
R24 SURVEYING LAB MANUAL for civil enggi
PPT
Mechanical Engineering MATERIALS Selection
PDF
Mohammad Mahdi Farshadian CV - Prospective PhD Student 2026
PDF
Evaluating the Democratization of the Turkish Armed Forces from a Normative P...
PPTX
Lecture Notes Electrical Wiring System Components
DOCX
ASol_English-Language-Literature-Set-1-27-02-2023-converted.docx
PDF
Operating System & Kernel Study Guide-1 - converted.pdf
PDF
Digital Logic Computer Design lecture notes
PPTX
Current and future trends in Computer Vision.pptx
DOCX
573137875-Attendance-Management-System-original
keyrequirementskkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkk
Project quality management in manufacturing
Construction Project Organization Group 2.pptx
SM_6th-Sem__Cse_Internet-of-Things.pdf IOT
Unit I ESSENTIAL OF DIGITAL MARKETING.pdf
Mitigating Risks through Effective Management for Enhancing Organizational Pe...
Enhancing Cyber Defense Against Zero-Day Attacks using Ensemble Neural Networks
Well-logging-methods_new................
Model Code of Practice - Construction Work - 21102022 .pdf
FINAL REVIEW FOR COPD DIANOSIS FOR PULMONARY DISEASE.pptx
R24 SURVEYING LAB MANUAL for civil enggi
Mechanical Engineering MATERIALS Selection
Mohammad Mahdi Farshadian CV - Prospective PhD Student 2026
Evaluating the Democratization of the Turkish Armed Forces from a Normative P...
Lecture Notes Electrical Wiring System Components
ASol_English-Language-Literature-Set-1-27-02-2023-converted.docx
Operating System & Kernel Study Guide-1 - converted.pdf
Digital Logic Computer Design lecture notes
Current and future trends in Computer Vision.pptx
573137875-Attendance-Management-System-original

Algorithms and computational complexity.ppt