Module 3 (Aad)
Module 3 (Aad)
3 STEPS:
1. DIVIDE
• Problems are divided into smaller sub-problems
• Identical to original problem
• Sub problems are of same size
2. CONQUER
• Sub problems are solved recursively
3. COMBINE
• Solutions of each sub problems are combine to get final solution
CONTROL ABSTRACTION
COMPLEXITY CALCULTAION
• Assume the size of Problem P is n and size of k sub problems are n1, n2,n3
…. nk
• The computing time of D and C is described by the recurrence relation:
EXAMPLE PROBLEMS
• BINARY SEARCH
• 2 WAY MERGE SORT
• FINDING MAXIMUM & MINIMUM
• STRASSEN’S MATRIX MULTIPLICATION
• QUICK SORT
• CONVEX HULL
Strassen’s Method
Volker Strassen discovered a method to compute C(Ab) using only 7
multiplications and 18 additions/subtractions.
This method involves computing 7 n/2 x n/2 intermediate matrices named
as P, Q, R, S, T , U & V
These sub matrices are computed using 7 multiplications and 10
additions/subtractions. Cij‘s are further calculated using 8
additions/subtractions
ANALYSIS
Example:
Q: Multiply
GREEDY ALGORITHMS
Algorithm design technique to solve optimization problems.
The goal is to find BEST SOLUTION.
Builds up solution piece by piece.
Chooses the next piece that offers the most obvious and immediate benefit.
2 Characteristics:
Greedy choice property
A global optimum can be arrived at by searching a
local optimum.
Optimal sub-structure
An optimal solution of problem contains an optimal
solution to sub problems.
Components:
1. A candidate set − A solution is created from this set.
2. A selection function − Used to choose the best candidate to be added to the
solution.
3. A feasibility function − Used to determine whether a candidate can be
used to contribute to the solution.
4. An objective function − Used to assign a value to a solution or a partial
solution.
5. A solution function − Used to indicate whether a complete solution has
been reached.
Control abstraction
EXAMPLE PROBLEMS
Minimum spanning tree
Knapsack problem
Single source Shortest path
Optimal storage on Tapes
Tree vertex splitting
Job sequencing
Best method:
1. Calculate profit/ weight ratio pi/wi.
2. Include the object which has maximum profit per unit capacity used i.e
object with maximum pi/wi value.
3. Calculate the fraction taken(x), the profit so far(p) and remaining capacity
4. Repeat steps 2 & 3 until maximum capacity has reached.
Example
Consider a Knapsack problem instance with N=7,M=15, P={
10,5,15,7,6,18,3}, W = {2,3,5,7,1,4,1}
Step 1: Calculate pi/wi
pi/wi = (10/2 , 5/3 , 15/5 , 7/7, 6/1, 18/4, 3/1}
pi/wi = {5, 1.67, 3, 1, 6, 4.5, 3}
Select objects in the decreasing order of pi/wi value
Object order for selection 5, 1, 6, 3, 7, 2, 4
5th object x5 =1 , p5 = 6, tp=6, remaining capacity= 15-1 =14
1st object x1 =1 , p1 = 10, tp =6+10=16, remaining capacity= 14-2 =12
6th object x6 =1 , p6 = 18, tp =16+18=34, remaining capacity= 12-4 =8
3rd object x3 =1 , p3 = 15, tp =34+15=49, remaining capacity= 8-5 =3
7th object x7 =1 , p7 = 3, tp =49+3= 52, remaining capacity= 3-1 = 2
2nd object x2 =2/3 , p2 = 5, tp = 52+5x2/3 = 55.3, remaining capacity= 2-2=0
Capacity reached maximum, cannot add any more objects. So set the
fraction taken of remaining objects as 0. Here only one remaining
object(4), its fraction I set to 0
So fraction x = { 1, 2/3, 1, 0, 1, 1, 1}
Total profit is 55.3
ALGORITHM:
MINIMUM COST SPANNING TREE
A tree T is said to be a spanning tree of a constructed graph G if T is a sub-
graph of G and T contains all vertices. For instance, the sub-graph in heavy
lines in figure below is a spanning tree of graph shown.
KRUSKAL’S ALGORITHM
Kruskal's algorithm to find the minimum cost spanning tree uses the
greedy approach.
This algorithm treats the graph as a forest and every node it has as an
individual tree.
A tree connects to another only and only if, it has the least cost among all
available options and does not violate MST properties.
ALGORITHM:
1. Remove all loops and parallel edges from the given graph. In case of
parallel edges, keep the one which has the least cost associated and
remove all others.
2. Arrange all edges in their increasing order of weight
3. Add the edge which has the least weightage
4. Repeat step 3 until all vertices are added
5. No circuit must be formed in between
EXAMPLE:
SINGLE SOURCE SHORTEST PATH ALGORITHM
In a shortest- paths problem, we are given a weighted, directed graphs G = (V,
E), with weight function w: E → R mapping edges to real-valued weights. The
weight of path p = (v0,v1,..... vk) is the total of the weights of its constituent edges:
We define the shortest - path weight from u to v by δ(u,v) = min (w (p): u→v), if
there is a path from u to v, and δ(u,v)= ∞, otherwise.
The shortest path from vertex s to vertex t is then defined as any path p with
weight w (p) = δ(s,t).
In a Single Source Shortest Paths Problem, we are given a Graph G = (V, E), we
want to find the shortest path from a given source vertex s ∈ V to every vertex v
∈ V.
5 Shortest path Algorithms
Let G be a graph and let s,t be two specified vertices of G. In shortest
spanning tree problems, we have to a path from s to t, if there is any, which
uses the least number of edges. Such a path, if it exists, is called a shortest
path from s to t. There are different types of shortest path algorithms:
1. Shortest path between 2 specified vertices.
In general, dij 6= dji , and the triangle inequality need not be satisfied.
That is dij + djk may be less than dik . The distance of directed path P is
defined as the sum of lengths of edges in P. The problem is to find the short-
est path and its length from starting vertex s to terminal vertex t. The most
efficient algorithm for finding shortest path is Dijkstra’s algorithm.
17
min[old label j, (old label of i + dij )
Figure 26:
18
2. Now C is the vertex with the smallest label. So we are making the
value of C as the next permanent label.
vi A B C D E F G H
A 0 8 2 5 ∞ ∞ ∞ ∞
3. Now calculate the distance from A to all other vertices through C. If
there is an edge from C to any other vertex, add the permanent label
of C with the weight of the edge from C. Check the newly calculated
value is smaller than the previous value. If so, replace the previous
value with new smaller value. For example: there is an edge CD of
weight 2. The permannet label of C is 2. So adding these we get the
value 4. The previous value of vertex D was 5. The newly calculated
value is smaller than the previous value; so replace 5 by 4.
vi A B C D E F G H
A 0 8 2 5 ∞ ∞ ∞ ∞
C 0 8 2 4 7 ∞ ∞ ∞
4. Now D is the vertex with the smallest label. So we are making the
value of D as the next permanent label.
vi A B C D E F G H
A 0 8 2 5 ∞ ∞ ∞ ∞
C 0 8 2 4 7 ∞ ∞ ∞
5. Now calculate the distance from A to all other vertices through C and
D. If there is an edge from D to any other vertex, add the perma-
nent label of D with the weight of the edge from D.Check the newly
calculated value is smaller than the previous value. If so, replace the
previous value with new smaller value.
vi A B C D E F G H
A 0 8 2 5 ∞ ∞ ∞ ∞
C 0 8 2 4 7 ∞ ∞ ∞
D 0 6 2 4 5 10 7 ∞
6. Now E is the vertex with the smallest label. So we are making the
value of E as the next permanent label.
vi A B C D E F G H
A 0 8 2 5 ∞ ∞ ∞ ∞
C 0 8 2 4 7 ∞ ∞ ∞
D 0 6 2 4 5 10 7 ∞
7. Now calculate the distance from A to all other vertices through C,D
and E. If there is an edge from E to any other vertex, add the perma-
nent label of E with the weight of the edge from E.Check the newly
19
calculated value is smaller than the previous value. If so, replace the
previous value with new smaller value.
vi A B C D E F G H
A 0 8 2 5 ∞ ∞ ∞ ∞
C 0 8 2 4 7 ∞ ∞ ∞
D 0 6 2 4 5 10 7 ∞
E 0 6 2 4 5 10 6 ∞
8. Now B and G are the vertices with the smallest label. We can arbitarily
choose either. We choose B. So we are making the value of B as the
next permanent label.
vi A B C D E F G H
A 0 8 2 5 ∞ ∞ ∞ ∞
C 0 8 2 4 7 ∞ ∞ ∞
D 0 6 2 4 5 10 7 ∞
E 0 6 2 4 5 10 6 ∞
9. Now calculate the distance from A to all other vertices through C,D,
E and B. If there is an edge from B to any other vertex, add the
permanent label of B with the weight of the edge from B.Check the
newly calculated value is smaller than the previous value. If so, replace
the previous value with new smaller value.
vi A B C D E F G H
A 0 8 2 5 ∞ ∞ ∞ ∞
C 0 8 2 4 7 ∞ ∞ ∞
D 0 6 2 4 5 10 7 ∞
E 0 6 2 4 5 10 6 ∞
B 0 6 2 4 5 10 6 ∞
10. Now G is the vertex with the smallest label. So we are making the
value of G as the next permanent label.
vi A B C D E F G H
A 0 8 2 5 ∞ ∞ ∞ ∞
C 0 8 2 4 7 ∞ ∞ ∞
D 0 6 2 4 5 10 7 ∞
E 0 6 2 4 5 10 6 ∞
B 0 6 2 4 5 10 6 ∞
11. Now calculate the distance from A to all other vertices through C,D,
E, B and G. If there is an edge from G to any other vertex, add the
permanent label of G with the weight of the edge from G.Check the
newly calculated value is smaller than the previous value. If so, replace
20
the previous value with new smaller value.
vi A B C D E F G H
A 0 8 2 5 ∞ ∞ ∞ ∞
C 0 8 2 4 7 ∞ ∞ ∞
D 0 6 2 4 5 10 7 ∞
E 0 6 2 4 5 10 6 ∞
B 0 6 2 4 5 8 6 ∞
G 0 6 2 4 5 8 6 12
12. Now F is the vertex with the smallest label. So we are making the
value of G as the next permanent label.
vi A B C D E F G H
A 0 8 2 5 ∞ ∞ ∞ ∞
C 0 8 2 4 7 ∞ ∞ ∞
D 0 6 2 4 5 10 7 ∞
E 0 6 2 4 5 10 6 ∞
B 0 6 2 4 5 8 6 ∞
G 0 6 2 4 5 8 6 12
13. Now calculate the distance from A to all other vertices through C, D,
E, B, G and F. If there is an edge from F to any other vertex, add the
permanent label of F with the weight of the edge from F.Check the
newly calculated value is smaller than the previous value. If so, replace
the previous value with new smaller value.
vi A B C D E F G H
A 0 8 2 5 ∞ ∞ ∞ ∞
C 0 8 2 4 7 ∞ ∞ ∞
D 0 6 2 4 5 10 7 ∞
E 0 6 2 4 5 10 6 ∞
B 0 6 2 4 5 8 6 ∞
G 0 6 2 4 5 8 6 12
F 0 6 2 4 5 8 6 11
14. Now we have got the distance from A to H which is the current label
of H i.e. the distance of A to H is 11.
The algorithm is represented by the flowchart in Fig:27
21
ALGORITHM:
EXAMPLE:
ANALYSIS
EXAMPLE: