algorithms lab manual
TRANSCRIPT
-
7/27/2019 Algorithms Lab Manual
1/36
1
ALGORITHMS LAB
MANUAL
(MC9229)
Info Institute of EngineeringKovilpalayam
Coimbatore 641107.
-
7/27/2019 Algorithms Lab Manual
2/36
2
Table of Contents
S.No. List of Experiments Page No.
1 ui!" #ort $
2 %inary #ear!& 11
$ %inary Tree Traversal 1'
4 (ars&all)s *lgorit&m 2$
+ ,i-"stra)s *lgorit&m 2'
6 rim)s *lgorit&m $1
7 Knapsa!" roblem ,ynami! rogramming $6
/ #ubset roblem %a!"tra!"ing 41
' Travelling #alesman roblem %ran!& an %oun
10 #trassen)s atri ultipli!ation
-
7/27/2019 Algorithms Lab Manual
3/36
$
Aim3 Implement t&e #orting using ui!" #&ort met&o.
Algorithm3
T&e steps are3
1. i!" an element !alle apivot from t&e list.2. 5eorer t&e list so t&at all elements it& values less t&an t&e pivot !ome before t&e pivot
&ile all elements it& values greater t&an t&e pivot !ome after it e8ual values !an go
eit&er ay9. *fter t&is partitioning t&e pivot is in its final position. T&is is !alle t&epartition operation.
$. 5e!ursively sort t&e sub:list of lesser elements an t&e sub:list of greater elements.
T&e base !ase of t&e re!ursion are lists of si;e ;ero or one &i!& never nee to be sorte.In
simple pseuo!oe t&e algorit&m mig&t be epresse as t&is3fun!tion 8ui!"sortarray9
var list less greater
if lengt&array9 < 1
return arraysele!t an remove a pivot valuepivot from array
for ea!& in arrayif < pivot t&en appen to less
else appen to greater
return !on!atenate8ui!"sortless9 pivot 8ui!"sortgreater99
Program3
=in!lue>stio.&>=in!lue>!onio.&>
int i-npivota?20@A
voi 8ui!"int a?@int leftint rig&t9Avoi sapint a?@int iint -9A
voi main9
Bint na?20@A
tet!olor1+9A
!lrs!r9A
printf>nnDICK #5T>9Aprintf>nnEnter t&e limit 3 >9A
s!anf>F>Gn9A
tet!olor49Atet!olor+9A
!lrs!r9A
printf>nnEnter t&e elementnn>9AforiH0Ai
s!anf>F>Ga?i@9A
8ui!"a0n:19A
tet!olor109A
-
7/27/2019 Algorithms Lab Manual
4/36
4
printf>nnT&e sorte list is nn>9A
foriH0Ai
printf>F >a?i@9Aget!&9A
voi 8ui!"int a?@int firstint last9B
iffirst
BpivotHa?first@A
iHfirstA
-HlastA
&ileiB
&ilea?i@JHpivotGGi
iA
&ilea?-@LHpivotGG-Lfirst9-::A
ifisapai-9A
sapafirst-9A8ui!"afirst-:19A
8ui!"a-1last9A
voi sapint a?@int iint -9
B
int tempAtempHa?i@A
a?i@Ha?-@A
a?-@HtempA
SAMPLE INPUT AND OUTPUT:
Enter t&e limit 3 +
Enter t&e elements+
4
$2
1
T&e sorteMlist is1 2 $ 4 +
-
7/27/2019 Algorithms Lab Manual
5/36
+
2. Aim3 To implement %inary #ear!&
Algorithm3BinarySearchA?0..n : 1@ key lo hi9
MM Input3 *n integer sorte arrayA?0..n 1@.
MM Output3 returns mid if "ey foun9 or :1 "ey not foun9if lo L hi
return :1 MM "ey not foun
mid N low high9M2
if key HA?mid@ return mid MM "ey foun
if key JA?mid@
returnBinarySearch A n key lo mid 19
else
returnBinarySearch A n key mid 1 hi9
enBinaySearch.
Program3
=in!lue>stio.&>=in!lue>!onio.&>
voi main9
Bint a?2+@i-tempsnlomi&ig&A
!lrs!r9A
printf>nEnter t&e Oimilt 3 >9A
s!anf>F>Gn9Aprintf>nnEnter t&e elementsn>9A
foriH0Ai
B
s!anf>F>Ga?i@9A
foriH0Ai
B
for-H0A-
Bifa?-@La?-1@9
B
tempHa?-@Aa?-@Ha?-1@A
a?-1@HtempA
printf>nn#orte list>9A
foriH0Ai
-
7/27/2019 Algorithms Lab Manual
6/36
6
B
printf>nF>a?i@9A
printf>nnEnter t&e elements to be sear!&e 3 >9A
s!anf>F>Gs9A
&ig&Hn:1AloH0A
&ileloJH&ig&9
BmiHlo&ig&9M2A
ifsLa?mi@9
loHmi1A
else ifs&ig&Hmi:1A
else ifsHHa?mi@9
B
printf>nnT&e element F is foun>s9Aget!&9A
eit09A
printf>nnT&e element F is not foun>s9Aget!&9A
SAMPLE INPUT AND OUTPUT:
Enter t&e elements+
4
$2
1
#orte list1
2
$
4+
Enter t&e element to be sear!&e 3 +
T&e element + is foun.
-
7/27/2019 Algorithms Lab Manual
7/36
7
$. Aim3
To rite a C:program to implement binary tree traversal.
Des!ription3
T&e most basi! traversal algorit&ms for general trees arepreordertraversal anpostorder
traversal is!usse in #e!tions 7.2.2 an 7.2.$ respe!tively of t&e Poori!& an Tamassia tet.Essentially a preorer traversal of a tree involves visiting a noe of t&e tree starting at its root
an t&en preorer traversing t&e subtrees roote at ea!& of its !&ilren. * postorer traversal on
t&e ot&er &an involves first postorer traversing t&e subtrees roote at ea!& of t&e !&ilren of anoe an t&en visiting t&e noe itself starting at t&e root. T&ese algorit&ms are given in
pseuo!oe belo. Qote t&at t&e !alls preorder(T,T.root())an postorder(T,T.root())
perform preorer an postorer traversals respe!tively of an entire tree T.
AlgorithmAlgorithmpreorder(T, v)
Input:A tree T and a node v of T.
Output:Depends on the action performed on a visit to a node.
visit node v
for each node w in T.children(v)
preorder(T,w) // recursivel traverse the su!tree rooted at the child w
of v
Algorithmpostorder(T, v)
Input:A tree T and a node v of T.
Output:Depends on the action performed on a visit to a node.
for each node w in T.children(v) postorder(T,w) // recursivel traverse the su!tree rooted at the child w
of v
visit node v
"oing:
=in!lueJstio.&L
=in!lueJ!onio.&L
=in!lueJstlib.&L
typeef stru!t treenoe
B
int ataA
-
7/27/2019 Algorithms Lab Manual
8/36
/
stru!t treenoe RleftA
stru!t treenoe Rrig&tA
tnoeA
tnoe RinsertioninttnoeR9A
voi preorertnoe R9A
voi inorertnoe R9A
voi postorertnoe R9A
voi main9
B
tnoe RTHQDOOA
int !&1nA
!&ar !&2A
o
B
!lrs!r9A
printf>nttRRRRperation (it& TreeRRRR>9A
printf>nt1.Insertion>9A
printf>nt2.Inorer Traversal>9A
printf>nt$.reorer Traversal>9A
printf>nt4.ostorer Traversal>9A
printf>ntEnter Sour C&oi!e 3>9A
s!anf>F>G!&19A
sit!&!&19
-
7/27/2019 Algorithms Lab Manual
9/36
'
B
!ase 13
printf>nn Enter t&e element to be inserte 3>9A
s!anf>F>Gn9A
THinsertionnT9A
printf>Q is F>n9A
brea"A
!ase 23
inorerT9A
brea"A
!ase $3
preorerT9A
brea"A
!ase 43
postorerT9A
brea"A
efault3
printf>nnInvali ption>9A
brea"A
printf>nn,o you ant to !ontinue yMn3>9A
s!anf>Fs>G!&29A
&ile!&2HHy9A
-
7/27/2019 Algorithms Lab Manual
10/36
10
get!&9A
tnoe Rinsertionint tnoe RT9
B
ifTHHQDOO9
B
THtnoe R9mallo!si;eoftnoe99A
ifTHHQDOO9
printf>nout of spa!e>9A
else
B
T:LataHA
T:LleftHT:Lrig&tHQDOOA
else
B
ifJT:Lata99
T:LleftHinsertionT:Lleft9A
else
B
ifLT:Lata9
T:Lrig&tHinsertionT:Lrig&t9A
-
7/27/2019 Algorithms Lab Manual
11/36
11
return TA
voi preorertnoe RT9
B
ifTUHQDOO9
B
printf>tF>T:Lata9A
preorerT:Lleft9A
preorerT:Lrig&t9A
voi postorertnoe RT9
B
ifTUHQDOO9
B
postorerT:Lleft9A
postorerT:Lrig&t9A
printf>tF>T:Lata9A
voi inorertnoe RT9
-
7/27/2019 Algorithms Lab Manual
12/36
12
B
ifTUHQDOO9
B
inorerT:Lleft9A
printf>tF>T:Lata9A
inorerT:Lrig&t9A
OUTPUT:
RRRRperation (it& TreeRRRR
1.Insertion
2.Inorer Traversal
$.reorer Traversal
4.ostorer Traversal
Enter Sour C&oi!e 31
Enter t&e element to be inserte 324
Q is 24
,o you ant to !ontinue yMn3y
-
7/27/2019 Algorithms Lab Manual
13/36
1$
RRRRperation (it& TreeRRRR
1.Insertion
2.Inorer Traversal
$.reorer Traversal
4.ostorer Traversal
Enter Sour C&oi!e 31
Enter t&e element to be inserte 3$4
Q is $4
,o you ant to !ontinue yMn3y
RRRRperation (it& TreeRRRR
1.Insertion
2.Inorer Traversal
$.reorer Traversal
4.ostorer Traversal
Enter Sour C&oi!e 31
Enter t&e element to be inserte 340
Q is 40
,o you ant to !ontinue yMn3y
RRRRperation (it& TreeRRRR
1.Insertion
-
7/27/2019 Algorithms Lab Manual
14/36
14
2.Inorer Traversal
$.reorer Traversal
4.ostorer Traversal
Enter Sour C&oi!e 32
24 $4 40
,o you ant to !ontinue yMn3y
RRRRperation (it& TreeRRRR
1.Insertion
2.Inorer Traversal
$.reorer Traversal
4.ostorer Traversal
Enter Sour C&oi!e 3$
24 $4 40
,o you ant to !ontinue yMn3y
RRRRperation (it& TreeRRRR
1.Insertion
2.Inorer Traversal
$.reorer Traversal
4.ostorer Traversal
Enter Sour C&oi!e 34
40 $4 24
,o you ant to !ontinue yMn3n
-
7/27/2019 Algorithms Lab Manual
15/36
1+
4. Aim3
To implement (ars&all)s algorit&m
Des!ription3
T&e input to t&is algorit&m is an a-a!en!y matri &i!& &as 1s for any ro:!olumn pair
if t&ere eists an ege beteen t&em. (&at ell be oing &ere is :: !&e!"ing out for everyverte t&e in!oming an outgoing lin"s. If t&ere eist bot& in!oming an outgoing lin"s for a
parti!ular verte e ill put a 1 for t&e ro:!olumn pair forme by t&em in t&e a-a!en!y
matri.
* % C , E
* 0 0 0 1 0 *,9
% 0 0 0 1 0 %,9
C 0 1 0 0 0 C%9
, 0 0 0 0 1 ,E9E 0 0 1 0 0 EC9
,ire!te Prap& *-a!en!y atri
Oets ta"e t&e verte E into !onsieration. Sou &ave an ege !oming from , an you &ave anege going to CA 8uite visible from t&e matri. #o as I sai ell put a 1 for t&e element ,C in
t&e matri. #imilarly for t&e verte C ell put 1 for t&e element E%. (ell o t&is for all t&e
verte in t&e grap& an &at you get at t&e en is t&e Transitive Closure of t&is grap&. T&is ay
of fining t&e transitive !losure as erive by (ars&all an t&us t&e name (ars&allsalgorit&m.
Algorithm:
#tep:13 Copy t&e *-a!en!y matri into anot&er matri !alle t&e at& matri
#tep:23 Vin in t&e at& matri for every element in t&e Prap& t&e in!oming an outgoing eges#tep:$3 Vor every su!& pair of in!oming an outgoing eges put a 1 in t&e at& matri
"oing:
=in!lueJstio.&L=in!lueJ!onio.&L
int a?10@?10@A
voi main9B
int i-"nA
!lrs!r9A
printf>enter t&e no of verti!es n>9As!anf>F>Gn9A
printf>enter t&e a-a!en!y matri n>9A
foriH1AiJHnAi9
http://datastructures.itgo.com/graphs/adjmat.htmhttp://datastructures.itgo.com/graphs/adjmat.htm -
7/27/2019 Algorithms Lab Manual
16/36
16
for-H1A-JH1A-9
s!anf>F>Ga?i@?-@9A
for"H1A"JHnA"9foriH1AiJHnAi9
for-H1A-JHnA-9
a?i@?-@Ha?i@?-@WWa?i@?"@GGa?"@?i@Aprintf>n t t&e transitive !losure is n>9A
foriH1AiJHnAi9
B for-H1A-JHnA-9
printf>t F>a?i@?-@9A
printf>n>9A
get!&9A
DTDT3
enter t&e no of verti!es4
enter t&e a-a!en!y matri0 1 0 0
0 0 1 0
1 0 0 10 0 0 0
t&e transitive !losure is
0 1 0 0 0 0 1 0
1 0 0 1
0 0 0 0
+. Aim3
-
7/27/2019 Algorithms Lab Manual
17/36
17
To rite a program to implement ,i-"stras algorit&m
Des!ription3
Oet P XE9 be a grap& X:Xerti!es G E:Eges .(e &ave to !&oose one verte as sour!e
t&e problem aim is to fin out minimum istan!e beteen sour!e noe an all remaining noes.
T&is problem is t&e !ase of orere paraigm in Preey met&o.
T&is problem !an be implemente by an algorit&m !alle ,i-"astra)s algorit&m.
Algorithm3
MMP be a grap&
MMCost matri ?13n13n@ for t&e grap& P
-
7/27/2019 Algorithms Lab Manual
18/36
1/
MM#HBset of verti!es t&at pat& alreay generate
MMOet X be sour!e verte
MMist?-@A1JH-JHn enotes istan!e beteen X an -
voi main9
Bfor i3H1 to n o
Bs?i@HfalseA MM initiali;e s it& n
ist?i@H!ost?vi@A MMefine istan!e
s?v@HtrueA MMput v in s
[email protected] MM,istan!e beteen v an v is 0
for num3H2 to n:1 o
B
pat&s from v MM!&oose u from among t&ose verti!es not in # su!& t&at ist?u@HminA
s?u@HtrueA
forea!& a-as!ent to u it& s?@Hfalse9
ifist?@List?u@!ost?u@9
t&enist?@Hist?u@!ost?u@A MMupate t&e istan!e
Sour!e "oe3
=in!lueJstio.&L
=in!lueJ!onio.&L
=efine infinity $2767
int !ost?20@?20@nist?20@s?20@a?20@?20@Avoi setata9Avoi getata9A
voi pat&int9A
voi setata9
B
int i-"Aprintf>nEnter number of noes3 >9A
s!anf>F>Gn9A
printf>Enter *-a!en!y atri3 >9A
foriH1AiJHnAi9for-H1A-JHnA-9
s!anf>F>Ga?i@?-@9A
foriH1AiJHnAi9for-H1A-JHnA-9
B
ifiHH-9!ost?i@?i@H0A
-
7/27/2019 Algorithms Lab Manual
19/36
1'
else if a?i@?-@UH09
B
printf>nEnter !ost from F to F3 >i-9As!anf>F>G!ost?i@?-@9A
else !ost?i@?-@HinfinityA
voi getata9
B
int iAforiH1AiJHnAi9
ifist?i@HH$27679
printf>not rea!&able>9A
else printf> F>ist?i@9A
voi pat&int v9
Bint i-minuA
foriH1AiJHnAi9
B
s?i@H0Aist?i@H!ost?v@?i@A
s?v@H1Aist?v@H0A
foriH2AiJHnAi9
BminH$2767A
for-H1A-JHnA-9
ifs?-@HH0 GG ist?-@Jmin9
uH-As?u@H1A
for-H1A-JHnA-9
ifs?-@HH0 GG a?u@?-@HH19ifist?-@List?u@!ost?u@?-@9
ist?-@Hist?u@!ost?u@?-@A
-
7/27/2019 Algorithms Lab Manual
20/36
20
voi main9
B
int vA!lrs!r9A
setata9A
printf>nEnter t&e sour!e verte3 >9As!anf>F>Gv9A
pat&v9A
printf>n#&ortest pat&s > 9Agetata9A
get!&9A
Output3
Enter number of noes3 6
Enter *-a!en!y atri3 0 1 1 1 0 0
0 0 1 1 0 00 0 0 0 1 0
1 0 0 0 1 00 1 1 0 0 0
0 0 0 0 1 0
Enter !ost from 1 to 23 +0
Enter !ost from 1 to $3 4+
Enter !ost from 1 to 43 10
Enter !ost from 2 to $3 10
Enter !ost from 2 to 43 1+
Enter !ost from $ to +3 $0
Enter !ost from 4 to 13 20
Enter !ost from 4 to +3 1+
Enter !ost from + to 23 20
Enter !ost from + to $3 $+
Enter !ost from 6 to +3 $
Enter t&e sour!e verte3 1
#&ortest pat&s 0 4+ 4+ 10 2+not rea!&able
-
7/27/2019 Algorithms Lab Manual
21/36
21
#esult3 Yen!e ,i-"stras algorit&m is implemente
6. Aim3 To Find the minimum cost spanning tree of a given undirectedgraph using Prim's algorithm.
Algorithm:
PrimG9
MM Input3 ,ire!te eig&te grap& G
MM Output3 inimum #panning Tree TT
U N B1 MM !onsier verte 1 as starting verte
$hile U Z V olet u v9 be t&e lo$est !ost ege su!& t&at
u U an v V : UTN T B u v9 MM a ege to spanning treeUN U B v
return T
enPrim.
Program3
=in!lueJstio.&L
=in!lueJ!onio.&L
intn !ost?10@?10@A
%oiprim9B
inti-"lnr?10@tempmin[!ostH0tree?10@?$@A
MR Vor first smallest ege RM
tempH!ost?0@?0@AforiH0AiJ nAi9
B
for-H0A-J nA-9
B iftempL!ost?i@?-@9
B tempH!ost?i@?-@A
"HiA
lH-A
-
7/27/2019 Algorithms Lab Manual
22/36
22
MR Qo e &ave fist smallest ege in grap& RM
tree?0@?0@H"Atree?0@?1@HlA
tree?0@?2@HtempA
min[!ostHtempA
MR Qo e &ave to fin min is of ea!&
verte from eit&er " or lby initialising nr?@ array
RM
foriH0AiJ nAi9 B
if!ost?i@?"@J !ost?i@?l@9
nr?i@H"A
else nr?i@HlA
MR To ini!ate visite verte initialise nr?@ for t&em to 100 RM
nr?"@H100A
nr?l@H100AMR Qo fin out remaining n:2 eges RM
tempH''A
foriH1AiJ n:1Ai9
B for-H0A-J nA-9
B
ifnr?-@UH100 GG !ost?-@?nr?-@@ J temp9 B
tempH!ost?-@?nr?-@@A
H-A
MR Qo i &ave got net verte RM
tree?i@?0@HAtree?i@?1@Hnr?@A
tree?i@?2@H!ost?@?nr?@@A
min[!ostHmin[!ost!ost?@?nr?@@Anr?@H100A
MR Qo fin if is nearest to any vertet&an its previous near value RM
for-H0A-J nA-9
B
-
7/27/2019 Algorithms Lab Manual
23/36
2$
ifnr?-@UH100 GG !ost?-@?nr?-@@ L !ost?-@?@9
nr?-@HA
tempH''A
MR Qo i &ave t&e anser -ust going to print it RMprintf>n T&e min spanning tree is3: >9A
foriH0AiJ n:1Ai9
B for-H0A-J $A-9
printf>F> tree?i@?-@9A
printf>n>9A
printf>n in !ost3: F> min[!ost9A
%oimain9
Binti-A
!lrs!r9A
printf>n Enter t&e no. of verti!es3: >9A
s!anf>F> Gn9A
printf >n Enter t&e !osts of eges in matri form3: >9AforiH0AiJ nAi9
for-H0A-J nA-9
s!anf>F>G!ost?i@?-@9A
printf>n T&e matri is3: >9A
foriH0AiJ nAi9 B
for-H0A-J nA-9
printf>Ft>!ost?i@?-@9A
printf>n>9A
prim9A
get!&9A
Output :
Enter t&e no. of verti!es3: $
Enter t&e !osts of eges in matri form3:
'' 2 $
-
7/27/2019 Algorithms Lab Manual
24/36
24
2 '' +
$ + ''
T&e matri is3:
'' 2 $
2 '' +$ + ''
T&e min spanning tree is3:0 1 2
2 0 $
in !ost3: +
7. Aim3 To implement Knapsa!" problem using ,ynami! programming.
Algorithm3
Step &. In t&is step t&e problem is ivie into t&e sub:problems means t&at t&e !&ara!teri;e t&e
stru!ture of an optimal solution. Ea!& sub:problem is solve an "eep tra!" ea!& items value.
Step '.In t&is step t&e re!ursive solution is fin. In t&is step optimal solution is step fist is
efine re!ursively.
Step (.In t&is step !ompute t&e !ost of optimal solution. In "napsa!" problem !ost is maimum
benefits.to !al!ulate t&e !ost is using t&e algorit&m &i!& is !alle 0M1 Knapsa!" *lgorit&m.
Step ). In t&is step t&e fin out t&e optimal solution. Vor fining t&e optimal solution e use t&e
ba!"tra!"ing.
Program3
#include#includeint n,p[20,![20"int ma$int a,int %&return a>%(a:%")int *napsac*$int i,int rc&
if$i++n&return$![i
-
7/27/2019 Algorithms Lab Manual
25/36
2+
for$i+-"i
-
7/27/2019 Algorithms Lab Manual
26/36
26
if s r S?k@ d9 an s S?k 1@ d9
"?k@ N 0
SumoSu!s k 1 r S?k@9
en SumoSu!.#include#include
void main(){int s[20],d,sum=0,n,x[20],top=0,i,tot=0;clsc();pint!("n$nte the num%e o! values ");scan!("&d",'n);pint!("n$nte the values in ascendin ode ");!o(i=0;i=0){x[top]=x[top];sum=sums[x[top]];i!(sum==d){pint!("n");tot=tot;!o(i=0;id//top>=n){sum=sum+s[x[top]];i!(top>=){sum=sum+s[x[top+]];*top=top+;*else{top=top;x[top]=x[top+];
**i!(tot==0){pint!("not possi%le ");*
etch();*
-
7/27/2019 Algorithms Lab Manual
27/36
27
AIM
To solve traveling salesman problem using ynami! programming.
AL*O#IT+M
1. ,e!lare a !lass ynami! it& ata members !ost matri number of !ities istan!e
array.
2. ,e!lare a private member fun!tion to fin out minimum of an array an publi!member fun!tions to getata9 isplay9 sap9 fa!t9 permutations an solutions.
$. getata9o 5ea member of !ities as n.
o Vor value of i from 1 to n by 1 repeat. Vor values of - from 1 to n by 1 repeat.
5ea !?i@?-@
o Vor values of I varying from 0 to fa!tn:19 by 1.
Oet ?i@H0
4. sapab9
o Oet tHa.
o aHb
o bHt
+. min9
o Oet mH?0@o Vor values of i to fa!tn:19 by 1.
C&e!" if mL?i@9 if so
o 5eturn m
6. sol9o Vor values of I from 0 to n:2 by 1 repeat.
Oet b?i@Hi2
o Call permutation fun!tion it& bon:2 as parameters.
7. fa!t9
o Oet fH1.
o Vor values of I from 1 to m by 1 repeat. fHfRi
o 5eturn f
/. isplay9o Vor values of I from 0 to fa!tn:19 by 1 repeat
rint ?i@
o rint min9
-
7/27/2019 Algorithms Lab Manual
28/36
2/
o Dsing a &ile loop -Jfa!tn:19 print t&e pat& also.
'. permlist?@ " m9
o C&e!" if "Hm if so K1H1
Vor values of I from 0 to m by 1 repeat.
at&?i@?i@Hlist?i@
?i@H !?"1@?list?i@@
"1Hlist?i@
?i@H?i@!?"1@?1@
let iHi1
o else
for values of i from " to m by 1 repeat.
Call saplist?"@ list?i@9
Call permlist "1 m9
Call saplist?"@ list?i@9
P#O*#AM
MMTravelling #alesman problem
=in!lue Jiostream.&L
=in!lue J!onio.&L
!lass ynami!Bprivate3
int !?+@?+@n?24@p?24@?6@list?+@rA
publi!3ynami!9A
voi getata9A
voi isplay9Aint fa!tint num9A
int minint list?@9A
voi permint list?@ int " int m9A
voi sol9AA
ynami!33ynami!9
BrH0A
-
7/27/2019 Algorithms Lab Manual
29/36
2'
voi ynami!33getata9B
!outJJ>Enter no. of !ities3>A
!inLLnA!outJJenlA
for int iH0AiJnAi9
for int -H0A-JnA-9!?i@?-@H0A
for iH0AiJnAi9
B
for -H0A-JnA-9B
if iUH-9
B
if !?i@?-@HH09B
!outJJ>Enter !ost from >JJiJJ> to >JJ-JJ> 3>A!inLL!?i@?-@A
!?-@?i@H!?i@?-@A
for iH0AiJn:1Ai9list?i@Hi1A
int ynami!33fa!tint num9
B
int fH1Aif numUH09
for int iH1AiJHnumAi9
fHfRiA
return fA
voi ynami!33permint list?@ int " int m9B
int itempA
if "HHm9B
for iH0AiJHmAi9
B
p?r@?i1@Hlist?i@A
-
7/27/2019 Algorithms Lab Manual
30/36
$0
rA
else
for iH"AiJHmAi9
B tempHlist?"@A list?"@Hlist?i@A list?i@HtempA
permlist"1m9A
tempHlist?"@A list?"@Hlist?i@A list?i@HtempA
voi ynami!33sol9B
permlist0n:29A
for int iH0AiJfa!tn:19Ai9B
p?i@?0@H0Ap?i@?n@H0A
for iH0AiJfa!tn:19Ai9B
?i@H0A
for int -H0A-JnA-9
B?i@H?i@!?p?i@?-@@?p?i@?-1@@A
int ynami!33minint list?@9B
int minimumHlist?0@A
for int iH0AiJfa!tn:19Ai9
Bif list?i@Jminimum9
minimumHlist?i@A
return minimumA
voi ynami!33isplay9
-
7/27/2019 Algorithms Lab Manual
31/36
$1
B
int i-A
!outJJenlJJ>T&e !ost atri3>JJenlAfor iH0AiJnAi9
B
for -H0A-JnA-9!outJJ!?i@?-@JJ>t>A
!outJJenlA
!outJJenlJJ>T&e ossible pat&s an t&eir !orresponing !ost3>JJenlA
for iH0AiJfa!tn:19Ai9
B
for -H0A-Jn1A-9!outJJp?i@?-@JJ>t>A
!outJJ>::L >JJ?i@JJenlA
!outJJenlJJ>T&e s&ortest pat& 3>JJenlAfor iH0AiJfa!tn:19Ai9
Bif ?i@HHmin99
brea"A
for -H0A-JHnA-9
B
!outJJp?i@?-@JJ> >A
!outJJenlJJ>nT&e !ost of t&is pat& is >JJ?i@JJenlA
voi main9
B
!lrs!r9Aynami! tsA
ts.getata9A
ts.sol9A
ts.isplay9Aget!&9A
-
7/27/2019 Algorithms Lab Manual
32/36
$2
OUTPUT
Enter no. of !ities34
Enter !ost from 0 to 1 370
Enter !ost from 0 to 2 3$0Enter !ost from 0 to $ 37+
Enter !ost from 1 to 2 3/0
Enter !ost from 1 to $ 320Enter !ost from 2 to $ 3'0
T&e !ost atri3
0 70 $0 7+
70 0 /0 20$0 /0 0 '0
7+ 20 '0 0
T&e ossible pat&s an t&eir !orresponing !ost3
0 1 2 $ 0 ::L $1+0 1 $ 2 0 ::L 210
0 2 1 $ 0 ::L 20+
0 2 $ 1 0 ::L 210
0 $ 2 1 0 ::L $1+0 $ 1 2 0 ::L 20+
T&e s&ortest pat& 30 2 1 $ 0
T&e !ost of t&is pat& is 20+
-
7/27/2019 Algorithms Lab Manual
33/36
$$
10. Aim3 To implement #trassen)s atri ultipli!ation.
Algorithm31. Consider multiplying two 2x2 matrices, as follows:
@ A 6 F + @AB @FAC
D E B C DEB DFEC
The obvious way to compute the right side is just to do the multiplies and ! additions. "ut
imagine multiplies are a lot more expensive than additions, so we want to reduce the number of
multiplications if at all possible. #trassen uses a tric$ to compute the right hand side with one
less multiply and a lot more additions %and some subtractions&.
2. 'ere are the ( multiplies:
4- + $@ E& 6 $ C& + @ @C E EC
42 + $@ A& 6 C + @C AC
4; + $D E& 6 + D E
4 + @ 6 $F C& + @F @C
4 + E 6 $B & + EB E
4 + $D @& 6 $ F& + D DF @ @F
4= + $A E& 6 $B C& + AB AC EB EC
). #o to compute *+"-, start with 1( %which gets us the *+ and "- terms&, then add/subtract
some of the other s until *+"- is all we are left with. iraculously, the 0s are chosen so that
1(2 wor$s. #ame with the other ) results re3uired.
!. 4ow just reali5e this wor$s not just for 2x2 matrices, but for any %even& si5ed matrices where the
*..' are submatrices.
Program3
-
7/27/2019 Algorithms Lab Manual
34/36
$4
=in!lueJstio.&L
int main9B
int a?2@?2@b?2@?2@!?2@?2@i-Aint m1m2m$m4m+m6m7A
printf>Enter t&e 4 elements of first matri3 >9AforiH0AiJ2Ai9
for-H0A-J2A-9
s!anf>F>Ga?i@?-@9A
printf>Enter t&e 4 elements of se!on matri3 >9A
foriH0AiJ2Ai9
for-H0A-J2A-9s!anf>F>Gb?i@?-@9A
printf>nT&e first matri isn>9A
foriH0AiJ2Ai9Bprintf>n>9A
for-H0A-J2A-9printf>Ft>a?i@?-@9A
printf>nT&e se!on matri isn>9A
foriH0AiJ2Ai9B
printf>n>9A
for-H0A-J2A-9printf>Ft>b?i@?-@9A
m1H a?0@?0@ a?1@?1@9Rb?0@?0@b?1@?1@9A
m2H a?1@?0@a?1@?1@9Rb?0@?0@A
m$H a?0@?0@Rb?0@?1@:b?1@?1@9Am4H a?1@?1@Rb?1@?0@:b?0@?0@9A
m+H a?0@?0@a?0@?1@9Rb?1@?1@A
m6H a?1@?0@:a?0@?0@9Rb?0@?0@b?0@?1@9A
m7H a?0@?1@:a?1@?1@9Rb?1@?0@b?1@?1@9A
!?0@?0@Hm1m4:m+m7A
!?0@?1@Hm$m+A!?1@?0@Hm2m4A
!?1@?1@Hm1:m2m$m6A
printf>n*fter multipli!ation using n>9A
foriH0AiJ2Ai9B
printf>n>9A
for-H0A-J2A-9
-
7/27/2019 Algorithms Lab Manual
35/36
$+
printf>Ft>!?i@?-@9A
return 0A
#ample output3
Enter t&e 4 elements of first matri3 1
2$
4
Enter t&e 4 elements of se!on matri3 +
67
/
T&e first matri is
1 2$ 4
T&e se!on matri is
+ 6
7 /
*fter multipli!ation using
1' 22
4$ +0
-
7/27/2019 Algorithms Lab Manual
36/36
$6