0% found this document useful (0 votes)
27 views

Lecture7 Design Strategies Divide and Conquer and Greedy

Uploaded by

Bakunzi Daniel
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
27 views

Lecture7 Design Strategies Divide and Conquer and Greedy

Uploaded by

Bakunzi Daniel
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 26

Design Strategies

Special Appreciation to Prof. Dr. P.N Naghabushan

11/24/2024 1
Design Strategies
Divide & Conquer
Greedy

Dynamic

Backtracking
Branch & Bound

11/24/2024 2
Divide & Conquer :
How to feed a mass of people ?
Greedy :
What to select from a collection of items ?
Dynamic
What options should I choose after S.4 ?

What should I do after S.6?

How should I proceed after B.CS?


11/24/2024 3
Backtracking
I shall proceed until I get a solution or I hit a dead
end.
Then I retrace , find an alternate way and follow the
same strategy to proceed deep further. (BFS)

Branch & Bound


I shall explore all possible options at every level
(DFS)

11/24/2024 4
Divide & Conquer :

11/24/2024 5
P

Divide

P1 P2 Pn

Solve Solve Solve

Find Global Solution

11/24/2024 6
If P: Problem

P What to solve?

P1 P2
What to
solve? P3

P21 P22 P23 P31 P32

Stop at when how to


P221 P222 solve is known

P2221 P2222

11/24/2024 7
Then the algorithm is
P1

P1 P1 P1

P2.1 P2.1 P2.1


P2.2.1 P2.2.1

P P2 P2.2 P2.2.2 P 2.2.2.1


P 2.2.2.2
P2.3 P2.3 P2.3

P3.1 P3.1 P3.1


P3 P3.2 P3.2 P3.2

11/24/2024 8
This is strictly not called ‘DIVIDE & CONQUER’

This is not a Design Strategy

This is a Design Methodology


(Studied in Software Engineering)

This is called Top-Down Design Methodology

Then What is Divide & Conquer ?

11/24/2024 9
In Divide & Conquer
it is not the ‘PROBLEM’ which is divided ,

it is the ‘DATA SPACE’ of the problem


which is divided

11/24/2024 10
DATA

Partition the data into different ‘groups’


satisfying certain homogeneity conditions

Gr 1 Gr 2 Gr 3 Gr k
Solve by ‘specialized’ algorithm
suitable for each group

Alg 1 Alg 2 Alg 3 Alg k

11/24/2024 11
One Specific Case

Is it possible to get the groups of data in


such a way that
the same algorithm could be utilized to
work on each group of data

11/24/2024 12
For Instance :
Let us assume that there are two floors in a house
Ground Floor
First Floor
Let us assume each floor has two wings
Left Wing
Right Wing
Let us assume each wing has two rooms
Room 1
Room 2
Let us assume each room has two working tables
Table 1
Table 2
11/24/2024 13
I have misplaced my ‘mobile’ somewhere?

11/24/2024 14
SEARCH in the HOUSE

SEARCH in Ground Floor SEARCH in First Floor

LEFT WING RIGHTWING LEFT WING RIGHTWING

Room1 Room2 Room1 Room2 Room1 Room2 Room1 Room2

T1 T2 T1 T2 T1 T2 T1 T2 T1 T2 T1 T2 T1 T2 T1 T2

11/24/2024 15
SEARCH in the HOUSE

SEARCH in Ground Floor SEARCH in First Floor

LEFT WING RIGHTWING LEFT WING RIGHTWING

Room1 Room2 Room1 Room2 Room1 Room2 Room1 Room2

T1 T2 T1 T2 T1 T2 T1 T2 T1 T2 T1 T2 T1 T2 T1 T2

In this problem

Search Domain ( Data Space ) was divided more or less into two
equal halfs every time.
This is also a very specific case of dividing data into exactly 2 halves (Binary
Division) every time.

On every half the same Algorithm has to be applied


11/24/2024 16
A general structure of Divide& Conquer is as follows:

Original size of data space 1 n

Is it too big? Then DIVIDE 1 n/2 (n/2)+1 n

Is it too big? Then DIVIDE further

:
:
Has it become small enough ?

If yes, SOLVE
Start COMBINING local
solutions

So, GLOBAL Solution is obtained


11/24/2024 17
Divide & Conquer Strategy
Three Stages
Stage 1 Divide
[DIVIDE]
& Keep on ‘recursively’ dividing < bisecting the data space >
till the data space size becomes small enough

Stage 2 Resultant data space is so small that one could solve


[SOLVE]
the problem without applying any extra effort
[CONQUER]

Stage 3 Combine the local results in the reverse sequence of


[COMBINE] division operation to result in final SOLUTION

11/24/2024 18
A general control abstract to illustrate Divide & Conquer

Procedure DC (nstart,nfinish)
{
if SMALL (nstart,nfinish)
then {SOLVE (nstart,nfinish)}
else
{
nmid (nstart+nfinish)/2

COMBINE (DC(nstart, nmid),


DC(nmid+1,nfinish))
}
11/24/2024 } 19
Greedy Strategy

11/24/2024 20
Greedy Strategy
There are n number of candidates, and k number of seats.

The problem is to choose the most appropriate k candidates from n


candidates
A = a1 a2 a3 .. .. .. .. an

S = s1 s2 s3 .. .. .. .. sk
Since k  n SA
The approach to fill up ‘S’ is to maximize the objective called
MOST APPROPRIATE
An ambitious search through A will result in S.

 Greed to Maximize the Profit  GREEDY STRATEGY


11/24/2024 21
Greedy Strategy
Most of these problems have n inputs and require us to obtain a
subset that satisfies some constraints

Any Subset that satisfies the given constraints is called a feasible


solution

We are required to find a feasible solution that either maximizes or


minimizes a given objective function

 A feasible solution that does this is called an


OPTIMAL SOLUTION
11/24/2024 22
Greedy Strategy
We can suggest a two stage algorithm :

1. Arrange the input in the best possible sequence


i.e the most appropriate elements would be available starting
from the beginning of the list with the most deserving / the most
meritorious element appearing as the first element

2. Test element by element whether to include the current element


into the SOLUTION set or not.

Preprocessing Greedy Sequencing

Solving Creating the solution set

11/24/2024 23
Greedy Strategy
Once the data is given in the greedy sequence then the solution is
obtained using - O algorithm

t lb  first k searches successfully declaring the elements


as relevant
t lb  k  (k)

t ub  Scan goes up to the end of the data list

t ub  n  O(n)

However what is really most important is


PREPROCESSING – GREEDY SEQUENCING

11/24/2024 24
Greedy Strategy
Control abstract of greedy strategy based algorithm

Procedure greedy (A, n)


{
Solution  
for i  1 to n
{ x  Select(A)
If feasible(solution , x) then
solution  U(Solution, x)
}
}

11/24/2024 25
Example Knapsack Problem
 Knapsack is a leather / Canvas bag used by a soldier
Consider a knapsack of capacity M.
Consider storing ‘n’ items(objects) into this knapsack
Object ‘i’ occupies wi of the knapsack if one full load of the object is
placed.
Instead if a part of the object(fraction) 0  xi  1 of the object is
placed then occupancy is wixi
If pi is the profit earned by placing the ith object, then the profit
earned by placing fraction xi of the object is pixi
Therefore objective function is
MAXIMIZE  pixi 1in
Subject to  wixi  M and 0  xi  1 and 1  i  n.
The profits and weights are positive numbers.
11/24/2024 26

You might also like