SlideShare ist ein Scribd-Unternehmen logo
1 von 27
Divide-and-Conquer
Republic of Yemen
THAMAR UNIVERSITY
Faculty of Computer Science&
Information System
1 Eng: Mohammed Hussein
Divide-and-Conquer
2 Eng: Mohammed Hussein
Divide-and-Conquer
 The whole problem we want to solve may too big to understand or solve at
once.
 We break it up into smaller pieces, solve the pieces separately, and
combine the separate pieces together.
 Divide-and conquer is a general algorithm design paradigm:
1. Divide: divide the input data S in two or more disjoint subsets S1,
S2, …
2. Recur: solve the subproblems recursively
3. Conquer: combine the solutions for S1, S2, …, into a solution for S
 The base case for the recursion are subproblems of constant size
 Analysis can be done using recurrence equations
3 Eng: Mohammed Hussein
Recurrence Equation Analysis
 The conquer step of merge-sort consists of merging two sorted sequences, each with
n/2 elements and implemented by means of a doubly linked list, takes at most bn steps,
for some constant b.
 Likewise, the basis case (n < 2) will take at b most steps.
 Therefore, if we let T(n) denote the running time of merge-sort:
 We can therefore analyze the running time of merge-sort by finding a closed form
solution to the above equation.
 That is, a solution that has T(n) only on the left-hand side.






2if)2/(2
2if
)(
nbnnT
nb
nT
4 Eng: Mohammed Hussein
Solving recurrences
 Recurrences are like solving integrals, differential equations, etc.
 Should learn a few tricks.
 Three ways of solving recurrences
1. Guess-and-Test Method(If we know the answer)
2. RecursionTree Method (Very useful !)
3. MasterTheorem (Save our effort)
5 Eng: Mohammed Hussein
Guess-and-Test Method
 In the guess-and-test method, we guess a closed form solution and then try to prove it is
true by induction:
 Guess that:T(n) < c n log n.
 Wrong: we cannot make this last line be less than cn log n
ncn
nbncnncn
nbnncn
nbnnnc
nbnnTnT
log
loglog
log)2log(log
log))2/log()2/((2
log)2/(2)(











2iflog)2/(2
2if
)(
nnbnnT
nb
nT
6 Eng: Mohammed Hussein
Log(x/y)= log x – log y
Log 2 =1
‫بالتعويض‬
Guess-and-Test Method, Part 2
 Recall the recurrence equation:
 Guess #2 that:T(n) < cn log2 n.
 if c > b.
 So,T(n) is O(n log2 n).
 In general, to use this method, you need to have a good guess and you need to be good at
induction proofs.
ncn
nbncnncnncn
nbnnncn
nbnncn
nbnnnc
nbnnTnT
2
2
22
2
2
log
loglog2log
log))2(log2loglog2(log
log)2log(log
log))2/(log)2/((2
log)2/(2)(












2iflog)2/(2
2if
)(
nnbnnT
nb
nT
7 Eng: Mohammed Hussein
(x-y) 2= x2 -2xy +y2
Log 2 =1
nj <= nd if j <= d
Big-Oh of highest degree
Recurrences
 In Mathematic a recurrence is a function that is defined in
terms of one or more base cases, and itself, with smaller
arguments
 Examples:
8 Eng: Mohammed Hussein
Master Method (tree)
 The master theorem concerns the recurrence
 a≥1 , b> 1
 n is the size of the problem.
 a is the number of subproblems in the recursion. (‫)الفزوع‬
 n/b is the size of each subproblem. (Here it is assumed that
all subproblems are essentially the same size.)
 f (n) is the cost of the work done outside the recursive calls
)()/()( nfbnaTnT 
9 Eng: Mohammed Hussein
merge-sort ‫خوارسمية‬
‫االستدعاء‬ ‫عمليات‬ ‫عند‬‫الذاتي‬‫الدمج‬ ‫عملية‬ ‫تنفيذ‬ ‫مستويات‬ ‫مختلف‬ ‫في‬
The Tree Definition
 The root of this tree is node A.
 Definitions:
 Parent (A)
 Children (E, F)
 Siblings (C, D)
 Root (A)
 Leaf / Leaves
 K, L, F, G, M, I, J…
 The degree of a node is the number of sub-trees of the node.
 The level of a node:
 Initially letting the root be at level one
 the level of the node’s parent plus one. Level H=level(Parent (H)+1));
 The height or depth of a tree is the maximum level of any node in the tree.
 height =Max(level(tree))
10 Eng: Mohammed Hussein
Divide and conquer (tree)
 We analyze this in some generality:
 suppose we have a pieces, each of size n/b and merging takes time f(n).
 (In this example a=b=2 and f(n)=O(log n) but it will not always be true that
a=b sometimes the pieces will overlap.)
 For simplicity, let's assume n is a power of b, and that the recursion stops
when n is 1.
 Notice that the size of a node depends only on its level: size(i) = n/(bi).
 What is time taken by a node at level i? : time(i) = f(n/ bi)
 How many levels can we have before we get down to n=1?
 For bottom level, n/bi =1, so n=bi and i=(log n)/(log b).
 How many items at level i?: ai.
11 Eng: Mohammed Hussein
)()/()( nfbnaTnT 
time(i) = f(n/ bi)
The Recursion Tree
 Draw the recursion tree for the recurrence relation and look for a pattern:
 At each successive level of recursion the sub problems get halved in size (n/2).
 At the (log n) th level the sub problems get down to size 1, and so the recursion ends.
Level/
depth
T’s size
0 1 n
1 2 n/2
k 2k n/2k
… … …






2if)2/(2
2if
)(
nbnnT
nb
nT
time
bn
bn
bn
…
Total time = bn + bn log n
(last level plus all previous levels)
2k =n  k= log n for each level.
log n
Notice that the size of a node depends only on its level
12 Eng: Mohammed Hussein
time=b x size = bn
Steps using recursion tree.
 Height = k=log n
 Levels = log n + 1
 Cost/level = cn
 Bottom node number n = 2log n for
height log n dividing problem by 2
each time.
 Recall that a balanced binary tree of
height k has k + 1 levels.
 Recall: a
log an = n
 so 2
log 2n = 2
log n =n
 Bottom cost = T(1) * n = cn
 Note that T(1) is problem size n=1,
there are n subproblems at bottom.
13 Eng: Mohammed Hussein 2k =n  k= log n for each level.
Merge Sort - Finding O
 Recall that a balanced binary tree of height k has k + 1 levels.
 A problem of size n=16 divided as described by the following recurrence equation
would then be represented by the tree.
 Use the implied coefficient c for arithmetic purposes:
14 Eng: Mohammed Hussein
16=2
4
Master Method
 Many divide-and-conquer recurrence equations have the form:
 The MasterTheorem:






dnnfbnaT
dnc
nT
if)()/(
if
)(
.1somefor)()/(provided
)),((is)(then),(is)(if3.
)log(is)(then),log(is)(if2.
)(is)(then),(is)(if1.
log
1loglog
loglog










nfbnaf
nfnTnnf
nnnTnnnf
nnTnOnf
a
kaka
aa
b
bb
bb
15 Eng: Mohammed Hussein
very small f(n)
moderate f(n)
very large f(n)
Analysis of Merge Sort
Analysis of Merge Sort
 Divide:Trivial.
 Conquer:Recursively sort 2subarrays.
 Combine:Linear-time merge.
 Using Master theorem
16 Eng: Mohammed Hussein
Analysis of Binary Search
 Recurrence for the worst case
Master Method, Example 1
 The form:
 The MasterTheorem:
 Example:






dnnfbnaT
dnc
nT
if)()/(
if
)(
.1somefor)()/(provided
)),((is)(then),(is)(if3.
)log(is)(then),log(is)(if2.
)(is)(then),(is)(if1.
log
1loglog
loglog










nfbnaf
nfnTnnf
nnnTnnnf
nnTnOnf
a
kaka
aa
b
bb
bb
)()2/(4)( nOnTnT 
Solution: logba =log24=2, so case 1 says T(n) is Θ(n2).
18 Eng: Mohammed Hussein
Master Method, Example 2
 The form:
 The MasterTheorem:
 Example:






dnnfbnaT
dnc
nT
if)()/(
if
)(
.1somefor)()/(provided
)),((is)(then),(is)(if3.
)log(is)(then),log(is)(if2.
)(is)(then),(is)(if1.
log
1loglog
loglog










nfbnaf
nfnTnnf
nnnTnnnf
nnTnOnf
a
kaka
aa
b
bb
bb
)log()2/(2)( nnnTnT 
Solution: logba=log22=1, so case 2 says T(n) is Θ(n log2 n).
19 Eng: Mohammed Hussein
Master Method, Example 3
 The form:
 The MasterTheorem:
 Example:






dnnfbnaT
dnc
nT
if)()/(
if
)(
.1somefor)()/(provided
)),((is)(then),(is)(if3.
)log(is)(then),log(is)(if2.
)(is)(then),(is)(if1.
log
1loglog
loglog










nfbnaf
nfnTnnf
nnnTnnnf
nnTnOnf
a
kaka
aa
b
bb
bb
)log()3/()( nnnTnT 
Solution: logba=0, so case 3 says T(n) is Θ(n log n).
20 Eng: Mohammed Hussein
Master Method, Example 4
 The form:
 The MasterTheorem:
 Example:






dnnfbnaT
dnc
nT
if)()/(
if
)(
.1somefor)()/(provided
)),((is)(then),(is)(if3.
)log(is)(then),log(is)(if2.
)(is)(then),(is)(if1.
log
1loglog
loglog










nfbnaf
nfnTnnf
nnnTnnnf
nnTnOnf
a
kaka
aa
b
bb
bb
)()2/(8)( 2
nnTnT 
Solution: logba=3, so case 1 says T(n) is Θ(n3).
21 Eng: Mohammed Hussein
Master Method, Example 5
 The form:
 The MasterTheorem:
 Example:






dnnfbnaT
dnc
nT
if)()/(
if
)(
.1somefor)()/(provided
)),((is)(then),(is)(if3.
)log(is)(then),log(is)(if2.
)(is)(then),(is)(if1.
log
1loglog
loglog










nfbnaf
nfnTnnf
nnnTnnnf
nnTnOnf
a
kaka
aa
b
bb
bb
)()3/(9)( 3
nnTnT 
Solution: logba=2, so case 3 says T(n) is Θ(n3).
22 Eng: Mohammed Hussein
Master Method, Example 6
 The form:
 The MasterTheorem:
 Example:






dnnfbnaT
dnc
nT
if)()/(
if
)(
.1somefor)()/(provided
)),((is)(then),(is)(if3.
)log(is)(then),log(is)(if2.
)(is)(then),(is)(if1.
log
1loglog
loglog










nfbnaf
nfnTnnf
nnnTnnnf
nnTnOnf
a
kaka
aa
b
bb
bb
)1()2/()(  nTnT
Solution: logba=0, so case 2 says T(n) is Θ(log n).
(binary search)
23 Eng: Mohammed Hussein
Master Method, Example 7
 The form:
 The MasterTheorem:
 Example:






dnnfbnaT
dnc
nT
if)()/(
if
)(
.1somefor)()/(provided
)),((is)(then),(is)(if3.
)log(is)(then),log(is)(if2.
)(is)(then),(is)(if1.
log
1loglog
loglog










nfbnaf
nfnTnnf
nnnTnnnf
nnTnOnf
a
kaka
aa
b
bb
bb
)(log)2/(2)( nnTnT 
Solution: logba=1, so case 1 says T(n) is Θ(n).
(heap construction)
24 Eng: Mohammed Hussein
Eng: Mohammed Hussein25
Eng: Mohammed Hussein26
Eng: Mohammed Hussein
27

Weitere ähnliche Inhalte

Was ist angesagt?

Algorithm Using Divide And Conquer
Algorithm Using Divide And ConquerAlgorithm Using Divide And Conquer
Algorithm Using Divide And ConquerUrviBhalani2
 
Analysis of algorithms
Analysis of algorithmsAnalysis of algorithms
Analysis of algorithmsGanesh Solanke
 
heap Sort Algorithm
heap  Sort Algorithmheap  Sort Algorithm
heap Sort AlgorithmLemia Algmri
 
daa-unit-3-greedy method
daa-unit-3-greedy methoddaa-unit-3-greedy method
daa-unit-3-greedy methodhodcsencet
 
0 1 knapsack using branch and bound
0 1 knapsack using branch and bound0 1 knapsack using branch and bound
0 1 knapsack using branch and boundAbhishek Singh
 
Hashing Technique In Data Structures
Hashing Technique In Data StructuresHashing Technique In Data Structures
Hashing Technique In Data StructuresSHAKOOR AB
 
finding Min and max element from given array using divide & conquer
finding Min and max element from given array using  divide & conquer finding Min and max element from given array using  divide & conquer
finding Min and max element from given array using divide & conquer Swati Kulkarni Jaipurkar
 
Data Structure and Algorithms Merge Sort
Data Structure and Algorithms Merge SortData Structure and Algorithms Merge Sort
Data Structure and Algorithms Merge SortManishPrajapati78
 
Algorithms Lecture 2: Analysis of Algorithms I
Algorithms Lecture 2: Analysis of Algorithms IAlgorithms Lecture 2: Analysis of Algorithms I
Algorithms Lecture 2: Analysis of Algorithms IMohamed Loey
 
Asymptotic Notations
Asymptotic NotationsAsymptotic Notations
Asymptotic NotationsRishabh Soni
 
Stressen's matrix multiplication
Stressen's matrix multiplicationStressen's matrix multiplication
Stressen's matrix multiplicationKumar
 
Dinive conquer algorithm
Dinive conquer algorithmDinive conquer algorithm
Dinive conquer algorithmMohd Arif
 
Recursion tree method
Recursion tree methodRecursion tree method
Recursion tree methodRajendran
 
Quick Sort , Merge Sort , Heap Sort
Quick Sort , Merge Sort ,  Heap SortQuick Sort , Merge Sort ,  Heap Sort
Quick Sort , Merge Sort , Heap SortMohammed Hussein
 

Was ist angesagt? (20)

Algorithm Using Divide And Conquer
Algorithm Using Divide And ConquerAlgorithm Using Divide And Conquer
Algorithm Using Divide And Conquer
 
Analysis of algorithms
Analysis of algorithmsAnalysis of algorithms
Analysis of algorithms
 
Red black tree
Red black treeRed black tree
Red black tree
 
heap Sort Algorithm
heap  Sort Algorithmheap  Sort Algorithm
heap Sort Algorithm
 
daa-unit-3-greedy method
daa-unit-3-greedy methoddaa-unit-3-greedy method
daa-unit-3-greedy method
 
0 1 knapsack using branch and bound
0 1 knapsack using branch and bound0 1 knapsack using branch and bound
0 1 knapsack using branch and bound
 
Merge Sort
Merge SortMerge Sort
Merge Sort
 
Hashing Technique In Data Structures
Hashing Technique In Data StructuresHashing Technique In Data Structures
Hashing Technique In Data Structures
 
finding Min and max element from given array using divide & conquer
finding Min and max element from given array using  divide & conquer finding Min and max element from given array using  divide & conquer
finding Min and max element from given array using divide & conquer
 
Data Structure and Algorithms Merge Sort
Data Structure and Algorithms Merge SortData Structure and Algorithms Merge Sort
Data Structure and Algorithms Merge Sort
 
Algorithms Lecture 2: Analysis of Algorithms I
Algorithms Lecture 2: Analysis of Algorithms IAlgorithms Lecture 2: Analysis of Algorithms I
Algorithms Lecture 2: Analysis of Algorithms I
 
Asymptotic Notations
Asymptotic NotationsAsymptotic Notations
Asymptotic Notations
 
Randomized algorithms ver 1.0
Randomized algorithms ver 1.0Randomized algorithms ver 1.0
Randomized algorithms ver 1.0
 
Branch and bound
Branch and boundBranch and bound
Branch and bound
 
Stressen's matrix multiplication
Stressen's matrix multiplicationStressen's matrix multiplication
Stressen's matrix multiplication
 
Dinive conquer algorithm
Dinive conquer algorithmDinive conquer algorithm
Dinive conquer algorithm
 
Greedy method
Greedy method Greedy method
Greedy method
 
Recursion tree method
Recursion tree methodRecursion tree method
Recursion tree method
 
Divide and conquer
Divide and conquerDivide and conquer
Divide and conquer
 
Quick Sort , Merge Sort , Heap Sort
Quick Sort , Merge Sort ,  Heap SortQuick Sort , Merge Sort ,  Heap Sort
Quick Sort , Merge Sort , Heap Sort
 

Ähnlich wie Divide and Conquer

Copy of y16 02-2119divide-and-conquer
Copy of y16 02-2119divide-and-conquerCopy of y16 02-2119divide-and-conquer
Copy of y16 02-2119divide-and-conquerJoepang2015
 
Algorithm Design and Complexity - Course 3
Algorithm Design and Complexity - Course 3Algorithm Design and Complexity - Course 3
Algorithm Design and Complexity - Course 3Traian Rebedea
 
Sure interview algorithm-1103
Sure interview algorithm-1103Sure interview algorithm-1103
Sure interview algorithm-1103Sure Interview
 
Divide and conquer strategy
Divide and conquer strategyDivide and conquer strategy
Divide and conquer strategyNisha Soms
 
dynamic programming Rod cutting class
dynamic programming Rod cutting classdynamic programming Rod cutting class
dynamic programming Rod cutting classgiridaroori
 
T2311 - Ch 4_Part1.pptx
T2311 - Ch 4_Part1.pptxT2311 - Ch 4_Part1.pptx
T2311 - Ch 4_Part1.pptxGadaFarhan
 
SURF 2012 Final Report(1)
SURF 2012 Final Report(1)SURF 2012 Final Report(1)
SURF 2012 Final Report(1)Eric Zhang
 
dynamic programming complete by Mumtaz Ali (03154103173)
dynamic programming complete by Mumtaz Ali (03154103173)dynamic programming complete by Mumtaz Ali (03154103173)
dynamic programming complete by Mumtaz Ali (03154103173)Mumtaz Ali
 
Lecture 5 6_7 - divide and conquer and method of solving recurrences
Lecture 5 6_7 - divide and conquer and method of solving recurrencesLecture 5 6_7 - divide and conquer and method of solving recurrences
Lecture 5 6_7 - divide and conquer and method of solving recurrencesjayavignesh86
 
Algorithm review
Algorithm reviewAlgorithm review
Algorithm reviewchidabdu
 
Divide and conquer
Divide and conquerDivide and conquer
Divide and conquerVikas Sharma
 

Ähnlich wie Divide and Conquer (20)

Daa chapter 2
Daa chapter 2Daa chapter 2
Daa chapter 2
 
L2
L2L2
L2
 
Copy of y16 02-2119divide-and-conquer
Copy of y16 02-2119divide-and-conquerCopy of y16 02-2119divide-and-conquer
Copy of y16 02-2119divide-and-conquer
 
Algorithm Design and Complexity - Course 3
Algorithm Design and Complexity - Course 3Algorithm Design and Complexity - Course 3
Algorithm Design and Complexity - Course 3
 
2.pptx
2.pptx2.pptx
2.pptx
 
Sure interview algorithm-1103
Sure interview algorithm-1103Sure interview algorithm-1103
Sure interview algorithm-1103
 
Divide and conquer strategy
Divide and conquer strategyDivide and conquer strategy
Divide and conquer strategy
 
Algorithm Assignment Help
Algorithm Assignment HelpAlgorithm Assignment Help
Algorithm Assignment Help
 
Algorithms Exam Help
Algorithms Exam HelpAlgorithms Exam Help
Algorithms Exam Help
 
02 Notes Divide and Conquer
02 Notes Divide and Conquer02 Notes Divide and Conquer
02 Notes Divide and Conquer
 
dynamic programming Rod cutting class
dynamic programming Rod cutting classdynamic programming Rod cutting class
dynamic programming Rod cutting class
 
L2
L2L2
L2
 
T2311 - Ch 4_Part1.pptx
T2311 - Ch 4_Part1.pptxT2311 - Ch 4_Part1.pptx
T2311 - Ch 4_Part1.pptx
 
SURF 2012 Final Report(1)
SURF 2012 Final Report(1)SURF 2012 Final Report(1)
SURF 2012 Final Report(1)
 
dynamic programming complete by Mumtaz Ali (03154103173)
dynamic programming complete by Mumtaz Ali (03154103173)dynamic programming complete by Mumtaz Ali (03154103173)
dynamic programming complete by Mumtaz Ali (03154103173)
 
Merge Sort
Merge SortMerge Sort
Merge Sort
 
Lecture 5 6_7 - divide and conquer and method of solving recurrences
Lecture 5 6_7 - divide and conquer and method of solving recurrencesLecture 5 6_7 - divide and conquer and method of solving recurrences
Lecture 5 6_7 - divide and conquer and method of solving recurrences
 
Algorithm review
Algorithm reviewAlgorithm review
Algorithm review
 
03 dc
03 dc03 dc
03 dc
 
Divide and conquer
Divide and conquerDivide and conquer
Divide and conquer
 

Mehr von Mohammed Hussein

Mehr von Mohammed Hussein (12)

Comnet Network Simulation
Comnet Network SimulationComnet Network Simulation
Comnet Network Simulation
 
Multimedia lecture6
Multimedia lecture6Multimedia lecture6
Multimedia lecture6
 
Sorting Algorithms
Sorting AlgorithmsSorting Algorithms
Sorting Algorithms
 
Multimedia Network
Multimedia NetworkMultimedia Network
Multimedia Network
 
Iteration, induction, and recursion
Iteration, induction, and recursionIteration, induction, and recursion
Iteration, induction, and recursion
 
Control system
Control systemControl system
Control system
 
Internet programming lecture 1
Internet programming lecture 1Internet programming lecture 1
Internet programming lecture 1
 
JAVA SCRIPT
JAVA SCRIPTJAVA SCRIPT
JAVA SCRIPT
 
PHP
 PHP PHP
PHP
 
Wireless lecture1
Wireless lecture1Wireless lecture1
Wireless lecture1
 
Algorithms Analysis
Algorithms Analysis Algorithms Analysis
Algorithms Analysis
 
Multimedia lecture ActionScript3
Multimedia lecture ActionScript3Multimedia lecture ActionScript3
Multimedia lecture ActionScript3
 

Kürzlich hochgeladen

Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in DelhiRussian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhikauryashika82
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactdawncurless
 
Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingTechSoup
 
Class 11th Physics NEET formula sheet pdf
Class 11th Physics NEET formula sheet pdfClass 11th Physics NEET formula sheet pdf
Class 11th Physics NEET formula sheet pdfAyushMahapatra5
 
Disha NEET Physics Guide for classes 11 and 12.pdf
Disha NEET Physics Guide for classes 11 and 12.pdfDisha NEET Physics Guide for classes 11 and 12.pdf
Disha NEET Physics Guide for classes 11 and 12.pdfchloefrazer622
 
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Sapana Sha
 
Unit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxUnit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxVishalSingh1417
 
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...christianmathematics
 
APM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAPM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAssociation for Project Management
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxheathfieldcps1
 
Key note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfKey note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfAdmir Softic
 
General AI for Medical Educators April 2024
General AI for Medical Educators April 2024General AI for Medical Educators April 2024
General AI for Medical Educators April 2024Janet Corral
 
Beyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactBeyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactPECB
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdfQucHHunhnh
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introductionMaksud Ahmed
 
Arihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdfArihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdfchloefrazer622
 
The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13Steve Thomason
 
fourth grading exam for kindergarten in writing
fourth grading exam for kindergarten in writingfourth grading exam for kindergarten in writing
fourth grading exam for kindergarten in writingTeacherCyreneCayanan
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdfQucHHunhnh
 
IGNOU MSCCFT and PGDCFT Exam Question Pattern: MCFT003 Counselling and Family...
IGNOU MSCCFT and PGDCFT Exam Question Pattern: MCFT003 Counselling and Family...IGNOU MSCCFT and PGDCFT Exam Question Pattern: MCFT003 Counselling and Family...
IGNOU MSCCFT and PGDCFT Exam Question Pattern: MCFT003 Counselling and Family...PsychoTech Services
 

Kürzlich hochgeladen (20)

Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in DelhiRussian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impact
 
Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy Consulting
 
Class 11th Physics NEET formula sheet pdf
Class 11th Physics NEET formula sheet pdfClass 11th Physics NEET formula sheet pdf
Class 11th Physics NEET formula sheet pdf
 
Disha NEET Physics Guide for classes 11 and 12.pdf
Disha NEET Physics Guide for classes 11 and 12.pdfDisha NEET Physics Guide for classes 11 and 12.pdf
Disha NEET Physics Guide for classes 11 and 12.pdf
 
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
 
Unit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxUnit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptx
 
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
 
APM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAPM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across Sectors
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptx
 
Key note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfKey note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdf
 
General AI for Medical Educators April 2024
General AI for Medical Educators April 2024General AI for Medical Educators April 2024
General AI for Medical Educators April 2024
 
Beyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactBeyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global Impact
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdf
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introduction
 
Arihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdfArihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdf
 
The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13
 
fourth grading exam for kindergarten in writing
fourth grading exam for kindergarten in writingfourth grading exam for kindergarten in writing
fourth grading exam for kindergarten in writing
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdf
 
IGNOU MSCCFT and PGDCFT Exam Question Pattern: MCFT003 Counselling and Family...
IGNOU MSCCFT and PGDCFT Exam Question Pattern: MCFT003 Counselling and Family...IGNOU MSCCFT and PGDCFT Exam Question Pattern: MCFT003 Counselling and Family...
IGNOU MSCCFT and PGDCFT Exam Question Pattern: MCFT003 Counselling and Family...
 

Divide and Conquer

  • 1. Divide-and-Conquer Republic of Yemen THAMAR UNIVERSITY Faculty of Computer Science& Information System 1 Eng: Mohammed Hussein
  • 3. Divide-and-Conquer  The whole problem we want to solve may too big to understand or solve at once.  We break it up into smaller pieces, solve the pieces separately, and combine the separate pieces together.  Divide-and conquer is a general algorithm design paradigm: 1. Divide: divide the input data S in two or more disjoint subsets S1, S2, … 2. Recur: solve the subproblems recursively 3. Conquer: combine the solutions for S1, S2, …, into a solution for S  The base case for the recursion are subproblems of constant size  Analysis can be done using recurrence equations 3 Eng: Mohammed Hussein
  • 4. Recurrence Equation Analysis  The conquer step of merge-sort consists of merging two sorted sequences, each with n/2 elements and implemented by means of a doubly linked list, takes at most bn steps, for some constant b.  Likewise, the basis case (n < 2) will take at b most steps.  Therefore, if we let T(n) denote the running time of merge-sort:  We can therefore analyze the running time of merge-sort by finding a closed form solution to the above equation.  That is, a solution that has T(n) only on the left-hand side.       2if)2/(2 2if )( nbnnT nb nT 4 Eng: Mohammed Hussein
  • 5. Solving recurrences  Recurrences are like solving integrals, differential equations, etc.  Should learn a few tricks.  Three ways of solving recurrences 1. Guess-and-Test Method(If we know the answer) 2. RecursionTree Method (Very useful !) 3. MasterTheorem (Save our effort) 5 Eng: Mohammed Hussein
  • 6. Guess-and-Test Method  In the guess-and-test method, we guess a closed form solution and then try to prove it is true by induction:  Guess that:T(n) < c n log n.  Wrong: we cannot make this last line be less than cn log n ncn nbncnncn nbnncn nbnnnc nbnnTnT log loglog log)2log(log log))2/log()2/((2 log)2/(2)(            2iflog)2/(2 2if )( nnbnnT nb nT 6 Eng: Mohammed Hussein Log(x/y)= log x – log y Log 2 =1 ‫بالتعويض‬
  • 7. Guess-and-Test Method, Part 2  Recall the recurrence equation:  Guess #2 that:T(n) < cn log2 n.  if c > b.  So,T(n) is O(n log2 n).  In general, to use this method, you need to have a good guess and you need to be good at induction proofs. ncn nbncnncnncn nbnnncn nbnncn nbnnnc nbnnTnT 2 2 22 2 2 log loglog2log log))2(log2loglog2(log log)2log(log log))2/(log)2/((2 log)2/(2)(             2iflog)2/(2 2if )( nnbnnT nb nT 7 Eng: Mohammed Hussein (x-y) 2= x2 -2xy +y2 Log 2 =1 nj <= nd if j <= d Big-Oh of highest degree
  • 8. Recurrences  In Mathematic a recurrence is a function that is defined in terms of one or more base cases, and itself, with smaller arguments  Examples: 8 Eng: Mohammed Hussein
  • 9. Master Method (tree)  The master theorem concerns the recurrence  a≥1 , b> 1  n is the size of the problem.  a is the number of subproblems in the recursion. (‫)الفزوع‬  n/b is the size of each subproblem. (Here it is assumed that all subproblems are essentially the same size.)  f (n) is the cost of the work done outside the recursive calls )()/()( nfbnaTnT  9 Eng: Mohammed Hussein merge-sort ‫خوارسمية‬ ‫االستدعاء‬ ‫عمليات‬ ‫عند‬‫الذاتي‬‫الدمج‬ ‫عملية‬ ‫تنفيذ‬ ‫مستويات‬ ‫مختلف‬ ‫في‬
  • 10. The Tree Definition  The root of this tree is node A.  Definitions:  Parent (A)  Children (E, F)  Siblings (C, D)  Root (A)  Leaf / Leaves  K, L, F, G, M, I, J…  The degree of a node is the number of sub-trees of the node.  The level of a node:  Initially letting the root be at level one  the level of the node’s parent plus one. Level H=level(Parent (H)+1));  The height or depth of a tree is the maximum level of any node in the tree.  height =Max(level(tree)) 10 Eng: Mohammed Hussein
  • 11. Divide and conquer (tree)  We analyze this in some generality:  suppose we have a pieces, each of size n/b and merging takes time f(n).  (In this example a=b=2 and f(n)=O(log n) but it will not always be true that a=b sometimes the pieces will overlap.)  For simplicity, let's assume n is a power of b, and that the recursion stops when n is 1.  Notice that the size of a node depends only on its level: size(i) = n/(bi).  What is time taken by a node at level i? : time(i) = f(n/ bi)  How many levels can we have before we get down to n=1?  For bottom level, n/bi =1, so n=bi and i=(log n)/(log b).  How many items at level i?: ai. 11 Eng: Mohammed Hussein )()/()( nfbnaTnT  time(i) = f(n/ bi)
  • 12. The Recursion Tree  Draw the recursion tree for the recurrence relation and look for a pattern:  At each successive level of recursion the sub problems get halved in size (n/2).  At the (log n) th level the sub problems get down to size 1, and so the recursion ends. Level/ depth T’s size 0 1 n 1 2 n/2 k 2k n/2k … … …       2if)2/(2 2if )( nbnnT nb nT time bn bn bn … Total time = bn + bn log n (last level plus all previous levels) 2k =n  k= log n for each level. log n Notice that the size of a node depends only on its level 12 Eng: Mohammed Hussein time=b x size = bn
  • 13. Steps using recursion tree.  Height = k=log n  Levels = log n + 1  Cost/level = cn  Bottom node number n = 2log n for height log n dividing problem by 2 each time.  Recall that a balanced binary tree of height k has k + 1 levels.  Recall: a log an = n  so 2 log 2n = 2 log n =n  Bottom cost = T(1) * n = cn  Note that T(1) is problem size n=1, there are n subproblems at bottom. 13 Eng: Mohammed Hussein 2k =n  k= log n for each level.
  • 14. Merge Sort - Finding O  Recall that a balanced binary tree of height k has k + 1 levels.  A problem of size n=16 divided as described by the following recurrence equation would then be represented by the tree.  Use the implied coefficient c for arithmetic purposes: 14 Eng: Mohammed Hussein 16=2 4
  • 15. Master Method  Many divide-and-conquer recurrence equations have the form:  The MasterTheorem:       dnnfbnaT dnc nT if)()/( if )( .1somefor)()/(provided )),((is)(then),(is)(if3. )log(is)(then),log(is)(if2. )(is)(then),(is)(if1. log 1loglog loglog           nfbnaf nfnTnnf nnnTnnnf nnTnOnf a kaka aa b bb bb 15 Eng: Mohammed Hussein very small f(n) moderate f(n) very large f(n)
  • 16. Analysis of Merge Sort Analysis of Merge Sort  Divide:Trivial.  Conquer:Recursively sort 2subarrays.  Combine:Linear-time merge.  Using Master theorem 16 Eng: Mohammed Hussein
  • 17. Analysis of Binary Search  Recurrence for the worst case
  • 18. Master Method, Example 1  The form:  The MasterTheorem:  Example:       dnnfbnaT dnc nT if)()/( if )( .1somefor)()/(provided )),((is)(then),(is)(if3. )log(is)(then),log(is)(if2. )(is)(then),(is)(if1. log 1loglog loglog           nfbnaf nfnTnnf nnnTnnnf nnTnOnf a kaka aa b bb bb )()2/(4)( nOnTnT  Solution: logba =log24=2, so case 1 says T(n) is Θ(n2). 18 Eng: Mohammed Hussein
  • 19. Master Method, Example 2  The form:  The MasterTheorem:  Example:       dnnfbnaT dnc nT if)()/( if )( .1somefor)()/(provided )),((is)(then),(is)(if3. )log(is)(then),log(is)(if2. )(is)(then),(is)(if1. log 1loglog loglog           nfbnaf nfnTnnf nnnTnnnf nnTnOnf a kaka aa b bb bb )log()2/(2)( nnnTnT  Solution: logba=log22=1, so case 2 says T(n) is Θ(n log2 n). 19 Eng: Mohammed Hussein
  • 20. Master Method, Example 3  The form:  The MasterTheorem:  Example:       dnnfbnaT dnc nT if)()/( if )( .1somefor)()/(provided )),((is)(then),(is)(if3. )log(is)(then),log(is)(if2. )(is)(then),(is)(if1. log 1loglog loglog           nfbnaf nfnTnnf nnnTnnnf nnTnOnf a kaka aa b bb bb )log()3/()( nnnTnT  Solution: logba=0, so case 3 says T(n) is Θ(n log n). 20 Eng: Mohammed Hussein
  • 21. Master Method, Example 4  The form:  The MasterTheorem:  Example:       dnnfbnaT dnc nT if)()/( if )( .1somefor)()/(provided )),((is)(then),(is)(if3. )log(is)(then),log(is)(if2. )(is)(then),(is)(if1. log 1loglog loglog           nfbnaf nfnTnnf nnnTnnnf nnTnOnf a kaka aa b bb bb )()2/(8)( 2 nnTnT  Solution: logba=3, so case 1 says T(n) is Θ(n3). 21 Eng: Mohammed Hussein
  • 22. Master Method, Example 5  The form:  The MasterTheorem:  Example:       dnnfbnaT dnc nT if)()/( if )( .1somefor)()/(provided )),((is)(then),(is)(if3. )log(is)(then),log(is)(if2. )(is)(then),(is)(if1. log 1loglog loglog           nfbnaf nfnTnnf nnnTnnnf nnTnOnf a kaka aa b bb bb )()3/(9)( 3 nnTnT  Solution: logba=2, so case 3 says T(n) is Θ(n3). 22 Eng: Mohammed Hussein
  • 23. Master Method, Example 6  The form:  The MasterTheorem:  Example:       dnnfbnaT dnc nT if)()/( if )( .1somefor)()/(provided )),((is)(then),(is)(if3. )log(is)(then),log(is)(if2. )(is)(then),(is)(if1. log 1loglog loglog           nfbnaf nfnTnnf nnnTnnnf nnTnOnf a kaka aa b bb bb )1()2/()(  nTnT Solution: logba=0, so case 2 says T(n) is Θ(log n). (binary search) 23 Eng: Mohammed Hussein
  • 24. Master Method, Example 7  The form:  The MasterTheorem:  Example:       dnnfbnaT dnc nT if)()/( if )( .1somefor)()/(provided )),((is)(then),(is)(if3. )log(is)(then),log(is)(if2. )(is)(then),(is)(if1. log 1loglog loglog           nfbnaf nfnTnnf nnnTnnnf nnTnOnf a kaka aa b bb bb )(log)2/(2)( nnTnT  Solution: logba=1, so case 1 says T(n) is Θ(n). (heap construction) 24 Eng: Mohammed Hussein