You are on page 1of 27

CS 384: AAD laboratory; Assignment-II 2017

Soumya Gourab Sahoo 115CS0237

Q.5. STRASSENS’s Matrix multiplication

Practical implementation of Strassens’s matrix multiplication algorithm usually


switch to the brute force method after matrix sizes become smaller than some
crossover point. Run an experiment to determine such crossover point on your
computer system.

Solution:

Driver Program

A = zeros(32,32);
B = zeros(32,32);
C = zeros(32,32);
a = zeros(1,5);
b = zeros(1,5);
c = zeros(1,5);

for x = 1:5
n = 2 ^ x;
A = round(rand(n,n) * 100);
B = round(rand(n,n) * 100);
a(x) = Strassen(A,B,4);
b(x) = Normal_multi(A,B);
c(x) = n;
end
plot(c,a,c,b);
title('Multiplication Simulation');
xlabel('Number of elements');
ylabel('No. of multiplications');
disp('Analysis of multiplication');
legend('Strassen','Normal' );
grid on;
a
b
c

Function for Strassen Multiplication

function count = Strassen(A,B,nmin)


count=0;
[C,count] = Strassens(A, B, nmin,count);
end

function [C,count] = Strassens(A, B, nmin,count)

Page 1 of 27
CS 384: AAD laboratory; Assignment-II 2017
Soumya Gourab Sahoo 115CS0237

n = length(A);
if n ~= 2^( log2(n) )
error('The matrix dimension must be a power of 2.')
end

if n <= nmin
count = Normal_multi(A,B);
C = A*B;
else
count = count + 1;
% disp('inside Else');
m = n/2; i = 1:m; j = m+1:n;
[P1,count] = Strassens( A(i,i)+A(j,j), B(i,i)+B(j,j), nmin,count);
[P2,count] = Strassens( A(j,i)+A(j,j), B(i,i), nmin,count);
[P3,count] = Strassens( A(i,i), B(i,j)-B(j,j), nmin,count);
[P4,count] = Strassens( A(j,j), B(j,i)-B(i,i), nmin,count);
[P5,count] = Strassens( A(i,i)+A(i,j), B(j,j), nmin,count);
[P6,count] = Strassens( A(j,i)-A(i,i), B(i,i)+B(i,j), nmin,count);
[P7,count] = Strassens( A(i,j)-A(j,j), B(j,i)+B(j,j), nmin,count);
C = [ P1+P4-P5+P7 P3+P5; P2+P4 P1+P3-P2+P6 ];
end

end

Normal Multiplication Function

function [ count ] = Normal_multi( A,B )


%UNTITLED4 Summary of this function goes here
% Detailed explanation goes here
n = length(A);
count = 0;
C = zeros(n,n);
for i = 1:n
for j = 1:n
for k = 1:n
count = count + 1;
C(i,j) = C(i,j) + A(i,k) * B(k,j);
end
end
end
end

Graphical Analysis

Page 2 of 27
CS 384: AAD laboratory; Assignment-II 2017
Soumya Gourab Sahoo 115CS0237

Note: The Graph for the Strassen Multiplication shows almost the
x axis as the no of multiplications in case of Strassen algorithm
restricts to 64 whereas the no of multiplications in case of
normal algorithm is in order of 104.

Q.6. QUICK SELECT

Use the QUICK SELECT algorithm to find 3rd largest element in an array of n
integers. Analyze the performance of QUICK SELECT algorithm for the different
instance of size 50 to 500 element. Record your observation with the number of
comparison made vs. instance.

Page 3 of 27
CS 384: AAD laboratory; Assignment-II 2017
Soumya Gourab Sahoo 115CS0237

Solution:

Driver Program

x=zeros(1,100);
y1=zeros(1,100);
a=zeros(1,100);

for i=50:1:500
a=round(rand(1,i)*100);
x(i)=i;
y1(i)=quicks1(a,i);
end
plot(x,y1);
xlabel('no of elements ');
ylabel('no of comparisons');
legend('quick select to find 3rd largest');
grid on;

Function for Quick Select

function [ k ] = quicks1( a,n )


%UNTITLED9 Summary of this function goes here
% Detailed explanation goes here

k=0;
lb=1;
ub=n;
[a,k]=quicksort(a,lb,ub,k,n);
end

function [a,k]= quicksort(a,lb,ub,k,n)


pivot=a(lb);
i=ub;
j=ub;

while i > lb
if lb == (n-3)
break;
end
k=k+1;
if a(i) >= pivot

Page 4 of 27
CS 384: AAD laboratory; Assignment-II 2017
Soumya Gourab Sahoo 115CS0237

temp = a(i);
a(i) = a(j);
a(j) = temp;
j=j-1;
end
i=i-1;
end
temp = a(lb);
a(lb)=a(j);
a(j)=temp;
if lb == (n-3)
return;
end
if j-lb>1
[a,k]=quicksort(a,lb,j-1,k,n);
end
if ub-j>1
[a,k]=quicksort(a,j+1,ub,k,n);
end
end

Graphical Analysis

Page 5 of 27
CS 384: AAD laboratory; Assignment-II 2017
Soumya Gourab Sahoo 115CS0237

Q.7. Matrix Chain Multiplication Comparision

Given a matrix chain A1 …An with the dimension of each of the matrices given by
the vector p = <12,21,65,18,24,93,121,16,41,31,47,5,47,29,76,18,72,15>. (n=17)
Write and run both the dynamic programming and memorized versions of this
algorithm to determine the minimum number of multiplications that are needed
(use type longint) and the factorization that produces this best case number of
multiplications. Run each of the two programs over an appropriately large
number of times (put each in a loop to run repeatedly x times) and obtain the
times at the beginning and end of the run. Use these times to determine the
comparative runtimes of the two algorithms.

Solution:
Driver Program

y= [12,21,65,18,24,93,121,16,41,31,47,5,47,29,76,18,72,15];
count3 = Matgoura(y,17);
fprintf('No. of operations: %d\n',count3);

Function for Matrix chain Mutiplication

function [ count ] = Matgoura( p,n )


%UNTITLED2 Summary of this function goes here
% Detailed explanation goes here
m=zeros(n,n);

for l=2:n
for i=1:n-l+1
j=i+l-1;
m(i,j)=inf;
for k=i:j-1
q=m(i,k)+m(k+1,j)+p(i)*p(k+1)*p(j+1);
if(q<m(i,j))
m(i,j)=q;
end
end
end
end
count=m(1,n);
disp(m)

Page 6 of 27
CS 384: AAD laboratory; Assignment-II 2017
Soumya Gourab Sahoo 115CS0237

end

Matrix Output

Page 7 of 27
CS 384: AAD laboratory; Assignment-II 2017
Soumya Gourab Sahoo 115CS0237

Q.8. BINOMIAL COEFFICIENTS


Computing the binomial coefficients C(n, k) defined by the following recursive
formula:
1, if k=0 or k=n;

{
C( n ,k )= C (n−1, k )+C (n−1 ,k−1), if 0<k<n ;
0, otherwise.

Write the program with three different algorithm to compute binomial


coefficients C(n, k) and compare them?

Solution:

c = zeros(1,10);
d = zeros(1,10);
f = zeros(1,10);
n = 10;
j=10;
i=1;
for k=1:n
d(i)=binomialCoeff(j,k);
f(i)=binomialCoef(j,k);
c(i)=k;
i=i+1;

end
plot(c,d,c,f);
title('Binomial Coefficient');
legend('1st method','2nd method');
xlabel('value of k');
ylabel('Number of comparisions');
disp('Binomial Coefficient');

grid on;

1st method

function [ a ] = binomialCoeff(n,k)
C=zeros(n+1,k+1);
a=0;
for i = 1:n+1
for j = 1:min(i, k+1)
a=a+1;
if j == 1 || j == i
C(i,j) = 1;

Page 8 of 27
CS 384: AAD laboratory; Assignment-II 2017
Soumya Gourab Sahoo 115CS0237

else
C(i,j) = C(i-1,j-1) + C(i-1,j);
end

end

end
% disp(C)
% C(n+1,k+1)
% a

end

2nd method

function [ a ] = binomialCoef(n,k)
D=zeros(k+1);
a=0;
D(1)=1;
for i = 2:n+1
for j = min(i, k+1):-1:2
a=a+1;
D(j) = D(j) + D(j-1);
end
end
disp(D)
end

% C(n+1,k+1)
% a

Graphical Anlaysis

Page 9 of 27
CS 384: AAD laboratory; Assignment-II 2017
Soumya Gourab Sahoo 115CS0237

Note: the third method is not done by me..

Q.9. SPANNING TREE

Page 10 of 27
CS 384: AAD laboratory; Assignment-II 2017
Soumya Gourab Sahoo 115CS0237

Write a program obtain minimum cost spanning tree the above NSF network
using Prim’s algorithm, Kruskal’s algorithm and Boruvka’s algorithm..

Solution:

MainProgram

Graph = zeros(14, 14);


Graph(1, :) = [0 3 5 0 0 0 8 0 0 0 0 0 0 0];
Graph(2, :) = [3 0 3 2 0 0 0 0 0 0 0 0 0 0];
Graph(3, :) = [5 3 0 0 0 0 0 0 0 0 0 0 0 3];
Graph(4, :) = [0 2 0 0 1 0 0 0 0 8 0 0 0 0];
Graph(5, :) = [0 0 0 1 0 2 0 0 0 0 0 0 0 3];
Graph(6, :) = [0 0 0 0 2 0 2 0 0 0 0 0 0 0];
Graph(7, :) = [8 0 0 0 0 2 0 1 0 0 0 0 0 0];
Graph(8, :) = [0 0 0 0 0 0 1 0 2 0 3 0 3 0];
Graph(9, :) = [0 0 0 0 0 0 0 2 0 3 0 2 0 0];
Graph(10,:) = [0 0 0 8 0 0 0 0 3 0 1 0 0 0];
Graph(11,:) = [0 0 0 0 0 0 0 3 0 1 0 4 0 0];
Graph(12,:) = [0 0 0 0 0 0 0 0 2 0 4 0 0 8];
Graph(13,:) = [0 0 0 0 0 0 0 3 0 0 0 0 0 4];
Graph(14,:) = [0 0 3 0 3 0 0 0 0 0 0 8 4 0];

[Edges, Weights] = Prims(Graph);


Edges
Weights

[Edges, Weights] = Kruskal(Graph);


Edges

Page 11 of 27
CS 384: AAD laboratory; Assignment-II 2017
Soumya Gourab Sahoo 115CS0237

Weights

Prim’s Algorithm

function [ Edges, Weights ] = Prims( Graph )

INF = 999;

for i = 1:length(Graph)
for j = 1:length(Graph)
if( Graph(i,j) == 0)
Graph(i,j) = INF;
end
end
end

Graph

Edges = zeros(length(Graph)-1, 2);


Weights = zeros(length(Graph) - 1,1);

for i = 1:length(Graph) - 1
[minrowwise, colindex] = min(Graph(1:i, :), [], 2);
[mincolwise, rowindex] = min(minrowwise);
Edges(i,:) = [rowindex colindex(rowindex)];
Weights(i) = Graph(rowindex, colindex(rowindex));
Graph(rowindex, colindex) = INF;
end

end

Kruskal’s Algorithm

function [ Edges, Weights ] = Kruskal( Graph )

Edges = zeros(length(Graph)-1, 2);


Weights = zeros(length(Graph) - 1,1);
EdgesList = zeros(length(Graph)-1, 2);
WeightsList = zeros(length(Graph) - 1,1);

k = 1;
for i = 1:length(Graph)
for j = i+1:length(Graph)
if( Graph(i,j) ~= 0)
EdgesList(k,:) = [i, j];
WeightsList(k,:) = Graph(i,j);
k = k + 1;

Page 12 of 27
CS 384: AAD laboratory; Assignment-II 2017
Soumya Gourab Sahoo 115CS0237

end
end
end

for i = 2:length(WeightsList)
temp1 = EdgesList(i);
temp2 = WeightsList(i);
for j = i-1:-1:1
if(temp2 < WeightsList(j))
WeightsList(j+1) = WeightsList(j);
WeightsList(j) = temp2;
EdgesList(j+1) = EdgesList(j);
EdgesList(j) = temp1;
end

end
end

Vertices = zeros(length(Graph), 1);

Edges(1, :) = EdgesList(1, :);


Vertices(Edges(1, 1)) = 1;
Vertices(Edges(1, 2)) = 1;

k = 1;
for i = 2:length(WeightsList)
if(k <= sum(Vertices) - 1)
k = k + 1;
Edges(k, :) = EdgesList(i, :);
Weights(k, :) = WeightsList(i, :);
Vertices(Edges(k, 1)) = 1;
Vertices(Edges(k, 2)) = 1;
end
end

end

Output:

Page 13 of 27
CS 384: AAD laboratory; Assignment-II 2017
Soumya Gourab Sahoo 115CS0237

Note: Boruvka’s Algorithm to find the minimum spanning tree could not be done
by me

Q.10. 0-1 KNAPSACK PROBLEM


Write a program that computes optimal solution to the 0–1 Knapsack Problem
using dynamic programming? You may test your program with the following
example:

Page 14 of 27
CS 384: AAD laboratory; Assignment-II 2017
Soumya Gourab Sahoo 115CS0237

There are n = 5 objects with integer weights w[1..5] = {1,2,5,6,7}, and values
v[1..5] = {1,6,18,22,28}. Assuming a knapsack capacity of 11).

Solution:

Driver Program
n = 5;
Weights = [1, 2, 5, 6, 7]
Values = [1, 6, 18, 22, 28]
KnapsackCapacity = 11;

[Max, OptSequence] = Knapsack(Weights, Values, KnapsackCapacity);


Max

Items = find(OptSequence)
sum(Weights(Items))
sum(Values(Items))

Function for KnapSack Problem

function [Max ,OptSequence] = Knapsack(Weights, Values, Capacity)


A = zeros(length(Weights)+1,Capacity+1);
for j = 1:length(Weights)
for Y = 1:Capacity
if Weights(j) > Y
A(j+1,Y+1) = A(j,Y+1);
else
A(j+1,Y+1) = max( A(j,Y+1), Values(j) + A(j,Y-Weights(j)+1));
end
end
end

Max = A(end,end);

OptSequence = zeros(length(Weights),1);
a = Max;
j = length(Weights);
Y = Capacity;
while a > 0
while A(j+1,Y+1) == a
j = j - 1;
end
j = j + 1;
OptSequence(j) = 1;

Page 15 of 27
CS 384: AAD laboratory; Assignment-II 2017
Soumya Gourab Sahoo 115CS0237

Y = Y - Weights(j);
j = j - 1;
a = A(j+1,Y+1);
end
end

Output

Q.11. STRING MATCHING ALGORITHM


Given two strings P and T over the same alphabet set S, determine whether P
occurs as a substring in T (or find in which position(s) P occurs as a substring in T).
The strings P and T are called pattern and text respectively. Compare the
efficiency of three string matching algorithms (Brute-Force Algorithm, Knuth-

Page 16 of 27
CS 384: AAD laboratory; Assignment-II 2017
Soumya Gourab Sahoo 115CS0237

Morris-Pratt and Boyer-Moore Algorithm ) by varying pattern length [1-15] for


n=5000.

Solution:

Driver Program

text =
'ifatcmkzozjwuivwlzopicwhxfrdjnsslfhzbcsoyzsmvcssvhnbeqcjshaevbezgwqpwsvmfvnly
fkhceztlncaukjdkgpvdbiqbxoowbyjfzajqehusbydkajkoiknvtklxbsrxveqsyfhlocxsljyvjx
syawlxofsdwowgajkuhclewrhhkqqlrmqiertbsuthrgzqtcctyfbaunedwrlqeucfzfnplnscvzxu
tkmibszezcztfiocdbivwosncmgpdctjzdryfdjydteqjlyifkpxmucocwhgaxedcgnmzyptmnkuui
vkbugtssujkatoupcpxgtnwluszwnwutoskkldjxkhybqmibylhtwtlglekgxdksibuunlrghkpgkq
lkollxfvxcjnikupoyeqihkgsafebnukxegaomcbtnfprigocuffphyimyuswwmkutvcqmjdriyyqy
nercjtmvrgpvpkrxiqvupogeilmqabaazcspyeqzrdqvjwrdedegmijrybqlotwokzdnsathjfyhux
rvojfasgvopawggycwghtmsrwxxugyumpoqnwlugfexsruwbwcpcuqcvrpknytmopnycdfqdlgsqob
dqkurzwbboekqymecezijhhjwlioiwzpttycyffqyarpiogqwdnoroilbcghbouqdhudvkjsolhsit
flhayehkzviknrkzppcrqpdyqrscwdfaeddkoytundiepeodkqgwuhsisljltqvcrspwdesxuoekvf
eedvvvugxxwohbmshqmglkudfrswlcfiptngsbumqfqkflybiwgipotzzjpcxymlrsnjlimowrzpzb
rozqfqgrautujybhmxaixpynmzvufjdvqoxzhwvggpqgnnwwpnfmrjttockulrgcdppkodkookpqel
jcusxkuoucyjlxhqfasnfrhwfpazeuqnsbqhblgkflwystgqhxcqwuvwuiecniarsryofdfnkrumgk
mbhrjmswrwylhmxhhrfljbdchkofuutyayplrdpgfmhzevusfoutqbxhcmmjlatjgaxdgvmxtydadi
koumjuwncgphwqbajhihekzrttpkfqdwqzyqqpttrlatmpxodpwbtnxagiretmgwojiaxhktwzjnqu
iypbxjotkjnsxodqczxwuavlxvxuithkujdaltpfbgbawlddwsjwvnygospqnaklqusvouyjtnkskx
efcmxfrrsrzsrxwfnbtnefdlnwjwvsbgsobujcwrnjxjrtsuucqgpqvvsdrybvyiinubsevmiujmkw
vefoclxaapdqdgzeebuhfqjjfsesdicekflznrjbculfcahixwlfcxaypqwiyblxrouhcqvawfrhwq
gxvyzhkefsxnpwykdujmlexhkzlxdetjlynnmnqapxaaofkumxxebpyesaihnugtooljighnyfrvro
jwtpguvzerqpnynmcicnvctwomowyttwprwjidbtyqjvhjsjotiptpaerjddfelnskplpddglcyosi
mxntwlntskubjutgjfqxnibxjsewptfqrnkrhqmlqkeumvancggtmxrxpkagmlelgxtbhaswgmfefx
umycfocjmaievmxhwnvkdqpfjrauhqqldtaauhudrpkfrhdupocppmmbfctnchhcydplvclxzpmfmc
volodevjcnqijgifgdwcdicnsqrfmsnrwsgxlggtdzermbfkxgfbnlthwdeohnpichdpmsancfrmgn
hgxqirbgvrxxnckywujalccrzxzgshyfujratyheciiutdzyjqurkxrgmdoyvpkrqxecjjzsuoibrd
xxcsbsfglyffyyievbgszievnpuderlnejponshyfmlgjietwndmhhmyloqkjfplhmyhuaeadmlpgc
kkzvkozlooxswsdbpvcmpyzapxjytzpnbxjxoiejdesqoqnzeyqpgnlykkrcarqeghctjcjttyscjh
jfuopxeijskaqiapgdfqtzfolhzdfrostkzsfjvnxmniizcqrfsblysrjchlnyjzmqteplhswlmkxo
erodqkryzluoaqkrraextpiqnzpmehthrwlrweadfqgbtooqavkubwsfnqlczgpphkmpfjtsxjwsbf
nukyjmdpeedhzkrorzvfyuegrolkonkuiiivgymyyxqpgfkrzcdempufpbhvxdejtcatnmjrqohwhi
stdfddglmsoekqrutyhirzphkrgxdbdqmcsoxewleqdunpofpxvnfxezelfgrnbmmgbnbjqgejatza
ofykifvtmjtjtezitqbuizhhwdgvbbpwztgkpeccvkfdrxxcgljlifzoamvglwtqthkvvvbsxjqbwk
wcktmaumxcqvqdgioznrvvauxfsntghsbhunnfzqkbzhkmdyqefbdezvsagjnusgnnzwaxircrhmmb
apztxknyuaaryhiabgfwunemtmsfhunoepjdgjgydwgflsckjfoyqmckezelshnlfxiviuvrrjqqqf
mkgdqksjabdmsxdkuwokebtdifqttxmpntvmxcrrdxbzkicgqfpiyewnesjjyttpplojahxqnbpeyj
fygmcgtsazudqbcqnaonwaslihnpctcioetmnvaywbulfbouzsfaheqdskkcrvjyuhddwdqoonyfqo
dfmdmzwdfwtofnmkbactdziktkoiljrxrxcghjlswcsvtxmjggqbvqzvznpiipmiznhmlhfgfozvpd
ehxbhmtygdcdicgopijzfdmuixgddqncprpslsgxqzbzefkzlwaesqrjajofmrstgcnebvdvlfshbk
zeagfgeagdogatrodtiwwtufplpbgopdtdfobaztxtszonwesivdjeeoandaxmrmmsqdlmzohhiyov
kwsehphrcwjwemlnzvzhbmkygwwygcsaxeidvezvddxzewhicemhhiwuijgtsfpxqyruobcwjrrchj

Page 17 of 27
CS 384: AAD laboratory; Assignment-II 2017
Soumya Gourab Sahoo 115CS0237

pxmtcfgxvrdloarxogtrnseefkvekxjlkvnylqgouahqpqeevgzokdrwhobdjqpbtfafuqjnrzfnul
muzisssaoqxjhtkbfaaqlmzfoookefjltoehokipipqhdfdlvnytgskywsxczawrpidfxuutqisuge
lcmrzteizeynjttyvpeztzhqblpajraumfqxnoeooaeqginrmxlfdlhmcvpdcthotqlvracjxapwvz
ugsdnyoqhtptvdwrwfcwgnwxbmgfluqryhfmjkbrzpnsogneiupqvuareypttkyfhykjkstnjyayow
yqfkcdcbifggslzxppuoexluambiyunsswllcbaxmrkmziqspaqbosgvxabhoeppsjjvunuxtzsyih
teiyyqupahkejuiksstymnvzhsgghfadnlfnzieqsjmirpdnmpeqoxbnchpaphnmjhcovykvvofnur
igcfgdunvrblhdyccqolamrjhmrvqavmmehudkghxyfagfoosondcxxhaoosdfglsjzgstpeyivefd
imgnafsroymcyaohlkwihuhjnecplibkovlrdspsiufrjpegiuaxocjsbpbgpzptxxrxwcfexlypfs
zoyefhxzcepahqppgbunnzevxktuwydffddcbawaulkbmpdhsybqngsbxzfzegpmspysfhevtpspcx
eboropxmjmtfumeptunnazrhmjwffyublowwmxyzumyohhzeaxskptevnvynvigtaqwgawpbjgehzz
eszxjiysqqqhziystzfvjbnzufswmapwgiaxvfhrsljpocqazkasdfggwjscyyzioejxvtnsmugraj
awawbyzvdmuinyvjgnfxaxcquppbrxnlsjhzepknfbxnqyqdlrscnffnfgnzgmrpvwtcnxiydxhxbq
mfhnmhqmdegxjtdvcvrxmbeezsyxrcdxudhhfutctadwzlweqtctqucpwoxcevpvqiqwrxldzskdbq
pamjkudocogambncwidzdaajnhqqrgtcggrxdbtlwsbannbdryzoqupbaerdbgenxxvpqukkiocnai
nosaupaburflrtmenoilmosupvmcurnukhtwvmjfvwmlqizafeeogmihhqpmwscjlorleyskygxcfr
hlqizkzpfqmvadnuzwtjxmdxgdjeosfvbtczkfyyplnvnqvsjkauwysypaydgxnhszpozibtltebql
deufvsxroaajpcywdanhsugdvfunblghytaqsfhzojhndifrdqbbonzfodqmafqokqflisaclubdvv
gqfxfjxfozncngxswcczoyjythjtrjcelxacrpjihnjymqydcbklkmbedyerwtptpvrahxcclzcwvu
ezuxxkuqefmsdtcpmqskrtyupqtxxtplaxwkjciybabkgsucgpfoacrwytxkifduujjwxntkgqimeq
dvbzqiaxlzkpsfuohbwghbsjbumohieawzfmzupzrgrbrgykkgeutcnrxuttxfnhkxglulvtljfnvp
buucsssjbitbkegjyfcoueszaelyyaslxqtxiysyrfcxtjwrnxsnultdkaltkdcyozrjdpxtjwyapo
ptlkviszkjxfgpslffslwxcwljriodyumcusjighnngotbvprumxfnaqgegiedyamofdqlxqkqxovm
nvvedsdblazlqjgtjuwqnpmbfqofqymymwfyooodusnrtwwkcrbmhjbgohduorpzfpyyhezlnptcil
yozzyvhermeukikngtyklzpmxuqweuhodrswffiwqchuectemtcxjdmywqjoatgmsferdlqazgpkdg
agcziaahvlgxkprpspaszdastwkqvuqfqbdxoejewefkfkuzqidglfsdybrjcyfqgtrdtqefriywif
dydwkwtpt';
pattern = 'adbbdfdsagdfrth';

x = zeros(1,15);
yb = zeros(1,15);
ykmp = zeros(1,15);
for i = 1:15
x(i) = i;
[~, yb(i)] = BruteForce(pattern(1:i), text);
[~, ykmp(i)] = KMP(pattern(1:i),text);
end

plot(x, yb, x, ykmp);


legend('BruteForce', 'KMP');

BruteForce Method

function [ index, count ] = BruteForce( pattern, text)

index = -1;
n = length(pattern);
m = length(text);
count = 0;
for i = 1:m-n+1
count = count + 1;
pml = 0;

Page 18 of 27
CS 384: AAD laboratory; Assignment-II 2017
Soumya Gourab Sahoo 115CS0237

for j = 1:n
if(strcmp(text(i+j-1), pattern(j)) == 1)
count = count + 1;
pml = pml + 1;
else
count = count + 1;
break;
end
end
count = count + 1;
if(pml == length(pattern))
index = i;
break;
end
end

end

Knuth-Morris-Pratt Algorithm

function [ index, count ] = KMP( pattern, text )

n = length(pattern);
m = length(text);
count = 0;
index = -1;

[PartialMatchTable, count] = genPartialMatchTable(pattern, count);

for i = 1:m-n+1
partialmatchlength = 0;
count = count + 1;
for j = 1:n
if(strcmp(text(i+j-1), pattern(j)) == 1)
count = count + 1;
partialmatchlength = partialmatchlength + 1;
else
count = count + 1;
if(partialmatchlength > 0)
i = i + partialmatchlength -
PartialMatchTable(partialmatchlength);
end
break;
end
end

if(partialmatchlength == length(pattern))
count = count + 1;
index = i;

Page 19 of 27
CS 384: AAD laboratory; Assignment-II 2017
Soumya Gourab Sahoo 115CS0237

break;
end
end
end

function [ PartialMatchTable, count ] = genPartialMatchTable( str, count )

PartialMatchTable = zeros(length(str), 1);

for i = 1:length(str)
count = count + 1;
[PartialMatchTable(i), count] = calPMTval(str(1:i), count);
end

end

function [ val, count ] = calPMTval( str , count )

val = 0;

for i = 1:length(str) - 1
count = count + length(str(1:end - i));
if(strcmp(str(1:end - i), str(1+i:end)) == 1)
val = length(str(1:end - i));
break;
end
end

Graphical Analysis

Page 20 of 27
CS 384: AAD laboratory; Assignment-II 2017
Soumya Gourab Sahoo 115CS0237

Note: The Boyer-Moore Algorithm could not be implemented by me in this


question.

Q.12. MATRIX CHAIN MULTIPLICATION


Write a program to compute the best ordering of matrix multiplication. Include
the routine to print the actual ordering.

Solution:

y= [12,21,65,18,24,93,121,16,41,31,47,5,47,29,76,18,72,15];

Page 21 of 27
CS 384: AAD laboratory; Assignment-II 2017
Soumya Gourab Sahoo 115CS0237

count3 = Matgoura(y,17);
fprintf('No. of operations: %d\n',count3);

function [ count ] = Matgoura( p,n )


%UNTITLED2 Summary of this function goes here
% Detailed explanation goes here
m=zeros(n,n);

for l=2:n
for i=1:n-l+1
j=i+l-1;
m(i,j)=inf;
for k=i:j-1
q=m(i,k)+m(k+1,j)+p(i)*p(k+1)*p(j+1);
if(q<m(i,j))
m(i,j)=q;
end
end
end
end
count=m(1,n);
disp(m)
end

Q.13. PATHING ALGORITHM


Use Floyd–Warshall algorithm (also known as Floyd's algorithm) to compute all
pair shortest path for any one of the following standard network

Page 22 of 27
CS 384: AAD laboratory; Assignment-II 2017
Soumya Gourab Sahoo 115CS0237

ARPA Network

Page 23 of 27
CS 384: AAD laboratory; Assignment-II 2017
Soumya Gourab Sahoo 115CS0237

NSF Network

Page 24 of 27
CS 384: AAD laboratory; Assignment-II 2017
Soumya Gourab Sahoo 115CS0237

NATIONAL NETWORK

Solution:

graphs=zeros(20,20);
path=zeros()
for i =1:20
for j=1:20
graphs(i,j)=10000;
if(i==j)
graphs(i,j)=0;
end
end
end
graphs(1,18)=2;
graphs(1,20)=3;
graphs(1,15)=1;
graphs(2,3)=4;
graphs(2,4)=5;
graphs(2,7)=6;
graphs(3,2)=4;
graphs(3,6)=8;
graphs(3,5)=7;
graphs(4,2)=5;
graphs(4,8)=9;
graphs(4,9)=10;
graphs(5,3)=7;
graphs(5,6)=11;
graphs(5,12)=12;
graphs(6,3)=8;
graphs(6,5)=11;
graphs(6,7)=13;
graphs(7,2)=6;
graphs(7,8)=14;
graphs(7,6)=13;
graphs(8,7)=14;
graphs(8,4)=9;
graphs(8,11)=15;
graphs(9,4)=10;
graphs(9,10)=16;
graphs(9,11)=17;
graphs(9,13)=18;
graphs(10,11)=19;
graphs(10,9)=16;
graphs(10,12)=20;
graphs(11,8)=15;
graphs(11,10)=19;
graphs(11,9)=17;
graphs(11,20)=21;
graphs(12,5)=12;
graphs(12,10)=20;

Page 25 of 27
CS 384: AAD laboratory; Assignment-II 2017
Soumya Gourab Sahoo 115CS0237

graphs(12,14)=22;
graphs(13,9)=18;
graphs(13,15)=23;
graphs(13,19)=24;
graphs(14,12)=22;
graphs(14,16)=25;
graphs(14,18)=26;
graphs(15,1)=1;
graphs(15,13)=23;
graphs(15,16)=27;
graphs(15,19)=28;
graphs(16,17)=29;
graphs(16,14)=25;
graphs(16,15)=27;
graphs(17,20)=31;
graphs(17,16)=29;
graphs(17,19)=30;
graphs(18,20)=32;
graphs(18,14)=26;
graphs(18,1)=2;
graphs(19,15)=28;
graphs(19,13)=24;
graphs(19,17)=30;
graphs(20,1)=3;
graphs(20,11)=21;
graphs(20,18)=32;
graphs(20,17)=31;
graphs

dist=zeros(20,20);
for i=1:20
for j=1:20
dist(i,j)=graphs(i,j);
end
end
dist
for k=1:20
for i=1:20
for j=1:20
if ((dist(i,k)+dist(k,j)) <= dist(i,j))
dist(i,j)= dist(i,k)+ dist(k,j);
end
end
end
end
dist

Page 26 of 27
CS 384: AAD laboratory; Assignment-II 2017
Soumya Gourab Sahoo 115CS0237

Output:

Note: the output shows the shortest path cost from each node to each node, i.e. the first shows the
minimum or shortest path cost from node A to A then B ,then C, & so on……….

Page 27 of 27

You might also like