You are on page 1of 49

DESIGN AND ANALYSIS OF ALGORITHMS

(19ECS234)
G V Sivanarayana
Assistant Professor
Department of CSE
GITAM Institute of Technology (GIT)
Visakhapatnam – 530045

03 March 2021,02:00-02:50 P.M Department of CSE, GIT Course Code and Title:19ECS234 and DESIGN AND ANALYSIS OF ALGORITHMS GVSN 1
Topics to be Covered

• All-Pairs Shortest Paths


• Multistage graph
• Optimal Binary search tree
• Reliability Design
• Travelling sales person problem

03 March 2021,02:00-02:50 P.M


Department of CSE, GIT Course Code and Title:19ECS234 and DESIGN AND ANALYSIS OF ALGORITHMS GVSN 2
UNIT - III

Dynamic Programming: The general method, multistage graphs, all pairs shortest
paths, optimal binary search trees, reliability design, the travelling sales person
problem.

03 March 2021,02:00-02:50 P.M Department of CSE, GIT Course Code and Title:19EID237 and Data Structures with Python GVSN 3
Learning Outcomes:
After completion of this unit, the student will be able to
● Compare dynamic method with previous methods (L2)
● Apply dynamic method for developing algorithms (L3)
● Illustrate the merits of dynamic method (L2)
● Analyze the performance of algorithms (L4)

03 March 2021,02:00-02:50 P.M Department of CSE, GIT Course Code and Title:19EID237 and Data Structures with Python GVSN 4
DYNAMIC PROGRAMMING

 Divide & Conquer algorithm partition the problem into disjoint subproblems solve the subproblems

recursively and then combine their solution to solve the original problems.

 Dynamic Programming is used when the subproblems are not independent, e.g. when they share the

same subproblems. In this case, divide and conquer may do more work than necessary, because it

solves the same sub problem multiple times.

 Dynamic Programming is a Bottom-up approach, we solve all possible small problems and then

combine to obtain solutions for bigger problems.

03 March 2021,02:00-02:50 P.M Department of CSE, GIT Course Code and Title:19ECS234 and DESIGN AND ANALYSIS OF ALGORITHMS GVSN 5
DYNAMIC PROGRAMMING

 The idea is to simply store the results of subproblems, so that we do not have to re-compute them when
needed later.
 This simple optimization reduces time complexities from exponential to polynomial.
 For example, if we write simple recursive solution for Fibonacci Numbers, we get exponential time
complexity and if we optimize it by storing solutions of subproblems, time complexity reduces to
linear.
int fib(int n) f[0] = 0
{ f[1] = 1
if (n <= 1) Dynamic Programming : Linear
return 1; for (i=2; i <=n; i++)
return fib(n-1) + fib(n-2); f[i] = f[i-1] + f[i-2];
}
return f[n];
Recursion : Exponential
03 March 2021,02:00-02:50 P.M Department of CSE, GIT Course Code and Title:19ECS234 and DESIGN AND ANALYSIS OF ALGORITHMS GVSN 6
DYNAMIC PROGRAMMING

 Dynamic programming is an algorithm design technique.

 Dynamic programming is required to take into account the fact that the problems may not be

partitioned into independent sub problems

 The sub problem is solved only once and the answer saved in a table thereby avoiding the

work of re-computing the answer every time.

 Solution to a problem can be viewed as the result of a sequence of decisions.

 Dynamic Programming is also used in optimization problems.

03 March 2021,02:00-02:50 P.M Department of CSE, GIT Course Code and Title:19ECS234 and DESIGN AND ANALYSIS OF ALGORITHMS GVSN 7
ALL PAIRS SHORTEST PATHS

 The all pair shortest path algorithm is also known as Floyd-Warshall algorithm is used to find all pair
shortest path problem from a given weighted graph.
 As a result of this algorithm, it will generate a matrix, which will represent the minimum distance from
any node to all other nodes in the graph.
 At first the output matrix is same as given cost matrix of the graph. After that the output matrix will be
updated with all vertices k as the intermediate vertex.
 We initialize the solution matrix same as the input graph matrix as a first step.
 Then we update the solution matrix by considering all vertices as an intermediate vertex.
 The idea is to one by one pick all vertices and updates all shortest paths which include the picked
vertex as an intermediate vertex in the shortest path.
03 March 2021,02:00-02:50 P.M Department of CSE, GIT Course Code and Title:19ECS234 and DESIGN AND ANALYSIS OF ALGORITHMS GVSN 8
ALL PAIRS SHORTEST PATHS

 Let G=(V , E) be a directed graph with n vertices.


 Let cost be a cost adjacency matrix for G such that
 cost(i , i) = 0 1≤ i ≤ n
 cost(i , j) = cost( i , j) if (i , j) E(G)
 cost(i , j) = ∞ if i(i , j) 󠆡E(G)
 The all-paths shortest-problem is to determine a matrix A such that A(i , j) is the
length of a shortest path from i to j.
 Ak (i , j) = min {Ak-1 (i , j) , Ak-1 (i , k) + Ak-1 (k , j) } , k ≥ 1

03 March 2021,02:00-02:50 P.M Department of CSE, GIT Course Code and Title:19ECS234 and DESIGN AND ANALYSIS OF ALGORITHMS GVSN 9
Example

6
1
4
2 A0 1 2 3
11 1 0 4 11
3 2
2 6 0 2
3
3 3 ∞ 0

08 March 2021,03:00-03:50 P.M Department of CSE, GIT Course Code and Title:19ECS234 and DESIGN AND ANALYSIS OF ALGORITHMS GVSN
A1 ( 1, 1) K=1 , i=1 , j=1
Ak (i , j) = min {Ak-1 (i , j) , Ak-1 (i , k) + Ak-1 (k , j) } , k ≥ 1

A1 (1 , 1) = min {A1-1 (1 , 1) , A1-1 (1 , 1) + A1-1 (1 , 1) } A1 1 2 3


A0 1 2 3
= min {A0 (1 , 1) , A0 (1 , 1) + A0 (1 , 1) } 1 0 4 11
1 0 4 11
= min {0 , 0+ 0) } =0 2 6 0 2
A ( 1, 2) K=1 , i=1 , j=2
1
2 6 0 2
3 3 7 0
3 3 ∞ 0
A1 (1 , 2) = min {A1-1 (1 , 2) , A1-1 (1 , 1) + A1-1 (1 , 2) }
= min {A0 (1 , 2) , A0 (1 , 1) + A0 (1 , 2) }
= min {4 , 0+ 4) } =4
A1 ( 1, 3) K=1 , i=1 , j=3 A1 ( 3, 2) K=1 , i=3 , j=2
A1 (1 , 3) = min {A1-1 (1 , 3) , A1-1 (1 , 1) + A1-1 (1 , 3) } A1 (3 , 2) = min {A1-1 (3 , 2) , A1-1 (3 , 1) + A1-1 (1 , 2) }
= min {A0 (1 , 3) , A0 (1 , 1) + A0 (1 , 3) } = min {A0 (3 , 2) , A0 (3 , 1) + A0 (1 , 2) }
= min {11 , 0+11) } =11 = min {∞ , 3+4) } =7
08 March 2021,03:00-03:50 P.M Department of CSE, GIT Course Code and Title:19ECS234 and DESIGN AND ANALYSIS OF ALGORITHMS GVSN
A2 ( 1, 2) K=2 , i=1 , j=2
Ak (i , j) = min {Ak-1 (i , j) , Ak-1 (i , k) + Ak-1 (k , j) } , k ≥ 1

A2 (1 , 2) = min {A2-1 (1 , 2) , A2-1 (1 , 2) + A2-1 (2 , 2) } A2 1 2 3


A1 1 2 3
= min {A1 (1 , 2) , A1 (1 , 2) + A1 (2 , 2) } 1 0 4 6
1 0 4 11
= min {4 , 4+ 0) } =4 2 6 0 2
A ( 1, 3) K=2 , i=1 , j=3
2
2 6 0 2
3 3 7 0
3 3 7 0
A2 (1 , 3) = min {A2-1 (1 , 3) , A2-1 (1 , 2) + A2-1 (2 , 3) }
= min {A1 (1 , 3) , A1 (1 , 2) + A1 (2 , 3) }
= min {11 , 4+ 2) } =6

08 March 2021,03:00-03:50 P.M Department of CSE, GIT Course Code and Title:19ECS234 and DESIGN AND ANALYSIS OF ALGORITHMS GVSN
A3 ( 1, 3) K=3 , i=1 , j=3
Ak (i , j) = min {Ak-1 (i , j) , Ak-1 (i , k) + Ak-1 (k , j) } , k ≥ 1

A3 (1 , 3) = min {A3-1 (1 , 3) , A3-1 (1 , 3) + A3-1 (3 , 3) } A3 1 2 3


A2 1 2 3
= min {A2 (1 , 3) , A2 (1 , 3) + A2 (3 , 3) } 1 0 4 6
1 0 4 6
= min {6 , 6+ 0) } =6 2 5 0 2
A ( 2, 1) K=3 , i=2 , j=1
3
2 6 0 2
3 3 7 0
3 3 7 0
A3 (2 , 1) = min {A3-1 (2 , 1) , A3-1 (2 , 3) + A3-1 (3 , 1) }
= min {A2 (2 , 1) , A2 (2 , 3) + A2 (3 , 1) }
= min {6 , 2+ 3) } =5

08 March 2021,03:00-03:50 P.M Department of CSE, GIT Course Code and Title:19ECS234 and DESIGN AND ANALYSIS OF ALGORITHMS GVSN
Algorithm Allpaths( cost, A, n)
// cost[1:n, 1:n] is the cost adjacency matrix of a graph with n vertices;
// A[i , j] is the cost of a shortest path from vertex i to vertex j .
// cost[i , i]=0 , for 1 ≤ i ≤ n.
{
for i:= 1 to n do
for j:= 1 to n do
A[i , j] = cost [i , j]; // copy cost into A.
for k:= 1 to n do
for i:= 1 to n do
for j:= 1 to n do
A[ i , j]= min(A[ i , j], A[ i , k]+ A[ k , j]);
}

08 March 2021,03:00-03:50 P.M Department of CSE, GIT Course Code and Title:19ECS234 and DESIGN AND ANALYSIS OF ALGORITHMS GVSN 14
MULTISTAGE GRAPHS

• A multistage graph G=(V , E) be a directed graph in which the vertices partitioned


into k ≥ 2 disjoint sets Vi , 1 ≤ i ≤ k .

• If ( u , v ) is an edge in E, u Vi and v Vi+1 for some i , 1 ≤ i ≤ k .

• The sets |V1| = |Vk| = 1.


• Let s and t be the source and destination vertices respectively.
• The multistage graph problem is to find a minimum – cost path from s to t .
• The cost of a path from s to t is the sum of the costs of the edges on the path.

15 March 2021,03:00-03:50 P.M Department of CSE, GIT Course Code and Title:19ECS234 and DESIGN AND ANALYSIS OF ALGORITHMS GVSN 15
Backward approach
4
A D 18
1 9
11
2 5 13
s B E t
16
5 C F 2
2
V1 V2 V3 V4

s t d(s , t)=min{1+d(A , t) , 2+d(B , t) , 5+d(C , t)}


d(s, t)= min{ 1+22 ,2+18 , 5+4}
A d(s , t)= min { 23 , 20 ,9} =9
1

2 t
s B

5 s C F t
C
D

4
A t
11

d( A , t)= min{ 4 + d (D, t) , 11+ d (E , t)} C t


d( A , t)= min{ 4 + 18 , 11+ 13}
d( A , t)= min{ 22 , 24 } = 22
2 F
D
9
d( C , t) = min{ 2 + d(F , t)}
B 5 E t
d( C , t) = min{ 2 + 2}
16 D 18
d( C , t) = min{ 4 } = 4
F
13
E t
d( B , t) = min{ 9 + d( D , t) , 5 + d( E , t) , 16 + d( F ,t) }
d( B , t) = min{ 9 + 18 , 5 + 13 , 16 + 2 }
d( B , t) = min{ 27 , 18 , 18 } = 18 F 2
Algorithm Bgraph(G,k,n,p)
{
bcost[1] := 0.0; // cost is initializes to zero
for j :=2 to n do // we will start from next stage of source, so total are 2 to n
   { // Compute bcost[j], 
Let r be such that (r, j)is an edge of  G and bcost[r]+ c[r, j] is minimum; // calculate min cost of
edge in next level
         bcost[j] := bcost[r] + c[r, j]; 
         d[j] := r;  // adds the min cost node to ‘d’ array
     }       
// Find a minimum-cost path.
        p[1] := 1;  p[k] := n;   // start node(p[1]) and end nodes(p[k])
     for j := k – 1 to 2 do p[j] := d[p[j + 1]];  // as of backward approach we will add nodes and
edges from last to source
 }
Time Complexity = Ɵ(|V|+|E|)
15 March 2021,03:00-03:50 P.M Department of CSE, GIT Course Code and Title:19ECS234 and DESIGN AND ANALYSIS OF ALGORITHMS GVSN 18
Forward approach
4
A D 18
1 9
11
2 5 13
s B E t
16
5 C F 2
2
V1 V2 V3 V4
4
A D
1 9
A
1
2
d( s , A ) = 1 s B
2
s B
d( s , B ) = 2
d( s , D ) = min { 1 + d( A , D ) , 2 + d( B , D )}
d( s , C ) = 5
5 C d( s , D ) = min { 1 + 4 , 2 + 9 }
d( s , D ) = min { 5 , 11 } = 5
A
1
11
2 5
s B E
D 18
5
d( s , E ) = min { 1 + d( A , E ) , 2 + d( B , E ) 7 13
s E t
d( s , E ) = min { 1 + 11 , 2 + 5 }
d( s , E ) = min { 12 , 7 } = 7
7 F 2
2
s B
16 d( s , t )= min { d( s , D) + d ( D , t) , d( s , E) + d ( E , t) , d( s , F) + d ( F , t) }
5 C F d( s , t )= min { 5 + 18 , 7 + 13 , 7 + 2 }
2
d( s , t )= min { 23 , 20 , 9 } = 9
d( s , F ) = min { 2 + d( B , F ) , 5 + d( C , F ) }
d( s , F ) = min { 2 + 16 , 5 + 2 }
d( s , F ) = min { 18 , 7 } = 7 s C F t
Algorithm FGraph(G , k, n, p)
 // The input is a k-stage graph G = (V, E) with n vertices indexed in order of stages.
// E is a set of edges and c[i,j] is the cost of (i, j).p[1 : k] is a minimum-cost path.
 {
 cost[n]:=0.0;
  for j :=n -1 to 1 step-1 do
  { / / Compute cost[j].
  Let r be a vertex such that (j,r) is an edge of G and c[j,r] + cost[r]is  minimum;   
  cost[j]= c[j,r]+ cost[r];
d|j]:=r;
  } 
   // Find a minimum-cost path.
  p[1]:=1;p[k]:=n; 
  for j :=2 to k -1 do p[j]:=d[p[j-1]];
 } Time Complexity = Ɵ(|V|+|E|)

15 March 2021,03:00-03:50 P.M Department of CSE, GIT Course Code and Title:19ECS234 and DESIGN AND ANALYSIS OF ALGORITHMS GVSN 21
RELIABILITY DESIGN

 In reliability design, the problem is to design a system that is composed of several devices connected in series.
 Let ri be the reliability of device Di. Then, the reliability of the entire system is ri. ( – product operation)

D1 D2 D3 .... Dn

 In this design, when devices are connected in a series, then if any of the device not working due to any
hardware or software issues, then entire system will not work because all devices are connected serially.

 To solve this issue, if we can design a machine with duplicate devices in every stage, then if any of the devices
gets the problem, other duplicate devices will handle the job.

16 March 2021,03:00-03:50 P.M Department of CSE, GIT Course Code and Title:19ECS234 and DESIGN AND ANALYSIS OF ALGORITHMS GVSN 22
 If we duplicate the devices at each stage then the reliability of the system can be increased.
 If stage i contains mi copies of devices  Di , then the probability that all  mi  have a malfunction is (1 - ri)mi .

 Hence the reliability of stage i becomes 1 – (1 - ri)mi


Dn
D1 D3 Dn
D2
D1 D3 Dn
D2 ....
D1 D3 Dn

 Malfunctioning means mot working properly.


 Functioning of device + Not Functioning properly = 1 (maximum probability is always 1)
 So, malfunctioning = 1 - ri for a single device. If we have more number of devieces, then it will be (1 - ri)mi .

 Functioning properly(Reliability) = 1 – (1 - ri)mi

16 March 2021,03:00-03:50 P.M Department of CSE, GIT Course Code and Title:19ECS234 and DESIGN AND ANALYSIS OF ALGORITHMS GVSN 23
 Let assume that the reliability of stage i is given by function Øi (mi) i≤ n.
 The reliability of the system of stages is
 The reliability design problem is to use device duplication to maximize reliability.
 But this maximization is to be carried out under a cost constraint.
 Let ci be the cost of each unit of device i and let C be the maximum allowable cost of the system being
designed.
Maximize  maximize the reliability
Subject to ≤ C where ≥ 1 ,
 under the constraint is devices cost in all stages should not be greater than the total cost of the machine.
ui = [ (C+ - ] [C- Total Cost, ci – cost of device in that stage, cj – sum of all devices cost]

ui  how many number of maximum duplicate devices we can design in that stage
16 March 2021,03:00-03:50 P.M Department of CSE, GIT Course Code and Title:19ECS234 and DESIGN AND ANALYSIS OF ALGORITHMS GVSN 24
• Design a three stage system with device types D1 , D2 , D3 . The cost are $30 , $ 15 , $ 20 respectively. The
cost of the system is to be no more than $105. The reliability of each device type is 0.9 , 0.8 , 0.5
respectively.
u2 = [ (C+ - ]
sol: c1 = 30 , c2 = 15 , c3 = 20
= [( 105+15- (c1+ c2+ c3))/15]
r1 = 0.9 , r2 = 0.8 , r3 = 0.5
= [(120-(30+15+20))/15]
C=105
= 55/15=3.66=3
ui = [ (C+ - ]
u3 = [ (C+ - ]
u1 = [ (C+ - ]
= [( 105+20- (c1+ c2+ c3))/20]
= [( 105+30- (c1+ c2+ c3))/30]
= [(125-(30+15+20))/20]
= [(135-(30+15+20))/30]
= 60/20=3
= 70/30=2.33=2
19 March 2021,03:00-03:50 P.M Department of CSE, GIT Course Code and Title:19ECS234 and DESIGN AND ANALYSIS OF ALGORITHMS GVSN 25
c1 = 30 , c2 = 15 , c3 = 20

r1 = 0.9 , r2 = 0.8 , r3 = 0.5 D1

C=105
D1
D1

S11 = ( 0.9 , 30)


1 – (1 - ri)mi
=1 – (1 – 0.9)2
S21 = ( 0.99 , 60)
=1 – (0.1)2
=1 – 0.01
1D 2D = 0.99
S1 = {( 0.9 , 30)
1
, ( 0.99 ,1 60)}

19 March 2021,03:00-03:50 P.M Department of CSE, GIT Course Code and Title:19ECS234 and DESIGN AND ANALYSIS OF ALGORITHMS GVSN 26
c1 = 30 , c2 = 15 , c3 = 20
D2
r1 = 0.9 , r2 = 0.8 , r3 = 0.5 D2
D1 D1 D2
D2
C=105
D2 D1
D2
D1 D2 D1
S1 = {( 0.9 , 30) , ( 0.99 , 60)} D1 D2
1D1 2D1
0.9 X 0.8 = 0.72

=1 – (1- 0.8)3 0.99 X 0.8 = 0.792


D2
D1
=1 – (0.2)2 D2
S1 = ( 0.72 , 45)
2

=1 – 0.008 D1 D2
= ( 0.792 , 75) D1 D2
= 0.992
S = ( 0.864 , 60)
2
2
=1 – (1- 0.8)2
0.9 X 0.992 = 0.8928
=1 – (0.2)2
= ( 0.95 , 90) =1 – 0.04
0.99 X 0.992 = 0.982
= 0.96
S32 = (0.892 , 75)
0.9 X 0.96 = 0.864
= ( 0.982 , 105) 0.99 X 0.96 = 0.95
1D1,1D2 1D1,2D2 2D1,1D2 1D1,3D2 2D1,2D2 2D1,3D3
S =2021,03:00-03:50
2
19 March { ( 0.72 , 45),P.M (0.864 , 60) ( 0.792
Department , 75)
of CSE, GIT , (0.892 ,Course
75),Code
( 0.95 , 90) , ( 0.982
and Title:19ECS234 , 105)}
and DESIGN AND ANALYSIS OF ALGORITHMS GVSN 27
(1D1 , 1D2) (1D1 , 2D2) (1D1 , 3D2) D2
D3
D1 D2 D1 D2 D3
S2 = { ( 0.72 , 45) , (0.864 , 60) , (0.892 , 75)}
S13 = ( 0.36 , 65) 0.864 X 0.5 = 0.432 0.72 X 0.5 = 0.36
D2 D3
D3
= ( 0.432 , 80) D1 D2 D1 D2
D3 D3
= ( 0.446 , 95) 1- (1- 0.5)2
0.864 X 0.75 = 0.648
S 3
= ( 0.54 , 85) D2 =1- (0.5)2
2
D2 = 1- 0.25= 0.75
D3 0.72 X 0.75 = 0.54
= ( 0.648 , 100) D1
D2 D3
S33 = ( 0.63 , 105) D1 D2 D3
0.892 X 0.5 = 0.446 D3
1- (1- 0.5)3
=1- (0.5)2
S = { ( 0.36
3
, 65)
1D1,1D 2,1D3
, (0.432
1D1,2D,2,1D
80)3 , (0.54 , 85)
1D1,1D , ( 0.446
2,2D3 1D1,3D,2,1D
95)3 , ( 0.648
1D1,2D,2,2D
100)
3
, ( 0.63
1D1,1D, 2105)
,3D3 } = 1- 0.125= 0.875
0.72 X 0.875 = 0.63
Maximum Reliability = 1D1,2D2,2D3 = (0.648 , 100)

D2 D3
D1
D2 D3

19 March 2021,03:00-03:50 P.M Department of CSE, GIT Course Code and Title:19ECS234 and DESIGN AND ANALYSIS OF ALGORITHMS GVSN 29
TRAVELING SALESPERSON PROBLEM

 Let G( V , E ) be a directed graph with edge costs cij .

 The variable cij is defined such that cij > 0 for all i and j and cij = ∞ if ( i , j )
 A tour of G is a directed simple cycle that includes every vertex in V. The cost of a tour is the sum
of the cost of the edges on the tour.
 The traveling salesperson problem is to find a tour of minimum cost.
g( i , S ) = min {cij + g( j , S – { j} )}
where i – choosen vertex, S – set of all other vertices in the graph Ex: g(1,{2,3,4}) = min{c12 + g(2, {3,4})
cij – cost from i to j (j is one of the vertex from set S)

S – {j}  one vertex from j is taken in cij, so remaining are S-{j}

 Initially g(i, Ø) = ci1 (initially


23 March 2021,02:00-02:50 P.M set contains
Department of CSE, GIT no values)
Course Code and Title:19ECS234 and DESIGN AND ANALYSIS OF ALGORITHMS GVSN 30
10
1 5 2
1 2 3 4
20 9
1 0 10 15 20
6 15 10 8 2 5 0 9 10
13 8 3 6 13 0 12
3 9 4 4 8 8 9 0
12

g( i , S ) = min {cij + g( j , S – { j} )}
Source vertex j

g(i, Ø) = ci1 g( 2 , Ø , 1) = c21 = 5 When |S| = 0

g( 3 , Ø , 1) = c31 = 6
g( 4 , Ø , 1) = c41 = 8

23 March 2021,02:00-02:50 P.M Department of CSE, GIT Course Code and Title:19ECS234 and DESIGN AND ANALYSIS OF ALGORITHMS GVSN 31
g( 2 , 3 , 1) = c23 + g( 3, 1) = 9+6 = 15
When |S| = 1
g( 2 , 4 , 1) = c24 + g( 4, 1) = 10+8 = 18
1 2 3 4
g( 3 , 2 , 1) = c32 + g( 2, 1) = 13+5 = 18
1 0 10 15 20
g( 3 , 4 , 1) = c34 + g( 4, 1) = 12+8 = 20 2 5 0 9 10
g( 4 , 2 , 1) = c42 + g( 2, 1) = 8+5 = 13 3 6 13 0 12
4 8 8 9 0
g( 4 , 3 , 1) = c43 + g( 3, 1) = 9+6 = 15
g( 2 , {3,4} , 1) = min {c23 + g( 3, 4 ,1) , c24 + g( 4, 3 ,1) }
= min { (9 + 20) , (10 + 15) } = 25
g( 3 , {2,4} , 1) = min {c32 + g( 2, 4 ,1) , c34 + g( 4, 2 ,1) }
When |S| = 2
= min { (13 + 18) , (12 + 13) } = 25
g( 4 , {2,3} , 1) = min {c42 + g( 2, 3 ,1) , c43 + g( 3, 2 ,1) }
= min { (8 + 15) , (9 + 18) } = 23

23 March 2021,02:00-02:50 P.M Department of CSE, GIT Course Code and Title:19ECS234 and DESIGN AND ANALYSIS OF ALGORITHMS GVSN 32
When |S| = 3

g( 1 , {2,3,4} , 1) = min {(c12 + g( 2,{3,4} ,1)), (c13 + g( 3, {2,4} ,1) , (c14 + g( 4, {2,3} ,1) }
= min {( 10+ 25), (15 + 25) , (20 + 23) }
= min {35, 40 , 43 } = 35
The optimal solution is 35
1– 2 – 4 – 3 – 1
10 9
10
1 2 4 3

Time complexity for the travelling sales person problem is O(n2 2n )

23 March 2021,02:00-02:50 P.M Department of CSE, GIT Course Code and Title:19ECS234 and DESIGN AND ANALYSIS OF ALGORITHMS GVSN 33
OPTIMAL BINARY SEARCH TREES

 A Binary Search Tree is a tree where the key values are stored in the internal nodes. The
external nodes are null nodes.
 For each internal node all the keys in the left sub-tree are less than the keys in the node,
and all the keys in the right sub-tree are greater the keys in the node.

24 March 2021,09:00-10:50 A.M Department of CSE, GIT Course Code and Title:19ECS234 and DESIGN AND ANALYSIS OF ALGORITHMS GVSN 34
Two Binary Search Trees (BST)
1 1
10 10
2 2 2 2
5 25 5 20
3 3 3
20 15 25
4
15
• For each identifier with equal searching probability , average number of comparisons for a successful
search:

Left: (1+2+2+3+4)/5 = 2.4 Right: (1+2+2+3+3)/5 = 2.2

• probability(5, 10, 15, 20, 25) = (0.3, 0.3, 0.05, 0.05, 0.3)

Left :1  0.3 + 2  0.3+ 2 0.3 + 3  0.05 + 4  0.05 = 1.85

Right: 1  0.3 + 2  0.3+ 2 0.05 + 3  0.05 + 3  0.3= 2.05


24 March 2021,09:00-10:50 A.M Department of CSE, GIT Course Code and Title:19ECS234 and DESIGN AND ANALYSIS OF ALGORITHMS GVSN 35
Extended Binary Trees

 Add external nodes to the original binary search tree 0


10
 Take external nodes as failure nodes .
1 1
 External / internal path length 5 25
2 2 2 2
 Internal path length, I, is: 20
3 3
I=0+1+1+2+3=7 15
4 4
 External path length, E, is :

E = 2 + 2 + 4 + 4 + 3 + 2 = 17

 E = I + 2n, where n is number of internal nodes.

24 March 2021,09:00-10:50 A.M Department of CSE, GIT Course Code and Title:19ECS234 and DESIGN AND ANALYSIS OF ALGORITHMS GVSN 36
 In the binary search tree( BST):

 Let assume that the set of Identifiers is a1, a2, …, an with a1 < a2 < … < an
1
 pi : probability of successful search for ai
10
 qi : probability of unsuccessful
n
search
n
ai < x < ai+1 2 2
 p  q 1
i i
3
5
3
25
3
i 1 i 0 3
 Total cost 20
n n 4 4
 p  level (a )   q  (level (failure node i)  1)
i i i 15
i 1 i 0 5 5

 An optimal binary search tree for a1, …, an is the one that minimizes the total cost.
24 March 2021,09:00-10:50 A.M Department of CSE, GIT Course Code and Title:19ECS234 and DESIGN AND ANALYSIS OF ALGORITHMS GVSN 37
Cost(l)= ∑1≤i≤k-1 pi * level(ai) + ∑0≤i≤k-1 qi * (level (Ei)-1)

Cost(r)= ∑k<i≤n pi * level(ai) + ∑k<i≤n qi * (level (Ei)-1)

Tij : an optimal binary search tree for ai+1, …, aj, i < j.

cij : cost of Tij, where cii=0.


j
rij : root of Tij , wij  qi   (q
k i 1
k  pk )
wij : weight of Tij ,

T0n is an optimal binary search for a1, …, an.

24 March 2021,09:00-10:50 A.M Department of CSE, GIT Course Code and Title:19ECS234 and DESIGN AND ANALYSIS OF ALGORITHMS GVSN 38
ak

L R

cij = pk + cost(L) + cost(R) + weight(L) + weight(R)

= pk + ci,k1 + ckj + wi,k1 + wk,j

1≤k≤n
c(0,n) = min{c(0,k-1) + c(k,n) + p(k) + w(0,k-1) + w(k,n)

i<k≤j
c(i,j) = min{c(i,k-1) + c(k,j) + p(k) + w(i,k-1) + w(k,j) }

i<k≤j
24 March 2021,09:00-10:50 A.M Department of CSE, GIT Course Code and Title:19ECS234 and DESIGN AND ANALYSIS OF ALGORITHMS GVSN 39
c(i,j) = min{c(i,k-1) + c(k,j)} + w(i,j)
Let n = 4 and (a1, a2, a3, a4) = (do, if, int, while).

Let p(1:4)=(p1, p2, p3, p4) = (3, 3, 1, 1)

q(0:4)=(q0, q1, q2, q3, q4) = (2, 3, 1, 1, 1). Construct OBST (T04 ) 2
0 1 i 3 4
Sol: Initially w(i , i)=q(i) , c(i , i) = 0 and r( i , i )=0 w00=2 w11=3 w22=1 w33=1 w44=1
0 c00=0 c11=0 c22=0 c33=0 c44=0
w(0 , 0) = q(0)=2 w(3 , 3) = q(3)=1 r00=0 r11=0 r22=0 r33=0 r44=0
c(0 , 0) = 0 c(3 , 3) = 0 w01=8 w12=7 w23=3 w34=3
r(0 , 0) = 0 r(3 , 3) = 0 1 c01=8 c12=7 c23=3 c34=3
|j-i| r01=1 r12=2 r23=3 r34=4
w02=12 w13=9 w24=5
w(1 , 1) = q(1)=3 w(4 , 4) = q(4)=1 2 c02=19 c13=12 c24=8
c(1 , 1) = 0 c(4 , 4) = 0 r02=1 r13=2 r24=3
r(1 , 1) = 0 r(4 , 4) = 0 w03=14 w14=11
3 c03=25 c14=19
r03=2 r14=2
w(2 , 2) = q(2)=1 w04=16
c(2 , 2) = 0 4 c04=32
r(2 , 2) = 0 r04=2

24 March 2021,09:00-10:50 A.M Department of CSE, GIT Course Code and Title:19ECS234 and DESIGN AND ANALYSIS OF ALGORITHMS GVSN 40
w(i,j) = w(i,j-1)+p(j)+q(j)
|j-i|=1 c(i,j) = min{c(i,k-1) + c(k,j)} + w(i,j)
i<k≤j
r(i,j) = k
w(0 , 1) = w(0,0) +p(1) + q(1)
= 2 + 3 + 3 =8 0 1 2 3 4
w00=2 w11=3 w22=1 w33=1 w44=1
c(0 , 1) = min{c(0,1-1) + c(1,1)} + w(0,1)
0 c00=0 c11=0 c22=0 c33=0 c44=0
0<k≤1
r00=0 r11=0 r22=0 r33=0 r44=0
= 0+0+8=8 w01=8 w12=7 w23=3 w34=3
r(0 , 1) = 1 1 c01=8 c12=7 c23=3 c34=3
w(1 , 2) = w(1,2-1) +p(2) + q(2) r01=1 r12=2 r23=3 r34=4
w02=12 w13=9 w24=5
= w(1,1) +p(2) + q(2)
2 c02=19 c13=12 c24=8
= 3 + 3 + 1 =7
r02=1 r13=2 r24=3
c(1 , 2) = min{c(1,2-1) + c(2,2)} + w(1,2) w03=14 w14=11
1<k≤2 c03=25 c14=19
3
= 0+0+7=7 r03=2 r14=2
r(1 , 2) = 2 w04=16 w(3 , 4) = w(3,4-1) +p(4) + q(4)
4 c04=32
w(2 , 3) = w(2,3-1) +p(3) + q(3) = w(3,3) +p(4) + q(4)
r04=2 = 1 + 1 + 1 =3
= w(2,2) +p(3) + q(3)
= 1 + 1 + 1 =3 c(3 , 4) = min{c(3,4-1) + c(4,4) }+ w(3,4)
c(2 , 3) = min{c(2,3-1) + c(3,3)} + w(2,3) 3<k≤4
2<k≤3 = 0+0+3=3
= 0+0+3=3 r(3 , 4) = 4
r(2 , 3) = 3
24 March 2021,02:00-02:50 P.M Department of CSE, GIT Course Code and Title:19ECS234 and DESIGN AND ANALYSIS OF ALGORITHMS GVSN 41
w(0 , 2) = w(0,2-1) +p(2) + q(2) |j-i|=2
= w(0,1) +p(2) + q(2)
= 8 + 3 + 1 =12
c(0 , 2) = min{(c(0,1-1) + c(1,2)) , (c(0,2-1) + c(2,2))} + w(0,2)
0<k≤2 K=1 K=2
= min{(c(0,0)+c(1,2)) ,(c(0,1)+c(2,2))}+w(0,2)
0 1 2 3 4
= min{(0+7),(8+0)}+12=7+12=19 w00=2 w11=3 w22=1 w33=1 w44=1
r(0 , 2) = 1 c00=0 c11=0 c22=0 c33=0 c44=0
0
w(1 , 3) = w(1,3-1) +p(3) + q(3) r00=0 r11=0 r22=0 r33=0 r44=0
= w(1,2) +p(3) + q(3) w01=8 w12=7 w23=3 w34=3
= 7 + 1 + 1 =9 1 c01=8 c12=7 c23=3 c34=3
c(1 , 3) = min{(c(1,2-1) + c(2,3)) , (c(1,3-1) + c(3,3))} + w(1,3) r01=1 r12=2 r23=3 r34=4
1<k≤3 K=2 K=3 w02=12 w13=9 w24=5
2 c02=19 c13=12 c24=8
= min{(c(1,1)+c(2,3)) ,(c(1,2)+c(3,3))}+w(1,3)
r02=1 r13=2 r24=3
= min{(0+3),(7+0)}+12=3+9=12 w03=14 w14=11
r(1 , 3) = 2 c03=25 c14=19
w(2 , 4) = w(2,4-1) +p(4) + q(4) 3
r03=2 r14=2
= w(2,3) +p(4) + q(4) w04=16
= 3 + 1 + 1 =5 4 c04=32
c(2 , 4) = min{(c(2,3-1) + c(3,4)) , (c(2,4-1) + c(4,4))} + w(2,4) r04=2
2<k≤4 K=3 K=4
= min{(c(2,2)+c(3,4)) ,(c(2,3)+c(4,4))}+w(2,4)
= min{(0+3),(3+0)}+5=3+5=8
r(2 , 4) = 3
24 March 2021,02:00-02:50 P.M Department of CSE, GIT Course Code and Title:19ECS234 and DESIGN AND ANALYSIS OF ALGORITHMS GVSN 42
|j-i|=3

w(0 , 3) = w(0,3-1) +p(3) + q(3)


= w(0,2) +p(3) + q(3)
= 12 + 1 + 1 =14
c(0 , 3) = min{(c(0,1-1) + c(1,3)) , (c(0,2-1) + c(2,3)), (c(0,3-1) + c(3,3))} + w(0,3)
0<k≤3 K=1 K=2 K=3
= min{(c(0,0)+c(1,3)) ,(c(0,1)+c(2,3)),(c(0,2)+c(3,3))}+w(0,3) 0 1 2 3 4
= min{(0+12),(8+3),(19+0)}+14=11+14=25 w00=2 w11=3 w22=1 w33=1 w44=1
r(0 , 3) = 2 0 c00=0 c11=0 c22=0 c33=0 c44=0
r00=0 r11=0 r22=0 r33=0 r44=0
w01=8 w12=7 w23=3 w34=3
1 c01=8 c12=7 c23=3 c34=3
r01=1 r12=2 r23=3 r34=4
w(1 , 4) = w(1,4-1) +p(4) + q(4) w02=12 w13=9 w24=5
= w(1,3) +p(4) + q(4) 2 c02=19 c13=12 c24=8
= 9 + 1 + 1 =11 r02=1 r13=2 r24=3
c(1 , 4) = min{(c(1,2-1) + c(2,4)) , (c(1,3-1) + c(3,4)), (c(1,4-1) + c(4,4))} + w(1,4) w03=14 w14=11
1<k≤4 K=2 K=3 K=4 3 c03=25 c14=19
= min{(c(1,1)+c(2,4)) ,(c(1,2)+c(3,4)),(c(1,3)+c(4,4))}+w(2,4) r03=2 r14=2
w04=16
= min{(0+8),(7+3),(12+0)}+11=8+11=19 4 c04=32
r(1 , 4) = 2 r04=2

24 March 2021,02:00-02:50 P.M Department of CSE, GIT Course Code and Title:19ECS234 and DESIGN AND ANALYSIS OF ALGORITHMS GVSN 43
|j-i|=4

w(0 , 4) = w(0,4-1) +p(4) + q(4)


= w(0,3) +p(4) + q(4)
= 14 + 1 + 1 =16
c(0 , 4) = min{(c(0,1-1) + c(1,4)) , (c(0,2-1) + c(2,4)), (c(0,3-1) + c(3,4)), (c(0,4-1) + c(4,4))} + w(0,4)
0<k≤4 K=1 K=2 K=3 K=4
0 1 2 3 4
= min{(c(0,0)+c(1,4)) ,(c(0,1)+c(2,4)),(c(0,2)+c(3,4)),c(0,3)+c(4,4))}+w(0,3) w00=2 w11=3 w22=1 w33=1 w44=1
= min{(0+19),(8+8),(19+3),(25+0)}+16=16+16=32 0 c00=0 c11=0 c22=0 c33=0 c44=0
r(0 , 4) = 2 r00=0 r11=0 r22=0 r33=0 r44=0
w01=8 w12=7 w23=3 w34=3
1 c01=8 c12=7 c23=3 c34=3
r01=1 r12=2 r23=3 r34=4
w02=12 w13=9 w24=5
2 c02=19 c13=12 c24=8
r02=1 r13=2 r24=3
w03=14 w14=11
3 c03=25 c14=19
r03=2 r14=2
w04=16
4 c04=32
r04=2
24 March 2021,02:00-02:50 P.M Department of CSE, GIT Course Code and Title:19ECS234 and DESIGN AND ANALYSIS OF ALGORITHMS GVSN 44
r[ i , j ]
if

0 1 2 3 4
r[ i , k-1 ] r[ k , j ]
w00=2 w11=3 w22=1 w33=1 w44=1
do int c00=0 c11=0 c22=0 c33=0 c44=0
0
r00=0 r11=0 r22=0 r33=0 r44=0
r[ 0 , 4 ] = 2 w01=8 w12=7 w23=3 w34=3
1 c01=8 c12=7 c23=3 c34=3
while
r01=1 r12=2 r23=3 r34=4
w02=12 w13=9 w24=5
r[ 0 , 1 ] = 1 r[ 2 , 4 ] = 3 2 c02=19 c13=12 c24=8
r02=1 r13=2 r24=3
w03=14 w14=11
r[0,0]= 0 r[1,1]= 0 3 c03=25 c14=19
r03=2 r14=2
r[ 2 , 2 ] = 0 r[ 3 , 4 ] = 4
w04=16
4 c04=32
r04=2
r[ 3 , 3 ] = 0 r[ 4 , 4 ] = 0

24 March 2021,02:00-02:50 P.M Department of CSE, GIT Course Code and Title:19ECS234 and DESIGN AND ANALYSIS OF ALGORITHMS GVSN 45
Algorithm OBST(p,q,n)
// Given n distinct identifiers a1 < a2 < … < an, and probabilities
// p[i], 1 ≤ i ≤ n, and q[i], 0 ≤ i ≤ n this algorithm computes
//the cost c[i,j] of optimal binary search trees Tij

// for identifiers ai+1, …, aj. It also computes r[i,j] , the root of Tij.

//w[i,j] is the weight of Tij


{
for i:= 0 to n-1 do
{
w[i,i]:= q[i]; r[i,i]:= 0; c[i,i]:= 0; // initialize
w[i,i+1]:= q[i] + q[i+1] + p[i+1]; // optimal trees with one node
r[i,i+1] = i+1;
c[i,i+1] = q[i] + q[i+1] + p[i+1];
}
24 March 2021,02:00-02:50 P.M Department of CSE, GIT Course Code and Title:19ECS234 and DESIGN AND ANALYSIS OF ALGORITHMS GVSN 46
w[n,n]:= q[n]; r[n,n]:=0; c[n,n] =0;
for m:= 2 to n do // find optimal trees with m nodes
for i:= 0 to n-m do
{
j:= i+m;
w[i,j]:= w[i,j-1] + p[j] + q[j];
k := Find(c,r,i, j);
// A value of l in the range r[i, j-1]≤ l ≤ r[i+1, j] that minimizes c[i, l-1]+c[l,j];
c[i,j]:= w[i,j] + c[i,k-1] + c[k,j];
r[i,j]:= k;
}
write(c[0,n],w[0,n],r[0,n]);
}
Time complexity= O(n3)
Algorithm Find(c,r,i,j)
{
min:=∞;
For m:=r[i,j-1] to r[i+1,j] do
if(c[i,m-1]+c[m,j])<min then
{
min:= c[i,m-1]+c[m,j]); l:=m;
}
}
24 March 2021,02:00-02:50 P.M Department of CSE, GIT Course Code and Title:19ECS234 and DESIGN AND ANALYSIS OF ALGORITHMS GVSN 47
1. Find minimum cost tour for given graph using dynamic programming

2. Design a three stage system with device types D1 , D2 , D3 . The cost are $20 , $ 25 , $ 35
respectively. The cost of the system is to be no more than $175. The reliability of each device type is
0.8 , 0.7 , 0.4 respectively.
24 March 2021,02:00-02:50 P.M Department of CSE, GIT Course Code and Title:19ECS234 and DESIGN AND ANALYSIS OF ALGORITHMS GVSN 48
THANK YOU

24 March 2021,02:00-02:50 P.M Department of CSE, GIT Course Code and Title:19ECS234 and DESIGN AND ANALYSIS OF ALGORITHMS GVSN 49

You might also like