SlideShare a Scribd company logo
Unit-2
Algorithms Using
Divide-and-conquer
Introduction
 Divide-and-conquer is a top-down
technique for designing algorithms
that consists of dividing the problem
into smaller sub problems hoping
that the solutions of the sub
problems are easier to find and then
composing the partial solutions into
the solution of the original problem.
Divide-and-conquer
 1. Divide: Break the problem into
sub-problems of same type.
2.Conquer: Recursively solve
these sub-problems.
3.Combine: Combine the solution
sub-problems.
Divide-and-conquer
 For Example: Assuming that each
divide step creates two sub-
problems
Divide-and-conquer
Example-2
Divide-and-conquer
 For Example: If we can divide the
problem into more than two, it
looks like this
Analysis of Algorithm  II Unit version .pptx
Divide-and-conquer Detail
Divide/Break
 This step involves breaking the problem
into smaller sub-problems.
 Sub-problems should represent a part of
the original problem.
 This step generally takes a recursive
approach to divide the problem until no
sub-problem is further divisible.
 At this stage, sub-problems become
atomic in nature but still represent some
part of the actual problem.
Divide-and-conquer Detail
Conquer/Solve
 This step receives a lot of smaller sub-
problems to be solved.
 Generally, at this level, the problems are
considered 'solved' on their own.
Divide-and-conquer Detail
Merge/Combine
 When the smaller sub-problems are
solved, this stage recursively combines
them until they formulate a solution of
the original problem.
 This algorithmic approach works
recursively and conquer & merge steps
works so close that they appear as one.
Standard Algorithms
based on D & C
 The following algorithms are based on
divide-and-conquer algorithm design
paradigm.
 Merge Sort
 Quick Sort
 Binary Search
 Strassen's Matrix Multiplication
 Closest pair (points)
 Cooley–Tukey Fast Fourier Transform
(FFT) algorithm
Advantages of D & C
1. Solving difficult problems:
Divide and conquer is a powerful tool for solving
conceptually difficult problems: all it requires is a
way of breaking the problem into sub-problems,
of solving the trivial cases and of combining sub-
problems to the original problem.
2. Parallelism:
Divide and conquer algorithms are naturally
adapted for execution in multi-processor
machines, especially shared-memory systems
where the communication of data between
processors does not need to be planned in
advance, because distinct sub-problems can be
executed on different processors.
Advantages of D & C
3. Memory Access:
Divide-and-conquer algorithms naturally tend to
make efficient use of memory caches. The reason
is that once a sub-problem is small enough, it and
all its sub-problems can, in principle, be solved
within the cache, without accessing the slower
main memory.
4. Roundoff control:
In computations with rounded arithmetic, e.g.
with floating point numbers, a divide-and-conquer
algorithm may yield more accurate results than a
superficially equivalent iterative method.
Advantages of D & C
 For solving difficult problems like Tower
Of Hanoi, divide & conquer is a powerful
tool
 Results in efficient algorithms.
 Divide & Conquer algorithms are adapted
foe execution in multi-processor
machines
 Results in algorithms that use memory
cache efficiently.
Limitations of D & C
 Recursion is slow.
 Very simple problem may be more
complicated than an iterative approach.
Example: adding n numbers etc
Example of Multiplication of
N digit integers.
 Multiplication can be perform using divide
and conquer technique.
 First we know the decimal system of
number which are shown as under.
Number is 3754
3*103 + 7*102 + 5*101 + 4*100
Example of Multiplication of
N digit integers.
 2345 * 6789
2*103 + 3*102 + 4*101 + 5*100
6*103 + 7*102 + 8*101 + 9*100
4 3 2 1
Example of Multiplication of
N digit integers.
 2345 * 6789
2*103 + 3*102 + 4*101 + 5*100
6*103 + 7*102 + 8*101 + 9*100
4 3 2 1
Example of Multiplication of
N digit integers.
 2345 * 6789
2*103 + 3*102 + 4*101 + 5*100
6*103 + 7*102 + 8*101 + 9*100
4 3 2 1
Closest-Pair Problem:
Divide and Conquer
 Brute force approach requires comparing every point
with every other point
 Given n points, we must perform 1 + 2 + 3 + … + n-
2 + n-1 comparisons.
 Brute force  O(n2)
 The Divide and Conquer algorithm yields  O(n log
n)
 Reminder: if n = 1,000,000 then
 n2 =
 n log n =
1,000,000,000,000 whereas
20,000,000
Closest-Pair Algorithm
Given: A set of points in 2-D
Closest-Pair Algorithm
Step 1: Sort the points in one D
Lets sort based on the X-axis
O(n log n) using quicksort or mergesort
1
2
3
4
5
6
7
8
9
1
0
1
1
1
2
1
3 1
4
Closest-Pair Algorithm
Step 2: Split the points, i.e.,
Draw a line at the mid-point between 7
and 8
1
2
3
4
5
6
7
8
9
1
0
1
1
1
2
Sub-Problem
2
1
3 1
4
Closest-Pair Algorithm
Sub-Problem
1
Advantage: Normally, we’d have to
compare each of the 14 points with every
other point.
(n-1)n/2 = 13*14/2 = 91 comparisons
1
2
3
4
5
6
7
8
9
1
0
1
1
1
2
Sub-Problem
2
1
3 1
4
Closest-Pair Algorithm
Sub-Problem
1
Advantage: Now, we have two sub-
problems of half the size. Thus, we have to
do 6*7/2 comparisons twice, which is 42
comparisons
1
2
3
4
5
6
7
8
9
1
0
1
1
1
2
Sub-Problem
2
1
3 1
4
Closest-Pair Algorithm
d1
d2
Sub-Problem
1
solution d = min(d1, d2)
1
2
3
4
5
6
7
8
9
1
0
1
1
1
2
Sub-Problem
2
1
3 1
4
Closest-Pair Algorithm
Advantage: With just one split we cut the
number of comparisons in half. Obviously,
we gain an even greater advantage if we
split the sub-problems.
d = min(d1, d2)
d1
d2
Sub-Problem
1
Problem: However, what if the closest two
points are each from different sub-
problems?
1
2
3
4
5
6
7
8
9
1
0
1
1
1
2
Sub-Problem
2
1
3 1
4
Closest-Pair Algorithm
d1
d2
Sub-Problem
1
Here is an example where we have to
compare points from sub-problem 1 to the
points in sub-problem 2.
1
2
3
4
5
6
7
8
9
1
0
1
1
1
2
Sub-Problem
2
1
3 1
4
Closest-Pair Algorithm
d1
d2
Sub-Problem
1
1
2
3
4
5
6
7
8
9
1
0
1
1
1
2
Sub-Problem
2
1
3 1
4
Closest-Pair Algorithm
d1
d2
Sub-Problem
1
d d
However, we only have to compare points
inside the following “strip.”
d = min(d1, d2)
Step 3: In fact we can continue to split
until each sub-problem is trivial, i.e., takes
one comparison.
1
2
3
4
5
6
7
8
9
1
0
1
1
1
2
1
3 1
4
Closest-Pair Algorithm
Finally: The solution to each sub-problem
is combined until the final solution is
obtained
1
2
3
4
5
6
7
8
9
1
0
1
1
1
2
1
3 1
4
Closest-Pair Algorithm
Finally: On the last step the ‘strip’ will
likely be very small. Thus, combining the
two largest sub-problems won’t require
much work.
1
2
3
4
5
6
7
8
9
1
0
1
1
1
2
1
3 1
4
Closest-Pair Algorithm
1
2
3
5
6
7
8
1
0
1
1
1
2
1
3 1
4
Closest-Pair Algorithm
 In this example, it takes 22 comparisons to
find the closets-pair.
 The brute force algorithm would have taken
91 comparisons.
 But, the real advantage occurs when there are
millions of points.
4
9
Closest-Pair Algo
Float DELTA-LEFT, DELTA-RIGHT;
Float DELTA;
If n= 2 then
return distance form p(1) to p(2);
Else
P-LEFT <- (p(1),p(2),p(n/2));
P-RIGHT <- (p(n/2 + 1),p(n/2 + 2),p(n));
DELTA-LEFT <- Closest_pair(P-LEFT,n2);
DELTA-RIGHT <- Closest_pair(P-RIGHT,n2);
DELTA<- minimum(DELTA-LEFT,DELTA-RIGHT)
Closest-Pair Algo
For I in 1---s do
for j in i+1..s do
if(|x[i] – x[j] | > DELTA and
|y[i] – y[j]|> DELTA ) then
exit;
end
if(distance(q[I],q[j] <DELTA)) then
DELTA <- distance (q[i],q[j]);
end
end
Merge Sort
Closest-Pair Algo
Array a[]
Array b[]
Integer h,i,j,k;
h<- low;
i <- low;
j=<- mid + 1;
While( h<= mid and j<= high) do
if(a[h] <= a[j]) then
b[i]<- a[h]
h<-h+1
Closest-Pair Algo
Else
b[i]<- a[j]
j<- j+1;
end
i<- i+1;
End
If(h>mid) then
for(k<-j to high) do
b[i] <- a[k];
i<-i+1;
Closest-Pair Algo
Else
for(k<- to mid) do
b[i] <- a[k]
i=i+1;
End
For(k<-low to high)
a[k]<- b[k]
end
Timing Analysis
 D&C algorithm running time in
mainly affected by 3 factors
 The number of sub-instance(α)
into which a problem is split.
 The ratio of initial problem size to
sub problem size(ß)
 The number of steps required to
divide the initial instance and to
combine sub-solutions expressed
as a function of the input size n.
Timing Analysis
 P is D & C Where α sub instance each of
size n/ß
 Let Tp(n) donete the number of steps
taken by P on instances of size n.
Tp(n0) = constant (recursive-base);
Tp(n)= αTp (n/ß)+y(n);
umber
ub
ance
s number
sub size
α is n
of
inst
s
ß
i
of
y is constant

More Related Content

Similar to Analysis of Algorithm II Unit version .pptx (20)

PPTX
Divide and conquer
ramya marichamy
 
PDF
divide and conquer - data structured and algorithms
hntmyb5tneu
 
PDF
A simple study on computer algorithms by S. M. Risalat Hasan Chowdhury
S. M. Risalat Hasan Chowdhury
 
PPTX
Introduction-to-Divide-and-Conquer_(1).pptx
PrithviSingh87
 
PPTX
Brute Force and Divide & Conquer Technique
ssusered62011
 
PPTX
Divide and Conquer - Part 1
Amrinder Arora
 
DOC
algorithm Unit 2
Monika Choudhery
 
DOC
Unit 2 in daa
Nv Thejaswini
 
PDF
Divide and conquer
Emmanuel college
 
PDF
CS345-Algorithms-II-Lecture-1-CS345-2016.pdf
OpenWorld6
 
PPTX
Algorithm in computer science
Riazul Islam
 
PDF
Jurnal informatika
MamaMa28
 
PPTX
Divide and Conquer
Melaku Bayih Demessie
 
PPT
3-Chapter Three - Divide and Conquer.ppt
mershaabdisa
 
PPTX
CLOSEST PAIR (Final)
Aruneel Das
 
PPT
Divide and Conquer
Dr Shashikant Athawale
 
PPTX
Mastering on Divide and Conquer Algorithm
Mahmud Hasan Tanvir
 
PPTX
3. CPT121 - Introduction to Problem Solving - Module 1 - Unit 3.pptx
Agoyi1
 
Divide and conquer
ramya marichamy
 
divide and conquer - data structured and algorithms
hntmyb5tneu
 
A simple study on computer algorithms by S. M. Risalat Hasan Chowdhury
S. M. Risalat Hasan Chowdhury
 
Introduction-to-Divide-and-Conquer_(1).pptx
PrithviSingh87
 
Brute Force and Divide & Conquer Technique
ssusered62011
 
Divide and Conquer - Part 1
Amrinder Arora
 
algorithm Unit 2
Monika Choudhery
 
Unit 2 in daa
Nv Thejaswini
 
Divide and conquer
Emmanuel college
 
CS345-Algorithms-II-Lecture-1-CS345-2016.pdf
OpenWorld6
 
Algorithm in computer science
Riazul Islam
 
Jurnal informatika
MamaMa28
 
Divide and Conquer
Melaku Bayih Demessie
 
3-Chapter Three - Divide and Conquer.ppt
mershaabdisa
 
CLOSEST PAIR (Final)
Aruneel Das
 
Divide and Conquer
Dr Shashikant Athawale
 
Mastering on Divide and Conquer Algorithm
Mahmud Hasan Tanvir
 
3. CPT121 - Introduction to Problem Solving - Module 1 - Unit 3.pptx
Agoyi1
 

More from rajesshs31r (20)

PPT
UNIT I Basic terminology COMPUTER ARCHI.ppt
rajesshs31r
 
PPTX
OPERATING SYSTEM-STRUCTURE AND UNIT 1.pptx
rajesshs31r
 
PPTX
design analysis of algorithmaa unit 1.pptx
rajesshs31r
 
PDF
OPERATING SYSTEM-lecturenotesUNIT 1-1-19.pdf
rajesshs31r
 
PPTX
Unit ii networks ; Medium Access Control .pptx
rajesshs31r
 
PPTX
COA Presentation 4 ADDRESSING MODES.pptx
rajesshs31r
 
PPT
unit ii Wireless Local area netwokAn.ppt
rajesshs31r
 
PPT
Pipelining_Lecture computer Organisation .ppt
rajesshs31r
 
PPT
Open-Source Interconnection model layers .ppt
rajesshs31r
 
PPTX
lecture3-instructionset-120307014407-phpapp01.pptx
rajesshs31r
 
PPTX
UNIT IV Computer architecture Analysis.pptx
rajesshs31r
 
PPTX
mathematical nonrecursive algorithm.pptx
rajesshs31r
 
PPT
COmputer zDesign and ArchitectureDch1Slides (1).ppt
rajesshs31r
 
PPT
Computer Organization Design ch2Slides.ppt
rajesshs31r
 
PPT
computer-networkppt-110208141655-phpapp02.ppt
rajesshs31r
 
PPTX
functional and units in digital system COA unit 1.pptx
rajesshs31r
 
PPTX
Lesson one 1 OOAD study/ref material.pptx
rajesshs31r
 
PDF
Software Development Life Cycle steps.pdf
rajesshs31r
 
PDF
Analysis Design View layer Interface.pdf
rajesshs31r
 
PPTX
LN 1 Object Oriented Analysis Design notes.pptx
rajesshs31r
 
UNIT I Basic terminology COMPUTER ARCHI.ppt
rajesshs31r
 
OPERATING SYSTEM-STRUCTURE AND UNIT 1.pptx
rajesshs31r
 
design analysis of algorithmaa unit 1.pptx
rajesshs31r
 
OPERATING SYSTEM-lecturenotesUNIT 1-1-19.pdf
rajesshs31r
 
Unit ii networks ; Medium Access Control .pptx
rajesshs31r
 
COA Presentation 4 ADDRESSING MODES.pptx
rajesshs31r
 
unit ii Wireless Local area netwokAn.ppt
rajesshs31r
 
Pipelining_Lecture computer Organisation .ppt
rajesshs31r
 
Open-Source Interconnection model layers .ppt
rajesshs31r
 
lecture3-instructionset-120307014407-phpapp01.pptx
rajesshs31r
 
UNIT IV Computer architecture Analysis.pptx
rajesshs31r
 
mathematical nonrecursive algorithm.pptx
rajesshs31r
 
COmputer zDesign and ArchitectureDch1Slides (1).ppt
rajesshs31r
 
Computer Organization Design ch2Slides.ppt
rajesshs31r
 
computer-networkppt-110208141655-phpapp02.ppt
rajesshs31r
 
functional and units in digital system COA unit 1.pptx
rajesshs31r
 
Lesson one 1 OOAD study/ref material.pptx
rajesshs31r
 
Software Development Life Cycle steps.pdf
rajesshs31r
 
Analysis Design View layer Interface.pdf
rajesshs31r
 
LN 1 Object Oriented Analysis Design notes.pptx
rajesshs31r
 
Ad

Recently uploaded (20)

PDF
DIGESTION OF CARBOHYDRATES ,PROTEINS AND LIPIDS
raviralanaresh2
 
PDF
AI-assisted IP-Design lecture from the MIPLM 2025
MIPLM
 
PPTX
PLANNING FOR EMERGENCY AND DISASTER MANAGEMENT ppt.pptx
PRADEEP ABOTHU
 
PPTX
Ward Management: Patient Care, Personnel, Equipment, and Environment.pptx
PRADEEP ABOTHU
 
PDF
IMPORTANT GUIDELINES FOR M.Sc.ZOOLOGY DISSERTATION
raviralanaresh2
 
PDF
I3PM Industry Case Study Siemens on Strategic and Value-Oriented IP Management
MIPLM
 
PPTX
Different types of inheritance in odoo 18
Celine George
 
PPTX
Life and Career Skills Lesson 2.pptxProtective and Risk Factors of Late Adole...
ryangabrielcatalon40
 
PPTX
Lesson 1 Cell (Structures, Functions, and Theory).pptx
marvinnbustamante1
 
PDF
Lesson 1 - Nature of Inquiry and Research.pdf
marvinnbustamante1
 
PPTX
How to Configure Refusal of Applicants in Odoo 18 Recruitment
Celine George
 
PDF
TechSoup Microsoft Copilot Nonprofit Use Cases and Live Demo - 2025.06.25.pdf
TechSoup
 
PPTX
AIMA UCSC-SV Leadership_in_the_AI_era 20250628 v16.pptx
home
 
PPTX
Navigating English Key Stage 2 lerning needs.pptx
JaysonClosa3
 
PPTX
How to Manage Allocation Report for Manufacturing Orders in Odoo 18
Celine George
 
PDF
Indian National movement PPT by Simanchala Sarab, Covering The INC(Formation,...
Simanchala Sarab, BABed(ITEP Secondary stage) in History student at GNDU Amritsar
 
PDF
TLE 8 QUARTER 1 MODULE WEEK 1 MATATAG CURRICULUM
denniseraya1997
 
PPTX
ENGlish 8 lesson presentation PowerPoint.pptx
marawehsvinetshe
 
PDF
Vani - The Voice of Excellence - Jul 2025 issue
Savipriya Raghavendra
 
PDF
Lesson 1 : Science and the Art of Geography Ecosystem
marvinnbustamante1
 
DIGESTION OF CARBOHYDRATES ,PROTEINS AND LIPIDS
raviralanaresh2
 
AI-assisted IP-Design lecture from the MIPLM 2025
MIPLM
 
PLANNING FOR EMERGENCY AND DISASTER MANAGEMENT ppt.pptx
PRADEEP ABOTHU
 
Ward Management: Patient Care, Personnel, Equipment, and Environment.pptx
PRADEEP ABOTHU
 
IMPORTANT GUIDELINES FOR M.Sc.ZOOLOGY DISSERTATION
raviralanaresh2
 
I3PM Industry Case Study Siemens on Strategic and Value-Oriented IP Management
MIPLM
 
Different types of inheritance in odoo 18
Celine George
 
Life and Career Skills Lesson 2.pptxProtective and Risk Factors of Late Adole...
ryangabrielcatalon40
 
Lesson 1 Cell (Structures, Functions, and Theory).pptx
marvinnbustamante1
 
Lesson 1 - Nature of Inquiry and Research.pdf
marvinnbustamante1
 
How to Configure Refusal of Applicants in Odoo 18 Recruitment
Celine George
 
TechSoup Microsoft Copilot Nonprofit Use Cases and Live Demo - 2025.06.25.pdf
TechSoup
 
AIMA UCSC-SV Leadership_in_the_AI_era 20250628 v16.pptx
home
 
Navigating English Key Stage 2 lerning needs.pptx
JaysonClosa3
 
How to Manage Allocation Report for Manufacturing Orders in Odoo 18
Celine George
 
Indian National movement PPT by Simanchala Sarab, Covering The INC(Formation,...
Simanchala Sarab, BABed(ITEP Secondary stage) in History student at GNDU Amritsar
 
TLE 8 QUARTER 1 MODULE WEEK 1 MATATAG CURRICULUM
denniseraya1997
 
ENGlish 8 lesson presentation PowerPoint.pptx
marawehsvinetshe
 
Vani - The Voice of Excellence - Jul 2025 issue
Savipriya Raghavendra
 
Lesson 1 : Science and the Art of Geography Ecosystem
marvinnbustamante1
 
Ad

Analysis of Algorithm II Unit version .pptx

  • 2. Introduction  Divide-and-conquer is a top-down technique for designing algorithms that consists of dividing the problem into smaller sub problems hoping that the solutions of the sub problems are easier to find and then composing the partial solutions into the solution of the original problem.
  • 3. Divide-and-conquer  1. Divide: Break the problem into sub-problems of same type. 2.Conquer: Recursively solve these sub-problems. 3.Combine: Combine the solution sub-problems.
  • 4. Divide-and-conquer  For Example: Assuming that each divide step creates two sub- problems
  • 6. Divide-and-conquer  For Example: If we can divide the problem into more than two, it looks like this
  • 8. Divide-and-conquer Detail Divide/Break  This step involves breaking the problem into smaller sub-problems.  Sub-problems should represent a part of the original problem.  This step generally takes a recursive approach to divide the problem until no sub-problem is further divisible.  At this stage, sub-problems become atomic in nature but still represent some part of the actual problem.
  • 9. Divide-and-conquer Detail Conquer/Solve  This step receives a lot of smaller sub- problems to be solved.  Generally, at this level, the problems are considered 'solved' on their own.
  • 10. Divide-and-conquer Detail Merge/Combine  When the smaller sub-problems are solved, this stage recursively combines them until they formulate a solution of the original problem.  This algorithmic approach works recursively and conquer & merge steps works so close that they appear as one.
  • 11. Standard Algorithms based on D & C  The following algorithms are based on divide-and-conquer algorithm design paradigm.  Merge Sort  Quick Sort  Binary Search  Strassen's Matrix Multiplication  Closest pair (points)  Cooley–Tukey Fast Fourier Transform (FFT) algorithm
  • 12. Advantages of D & C 1. Solving difficult problems: Divide and conquer is a powerful tool for solving conceptually difficult problems: all it requires is a way of breaking the problem into sub-problems, of solving the trivial cases and of combining sub- problems to the original problem. 2. Parallelism: Divide and conquer algorithms are naturally adapted for execution in multi-processor machines, especially shared-memory systems where the communication of data between processors does not need to be planned in advance, because distinct sub-problems can be executed on different processors.
  • 13. Advantages of D & C 3. Memory Access: Divide-and-conquer algorithms naturally tend to make efficient use of memory caches. The reason is that once a sub-problem is small enough, it and all its sub-problems can, in principle, be solved within the cache, without accessing the slower main memory. 4. Roundoff control: In computations with rounded arithmetic, e.g. with floating point numbers, a divide-and-conquer algorithm may yield more accurate results than a superficially equivalent iterative method.
  • 14. Advantages of D & C  For solving difficult problems like Tower Of Hanoi, divide & conquer is a powerful tool  Results in efficient algorithms.  Divide & Conquer algorithms are adapted foe execution in multi-processor machines  Results in algorithms that use memory cache efficiently.
  • 15. Limitations of D & C  Recursion is slow.  Very simple problem may be more complicated than an iterative approach. Example: adding n numbers etc
  • 16. Example of Multiplication of N digit integers.  Multiplication can be perform using divide and conquer technique.  First we know the decimal system of number which are shown as under. Number is 3754 3*103 + 7*102 + 5*101 + 4*100
  • 17. Example of Multiplication of N digit integers.  2345 * 6789 2*103 + 3*102 + 4*101 + 5*100 6*103 + 7*102 + 8*101 + 9*100 4 3 2 1
  • 18. Example of Multiplication of N digit integers.  2345 * 6789 2*103 + 3*102 + 4*101 + 5*100 6*103 + 7*102 + 8*101 + 9*100 4 3 2 1
  • 19. Example of Multiplication of N digit integers.  2345 * 6789 2*103 + 3*102 + 4*101 + 5*100 6*103 + 7*102 + 8*101 + 9*100 4 3 2 1
  • 20. Closest-Pair Problem: Divide and Conquer  Brute force approach requires comparing every point with every other point  Given n points, we must perform 1 + 2 + 3 + … + n- 2 + n-1 comparisons.  Brute force  O(n2)  The Divide and Conquer algorithm yields  O(n log n)  Reminder: if n = 1,000,000 then  n2 =  n log n = 1,000,000,000,000 whereas 20,000,000
  • 21. Closest-Pair Algorithm Given: A set of points in 2-D
  • 22. Closest-Pair Algorithm Step 1: Sort the points in one D
  • 23. Lets sort based on the X-axis O(n log n) using quicksort or mergesort 1 2 3 4 5 6 7 8 9 1 0 1 1 1 2 1 3 1 4 Closest-Pair Algorithm
  • 24. Step 2: Split the points, i.e., Draw a line at the mid-point between 7 and 8 1 2 3 4 5 6 7 8 9 1 0 1 1 1 2 Sub-Problem 2 1 3 1 4 Closest-Pair Algorithm Sub-Problem 1
  • 25. Advantage: Normally, we’d have to compare each of the 14 points with every other point. (n-1)n/2 = 13*14/2 = 91 comparisons 1 2 3 4 5 6 7 8 9 1 0 1 1 1 2 Sub-Problem 2 1 3 1 4 Closest-Pair Algorithm Sub-Problem 1
  • 26. Advantage: Now, we have two sub- problems of half the size. Thus, we have to do 6*7/2 comparisons twice, which is 42 comparisons 1 2 3 4 5 6 7 8 9 1 0 1 1 1 2 Sub-Problem 2 1 3 1 4 Closest-Pair Algorithm d1 d2 Sub-Problem 1 solution d = min(d1, d2)
  • 27. 1 2 3 4 5 6 7 8 9 1 0 1 1 1 2 Sub-Problem 2 1 3 1 4 Closest-Pair Algorithm Advantage: With just one split we cut the number of comparisons in half. Obviously, we gain an even greater advantage if we split the sub-problems. d = min(d1, d2) d1 d2 Sub-Problem 1
  • 28. Problem: However, what if the closest two points are each from different sub- problems? 1 2 3 4 5 6 7 8 9 1 0 1 1 1 2 Sub-Problem 2 1 3 1 4 Closest-Pair Algorithm d1 d2 Sub-Problem 1
  • 29. Here is an example where we have to compare points from sub-problem 1 to the points in sub-problem 2. 1 2 3 4 5 6 7 8 9 1 0 1 1 1 2 Sub-Problem 2 1 3 1 4 Closest-Pair Algorithm d1 d2 Sub-Problem 1
  • 30. 1 2 3 4 5 6 7 8 9 1 0 1 1 1 2 Sub-Problem 2 1 3 1 4 Closest-Pair Algorithm d1 d2 Sub-Problem 1 d d However, we only have to compare points inside the following “strip.” d = min(d1, d2)
  • 31. Step 3: In fact we can continue to split until each sub-problem is trivial, i.e., takes one comparison. 1 2 3 4 5 6 7 8 9 1 0 1 1 1 2 1 3 1 4 Closest-Pair Algorithm
  • 32. Finally: The solution to each sub-problem is combined until the final solution is obtained 1 2 3 4 5 6 7 8 9 1 0 1 1 1 2 1 3 1 4 Closest-Pair Algorithm
  • 33. Finally: On the last step the ‘strip’ will likely be very small. Thus, combining the two largest sub-problems won’t require much work. 1 2 3 4 5 6 7 8 9 1 0 1 1 1 2 1 3 1 4 Closest-Pair Algorithm
  • 34. 1 2 3 5 6 7 8 1 0 1 1 1 2 1 3 1 4 Closest-Pair Algorithm  In this example, it takes 22 comparisons to find the closets-pair.  The brute force algorithm would have taken 91 comparisons.  But, the real advantage occurs when there are millions of points. 4 9
  • 35. Closest-Pair Algo Float DELTA-LEFT, DELTA-RIGHT; Float DELTA; If n= 2 then return distance form p(1) to p(2); Else P-LEFT <- (p(1),p(2),p(n/2)); P-RIGHT <- (p(n/2 + 1),p(n/2 + 2),p(n)); DELTA-LEFT <- Closest_pair(P-LEFT,n2); DELTA-RIGHT <- Closest_pair(P-RIGHT,n2); DELTA<- minimum(DELTA-LEFT,DELTA-RIGHT)
  • 36. Closest-Pair Algo For I in 1---s do for j in i+1..s do if(|x[i] – x[j] | > DELTA and |y[i] – y[j]|> DELTA ) then exit; end if(distance(q[I],q[j] <DELTA)) then DELTA <- distance (q[i],q[j]); end end
  • 38. Closest-Pair Algo Array a[] Array b[] Integer h,i,j,k; h<- low; i <- low; j=<- mid + 1; While( h<= mid and j<= high) do if(a[h] <= a[j]) then b[i]<- a[h] h<-h+1
  • 39. Closest-Pair Algo Else b[i]<- a[j] j<- j+1; end i<- i+1; End If(h>mid) then for(k<-j to high) do b[i] <- a[k]; i<-i+1;
  • 40. Closest-Pair Algo Else for(k<- to mid) do b[i] <- a[k] i=i+1; End For(k<-low to high) a[k]<- b[k] end
  • 41. Timing Analysis  D&C algorithm running time in mainly affected by 3 factors  The number of sub-instance(α) into which a problem is split.  The ratio of initial problem size to sub problem size(ß)  The number of steps required to divide the initial instance and to combine sub-solutions expressed as a function of the input size n.
  • 42. Timing Analysis  P is D & C Where α sub instance each of size n/ß  Let Tp(n) donete the number of steps taken by P on instances of size n. Tp(n0) = constant (recursive-base); Tp(n)= αTp (n/ß)+y(n); umber ub ance s number sub size α is n of inst s ß i of y is constant