8/21/2019 Data StructureLab 3-12-13(Final) 1
1/70
Ex.No:1Date: 14/09/13
Implementation of Graph Search Algorithm (BFS)
Aim
To Implement Breadth-First ear!h "BF# $l%orithm &sin% 'a(a.
Algorithm
tep 1: )&sh the root node in the *&e&e.
tep +: ,oop &ntil the &e&e is empt.
tep 3:emo(e the node rom the *&e&e.
tep 4: I the remo(ed node has &n(isited !hild nodes mar2 them as (isited and insert the
&n(isited !hildren in the &e&e.
Program
import 'a(a.io.DataInp&ttream
import 'a(a.io.IEx!eptionp&5li! !lass BF 6
p&5li! stati! (oid main"trin% ar%s78#thros IEx!eption6trin% !h;;
DataInp&ttream inpne DataInp&ttream"stem.in#
stem.o&t.println";Enter the limit o (ertexes;#int maxInte%er.parseInt"inp.read,ine"##
ertex;#stem.o&t.println";+.$dd Ed%e;#
stem.o&t.println";3.BF;#
stem.o&t.println";Enter o&r !hoi!e ;#int nInte%er.parseInt"inp.read,ine"##
sit!h"n#
6!ase 1: hile"!h.e&als";;##
1
8/21/2019 Data StructureLab 3-12-13(Final) 1
2/70
6
stem.o&t.println";Enter the (ertex;#
trin% sinp.read,ine"#!har as.!har$t"0#
%.add>ertex"a#
stem.o&t.print";Enter to !ontin&e insertin%;#!hinp.read,ine"#
?
5rea2!ase +:
hile"!h.e&als";;##
6
stem.o&t.println";Enter the ed%e;#trin% sinp.read,ine"#
%.addEd%e"s.!har$t"0#s.!har$t"1##
stem.o&t.print";Enter to !ontin&e insertin%;#
!hinp.read,ine"#?
5rea2!ase 3:
stem.o&t.println";BF;#
stem.o&t.println";-------------;#%.5s"#
stem.o&t.println";;#
5rea2
dea<:stem.o&t.println";In(alid @hoi!e;#?
stem.o&t.println";;#
stem.o&t.print";Enter to !ontin&e ;#!hinp.read,ine"#
?
?
?
!lass *&e&e 6
pri(ate int siAe pri(ate int78 &e$rra
pri(ate int ront
pri(ate int rear p&5li! *&e&e"int s# 6
siAe s
&e$rra ne int7siAe8
ront 0
2
8/21/2019 Data StructureLab 3-12-13(Final) 1
3/70
rear -1
?
p&5li! (oid insert"int '# 6
i "rear siAe - 1# 6
rear -1 ?
&e$rra7rear8 '
?
p&5li! int remo(e"# 6
int temp &e$rra7ront8
i "ront siAe# 6 ront 0
?
ret&rn temp
?
p&5li! 5oolean isEmpt"# 6 ret&rn "rear 1 ront CC "ront siAe - 1 rear##
?
?
!lass >ertex 6
p&5li! !har la5el
p&5li! 5oolean as>isited
p&5li! >ertex"!har la5# 6
la5el la5
as>isited alse ?
?
!lass ertex
pri(ate >ertex (ertex,ist78 pri(ate int ad'=at7878
pri(ate int n>ert
pri(ate *&e&e the*&e&e
p&5li!
8/21/2019 Data StructureLab 3-12-13(Final) 1
4/70
(ertex,ist ne >ertex7max>ertex8
ad'=at ne int7max>ertex87max>ertex8
n>ert 0 or "int ' 0 ' max>ertex '# 6
or "int 2 0 2 max>ertex 2# 6
ad'=at7'8728 0 ?
?
the*&e&e ne *&e&e"s#
?
p&5li! (oid add>ertex"!har la5# 6 i "n>ert max>ertex# 6
stem.o&t.println";>ertex limit ex!eeded ;#
? else 6
(ertex,ist7n>ert8 ne >ertex"la5# ?
?
p&5li! (oid addEd%e"!har start !har end# 6
int i ' or "' 0 ' n>ert '# 6
i "(ertex,ist7'8.la5el start# 6
5rea2
? ?
or "i 0 i n>ert i# 6
i "(ertex,ist7i8.la5el end# 6 5rea2
?
? i "i n>ert CC ' n>ert# 6
stem.o&t.println";>ertex not o&nd;#
? else 6
ad'=at7'87i8 1 ad'=at7i87'8 1
?
?
p&5li! (oid displa>ertex"int (# 6 stem.o&t.print"(ertex,ist7(8.la5el#
?
4
8/21/2019 Data StructureLab 3-12-13(Final) 1
5/70
p&5li! (oid 5s"# 6
(ertex,ist708.as>isited tr&e displa>ertex"0#
the*&e&e.insert"0#
int (+ hile "the*&e&e.isEmpt"##
6
int (1 the*&e&e.remo(e"# hile ""(+ %et$d'n(isited>ertex"(1## -1#
6
(ertex,ist7(+8.as>isited tr&e
displa>ertex"(+# the*&e&e.insert"(+#
?
?
or "int ' 0 ' n>ert '# 6
(ertex,ist7'8.as>isited alse ?
?
pri(ate int %et$d'n(isited>ertex"int (#
6
or "int ' 0 ' n>ert '# 6
i "ad'=at7(87'8 1 GG (ertex,ist7'8.as>isited alse# 6 ret&rn '
?
?
ret&rn -1
?
?
Output
5
8/21/2019 Data StructureLab 3-12-13(Final) 1
6/70
Result
Th&s the Breadth-First ear!h"BF# $l%orithm &sin% 'a(a as implemented and the
o&tp&t as (eriied s&!!ess&ll.Ex.No:+Date: 1H/09/13
6
8/21/2019 Data StructureLab 3-12-13(Final) 1
7/70
Implementation of Graph Search Algorithm (DFS)
Aim
To Implement Depth-First ear!h "DF# $l%orithm &sin% 'a(a.
Algorithm
tep 1: )&sh the root node in the ta!2.
tep +: ,oop &ntil sta!2 is empt.
tep 3: )ee2 the node o the sta!2.
tep 4: I the node has &n(isited !hild nodes %et the &n(isited !hild node mar2 it astra(ersed and p&sh it on sta!2.
tep : I the node does not ha(e an &n(isited !hild nodes pop the node rom the sta!2.
Program
import 'a(a.io.DataInp&ttreamimport 'a(a.io.IEx!eption
p&5li! !lass DF 6
p&5li! stati! (oid main"trin% ar%s78# thros IEx!eption 6 trin% !h ;;
DataInp&ttream inp ne DataInp&ttream"stem.in# stem.o&t.println";Enter the limit o (ertexes;#
int max Inte%er.parseInt"inp.read,ine"##
ertex;# stem.o&t.println";+.$dd Ed%e;#
stem.o&t.println";3.DF;# stem.o&t.println";Enter o&r !hoi!e ;# int n Inte%er.parseInt"inp.read,ine"##
sit!h "n# 6
!ase 1: hile "!h.e&als";;## 6
stem.o&t.println";Enter the (ertex;#
trin% s inp.read,ine"#
7
8/21/2019 Data StructureLab 3-12-13(Final) 1
8/70
!har a s.!har$t"0#
%.add>ertex"a#
stem.o&t.print";Enter to !ontin&e insertin%;# !h inp.read,ine"#
?
5rea2 !ase +:
hile "!h.e&als";;## 6
stem.o&t.println";Enter the ed%e;# trin% s inp.read,ine"#
%.addEd%e"s.!har$t"0# s.!har$t"1##
stem.o&t.print";Enter to !ontin&e insertin%;#
!h inp.read,ine"# ?
5rea2
!ase 3:
stem.o&t.println";DF;# stem.o&t.println";-------------;#
%.ds"# stem.o&t.println";;#
5rea2
dea<:
stem.o&t.println";In(alid @hoi!e;#
?
stem.o&t.println";;# stem.o&t.print";Enter to !ontin&e ;#
!h inp.read,ine"#
? ?
?
!lass ta!2 6
pri(ate int siAe
pri(ate int78 st pri(ate int top
p&5li! ta!2"int s# 6 siAe s
st ne int7siAe8
top -1 ?
p&5li! (oid p&sh"int 2e# 6
st7top8 2e
8
8/21/2019 Data StructureLab 3-12-13(Final) 1
9/70
?
p&5li! int pop"# 6 ret&rn "st7top--8#
?
p&5li! int pee2"# 6
ret&rn "st7top8#
?
p&5li! 5oolean isEmpt"# 6
ret&rn "top -1#
??
!lass >ertex 6
p&5li! !har la5el p&5li! 5oolean as>isited
p&5li! >ertex"!har la5# 6 la5el la5
as>isited alse
?
?
!lass ertex
pri(ate >ertex (ertex,ist78
pri(ate int ad'=at7878 pri(ate int n>ert
pri(ate ta!2 theta!2
p&5li! ertex '# 6
or "int 2 0 2 max>ertex 2# 6
ad'=at7'8728 0
?
9
8/21/2019 Data StructureLab 3-12-13(Final) 1
10/70
?
theta!2 ne ta!2"s#
?
p&5li! (oid add>ertex"!har la5# 6 i "n>ert max>ertex# 6
stem.o&t.println";>ertex limit ex!eeded ;#
? else 6 (ertex,ist7n>ert8 ne >ertex"la5#
?
?
p&5li! (oid addEd%e"!har start !har end# 6
int i '
or "' 0 ' n>ert '# 6
i "(ertex,ist7'8.la5el start# 6 5rea2
? ?
or "i 0 i n>ert i# 6
i "(ertex,ist7i8.la5el end# 6 5rea2
?
?
i "i n>ert CC ' n>ert# 6 stem.o&t.println";>ertex not o&nd;#
? else 6
ad'=at7'87i8 1 ad'=at7i87'8 1
?
?
p&5li! (oid displa>ertex"int (# 6
stem.o&t.print"(ertex,ist7(8.la5el#
?
p&5li! (oid ds"# 6
(ertex,ist708.as>isited tr&e displa>ertex"0#
theta!2.p&sh"0#
hile "theta!2.isEmpt"## 6 int ( %et$d'n(isited>ertex"theta!2.pee2"##
i "( -1# 6
theta!2.pop"#
? else 6
10
8/21/2019 Data StructureLab 3-12-13(Final) 1
11/70
(ertex,ist7(8.as>isited tr&e
displa>ertex"(#
theta!2.p&sh"(# ?
?
or "int ' 0 ' n>ert '# 6 (ertex,ist7'8.as>isited alse
?
? pri(ate int %et$d'n(isited>ertex"int (# 6
or "int ' 0 ' n>ert '# 6
i "ad'=at7(87'8 1 GG (ertex,ist7'8.as>isited alse# 6
ret&rn ' ?
?
ret&rn -1
??
Output
11
8/21/2019 Data StructureLab 3-12-13(Final) 1
12/70
12
8/21/2019 Data StructureLab 3-12-13(Final) 1
13/70
Result
Th&s the Depth-First ear!h "DF# $l%orithm &sin% 'a(a as implemented and theo&tp&t as (eriied s&!!ess&ll.Ex.No:3
13
8/21/2019 Data StructureLab 3-12-13(Final) 1
14/70
Date: +3/09/13
Implementation of Netor!"Flo
Aim
To Implement Netor2 Flo ptimiAaton )ro5lem &sin% 'a(a.
Algorithm
tep 1: tart the pro%ram.
tep +:Deine a !lass Netor2Flo.
tep 3: Enter the nodes o the %raph so&r!e node and sin2 node.
tep 4: Netor2Flo &n!tion !omp&tes the resid&al %raph.
tep : The Netor2Flo &n!tion ret&rns the (al&e o maxlo.
tep H: The mimim&m !&t is !omp&ted and printed &sin% the &n!tion print!&tset"#.
tep J: top the pro%ram.
Program
import 'a(a.&til.$rra,istimport 'a(a.&til.Kashet
import 'a(a.&til.Iterator
import 'a(a.&til.,in2ed,ist
import 'a(a.&til.*&e&e
import 'a(a.&til.!anner
import 'a(a.&til.et
p&5li! !lass netor2lo
6
pri(ate int78 parent
pri(ate *&e&eInte%erL &e&e
pri(ate int n&m5er>erti!es
pri(ate 5oolean78 (isited
pri(ate et)airL !&tet
pri(ate $rra,istInte%erL rea!ha5le
pri(ate $rra,istInte%erL &nrea!ha5le
14
8/21/2019 Data StructureLab 3-12-13(Final) 1
15/70
p&5li! netor2lo "int n&m5er>erti!es#
6
this.n&m5er>erti!es n&m5er>erti!es
this.&e&e ne ,in2ed,istInte%erL"#
parent ne int7n&m5er>erti!es 18
(isited ne 5oolean7n&m5er>erti!es 18
!&tet ne Kashet)airL"#
rea!ha5le ne $rra,istInte%erL"#
&nrea!ha5le ne $rra,istInte%erL"#
?
p&5li! 5oolean 5s "int so&r!e int %oal int %raph7878#
6
5oolean pathFo&nd alse int destination element
or "int (ertex 1 (ertex n&m5er>erti!es (ertex#
6
parent7(ertex8 -1
(isited7(ertex8 alse
?
&e&e.add"so&r!e#
parent7so&r!e8 -1
(isited7so&r!e8 tr&e
hile "&e&e.isEmpt"## 6
element &e&e.remo(e"#
destination 1
hile "destination n&m5er>erti!es#
6
i "%raph7element87destination8 L 0 GG (isited7destination8#
6
parent7destination8 element
&e&e.add"destination#
(isited7destination8 tr&e
?
destination
?
?
15
8/21/2019 Data StructureLab 3-12-13(Final) 1
16/70
i "(isited7%oal8#
6
pathFo&nd tr&e
?
ret&rn pathFo&nd
?
p&5li! int netor2Flo "int %raph7878 int so&r!e int destination#
6
int & (
int maxFlo 0
int pathFlo
int7878 resid&alerti!es 187n&m5er>erti!es 18
or "int so&r!e>ertex 1 so&r!e>ertex n&m5er>erti!es so&r!e>ertex# 6
or "int destination>ertex 1 destination>ertex n&m5er>erti!es
destination>ertex#
6
resid&alertex87destination>ertex8 %raph7so&r!e>ertex8
7destination>ertex8
?
?
/Mmax loM/ hile "5s"so&r!e destination resid&al$,E
or "( destination ( so&r!e ( parent7(8#
6
& parent7(8
pathFlo =ath.min"pathFlo resid&al
8/21/2019 Data StructureLab 3-12-13(Final) 1
17/70
?
/M!al!&late the !&t setM/
or "int (ertex 1 (ertex n&m5er>erti!es (ertex#
6
i "5s"so&r!e (ertex resid&al
8/21/2019 Data StructureLab 3-12-13(Final) 1
18/70
int n&m5erNodes
int so&r!e
int sin2
int maxFlo
!anner s!anner ne !anner"stem.in#
stem.o&t.println";Enter the n&m5er o nodes;#
n&m5erNodes s!anner.nextInt"#
%raph ne int7n&m5erNodes 187n&m5erNodes 18
stem.o&t.println";Enter the %raph matrix;#
or "int so&r!e>ertex 1 so&r!e>ertex n&m5erNodes so&r!e>ertex#
6
or "int destination>ertex 1 destination>ertex n&m5erNodes destination>ertex
# 6
%raph7so&r!e>ertex87destination>ertex8 s!anner.nextInt"#
?
?
stem.o&t.println";Enter the so&r!e o the %raph;#
so&r!e s!anner.nextInt"#
stem.o&t.println";Enter the sin2 o the %raph;#
sin2 s!anner.nextInt"#
netor2lo netor2lo ne netor2lo"n&m5erNodes#
maxFlo netor2lo.netor2Flo"%raph so&r!e sin2#
stem.o&t.println";The =ax lo in the %raph is ; maxFlo#
stem.o&t.println";The =inim&m @&t et in the
8/21/2019 Data StructureLab 3-12-13(Final) 1
19/70
p&5li! )air"int so&r!e int destination#
6
this.so&r!e so&r!e
this.destination destination
?
p&5li! )air"#
6
?
?
Output
Result
Th&s the Netor2 Flo ptimiAaton )ro5lem &sin% 'a(a as implemented and the o&tp&tas (eriied s&!!ess&ll.Ex.No:4Date: +3/09/13
19
8/21/2019 Data StructureLab 3-12-13(Final) 1
20/70
Implementation of #inear Programming $sing Simple% Algorithm
Aim
To sol(e ,inear )ro%rammin% ptimiAation )ro5lem ith J (aria5les and 4 !onstraints
&sin% simplex $l%orithm in 'a(a.
=aximiAe: x1x+3x3&5'e!t to:
x13x+x3x4 3-x13x3x +
+x1-x++x3xH 4
+x13x+-x3xJ+x1 x+ x3 x4 x xH xJL 0
Algorithm
tep 1:
8/21/2019 Data StructureLab 3-12-13(Final) 1
21/70
p&5li! !lass Testre(isedsimplex extends 5'e!t 6
p&5li! stati! (oid main"trin% ar%s78# 6
int n J
int m 4
do&5le eps 1.0e-
do&5le a7878 660 3 0 0 0 0?
63 1 3 1 1 0 0 0?
6+ -1 0 3 0 1 0 0?
64 + -1 + 0 0 1 0?
6+ + 3 -1 0 0 0 1??
int 5asi!(ar78 ne int7m 38
re(isedimplex"tr&e n m a eps 5asi!(ar#
i "5asi!(ar7m 18 L 0# 6
stem.o&t.println";No easi5le sol&tion.;# ? else 6
i "5asi!(ar7m +8 L 0# 6
stem.o&t.println";5'e!ti(e &n!tion is &n5o&nd.;#
? else 6
stem.o&t.println";ptimal sol&tion o&nd.;
;UnUn Basi! (aria5le >al&e;#
or "int i 1 i m i# 6
stem.o&t.print"; VHd V1J.Un; 5asi!(ar7i8 a7i8708#
?
stem.o&t.println";Unptimal (al&e o the o5'e!ti(e &n!tion ; a708708#
?
?
?
p&5li! stati! (oid re(isedimplex"5oolean maximiAe int n int m
do&5le a7878 do&5le epsilon int 5asi!(ar78# 6
int i ' 2 m+ p idx 0
do&5le o5'!oe78 ne do&5le7n 18
do&5le (ars&m78 ne do&5le7n 18
do&5le opt5asi!(al78 ne do&5le7m 38
do&5le a&x78 ne do&5le7m 38
do&5le or27878 ne do&5le7m 387m 38
do&5le part s&m
5oolean ineasi5le &n5o&nd a5ort o&t iterate
21
8/21/2019 Data StructureLab 3-12-13(Final) 1
22/70
i "maximiAe# 6
or "' 1 ' n '# 6
a7087'8 -a7087'8
?
?
ineasi5le alse
&n5o&nd alse
m+ m +
p m +
o&t tr&e
2 m 1
or "' 1 ' n '# 6
o5'!oe7'8 a7087'8
s&m 0.0
or "i 1 i m i# 6 s&m - a7i87'8
?
(ars&m7'8 s&m
?
s&m 0.0
or "i 1 i m i# 6
5asi!(ar7i8 n i
opt5asi!(al7i8 a7i8708
s&m - a7i8708
? opt5asi!(al728 0.0
opt5asi!(al7m+8 s&m
or "i 1 i m+ i# 6
or "' 1 ' m+ '# 6
or27i87'8 0.0
?
or27i87i8 1.0
?
iterate tr&e
do 6
// phase 1
i ""opt5asi!(al7m+8 L -epsilon# GG o&t# 6
o&t alse
p m 1
?
22
8/21/2019 Data StructureLab 3-12-13(Final) 1
23/70
part 0.0
// phase +
or "' 1 ' n '# 6
s&m or27p87m 18 M o5'!oe7'8 or27p87m +8 M (ars&m7'8
or "i 1 i m i# 6
s&m or27p87i8 M a7i87'8
?
i "part L s&m# 6
part s&m
2 '
?
?
i "part L -epsilon# 6
iterate alse i "o&t# 6
ineasi5le tr&e
? else 6
a708708 -opt5asi!(al7p8
?
? else 6
or "i 1 i p i# 6
s&m or27i87m 18 M o5'!oe728 or27i87m +8 M (ars&m728
or "' 1 ' m '# 6
s&m or27i87'8 M a7'8728 ?
a&x7i8 s&m
?
a5ort tr&e
or "i 1 i m i# 6
i "a&x7i8 L epsilon# 6
s&m opt5asi!(al7i8 / a&x7i8
i "a5ort CC "s&m part## 6
part s&m
idx i
?
a5ort alse
?
?
i "a5ort# 6
23
8/21/2019 Data StructureLab 3-12-13(Final) 1
24/70
&n5o&nd tr&e
iterate alse
? else 6
5asi!(ar7idx8 2
s&m 1.0 / a&x7idx8
or "' 1 ' m '# 6
or27idx87'8 M s&m
?
i ""idx 1# W + : 1#
do 6
s&m a&x7i8
opt5asi!(al7i8 - part M s&m
or "' 1 ' m '# 6
or27i87'8 - or27idx87'8 M s&m
? i ""i idx - 1# W + : 1#
? hile "i p#
opt5asi!(al7idx8 part
?
?
? hile "iterate#
// ret&rn res<s
5asi!(ar7m 18 "ineasi5le W 1 : 0#
5asi!(ar7m +8 "&n5o&nd W 1 : 0#
or "i 1 i m i# 6 a7i8708 opt5asi!(al7i8
?
i "maximiAe# 6
or "' 1 ' n '# 6
a7087'8 -a7087'8
?
a708708 -a708708
?
?
?
Output
24
8/21/2019 Data StructureLab 3-12-13(Final) 1
25/70
Result
Th&s the ,inear )ro%rammin% ptimiAation )ro5lem &sin% implex $l%orithm as
implemented and the o&tp&t as (eriied s&!!ess&ll.
Ex.No:Date: 0J/09/13
Implementation of &ill 'liming Algorithm for ra*eling Salesman
Prolem
25
8/21/2019 Data StructureLab 3-12-13(Final) 1
26/70
Aim
To Implement Tra(elin% alesman )ro5lem &sin% Kill @lim5in% $l%orithm in 'a(a.
Algorithm
tep1: tart the pro%ram
tep+: Enter the n&m5er o nodes.
tep3: pe!i the ei%ht or ea!h ed%e.
tep4: $ss&me the startin% node.
tep: Find the to&r and its !ost.
tepH: top the pro%ram.
Program
import 'a(a.&til.M
import 'a(a.text.M
!lass T)
6
int ei%ht7878nto&r78inal@ost
inal int INF1000
p&5li! T)"#
6
!anner sne !anner"stem.in#
stem.o&t.println";Enter no. o nodes:L;#
ns.nextInt"#
ei%htne int7n87n8
to&rne int7n-18
or"int i0ini#
6
or"int '0'n'#
6
i"i'#6
stem.o&t.print";Enter ei%ht o ;"i1#; to ;"'1#;:L;#
ei%ht7i87'8s.nextInt"#
?
?
?
26
8/21/2019 Data StructureLab 3-12-13(Final) 1
27/70
stem.o&t.println"#
stem.o&t.println";tartin% node ass&med to 5e node 1.;#
e(al"#
?
p&5li! int @T"int !&rrentNodeint inp&tet78int setiAe#
6
i"setiAe0#
ret&rn ei%ht7!&rrentNode8708
int minINFminindex0
int setToBe)assednToNext@all@T78ne int7n-18
or"int i0isetiAei#
6
int 20//initialise ne set
or"int '0'setiAe'#
6i"inp&tet7i8inp&tet7'8#
setToBe)assednToNext@all@T728inp&tet7'8
?
int temp@T"inp&tet7i8setToBe)assednToNext@all@TsetiAe-1#
i""ei%ht7!&rrentNode87inp&tet7i88temp# min#
6
minei%ht7!&rrentNode87inp&tet7i88temp
minindexinp&tet7i8
?
?ret&rn min
?
p&5li! int =IN"int !&rrentNodeint inp&tet78int setiAe#
6
i"setiAe0#
ret&rn ei%ht7!&rrentNode8708
int minINFminindex0
int setToBe)assednToNext@all@T78ne int7n-18
or"int i0isetiAei#//!onsiders ea!h node o inp&tet
6
int 20
or"int '0'setiAe'#
6
i"inp&tet7i8inp&tet7'8#
setToBe)assednToNext@all@T728inp&tet7'8
27
8/21/2019 Data StructureLab 3-12-13(Final) 1
28/70
?
int temp@T"inp&tet7i8setToBe)assednToNext@all@TsetiAe-1#
i""ei%ht7!&rrentNode87inp&tet7i88temp# min#
6
minei%ht7!&rrentNode87inp&tet7i88temp
minindexinp&tet7i8
?
?
ret&rn minindex
?
p&5li! (oid e(al"#
6
int d&mmet78ne int7n-18
or"int i1ini#
d&mmet7i-18iinal@ost@T"0d&mmetn-1#
!onstr&!tTo&r"#
?
p&5li! (oid !onstr&!tTo&r"#
6
int pre(io&set78ne int7n-18
int nextet78ne int7n-+8 or"int i1ini#
pre(io&set7i-18i
int setiAen-1
to&r708=IN"0pre(io&setsetiAe#or"int i1in-1i#
6
int 20
or"int '0'setiAe'#
6
i"to&r7i-18pre(io&set7'8#
nextet728pre(io&set7'8
?
--setiAe
to&r7i8=IN"to&r7i-18nextetsetiAe#
or"int '0'setiAe'#
pre(io&set7'8nextet7'8
?
displa"#
?
28
8/21/2019 Data StructureLab 3-12-13(Final) 1
29/70
p&5li! (oid displa"#
6
stem.o&t.println"#
stem.o&t.print";The to&r is 1-;#
or"int i0in-1i#
stem.o&t.print""to&r7i81#;-;#
stem.o&t.print";1;#
stem.o&t.println"#
stem.o&t.println";The inal !ost is ;inal@ost#
?
?
!lass T)Exp
6
p&5li! stati! (oid main"trin% ar%s78#
6T) o5'ne T)"#
?
?
Output
29
8/21/2019 Data StructureLab 3-12-13(Final) 1
30/70
Result
Th&s the 'a(a pro%ram or the implementation o Kill !lim5in% al%orithm &sin% tra(ellin%
salesman pro5lem has 5een exe!&ted s&!!ess&ll.Ex.No:HDate: 10/10/13
Implementation of D+namic Programming
30
8/21/2019 Data StructureLab 3-12-13(Final) 1
31/70
(,atri% 'hain ,ultiplication)
Aim
To Implement =atrix @hain =<ipli!ation &sin% Dnami! )ro%rammin% Te!hni&e in
'a(a.
Algorithm
tep 1: tart the pro%ram.
tep +: InitialiAe N and d.tep 3: @omp&te the minim&m n&m5er o element m<ipli!ations re&ired or the
matri!es deined in init.
tep 4: InitialiAin% the method o N and d.
tep : The dimensions o d are hard-!oded.tep H: N is initialiAed ith e(erthin% at ininit ex!ept the dia%onals
tep J: The (aria5le d stores the dimensions o the arras as deined in the notes $0 is a
d0xd1 matrix $1 is a d1xd+ matrix et!.tep Q: The (aria5le d has IXE 1 dimension.
tep 9: This !al!&lates the minim&m n&m5er o element m<ipli!ations re&ired
tep 10: To m<ipl the n&m5er o matri!es %i(en and their %i(en dimensions.tep 11: top the pro%ram.
Program
import 'a(a.io.Mimport 'a(a.&til.M
import 'a(a.lan%.trin%B&er
p&5li! !lass =@=
6p&5li! int N7878
p&5li! int d78
p&5li! int IXE
p&5li! stati! (oid main" trin% 7 8 ar%s # thros Ex!eption
6 =@= m!m ne =@="#
// InitialiAe N and d
m!m.init"#
// @omp&te the minim&m n&m5er o element m<ipli!ations re&ired or the matri!es
deined in init.
31
8/21/2019 Data StructureLab 3-12-13(Final) 1
32/70
int res< m!m.min=@="#
stem.o&t.println";res< ; res<# ?
// This method initialiAes N and d// The dimensions o d are hard-!oded
p&5li! (oid init"#
6N ne int7IXE87IXE8
d ne int7IXE18
// N is initialiAed ith e(erthin% at ininit ex!ept the dia%onals.or "int i 0 i IXE i#
6
or "int ' 0 ' IXE '#
6 i "i '#
N7i87'8 0 else
N7i87'8 999
? ?
// d stores the dimensions o the arras as deined in the notes $0 is a d0xd1
matrix $1 is a d1xd+ matrix et!.// hi!h is h d has IXE 1 dimensions.
d708 +
d718 4d7+8 +
d738 3
d748 1d78 4
?
// This method !al!&lates the minim&m n&m5er o element m<ipli!ations re&ired// to m<ipl the n&m5er o matri!es %i(en and their %i(en dimensions.
p&5li! int min=@="#
6
// Basi!all e are loo2in% or the optimal a to ;split; the matri!es here 2 deines
the ;split;. or "int i 1 i IXE i#
6
or "int ' 0 ' IXE-i '#
6
32
8/21/2019 Data StructureLab 3-12-13(Final) 1
33/70
or "int 2 ' 2 'i-1 2#
6
i "N7'87'i-18 L N7'8728N72187'i-18 d7'8Md7218Md7i'8# 6
N7'87'i-18 N7'8728N72187'i-18 d7'8Md7218Md7i'8
? ?
?
?
// &tp&t the inal N matrix
stem.o&t.println";N ;#
or "int i 0 i IXE i# 6
or "int ' 0 ' IXE '#
6
stem.o&t.print"N7i87'8 ;Ut;# ?
stem.o&t.println"# ?
stem.o&t.println"#
// The res< is stored in the irst ro last !ol&mn o N
ret&rn N7087IXE-18
??
Output
33
8/21/2019 Data StructureLab 3-12-13(Final) 1
34/70
Result
Th&s the=atrix @hain =<ipli!ation &sin% Dnami! )ro%rammin% Te!hni&e as
s&!!ess&ll (eriied in 'a(a.Ex.No:JDate: 1J/10/13
Implementation of D+namic Programming
(#ongest 'ommon Suse-uence)
34
8/21/2019 Data StructureLab 3-12-13(Final) 1
35/70
Aim
To Implement ,on%est @ommon &5se&en!e &sin% Dnami! )ro%rammin% in 'a(a.
Algorithm
tep 1:tart the pro%ram.
tep +:Enter to trin%s as inp&t to !he!2 their s&5se&en!e
tep 3:@al!&late the strin%sY len%th
tep 4:@reate a matrix hose siAe is e&al to the the inp&t strin%sY len%th
tep :@omp&te len%th o ,@ and all s&5pro5lems (ia dnami! pro%rammin%
tep H: e!o(er ,@ itsel and print it to standard o&tp&t
tep J:top the pro%ram.
Program
import 'a(a.io.DataInp&ttream
import 'a(a.&til.!anner
p&5li! !lass ,@6
p&5li! stati! (oid main"trin%78 ar%s# 6
stem.o&t.println";Enter the trin% x: ;#
trin% x !anner s!anIn ne !anner"stem.in#
x s!anIn.next,ine"#
stem.o&t.println";Enter the trin% : ;#
trin% s!anIn.next,ine"# s!anIn.!lose"#
int = x.len%th"# int N .len%th"#
// opt7i87'8 len%th o ,@ o x7i..=8 and 7'..N8 int7878 opt ne int7= 187N 18
35
8/21/2019 Data StructureLab 3-12-13(Final) 1
36/70
// !omp&te len%th o ,@ and all s&5pro5lems (ia dnami! pro%rammin%
or "int i = - 1 i L 0 i--# 6 or "int ' N - 1 ' L 0 '--# 6
i "x.!har$t"i# .!har$t"'## 6
opt7i87'8 opt7i 187' 18 1 ? else 6
opt7i87'8 =ath.max"opt7i 187'8 opt7i87' 18#
? ?
?
// re!o(er ,@ itsel and print it to standard o&tp&t
int i 0 ' 0
hile "i = GG ' N#6
i "x.!har$t"i# .!har$t"'##6
stem.o&t.print"x.!har$t"i##
i '
? else i "opt7i 187'8 L opt7i87' 18#
6
i ? else
6
' ?
?
stem.o&t.println"#
?
?
Output
36
8/21/2019 Data StructureLab 3-12-13(Final) 1
37/70
Result
Th&s the implemention o ,on%est @ommon &5se&en!e &sin% Dnami! )ro%rammin%
as s&!!ess&ll (eriied in 'a(a.
Ex.No:QDate: +1/10/13
37
8/21/2019 Data StructureLab 3-12-13(Final) 1
38/70
Implementation of N".ueen/s prolem using Recursi*e
Bac!trac!ing
Aim
To Implement N-*&een )ro5lem &sin% e!&rsi(e Ba!2tra!2in% in 'a(a.
Algorithm
tep 1: tart in the letmost !ol&mn
tep +: I all &eens are pla!ed ret&rn tr&e
tep 3: Tr all ros in the !&rrent !ol&mn. Do olloin% or e(er tried ro.
a# I the &een !an 5e pla!ed sael in this ro then mar2 this 7ro!ol&mn8 as part
o the sol&tion and re!&rsi(el !he!2 i pla!in% &een here leads to a sol&tion. 5# I pla!in% &een in 7ro !ol&mn8 leads to a sol&tion then ret&rn tr&e.
!# I pla!in% &een doesnYt lead to a sol&tion then &nmar2 this 7ro!ol&mn8
"Ba!2tra!2# and %o to step "a# to tr other ros.
tep 4: I all ros ha(e 5een tried and nothin% or2ed ret&rn alse to tri%%er
5a!2tra!2in%.
Program
import 'a(a.io.M!lass N*&een
6
stati! int nstati! int !o&nt 1
stati! int 5oard78 ne int7+08
p&5li! stati! (oid main "trin% ar%s78#thros Ex!eption
6
DataInp&ttream dis ne DataInp&ttream"stem.in#
stem.o&t.println";UnN *EEN )B,E= IN< B$@ZT$@ZIN
8/21/2019 Data StructureLab 3-12-13(Final) 1
39/70
&een"1n#
?
p&5li! stati! (oid p&tData"int n#//This &n!tion is &sed to print the sol&tion
6
stem.o&t.println"#stem.o&t.println"#
stem.o&t.println";ol&tion ;!o&nt; is : UnUn;#
or"int i1 ini#6
stem.o&t.print";Ut;i#
?
or"int i1 in i#6
stem.o&t.print";UnUn;i#
or"int '1 'n '#
6 i"5oard7i8 '#
stem.o&t.print";Ut*;#//*&een is pla!ed at the !orre!tposition and reerred to as *.
else
stem.o&t.print";Ut-;#//Empt position?
?
!o&nt?
p&5li! stati! int !he!2"int ro int !ol#//This &n!tion is &sed to !he!2 hether *&een !an
5e pla!ed in parti!&lar
6//ro and !ol&mn or it en!o&nters !onli!ts. et&rns 0 or !onli!ts and 1 or no
!onli!t.
or"int i1 i"ro-1# i#
6
i"5oard7i8 !ol#//!ol&mn !onli!ts
ret&rn 0else
i"=ath.a5s"5oard7i8-!ol# =ath.a5s"i-ro##//dia%onal !onli!ts
ret&rn 0
?
ret&rn 1//No !onli!ts
?
39
8/21/2019 Data StructureLab 3-12-13(Final) 1
40/70
p&5li! stati! (oid &een"int ro int n#//This &n!tion !he!2s or the next ree positionhere &een !an 5e pla!ed
6 int !ol
or"!ol1 !oln !ol#6
i"!he!2"ro!ol# 1#
6
5oard7ro8 !ol//No !onli!t so pla!e &eeni"ron#//dead end
p&tData"n#//print the !oni%&ration o !hess 5oard
else &een"ro1n#
?
?
??
Output
40
8/21/2019 Data StructureLab 3-12-13(Final) 1
41/70
Result
Th&s the N-&een[s )ro5lem &sin% e!&rsi(e Ba!2tra!2in% te!hni&e &sin% 'a(a asimplemented and the o&tp&t as (eriied s&!!ess&ll.
Ex.No:9Date: +Q/10/+013
Implementation of , " 'oloring prolem using Recursi*e
Bac!trac!ing
41
8/21/2019 Data StructureLab 3-12-13(Final) 1
42/70
Aim
To Implement = - @olorin% pro5lem &sin% e!&rsi(e Ba!2tra!2in% te!hni&e in 'a(a.
Algorithm
tep 1:
8/21/2019 Data StructureLab 3-12-13(Final) 1
43/70
6
stem.o&t.println";UtUtUtUt
8/21/2019 Data StructureLab 3-12-13(Final) 1
44/70
stem.o&t.println"#
o&nd tr&e
ret&rn
?
else
m@olorin%"21#
?
?
stati! (oid Next>al&e"int 2#
6
int '
hile"tr&e#
6
x728 "x7281#V"m1#i"x7280#
ret&rn
or"'1 'n '#
i" "
8/21/2019 Data StructureLab 3-12-13(Final) 1
45/70
Result
Th&s the implemention o = - @olorin% pro5lem &sin% e!&rsi(e Ba!2tra!2in%te!hni&e as s&!!ess&ll (eriied in 'a(a.Ex.No:10Date: 4/11/+013
Implementation of Ran0omi1e0 Algorithm for .uic! Sort45
8/21/2019 Data StructureLab 3-12-13(Final) 1
46/70
Aim
To Implement *&i!2 ort &sin% andomiAed $l%orithm in 'a(a.
Algorithm
TE) 1:@hoosin% the pi(ot
$ssi%n the irst element o the arra as pi(ot and assi%n se!ond element as loer
5o&nd as ell as assi%n the last element as &pper 5o&nd.
TE) +. )artitionin%
Di(ide all other elements "ex!ept the pi(ot# into to partitions.
$ll elements less than the pi(ot m&st 5e in the irst partition.
$ll elements %reater than the pi(ot m&st 5e in the se!ond partition
TE) 3. e!&rsi(el &i!2 sort the let and the ri%ht parts
se re!&rsion to sort 5oth partitions.
\oin the irst sorted partition the pi(ot and the se!ond sorted partition.
Program
import 'a(a.&til.M
!lass $rraIns
6
pri(ate lon%78 the$rra
pri(ate int nElems
p&5li! $rraIns"int max#
6
the$rra ne lon%7max8
nElems 0?
p&5li! (oid insert"lon% (al&e#
6
the$rra7nElems8 (al&e
nElems
?
46
8/21/2019 Data StructureLab 3-12-13(Final) 1
47/70
p&5li! (oid displa"#
6
stem.o&t.print";$;#
or"int '0 'nElems '#
stem.o&t.print"the$rra7'8 ; ;#
stem.o&t.println";;#
?
p&5li! (oid &i!2ort"#
6
re!*&i!2ort"0 nElems-1#
?
p&5li! (oid re!*&i!2ort"int let int ri%ht#
6
i"ri%ht-let 0#
ret&rnelse
6
andom rand ne andom"#
int pi(otIndex let rand.nextInt"ri%ht-let1#
sap"pi(otIndex ri%ht#
lon% pi(ot the$rra7ri%ht8
int partition partitionIt"let ri%ht pi(ot#
re!*&i!2ort"let partition-1#
re!*&i!2ort"partition1 ri%ht#?
?
p&5li! int partitionIt"int let int ri%ht lon% pi(ot#
6
int let)tr let-1
int ri%ht)tr ri%ht
hile"tr&e#
6
hile" the$rra7let)tr8 pi(ot #
hile"ri%ht)tr L 0 GG the$rra7--ri%ht)tr8 L pi(ot#
i"let)tr L ri%ht)tr#
5rea2
else
47
8/21/2019 Data StructureLab 3-12-13(Final) 1
48/70
sap"let)tr ri%ht)tr#
?
// end hile"tr&e#
sap"let)tr ri%ht#
ret&rn let)tr
?
// end partitionIt"#
p&5li! (oid sap"int dex1 int dex+#
6
lon% temp the$rra7dex18
the$rra7dex18 the$rra7dex+8
the$rra7dex+8 temp
? // end sap"
? // end !lass $rraIns
!lass andomiAed*&i!2ort
6
p&5li! stati! (oid main"trin%78 ar%s#
6
int maxiAe 1H
$rraIns arr
arr ne $rraIns"maxiAe#
or"int '0 'maxiAe '#6
lon% n "int#"'a(a.lan%.=ath.random"#M99#
arr.insert"n#
?
arr.displa"#
arr.&i!2ort"#
arr.displa"#
?
?
Output2
48
8/21/2019 Data StructureLab 3-12-13(Final) 1
49/70
Result
Th&s the implemention o *&i!2 ort &sin% andomiAed $l%orithm as s&!!ess&ll(eriied in 'a(a.Ex.No:11Date: J/11/+013
Implementation of Ran0omi1e0 Algorithm for ,erge Sort
49
8/21/2019 Data StructureLab 3-12-13(Final) 1
50/70
Aim
To Implement =er%e ort &sin% andomiAed $l%orithm in 'a(a.
Algorithm
tep1: tart the pro%ram.
tep+: ead ]n[ inp&t (al&es.
tep3: plit the elements into to parts 5ased on mid (al&e ie.mlh/+..
tep 4: epeat step 3 &ntil %et sin%le node.
tep: se randomiAed method to sort the elements rom lo position (al&es to mid
)osition G m1 position elements to hi%h position elements.
tep H: epeat step 4 &ntil sort all nodes.
tepJ: top the pro%ram
Program
import 'a(a.&til.M
p&5li! !lass =er%eort6p&5li! int a78ne int708
p&5li! (oid mer%eOsort"int loint hi%h#
6int mid
i"lohi%h#
6 mid"lohi%h#/+
mer%eOsort"lomid#
mer%eOsort"mid1hi%h#
mer%e"lomidhi%h#?
?p&5li! (oid mer%e"int loint midint hi%h#6
int hi'2
int 578ne int708hlo
ilo
'mid150
8/21/2019 Data StructureLab 3-12-13(Final) 1
51/70
hile""hmid#GG"'hi%h##
6 i"a7h8a7'8#
6
57i8a7h8 h
?
else 6
57i8a7'8
'
? i
?
i"hLmid#
6 or"2'2hi%h2#
6 57i8a728
i
??
else
6
or"2h2mid2# 6
57i8a728
i ?
?
or"2lo2hi%h2# a7285728?
p&5li! =er%eort"#
6
int n&mi
stem.o&t.println";=E
8/21/2019 Data StructureLab 3-12-13(Final) 1
52/70
stem.o&t.println"#
stem.o&t.println";No )lease Enter the "; n&m ;# nos. 7TKEN )E ENTE8:;#
or"i1in&mi#
6 a7i8ne !anner"stem.in#.nextInt"#
?
mer%eOsort"1n&m#
stem.o&t.println"#
stem.o&t.println";o the sorted list "&sin% =E
8/21/2019 Data StructureLab 3-12-13(Final) 1
53/70
Result
Th&s the implemention o =er%e ort &sin% andomiAed $l%orithm as s&!!ess&ll
(eriied in 'a(a.
Ex.No:1+Date: 11/11/+013
Implementation of #oc!ing an0 S+nchroni1ation mechanism for
'oncurrent Stac!s
53
8/21/2019 Data StructureLab 3-12-13(Final) 1
54/70
Aim
To Implement ,o!2in% and n!hroniAation me!hanism or @on!&rrent ta!2s in 'a(a.
Algorithm
tep1: tart the pro%ram
tep+ : @reate a =ain !lass named ith =!on!&rrentsta!2 &sin% templates
tep 3: @all $tomi! reeren!es &sin% atomi! &tilities ith node templates
tep 4: Deine !onstr&!tor or (aria5les initialiAation ith in !lass
tep : Deine and des!ription p&sh &n!tion ith templates or insert the data on the
nodes
tep H: Deine and des!ription o pop &n!tion ith templates or remo(e the data romnode
tep J: Deine and )ee2"# template to !he!2 the node is empt or not
tep 9 : Deine the =ain !lass
tep 10: !reate the o5'e!t to the !lass or !allin% the p&sh"# and pop"# &n!tions
tep 11: Insert the (al&es thro&%h p&sh"# template &n!tion
tep 1+ : top the )ro%ram
Program2
import 'a(a.&til.!on!&rrent.atomi!.$tomi!Inte%erimport 'a(a.&til.!on!&rrent.atomi!.$tomi!eeren!e
p&5li! !lass =@on!&rrentta!2
6
pri(ate $tomi!eeren!e head ne $tomi!eeren!e"#
p&5li! =@on!&rrentta!2"#
6
?
p&5li! (oid p&sh"T t#
6
Node n ne Node"t#
Node !&rrent
do
54
8/21/2019 Data StructureLab 3-12-13(Final) 1
55/70
6
!&rrent head.%et"#
n.setNext"!&rrent#
?hile"head.!ompare$ndet"!&rrent n##
?
p&5li! T pop"#
6
Node !&rrentKead n&ll
Node &t&reKead n&ll
do
6
!&rrentKead head.%et"#
i"!&rrentKead n&ll#
6 ret&rn n&ll
?
&t&reKead !&rrentKead.next
?hile"head.!ompare$ndet"!&rrentKead &t&reKead##
ret&rn !&rrentKead.data
?
p&5li! T pee2"#
6 Node n head.%et"#
i"nn&ll#
6
ret&rn n&ll
?
else
6
ret&rn n.data
?
?
pri(ate stati! !lass Node
6
pri(ate inal T data
pri(ate Node next
55
8/21/2019 Data StructureLab 3-12-13(Final) 1
56/70
pri(ate Node"T data#
6
this.data data
?
pri(ate (oid setNext"Node next#
6
this.next next
?
?
p&5li! stati! (oid main"trin%78 ar%s#
6
=@on!&rrentta!2 m ne =@on!&rrentta!2"# m.p&sh"1+#
m.p&sh"13#
m.p&sh"1#
stem.o&t.println"m.pop"##
stem.o&t.println"m.pop"##
stem.o&t.println"m.pop"##
stem.o&t.println"m.pop"##
?
?
Output
56
8/21/2019 Data StructureLab 3-12-13(Final) 1
57/70
Result
Th&s the implemention o ,o!2in% and n!hroniAation me!hanism or @on!&rrentta!2s as s&!!ess&ll (eriied in 'a(a.Ex.No:13Date: 14/11/+013
57
8/21/2019 Data StructureLab 3-12-13(Final) 1
58/70
Implementation of #oc!ing an0 S+nchroni1ation mechanism for
'oncurrent .ueues
Aim
To Implement ,o!2in% and n!hroniAation me!hanism or @on!&rrent *&e&es in 'a(a.
Algorithm
tep 1: tart the pro%ram
tep +:@reate the )rod&!er @lass to deli(er the messa%e to !ons&mer
tep 3: &sin% the 5lo!2in% &e&e &tilities or !on!&rrent &e&e operation
tep 4: @reate the r&n &n!tion to prod&!e ms% and to the &e&e
tep : @reate the !ons&mer !lass to !ons&min% the ms% rom prod&!er
tep H : !reate the r&n &n!tion ith !ons&mer !lass or %ettin% the ms% rom prod&!er
tep J: @reate the )rod&!er!ons&merser(i!e !lass to start the ser(i!es o prod&!er and
!ons&mer !lasses ser(i!es.
tep Q : top the pro%ram.
Program
PROD$'3R 'ONS$,3R S3R4I'3
import 'a(a.&til.!on!&rrent.$rraBlo!2in%*&e&e
import 'a(a.&til.!on!&rrent.Blo!2in%*&e&e
p&5li! !lass )rod&!er@ons&merer(i!e 6
p&5li! stati! (oid main"trin%78 ar%s# 6 //@reatin% Blo!2in%*&e&e o siAe 10
Blo!2in%*&e&e=essa%eL &e&e ne $rraBlo!2in%*&e&e=essa%eL"10# )rod&!er prod&!er ne )rod&!er"&e&e#
@ons&mer !ons&mer ne @ons&mer"&e&e#
//startin% prod&!er to prod&!e messa%es in &e&e ne Thread"prod&!er#.start"#
//startin% !ons&mer to !ons&me messa%es rom &e&e
58
8/21/2019 Data StructureLab 3-12-13(Final) 1
59/70
ne Thread"!ons&mer#.start"#
stem.o&t.println";)rod&!er and @ons&mer has 5een started;#
??
PROD$'3R
import 'a(a.&til.!on!&rrent.Blo!2in%*&e&e
p&5li! !lass )rod&!er implements &nna5le 6
pri(ate Blo!2in%*&e&e=essa%eL &e&e
p&5li! )rod&!er"Blo!2in%*&e&e=essa%eL #6
this.&e&e ?
^(erride p&5li! (oid r&n"# 6 //prod&!e messa%es
or"int i0 i10 i#6
=essa%e ms% ne =essa%e";;i# tr 6
Thread.sleep"i#
&e&e.p&t"ms%#
stem.o&t.println";)rod&!ed ;ms%.%et=s%"## ? !at!h "Interr&ptedEx!eption e# 6
e.printta!2Tra!e"#
? ?
//addin% exit messa%e
=essa%e ms% ne =essa%e";exit;# tr 6
&e&e.p&t"ms%#
? !at!h "Interr&ptedEx!eption e# 6
e.printta!2Tra!e"# ?
?
?
'ONS$,3R
import 'a(a.&til.!on!&rrent.Blo!2in%*&e&e
59
8/21/2019 Data StructureLab 3-12-13(Final) 1
60/70
p&5li! !lass @ons&mer implements &nna5le6
pri(ate Blo!2in%*&e&e=essa%eL &e&e
p&5li! @ons&mer"Blo!2in%*&e&e=essa%eL #6 this.&e&e
?
^(erride
p&5li! (oid r&n"# 6
tr6
=essa%e ms% //!ons&min% messa%es &ntil exit messa%e is re!ei(ed
hile""ms% &e&e.ta2e"##.%et=s%"# ;exit;#6
Thread.sleep"10#
stem.o&t.println";@ons&med ;ms%.%et=s%"## ?
?!at!h"Interr&ptedEx!eption e# 6 e.printta!2Tra!e"#
?
??
,3SSAG3
p&5li! !lass =essa%e 6
pri(ate trin% ms%
p&5li! =essa%e"trin% str#6
this.ms%str ?
p&5li! trin% %et=s%"# 6
ret&rn ms% ?
?
Output
60
8/21/2019 Data StructureLab 3-12-13(Final) 1
61/70
Result
Th&s the implemention o ,o!2in% and n!hroniAation me!hanism or @on!&rrent
*&e&es as s&!!ess&ll (eriied in 'a(a.
Ex.No:14Date: 1Q/11/+013
61
8/21/2019 Data StructureLab 3-12-13(Final) 1
62/70
Implementation of #oc!ing an0 S+nchroni1ation mechanism for
#in!e0 #ists
Aim
To Implement ,o!2in% and n!hroniAation me!hanism or ,in2ed ,ists in 'a(a.
Algorithm
tep1: tart the pro%ram
tep+ : @reate a !lass named ith ,in21
tep 3: Deine (aria5les to 5e &sed ith in this !lass
tep 4: Deine !onstr&!tor or (aria5les initialiAation ith in !lass
tep : !reate a &n!tion )rintlin2"# or o&tp&t (ietep H: @reate another !lass name ith ,in2edlist
tep J: Deine (aria5les and &n!tions to 5e &sed ith in this !lass
tep Q: Deine !onstr&!tor or (aria5les initialiAation ith in !lass
tep 9: The ,in21 !lass "First !reated !lass# or a!!essin% the (aria5les &sin% m<iple
inheritan!e
tep 10: @all lo!2 &n!tion &sin% !on!&rrent lo!2 &tilities
tep11: Deine and Des!ription o Insert"# &n!tion to insert (al&es
tep 1+: Insert (al&es rom the mail &n!tion on list
tep 13: Deine and Des!ription o Delete"#&n!tion or !on!&rrent deletion pro!ess
tep 14: @reate main !lass named ith @onlin2edlisttep 1:
8/21/2019 Data StructureLab 3-12-13(Final) 1
63/70
p&5li! do&5le data+
p&5li! ,in21 next,in2
//,in2 !onstr&!tor
p&5li! ,in21"int d1 do&5le d+#6
data1 d1 data+ d+
?
//)rint ,in2 data p&5li! (oid print,in2"#
6
stem.o&t.print";6; data1 ; ; data+ ;? ;#
??
!lass ,in2,ist1
6 pri(ate ,in21 irst
,o!2 lo!2
//,in2,ist !onstr&!tor p&5li! ,in2,ist1"#
6
lo!2 ne eentrant,o!2"# irst n&ll
?
//et&rns tr&e i list is empt
p&5li! 5oolean isEmpt"#
6
ret&rn irst n&ll ?
//Inserts a ne ,in2 at the irst o the list p&5li! (oid insert"int d1 do&5le d+#
6
lo!2.lo!2"#
tr
6
,in21 lin2 ne ,in21"d1 d+#
63
8/21/2019 Data StructureLab 3-12-13(Final) 1
64/70
lin2.next,in2 irst
irst lin2
?inall
6
lo!2.&nlo!2"#?
?
//Deletes the lin2 at the irst o the list
p&5li! ,in21 delete"#
6lo!2.lo!2"#
tr
6 ,in21 temp irst
irst irst.next,in2 ret&rn temp
?
inall
6
lo!2.&nlo!2"#
? ?
//)rints list data p&5li! (oid print,ist"#
6
lo!2.lo!2"#
tr
6
,in21 !&rrent,in2 irst stem.o&t.print";,ist: ;#
hile"!&rrent,in2 n&ll#
6 !&rrent,in2.print,in2"#
!&rrent,in2 !&rrent,in2.next,in2
? stem.o&t.println";;#
?
inall
6
64
8/21/2019 Data StructureLab 3-12-13(Final) 1
65/70
lo!2.&nlo!2"#
?
?// End o )rint &n!tion
?// End o !lass
!lass @on,in2,ist
6
p&5li! stati! (oid main"trin%78 ar%s# thros Ex!eption
6
,in2,ist1 list ne ,in2,ist1"#
Inp&ttreameader sr ne Inp&ttreameader"stem.in#
B&eredeader 5r ne B&eredeader"sr#
stem.o&t.print";UnUnEnter the to do&5le (al&es to insert in list : ;#
int p1 Inte%er.parseInt"5r.read,ine"##
do&5le p+ Do&5le.parseDo&5le"5r.read,ine"##
list.insert"p1p+#
stem.o&t.print";UnUnEnter the to do&5le (al&es to insert in list : ;#int p3 Inte%er.parseInt"5r.read,ine"##
do&5le p4 Do&5le.parseDo&5le"5r.read,ine"##
list.insert"p3p4#
list.print,ist"#
hile"list.isEmpt"##
6
,in21 deleted,in2 list.delete"# stem.o&t.print";deleted: ;#
deleted,in2.print,in2"#
stem.o&t.println";;# ?
list.print,ist"#
? // End o =ain?// End o @lass
Output2
65
8/21/2019 Data StructureLab 3-12-13(Final) 1
66/70
Result
Th&s the implemention o ,o!2in% and n!hroniAation me!hanism or ,in2ed ,ists as
s&!!ess&ll (eriied in 'a(a.
Ex.No:1Date: +1/11/+013
De*eloping Applications In*ol*ing 'oncurrenc+
(,atri% ,ultiplication)
66
8/21/2019 Data StructureLab 3-12-13(Final) 1
67/70
Aim
To Implement the =atrix =<ipli!ation &sin% @on!&rren! Te!hni&e in \a(a.
Algorithm2
tep 1: tart the pro%ram.
tep +: Enter the order o the =atrix
tep 3:
8/21/2019 Data StructureLab 3-12-13(Final) 1
68/70
6
int i'
B&eredeader 5rne B&eredeader"ne Inp&ttreameader"stem.in##
stem.o&t.print";Enter the order o =atrix : ;#
tr
6
nInte%er.parseInt"5r.read,ine"##
?!at!h"Ex!eption e#6?
in1ne int7n87n8
in+ne int7n87n8
o&tne int7n87n8
stem.o&t.println";Enter the First =atrix : ;#
or"i0ini#
6
or"'0'n'# 6
tr
6
in17i87'8Inte%er.parseInt"5r.read,ine"##
?!at!h"Ex!eption e#6?
?
?
stem.o&t.println";Enter the e!ond =atrix : ;#
or"i0ini#
6 or"'0'n'#
6
tr
6
in+7i87'8Inte%er.parseInt"5r.read,ine"##
?!at!h"Ex!eption e#6?
?
?
=at=<i mat78ne =at=<i7n8
or"i0ini#
mat7i8ne =at=<i"i#
tr
6
or"i0ini#
mat7i8.'oin"#
68
8/21/2019 Data StructureLab 3-12-13(Final) 1
69/70
?!at!h"Ex!eption e#6?
stem.o&t.println";T)T :;#
or"i0ini#
or"'0'n'#
stem.o&t.println"o&t7i87'8#
?
?
Output
69
8/21/2019 Data StructureLab 3-12-13(Final) 1
70/70
Result
Th&s the =atrix =<ipli!ation &sin% @on!&rren! Te!hni&e as implemented and
Top Related