Professional Documents
Culture Documents
2021 APL Assignment II
2021 APL Assignment II
2021 APL Assignment II
For each of the data formats: random, reverse ordered, and nearly sorted, run
your program say SORTTEST for all combinations of sorting algorithms and data
sizes and complete each of the following tables. When you have completed the
tables, analyze your data and determine the asymptotic behavior of each of the
sorting algorithms for each of the data types (i) Random data, (ii) Reverse
Ordered Data, (iii) Almost Sorted Data and (iv) Highly Repetitive Data . select
the suitable no of elements for the analysis that supports your program.Almost
Sorted
Code:
sorttest.m
clear all
N = 100;
nelt = zeros(1,N);
iscomp = zeros(1,N);
sscomp = zeros(1,N);
qscomp = zeros(1,N);
qsrandcomp = zeros(1,N);
mscomp = zeros(1,N);
Page 1 of 59
2021
CS 6381: Advanced Programming Laboratory; Assignment-I
bscomp = zeros(1,N);
k = 1;
for n = 1:N
nelt(k) = n;
isavg = zeros(1,100);
qsavg = zeros(1,100);
qsrandavg = zeros(1,100);
ssavg = zeros(1,100);
msavg = zeros(1,100);
bsavg = zeros(1,100);
for t = 1:100
a = round(rand(1,n)*100);
%a = sort(a,'descend');
%a = almost_sorted(a,0.1);
a = repeat(a,0.6);
isavg(t) = is(a,1,n);
ssavg(t) = ss(a,n);
qsavg(t) = qs(a,1,n);
qsrandavg(t) = qsrand(a,1,n);
msavg(t) = ms(a,n);
Page 2 of 59
2021
CS 6381: Advanced Programming Laboratory; Assignment-I
bsavg(t) = bs(a,n);
end
iscomp(k) = mean(isavg);
sscomp(k) = mean(ssavg);
qscomp(k) = mean(qsavg);
qsrandcomp(k) = mean(qsrandavg);
mscomp(k) = mean(msavg);
bscomp(k) = mean(bsavg);
k = k+1;
end
plot(nelt,bscomp,nelt,iscomp,nelt,sscomp,nelt,mscomp,nelt,qscomp,nelt,qsrandco
mp,'Linewidth',2)
legend('Bubble Sort', 'Insertion Sort', 'Selection Sort', 'Merge Sort', 'Quick Sort',
'Randomized Quick Sort')
title('Performance of Sorting Algorithms on Highly Repetitive Input')
xlabel('Number of Elements')
ylabel('Number of Comparisons')
grid on
Page 3 of 59
2021
CS 6381: Advanced Programming Laboratory; Assignment-I
bs.m
function [comp] = bs2(a,n)
comp = 0;
for i = 1:n-1
flag = 0;
for j = 1:n-i
comp = comp+1;
if a(j)>a(j+1)
%swap a(j) and a(j+1)
tt = a(j);
a(j) = a(j+1);
a(j+1) = tt;
flag = 1;
end
end
if flag == 0
break;
end
end
end
is.m
Page 4 of 59
2021
CS 6381: Advanced Programming Laboratory; Assignment-I
ss.m
function [comp] = ss(a,n)
comp = 0;
for i = 1:n-1
min = i;
for j = i:n
comp = comp+1;
if a(j)<=a(i)
min = j;
end
Page 5 of 59
2021
CS 6381: Advanced Programming Laboratory; Assignment-I
end
c = a(min);
a(min) = a(i);
a(i) = c;
end
end
ms.m
function [comp,a] = ms(a,n)
comp = 0;
if length(a)>1
mid = round(n/2);
[comp1,a1] = ms(a(1:mid),mid);
[comp2,a2] = ms(a(mid+1:n),n-mid);
[comp3,a] = msjoin(a1,a2);
comp = comp1+comp2+comp3;
end
end
msjoin.m
function [comp,a] = msjoin(a1,a2)
Page 6 of 59
2021
CS 6381: Advanced Programming Laboratory; Assignment-I
comp = 0;
a1ind = 1;
a2ind = 1;
a1len = length(a1);
a2len = length(a2);
a = zeros(1,a1len+a2len);
k = 1;
while(a1ind<=a1len)
a(k) = a1(a1ind);
Page 7 of 59
2021
CS 6381: Advanced Programming Laboratory; Assignment-I
a1ind = a1ind+1;
k = k+1;
end
while(a2ind<=a2len)
a(k) = a2(a2ind);
a2ind = a2ind+1;
k = k+1;
end
end
qs.m
function [comp] = qs(a,b,n)
comp = 0;
if(b<n)
[mid,comp1,a] = qspart(a,b,n);
comp = comp + comp1;
comp = comp + qs(a,b,mid-1);
comp = comp + qs(a,mid+1,n);
end
end
qspart.m
Page 8 of 59
2021
CS 6381: Advanced Programming Laboratory; Assignment-I
mid = i+1;
end
qsrand.m
function [comp] = qsrand(a,b,n)
comp = 0;
if(b<n)
Page 9 of 59
2021
CS 6381: Advanced Programming Laboratory; Assignment-I
[mid,comp1,a] = qspartrand(a,b,n);
comp = comp + comp1;
comp = comp + qs(a,b,mid-1);
comp = comp + qs(a,mid+1,n);
end
end
qspartrand.m
function [mid,comp,a] = qspartrand(a,b,n)
comp = 0;
p = round(b + (n-b)*rand());
pivot = a(p);
i = b-1;
for j = b:n-1
comp = comp+1;
if a(j) < pivot
i = i+1;
c = a(i);
a(i) = a(j);
a(j) = c;
end
end
c = a(i+1);
Page 10 of 59
2021
CS 6381: Advanced Programming Laboratory; Assignment-I
a(i+1) = a(n);
a(n) = c;
mid = i+1;
end
almost_sorted.m
function [a] = almost_sorted(a,percentage)
if(length(a)>=2)
a = sort(a);
swaps = round((percentage * length(a)) / 2);
for i=1:swaps
ind = ceil(rand()*length(a));
ind2 = ceil(rand()*length(a));
c = a(ind);
a(ind) = a(ind2);
a(ind2) = c;
end
end
end
repeat.m
function [a] = repeat(a,percentage)
Page 11 of 59
2021
CS 6381: Advanced Programming Laboratory; Assignment-I
repeats = round(percentage*length(a));
repeated = zeros(1,length(a));
value = a(1);
for i = 1:repeats
ind = ceil(rand()*length(a));
if(repeated(ind) == 0)
a(ind) = value;
repeated(ind) = 1;
end
end
end
Page 12 of 59
2021
CS 6381: Advanced Programming Laboratory; Assignment-I
Graphs:
Comments:
In the above graph we can see that on completely random data, the sorting
algorithms behave as expected, with Bubble, Selection, and Insertion sort
showing order of O(n2) growth rate, although insertion sort is noticeably faster
than the other two. Merge, Quick, and Randomized Quick sort all show O(nlgn)
growth. Due to the random nature of the data, Quick and Randomized Quick sort
both take the same time.
Page 13 of 59
2021
CS 6381: Advanced Programming Laboratory; Assignment-I
Comments:
In the worst case, which is completely reversed input, we can see that along with
Bubble, Insertion, and Selection sorts, Quick sort also grows with an order of
O(n2). However, it can be seen that Randomized Quick sort completely solves this
deficiency as it mostly prevents the worst case selection of the pivot. Thus it has a
worst case growth rate of O(nlgn), similar to the worst case growth rate of Merge
sort.
Page 14 of 59
2021
CS 6381: Advanced Programming Laboratory; Assignment-I
Comments:
On almost sorted input, Insertion sort has the best performance due to the low
number of inversions, as the time complexity of insertion sort actually depends on
the number of inversions. Hence it can be said that insertion sort is preferable
when data is mostly sorted. The other sorting algorithms perform as expected,
with Bubble and Selection sort growing at a rate of O(n 2), and Merge and Quick
sort growing at a rate of O(nlgn).
Page 15 of 59
2021
CS 6381: Advanced Programming Laboratory; Assignment-I
Comments:
In this case, merge sort performs the best, followed by Quick sort and
Randomized Quick sort. Both perform almost exactly the same, which can be
explained by the fact that randomization has little effect unless the data is
significantly unsorted. Insertion sort performs better than the other incremental
algorithms due to the low number of inversions, as the complexity of insertion
sort is determined by the number of inversions. Selection sort and Bubble sort
both take O(n2) time to perform the sorting in this case.
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.
Code:
quickselect.m
% Performance analysis of quickselect
% Sangramjit Chakraborty (221cs1265)
clear all
N = 500;
nelt = zeros(1,46);
qscomp = zeros(1,46);
qsrandcomp = zeros(1,46);
k = 1;
for n = 50:10:N
nelt(k) = n;
Page 17 of 59
2021
CS 6381: Advanced Programming Laboratory; Assignment-I
qsavg = zeros(1,100);
qsrandavg = zeros(1,100);
for t = 1:100
a = round(rand(1,n)*100);
qsavg(t) = qs(a,1,n);
qsrandavg(t) = qsrand(a,1,n);
end
qscomp(k) = mean(qsavg);
qsrandcomp(k) = mean(qsrandavg);
k = k+1;
end
plot(nelt,qscomp,nelt,qsrandcomp,'Linewidth',2)
grid on
Page 18 of 59
2021
CS 6381: Advanced Programming Laboratory; Assignment-I
qs.m
function [comp] = qs(a,b,n)
comp = 0;
if(b<n)
[mid,comp1,a] = qspart(a,b,n);
comp = comp + comp1;
if(mid == 3)
return;
end
if(mid>3)
comp = comp + qs(a,b,mid-1);
elseif(mid<3)
comp = comp + qs(a,mid+1,n);
end
end
end
qspart.m
function [mid,comp,a] = qspart(a,b,n)
comp = 0;
pivot = a(n);
Page 19 of 59
2021
CS 6381: Advanced Programming Laboratory; Assignment-I
i = b-1;
for j = b:n-1
comp = comp+1;
if a(j) < pivot
i = i+1;
c = a(i);
a(i) = a(j);
a(j) = c;
end
end
c = a(i+1);
a(i+1) = a(n);
a(n) = c;
mid = i+1;
end
qsrand.m
function [mid,comp,a] = qspart(a,b,n)
comp = 0;
pivot = a(n);
i = b-1;
for j = b:n-1
comp = comp+1;
Page 20 of 59
2021
CS 6381: Advanced Programming Laboratory; Assignment-I
mid = i+1;
end
qspartrand.m
function [mid,comp,a] = qspart(a,b,n)
comp = 0;
pivot = a(n);
i = b-1;
for j = b:n-1
comp = comp+1;
if a(j) < pivot
i = i+1;
c = a(i);
Page 21 of 59
2021
CS 6381: Advanced Programming Laboratory; Assignment-I
a(i) = a(j);
a(j) = c;
end
end
c = a(i+1);
a(i+1) = a(n);
a(n) = c;
mid = i+1;
end
Graph:
Page 22 of 59
2021
CS 6381: Advanced Programming Laboratory; Assignment-I
Comments:
Three algorithms have been tested in this experiment. Quick sort utilizes the
naive approach of first sorting the array and then returning the 3rd element from
the beginning. The average case time complexity of this approach is equal to
Quick Sort, which is O(nlgn). We can also see that the Quick Select and
Randomized Quick Select algorithms are expectedly a lot faster with an average
case complexity of O(n). Randomized Quick Select is faster in the worst case, with
complexity of O(n), as opposed to normal Quick Select with a complexity of O(n 2).
Page 23 of 59
2021
CS 6381: Advanced Programming Laboratory; Assignment-I
Code:
binsearch.m
% Performance analysis of binary search
% Sangramjit Chakraborty (221cs1265)
clear all
N = 20;
nelt = zeros(1,N);
rbcomp = zeros(1,N);
ibcomp = zeros(1,N);
a = [1 3 5 7 9 11 13 15 17 19 21 23 25 27 29 31 33 35 37 39];
a = sort(a);
a_neg = [0,2,4,6,8,10,12,14,16,18,20,22,24,26,28,30,32,34,36,38,40];
for i = 1:N+1
nelt(i) = i;
Page 24 of 59
2021
CS 6381: Advanced Programming Laboratory; Assignment-I
plot(nelt,rbcomp,nelt,ibcomp,'Linewidth',2)
grid on
ib.m
function [comp] = ib(a,b,n,key)
comp = 0;
while(b<n)
comp = comp+1;
mid = round((b+n)/2);
if(a(mid) == key)
Page 25 of 59
2021
CS 6381: Advanced Programming Laboratory; Assignment-I
return;
elseif(key<a(mid))
n = mid-1;
else
b = mid+1;
end
end
end
rb.m
function [comp] = rb(a,b,n,key)
comp = 0;
if(b<n)
mid = round((b+n)/2);
comp = comp+1;
if(mid == key)
return;
elseif(mid>key)
comp = comp + rb(a,b,mid-1,key);
else
comp = comp + rb(a,mid+1,n,key);
end
end
Page 26 of 59
2021
CS 6381: Advanced Programming Laboratory; Assignment-I
end
repeatib.m
function [] = repeatib(a,b,n,key)
for i = 1:10000
ib(a,b,n,key);
end
end
repeatrb.m
function [] = repeatrb(a,b,n,key)
for i = 1:10000
rb(a,b,n,key);
end
end
Graph:
Page 27 of 59
2021
CS 6381: Advanced Programming Laboratory; Assignment-I
Page 28 of 59
2021
CS 6381: Advanced Programming Laboratory; Assignment-I
Comments:
In this experiment, 10000 runs of searches for every element have been timed, as
a single run is too fast to measure accurately. All n-1 unsuccessful searches have
also been measured similarly. It can be seen from the above graphs that both
iterative and recursive versions of binary search have a very similar performance.
Recursive binary search can be seen to be slightly slower than the iterative
version, but the difference is insignificant.
Page 29 of 59
2021
CS 6381: Advanced Programming Laboratory; Assignment-I
Write a program obtain minimum cost spanning tree the above NSF network
using Prim’s algorithm, Kruskal’s algorithm and Boruvka’s algorithm. Use the
appropriate data structure to store the computed spanning tree for another
application (broadcasting a message to all nodes from any source node).
Code:
mstfinder.m
g = [0 3 5 Inf Inf Inf Inf 8 Inf Inf Inf Inf Inf Inf;
3 0 3 2 Inf Inf Inf Inf Inf Inf Inf Inf Inf Inf;
5 3 0 Inf Inf 3 Inf Inf Inf Inf Inf Inf Inf Inf;
Inf 2 Inf 0 1 Inf Inf Inf Inf Inf Inf Inf 8 Inf;
Inf Inf Inf 1 0 3 2 Inf Inf Inf Inf Inf Inf Inf;
Inf Inf 3 Inf 3 0 Inf Inf 4 Inf 8 Inf Inf Inf;
Inf Inf Inf Inf 2 Inf 0 2 Inf Inf Inf Inf Inf Inf;
8 Inf Inf Inf Inf Inf 2 0 Inf 1 Inf Inf Inf Inf;
Page 30 of 59
2021
CS 6381: Advanced Programming Laboratory; Assignment-I
Inf Inf Inf Inf Inf 4 Inf Inf 0 3 Inf Inf Inf Inf;
Inf Inf Inf Inf Inf Inf Inf 1 3 0 Inf 2 Inf 3;
Inf Inf Inf Inf Inf 8 Inf Inf Inf Inf 0 2 Inf 4;
Inf Inf Inf Inf Inf Inf Inf Inf Inf 2 2 0 3 Inf;
Inf Inf Inf 8 Inf Inf Inf Inf Inf Inf Inf 3 0 1;
Inf Inf Inf Inf Inf Inf Inf Inf Inf 3 4 Inf 1 0];
fprintf('\n');
fprintf('\n');
PrimsMST.m
function [tree, totalweight] = PrimsMST(graph,N)
VT = zeros(1,N);
predecessor = zeros(1,N);
weights = Inf(1,N);
root = 1;
VE = ones(1,N);
Page 32 of 59
2021
CS 6381: Advanced Programming Laboratory; Assignment-I
predecessor(root) = root;
weights(root) = 0;
totalweight = 0;
while(~isEmpty(VE))
u = 0;
for i = 1:N
if(VE(i) == 1)
u = i;
end
end
for i = 1:N
if(VE(i) == 1) && (weights(i)<weights(u))
u = i;
end
end
VE(u) = 0;
VT(u) = 1;
totalweight = totalweight + weights(u);
for v = 1:N
Page 33 of 59
2021
CS 6381: Advanced Programming Laboratory; Assignment-I
tree = cell(1,N);
for i = 1:N
if(i~=predecessor(i))
tree{i} = [tree{i} predecessor(i)];
tree{predecessor(i)} = [tree{predecessor(i)} i];
end
end
end
KruskalsMST.m
function [tree,totalweight] = KruskalsMST(graph,N)
edge_num = 0;
for i = 1:N
for j = i+1:N
if(graph(i,j)~=Inf)
edge_num = edge_num+1;
Page 34 of 59
2021
CS 6381: Advanced Programming Laboratory; Assignment-I
end
end
end
weights = zeros(1,edge_num);
vertices = zeros(edge_num,2);
k = 1;
for i = 1:N
for j = i+1:N
if(graph(i,j)~=Inf)
weights(k) = graph(i,j);
vertices(k,1) = i;
vertices(k,2) = j;
k = k+1;
end
end
end
EXP = zeros(1,edge_num);
TE = zeros(1,edge_num);
u_f = UnionFind(N);
Page 35 of 59
2021
CS 6381: Advanced Programming Laboratory; Assignment-I
added = 0;
while(added < N-1)
min = -1;
for i = 1:edge_num
if(EXP(i)==0)
min = i;
break;
end
end
for i = 1:edge_num
if(EXP(i)==0) && (weights(i)<weights(min))
min = i;
end
end
EXP(min) = 1;
v1 = vertices(min,1);
v2 = vertices(min,2);
root1 = find(u_f,v1);
root2 = find(u_f,v2);
if(root1~=root2)
Page 36 of 59
2021
CS 6381: Advanced Programming Laboratory; Assignment-I
union(u_f,root1,root2);
TE(min) = 1;
added = added+1;
end
end
totalweight = 0;
tree = cell(1,N);
for i = 1:edge_num
if(TE(i) == 1)
totalweight = totalweight + weights(i);
v1 = vertices(i,1);
v2 = vertices(i,2);
tree{v1} = [tree{v1} v2];
tree{v2} = [tree{v2} v1];
end
end
end
BoruvkasMST.m
function [tree,totalweight] = BoruvkasMST(graph,N)
edge_num = 0;
Page 37 of 59
2021
CS 6381: Advanced Programming Laboratory; Assignment-I
for i = 1:N
for j = i+1:N
if(graph(i,j)~=Inf)
edge_num = edge_num+1;
end
end
end
weights = zeros(1,edge_num);
vertices = zeros(edge_num,2);
k = 1;
for i = 1:N
for j = i+1:N
if(graph(i,j)~=Inf)
weights(k) = graph(i,j);
vertices(k,1) = i;
vertices(k,2) = j;
k = k+1;
end
end
end
cheapest = zeros(1,N);
Page 38 of 59
2021
CS 6381: Advanced Programming Laboratory; Assignment-I
TE = zeros(1,edge_num);
u_f = UnionFind(N);
added = 0;
while(added<N-1)
for x = 1:edge_num
u = vertices(x,1);
v = vertices(x,2);
w = weights(x);
r1 = find(u_f,u);
r2 = find(u_f,v);
if(r1~=r2)
if(cheapest(r1)==0) || (weights(cheapest(r1))>w)
cheapest(r1) = x;
end
if(cheapest(r2)==0) || (weights(cheapest(r2))>w)
cheapest(r2) = x;
end
end
end
for i = 1:N
Page 39 of 59
2021
CS 6381: Advanced Programming Laboratory; Assignment-I
if(cheapest(i)~=0)
u = vertices(cheapest(i),1);
v = vertices(cheapest(i),2);
w = weights(cheapest(i));
r1 = find(u_f,u);
r2 = find(u_f,v);
if(r1~=r2)
union(u_f,r1,r2);
TE(cheapest(i)) = 1;
cheapest(r1) = 0;
cheapest(r2) = 0;
added = added+1;
end
end
end
end
totalweight = 0;
tree = cell(1,N);
for i = 1:edge_num
Page 40 of 59
2021
CS 6381: Advanced Programming Laboratory; Assignment-I
if(TE(i) == 1)
totalweight = totalweight + weights(i);
v1 = vertices(i,1);
v2 = vertices(i,2);
tree{v1} = [tree{v1} v2];
tree{v2} = [tree{v2} v1];
end
end
end
UnionFind.m
classdef UnionFind < handle
properties
forest
end
methods
function obj = UnionFind(size)
obj.forest = zeros(1,size);
for i = 1:size
obj.forest(i) = i;
end
end
Page 41 of 59
2021
CS 6381: Advanced Programming Laboratory; Assignment-I
function union(obj,root1,root2)
obj.forest(root2) = root1;
end
end
end
isEmpty.m
function [result] = isEmpty(array)
result = true;
for i = 1:length(array)
if(array(i) == 1)
result = false;
return;
end
end
end
Page 42 of 59
2021
CS 6381: Advanced Programming Laboratory; Assignment-I
Results:
Page 43 of 59
2021
CS 6381: Advanced Programming Laboratory; Assignment-I
Comments:
Page 44 of 59
2021
CS 6381: Advanced Programming Laboratory; Assignment-I
In the above program we take the input graph in an adjacency matrix format. The
three algorithms have been used to compute the Minimum Spanning Tree of the
graph and returns an adjacency list containing the tree. An adjacency list is the
most suitable representation of the MST for the given application, i.e.
broadcasting of messages. That is because we can find the adjacent nodes of any
node in O(1) time in this representation, which would take O(n) time in an
adjacency matrix or array tree format. We can see in the above output that all
three algorithms find an MST with the same weight. However Prim’s algorithm
finds a different MST than Kruskal’s and Boruvka’s algorithms. Further scope of
improvement is there by using Priority Queue for Prim’s algorithm instead of
linear search, and optimizing the Union-Find’s union function to always join the
smaller heighted tree on the larger one for Kruskal’s and Boruvka’s algorithms.
Write a program to verify that a candidate solution for a TSP tour is correct. The
input for this program will consist of a graph, with the cities as the vertices and
the connecting roads as edges, and the certificate. The graph may be expressed
as an N × N matrix with the (i, j) th entry indicating the distance between the cities
numbered i and j. If no road directly connects this pair of cities, the matrix entry
will be 0. The certificate consists of a sequential list of the cities visited during the
tour and the target value that the tour is not to exceed. Demonstrate that your
program decides this question in O(P(N)) time.
Code:
N = input('Number of Vertices: ');
G = zeros(N,N);
Page 45 of 59
2021
CS 6381: Advanced Programming Laboratory; Assignment-I
for i = 1:N-1
for j = i+1:N
prompt = sprintf('Enter edge %d, %d: ',i,j);
x = input(prompt);
G(i,j) = x;
G(j,i) = x;
end
end
tour = zeros(1,N);
start = input('Enter start node: ');
current = start;
visited(current) = 1;
prev = -1;
total = 0;
valid = 1;
for i = 2:N
prompt = sprintf('Enter Node #%d in tour: ',i);
Page 46 of 59
2021
CS 6381: Advanced Programming Laboratory; Assignment-I
new = input(prompt);
if(visited(new) == 0)
prev = current;
current = new;
visited(current) = 1;
total = total + G(prev,current);
else
valid = 0;
end
end
if(total>limit)
valid = 0;
end
if(valid)
'Tour is valid'
else
'Tour is invalid'
end
Page 47 of 59
2021
CS 6381: Advanced Programming Laboratory; Assignment-I
Results:
Comments:
We see that the given tour is valid for the entered graph according to the given
conditions. The tour visits every city exactly once, and the total weight is lower
than or equal to the required weight.
Page 48 of 59
2021
CS 6381: Advanced Programming Laboratory; Assignment-I
Comments:
The entered tour is invalid for the given graph as the total weight of the tour
exceeds the required weight entered by the user, although every city is being
visited only once.
Page 49 of 59
2021
CS 6381: Advanced Programming Laboratory; Assignment-I
Comments:
The tour is invalid as there is a repeated node in the tour. Repeated nodes are not
allowed in TSP tours as they have to be Hamiltonian cycles.
AR
PA Network
Page 50 of 59
2021
CS 6381: Advanced Programming Laboratory; Assignment-I
NSF Network
NATIONAL NETWORK
Page 51 of 59
2021
CS 6381: Advanced Programming Laboratory; Assignment-I
Code:
fwutil.m
ARPA = [0 Inf Inf Inf Inf Inf Inf Inf Inf Inf Inf Inf Inf Inf 1 Inf
Inf 2 Inf 3;
Inf 0 4 5 Inf Inf 6 Inf Inf Inf Inf Inf Inf Inf Inf Inf Inf
Inf Inf Inf;
Inf 4 0 Inf 7 8 Inf Inf Inf Inf Inf Inf Inf Inf Inf Inf Inf
Inf Inf Inf;
Inf 5 Inf 0 Inf Inf Inf 9 10 Inf Inf Inf Inf Inf Inf Inf Inf
Inf Inf Inf;
Inf Inf 7 Inf 0 11 Inf Inf Inf Inf Inf 12 Inf Inf Inf Inf Inf
Inf Inf Inf;
Inf Inf 8 Inf 11 0 13 Inf Inf Inf Inf Inf Inf Inf Inf Inf Inf
Inf Inf Inf;
Inf 6 Inf Inf Inf 13 0 14 Inf Inf Inf Inf Inf Inf Inf Inf Inf
Inf Inf Inf;
Inf Inf Inf 9 Inf Inf 14 0 Inf Inf 15 Inf Inf Inf Inf Inf Inf
Inf Inf Inf;
Inf Inf Inf 10 Inf Inf Inf Inf 0 16 17 Inf 18 Inf Inf Inf Inf
Inf Inf Inf;
Inf Inf Inf Inf Inf Inf Inf Inf 16 0 19 20 Inf Inf Inf Inf Inf
Inf Inf Inf;
Inf Inf Inf Inf Inf Inf Inf 15 17 19 0 Inf Inf Inf Inf Inf Inf
Inf Inf 21;
Inf Inf Inf Inf 12 Inf Inf Inf Inf 20 Inf 0 Inf 22 Inf Inf Inf
Inf Inf Inf;
Inf Inf Inf Inf Inf Inf Inf Inf 18 Inf Inf Inf 0 Inf 23 Inf Inf
Inf 24 Inf;
Page 52 of 59
2021
CS 6381: Advanced Programming Laboratory; Assignment-I
Inf Inf Inf Inf Inf Inf Inf Inf Inf Inf Inf 22 Inf 0 Inf 25 Inf
26 Inf Inf;
1 Inf Inf Inf Inf Inf Inf Inf Inf Inf Inf Inf 23 Inf 0 27 Inf
Inf 28 Inf;
Inf Inf Inf Inf Inf Inf Inf Inf Inf Inf Inf Inf Inf 25 27 0 29
Inf Inf Inf;
Inf Inf Inf Inf Inf Inf Inf Inf Inf Inf Inf Inf Inf Inf Inf 29 0
Inf 30 31;
2 Inf Inf Inf Inf Inf Inf Inf Inf Inf Inf Inf Inf 26 Inf Inf Inf
0 Inf 32;
Inf Inf Inf Inf Inf Inf Inf Inf Inf Inf Inf Inf 24 Inf 28 Inf 30
Inf 0 Inf;
3 Inf Inf Inf Inf Inf Inf Inf Inf Inf 21 Inf Inf Inf Inf Inf 31
32 Inf 0];
Page 53 of 59
2021
CS 6381: Advanced Programming Laboratory; Assignment-I
Page 54 of 59
2021
CS 6381: Advanced Programming Laboratory; Assignment-I
fwnsf = floydwarshall(NSF,16);
disp(fwnsf)
NAT = [0 Inf Inf Inf Inf Inf Inf Inf 1 2 Inf Inf Inf Inf Inf Inf
Inf Inf Inf Inf Inf Inf Inf 3;
Inf 0 4 Inf Inf 5 Inf Inf Inf 6 Inf Inf Inf Inf Inf Inf Inf
Inf Inf Inf Inf Inf Inf Inf;
Inf 4 0 7 8 Inf Inf Inf Inf Inf Inf Inf Inf Inf Inf Inf Inf
Inf Inf Inf Inf Inf Inf Inf;
Inf Inf 7 0 9 Inf Inf 10 11 12 Inf Inf Inf Inf Inf Inf Inf
Inf Inf Inf Inf Inf Inf Inf;
Inf Inf 8 9 0 13 14 15 Inf Inf Inf Inf 16 Inf Inf Inf Inf
Inf Inf Inf Inf Inf Inf Inf;
Inf 5 Inf Inf 13 0 Inf Inf Inf Inf 17 Inf Inf Inf Inf Inf Inf
Inf Inf Inf Inf Inf Inf Inf;
Inf Inf Inf Inf 14 Inf 0 19 Inf Inf Inf Inf 20 21 22 23 Inf
Inf Inf Inf Inf Inf Inf Inf;
Inf Inf Inf 10 15 Inf 19 0 24 Inf Inf Inf Inf Inf Inf 25 26
27 Inf Inf Inf Inf Inf Inf;
1 Inf Inf 11 Inf Inf Inf 24 0 26 Inf Inf Inf Inf Inf Inf Inf
29 Inf Inf Inf Inf 31 Inf;
2 6 Inf 12 Inf Inf Inf Inf 26 0 Inf Inf Inf Inf Inf Inf Inf
Inf Inf Inf Inf Inf Inf Inf;
Inf Inf Inf Inf Inf 17 Inf Inf Inf Inf 0 33 34 Inf Inf Inf Inf
Inf Inf Inf Inf Inf Inf Inf;
Inf Inf Inf Inf Inf Inf Inf Inf Inf Inf 33 0 35 Inf Inf Inf Inf
Inf Inf Inf Inf Inf Inf Inf;
Page 55 of 59
2021
CS 6381: Advanced Programming Laboratory; Assignment-I
Inf Inf Inf Inf 16 Inf 20 Inf Inf Inf 34 35 0 36 Inf Inf Inf
Inf Inf Inf Inf Inf Inf Inf;
Inf Inf Inf Inf Inf Inf 21 Inf Inf Inf Inf Inf 36 0 37 Inf Inf
Inf Inf Inf Inf Inf Inf Inf;
Inf Inf Inf Inf Inf Inf 22 Inf Inf Inf Inf Inf Inf 37 0 38 Inf
Inf Inf Inf Inf Inf Inf Inf;
Inf Inf Inf Inf Inf Inf 23 25 Inf Inf Inf Inf Inf Inf 38 0 39
Inf Inf Inf Inf Inf Inf Inf;
Inf Inf Inf Inf Inf Inf Inf 26 Inf Inf Inf Inf Inf Inf Inf 39 0
40 Inf Inf Inf Inf Inf Inf;
Inf Inf Inf Inf Inf Inf Inf 27 29 Inf Inf Inf Inf Inf Inf Inf 40
0 41 Inf Inf Inf 43 Inf;
Inf Inf Inf Inf Inf Inf Inf Inf Inf Inf Inf Inf Inf Inf Inf Inf Inf
41 0 44 Inf Inf Inf Inf;
Inf Inf Inf Inf Inf Inf Inf Inf Inf Inf Inf Inf Inf Inf Inf Inf Inf
Inf 44 0 46 Inf Inf Inf;
Inf Inf Inf Inf Inf Inf Inf Inf Inf Inf Inf Inf Inf Inf Inf Inf Inf
Inf Inf 46 0 48 Inf Inf;
Inf Inf Inf Inf Inf Inf Inf Inf Inf Inf Inf Inf Inf Inf Inf Inf Inf
Inf Inf Inf 48 0 49 Inf;
Inf Inf Inf Inf Inf Inf Inf Inf 31 Inf Inf Inf Inf Inf Inf Inf Inf
43 Inf Inf Inf 49 0 50;
3 Inf Inf Inf Inf Inf Inf Inf Inf Inf Inf Inf Inf Inf Inf Inf Inf
Inf Inf Inf Inf Inf 50 0];
Page 56 of 59
2021
CS 6381: Advanced Programming Laboratory; Assignment-I
floydwarshall.m
function [dist] = floydwarshall(graph,N)
dist = graph;
for k = 1:N
for i = 1:N
for j = 1:N
dist(i,j) = min(dist(i,j), dist(i,k)+dist(k,j));
end
end
end
end
Results:
Page 57 of 59
2021
CS 6381: Advanced Programming Laboratory; Assignment-I
Comments:
We enter the given standard network graphs as adjacency matrices for the Floyd-
Warshall algorithm. We can see from the above output that the Floyd-Warshall
algorithm has successfully found the all-pairs shortest path weights for the given
Page 58 of 59
2021
CS 6381: Advanced Programming Laboratory; Assignment-I
graphs. We return the resulting weight matrices as matrices where the element in
ith row and jth column is the shortest distance between node i and j.
Page 59 of 59