You are on page 1of 95

# E*!

## N":\$ IMPLEMENTATION OF BRESENHAM+S ALGORITHM

DATE: LINE, CIRCLE, ELLIPSE!
LINE:
AIM:
T& stu,) a+, !mpleme+t \$rese+ham9s #i+e Dra-i+1 Al1&rithm
ALGORITHM:
STEP \$: !+put the t-& li+e e+,p&i+ts a+, st&re the le(t e+, p&i+t i+ ;3
0
,)
0
<.
STEP 2: #&a, ;3
0
,)
0
< i+t& the (rame *u((er,that is,pl&t the (irst p&i+t.
STEP %: Cal'ulate '&+sta+ts =3,=),2=),a+, 2=)-2=3,a+, &*tai+ the starti+1 value (&r the
,e'isi&+ parameter as p
0
>2=)-=3
STEP 4: At ea'h 3,al&+1 the li+e,starti+1 at .>0,per(&rm the (&ll&-i+1 test. i( p
.
? 0,the +e3t
p&i+t t& pl&t is ;3
.
@2,)
.
<a+, p
.@2
>p
.
@2=) &ther-ise,the +e3t p&i+t t& pl&t is
;3
.
@2,)
.
@2<a+, p
.@2
>p
.
@2=)-2=3
STEP 5: Repeat step 4=3 times.
SRIRAM ENGINEERING
COLLEGE

PERUMALPATTU-602024
www.jntuworld.com
www.jntuworld.com
www.jwjobs.net
P"g-.:
LINE
Ai+'lu,e?st,i&.hB
Ai+'lu,e?'&+i&.hB
Ai+'lu,e?st,li*.hB
Ai+'lu,e?1raphi's.hB
A,e(i+e M!%C 0
A,e(i+e M!%D 0
v&i, *e1i+;<E
i+t s'ale/3,s'ale/),3/(a't&r)/(a't&rE
v&i, mai+;<
F
i+t 3,),30,)0,3+,)+,,3,,),p,t-&,),t-&,),3,3e+,E
'har teG20HE
i+t ,>DETECT,*E
i+t CE%TREC,CE%TRED,MACC,MACDE
pri+t(;I J+ e+ter the starti+1 p&i+ts:I<E
s'a+(;I K, K,I,L30,L)0<E
pri+t(;IJ+ e+ter the e+, p&i+ts:I<E
s'a+(;I K,K,I,L3+,L)+<E
pri+t(;IJ+I<E
'lrs'r;<E
SRIRAM ENGINEERING
COLLEGE

PERUMALPATTU-602024
www.jntuworld.com
www.jntuworld.com
www.jwjobs.net
i+it1raph;L,,L*,II<E
'lear,evi'e;<E
set1raphm&,e;1et1raphm&,e;<<E
CE%TREC>1etma33;<M2E
CE%TRED>1etma3);<M2E
*e1i+;<E
,3>a*s;30-3+<E
,)>a*s;)0-)+<E
p>2N,)-,3E
t-&,)>2N,)E
t-&,),3>2N;,)-,3<E
i(;30B3+<
F
3>3+E
)>)+E
3e+,>30E
O
else
F
3>30E
)>)+E
3e+,>3+E
O
putpi3el;CE%TREC@3,CE%TRED@),6<E
-hile;3?3e+,<
SRIRAM ENGINEERING
COLLEGE

PERUMALPATTU-602024
www.jntuworld.com
www.jntuworld.com
www.jwjobs.net
F
3@@E
i(;p?0<
p>p@t-&,)E
else
F
)@@E
p>p@t-&,),3E
O
putpi3el;CE%TREC@3,CE%TRED@),6<E
O
1et'h;<E
'l&se1raph;<E
O
v&i, *e1i+;<
F
i+t CE%TREC>1etma33;<M2E
i+t CE%TRED>1etma3);<M2E
i+t MACC>1etma33;<E
MMi+t MACD>1etma33;<E
li+e;0,1etma3);<M2,1etma33;<,1etma3);<M2<E
li+e;1etma33;<M2,0,1etma33;<M2,1etma33;<<E
set'&l&r;P0<E
sette3tst)le;0,0,0<E
&utte3t3);CE%TREC@2,CE%TRED@P,IOR!G!%I<E
SRIRAM ENGINEERING
COLLEGE

PERUMALPATTU-602024
www.jntuworld.com
www.jntuworld.com
www.jwjobs.net
&utte3t3);MACC-40,CE%TRED@4,I8!R"TI<E
&utte3t3);CE%TREC@P,P,I"ECO%DI<E
O
OUTPUT:
E+ter the starti+1 p&i+ts:0 0
E+ter the e+, p&i+ts:200 200
"ECO%D
OR!G!%
8!R"T
SRIRAM ENGINEERING
COLLEGE

PERUMALPATTU-602024
www.jntuworld.com
www.jntuworld.com
www.jwjobs.net
RESULT:
Thus the pr&1ram (&r impleme+tati&+ &( \$rese+hamQs li+e ,ra-i+1 Al1&rithm is
su''ess(ull) '&mpile, a+, e3e'ute,.
CIRCLE:
AIM:
T& stu,) a+, !mpleme+t \$rese+ham9s Cir'le Dra-i+1 Al1&rithm
ALGORITHM:
STEP \$: !+put ra,ius r a+, 'ir'le 'e+ter;3',)'< a+, &*tai+ the (irst p&i+t &+ the 'ir'um(ere+'e &(
the 'ir'le 'e+tere, &+ the &ri1i+ as ;30,)0<>;0,r<
STEP 2: Cal'ulate the i+itial value &( the ,e'isi&+ parameter as p0>PM4-r
STEP %: At ea'h 3. p&siti&+,starti+1 at .>0,per(&rm the (&ll&-i+1 test:
!( p.?0 ,the +e3t p&i+t al&+1 the 'ir'le 'e+tere, &+;0,0< is;3.@2,).< a+,
P.@2>p.@23.@2 @ 2
Other-ise the +e3t p&i+t al&+1 the 'ir'le is;3.@2,).-2< a+, p.@2>p.@23.@2 @2 @2).@2 -here
23.@2>23.@2 a+, 2).@2>2).-2
STEP 4: Determi+e the s)mmetr) p&i+ts i+ the &ther seve+ &'ta+ts
STEP 5:M&ve ea'h 'al'ulate, pi3el p&siti&+ ;3,)< &+ t& the 'ir'ular path 'e+tere, &+ ;3',)'< a+,
pl&t the '&&r,i+ate values:
3>3@30, )>)@)'
STEP &: Repeat steps 6 thr&u1h P u+til 3B>)
SRIRAM ENGINEERING
COLLEGE

PERUMALPATTU-602024
www.jntuworld.com
www.jntuworld.com
www.jwjobs.net
CIRCLE:
Ai+'lu,e?st,i&.hB
Ai+'lu,e?'&+i&.hB
Ai+'lu,e?st,li*.hB
Ai+'lu,e?,&s.hB
Ai+'lu,e?1raphi's.hB
A,e(i+e 'e+3 620
A,e(i+e 'e+) 240
A,e(i+e MACC 66R
A,e(i+e MACD 4SR
A,e(i+e M!%C 0
A,e(i+e M!%D 0
v&i, mai+;<
F
v&i, 'ir;i+t a,i+t *,i+t '<E
i+t 1,>DETECT,1mE
i+t 3'e+t,)'e+t,ra,iE
pri+t(;IJ+ 'e+ter '&&r,i+atesI<E
s'a+(;IK,K,I,L3'e+t,L)'e+t<E
pri+t(;IJ+ E+ter the ra,iusI<E
s'a+(;IK,I,Lra,i<E
'lrs'r;<E
i+it1raph;L1,,L1m,II<E
'lear,evi'e;<E
set*.'&l&r;\$#UE<E
SRIRAM ENGINEERING
COLLEGE

PERUMALPATTU-602024
www.jntuworld.com
www.jntuworld.com
www.jwjobs.net
'ir;3'e+t,)'e+t,ra,i<E
1et'h;<E
'l&se1raph;<E
O
v&i, 'ir;i+t 3',i+t )',i+t ra,ius<
F
v&i, pl&tp&i+ts;i+t a,i+t *,i+t ',i+t ,<E
i+t p,3,)E
li+e;1etma33;<M2,0,1etma33;<M2,1etma33;<<E
li+e;0,1etma3);<M2,1etma33;<,1etma3);<M2<E
set*.'&l&r;222<E
3>0E
)>ra,iusE
pl&tp&i+ts;3',)',3,)<E
p>2-ra,iusE
-hile;3?)<
F
i(;p?0<
3@@E
else
F
3@@E
)--E
O
i(;p?0<
SRIRAM ENGINEERING
COLLEGE

PERUMALPATTU-602024
www.jntuworld.com
www.jntuworld.com
www.jwjobs.net
F
p>p@;2N3<@2E
O
else
F
p>p@;2N;3-)<<@2E
O
pl&tp&i+ts;3',)',3,)<E
O
O
v&i, pl&tp&i+ts;i+t 3',i+t )',i+t 3,i+t )<
F
putpi3el;'e+3@3'@3,'e+)-)'@),4<E
putpi3el;'e+3@3'-3,'e+)-)'@),4<E
putpi3el;'e+3@3'@3,'e+)-)'-),4<E
putpi3el;'e+3@3'-3,'e+)-)'-),4<E
putpi3el;'e+3@3'@),'e+)-)'@3,4<E
putpi3el;'e+3@3'-),'e+)-)'@3,4<E
putpi3el;'e+3@3'@),'e+)-)'-3,4<E
putpi3el;'e+3@3'-),'e+)-)'-3,4<E
O
SRIRAM ENGINEERING
COLLEGE

PERUMALPATTU-602024
www.jntuworld.com
www.jntuworld.com
www.jwjobs.net
OUTPUT:
E+ter '&-&r,i+taes :0 0
E+ter the ra,ius:200
SRIRAM ENGINEERING
COLLEGE

PERUMALPATTU-602024
www.jntuworld.com
www.jntuworld.com
www.jwjobs.net
RESULT:
Thus the pr&1ram (&r impleme+tati&+ &( \$rese+hamQs 'ir'le ,ra-i+1 Al1&rithm is
su''ess(ull) '&mpile, a+, e3e'ute,.
ELLIPSE:
AIM:
T& stu,) a+, !mpleme+t \$rese+ham9s Ellipse Dra-i+1 Al1&rithm
ALGORITHM:
STEP \$:!+put r3,r) a+, ellipse 'e+ter ;3',)'< a+, &*tai+ the (irst p&i+t &+ a+ ellipse 'e+tere, &+
the &ri1i+ as ;30,)0<>;0,r<
STEP 2: Cal'ulate the i+itial value &( the ,e'isi&+ parameter i+ re1i&+ 2 as
P20>r2)-r23r)@2M4r23
STEP %: At ea'h 3. p&siti&+ i+ re1i&+ 2,starti+1 at .>0,per(&rm the (&ll&-i+1 test:
!( p2.?0,the +e3t p&i+t al&+1 the ellipse 'e+tere, &+ ;0,0< is ;3.@2,).< a+,
P2.@2>p2.@2r2)3.@2@r2)
Other-ise the +e3t p&i+t al&+1 the 'ir'le is ;3.@2,).-2< a+,
P2.@2>p2.@2r2)3.@2-2r23).@2@r2)
STEP 4:Cal'ulate the i+itial value &( the ,e'isi&+ parameter i+ re1i&+ 2 usi+1 the last p&i+t
;30,)0< 'al'ulate, i+ re1i&+ 2 as
STEP 5:At ea'h p&siti&+ i+ re1i&+ 2,starti+1 at .>0,per(&rm the (&ll&-i+1 test:
!( p2.B0,the +e3t p&i+t al&+1 the ellipse 'e+tere, &+;0,0< is ;3.,).-2< a+,
STEP &:Determi+e the s)mmetr) p&i+ts i+ the &ther three Tua,ra+ts
STEP ':M&ve ea'h 'al'ulate, pi3el p&siti&+;3,)< &+t& the ellipti'al path 'e+tere, &+
;3',)'< a+, pl&t the '&&r,i+ate values: 3>3@30 )>)@)0
STEP (:Re/e-t the steps (&r re1i&+2 u+til 2r2)3B>2r23)
SRIRAM ENGINEERING
COLLEGE

PERUMALPATTU-602024
www.jntuworld.com
www.jntuworld.com
www.jwjobs.net
SRIRAM ENGINEERING
COLLEGE

PERUMALPATTU-602024
www.jntuworld.com
www.jntuworld.com
www.jwjobs.net
ELLIPSE:
Ai+'lu,e?st,i&.hB
Ai+'lu,e?'&+i&.hB
Ai+'lu,e?st,li*.hB
Ai+'lu,e?1raphi's.hB
Ai+'lu,e?math.hB
(l&at p,r32,r)2,3',)',3,),r),r3,tr)2,tr32,p3,p)E
(l&at +aE
i+t rE
i+t err&r'&,eE
v&i, pl&t;<
F
i+t .E
putpi3el;3'@3,)'@),\$#UE<E
putpi3el;3'-3,)'@),\$#UE<E
(&r;.>3'-3E.?>3'@3E.@@<
putpi3el;3'@3,)'-),\$#UE<E
putpi3el;3'-3,)'-),\$#UE<E
O
v&i, *rell;<
F
pl&t;<E
r)2>r)Nr)E
r32>r3Nr3E
tr)2>2Nr)2E
SRIRAM ENGINEERING
COLLEGE

PERUMALPATTU-602024
www.jntuworld.com
www.jntuworld.com
www.jwjobs.net
tr32>2Nr32E
3>0E
)>r)E
pl&t;<E
+a>;(l&at<;rN;2M4<<E
i(;;+a<@0.P?+a<
+a>'eil;+a<E
else
p>r)2-r32Nr)@+aE
p3>0E
p)>tr32N)E
-hile;p3?p)<
F
3@@E
p3@>tr)2E
i(;pB>0<
F
)--E
p)>p)-tr32E
O
i(;p?0<
p@>r)2@p3E
else
p>p@r)2@p3-p)E
pl&t;<E
SRIRAM ENGINEERING
COLLEGE

PERUMALPATTU-602024
www.jntuworld.com
www.jntuworld.com
www.jwjobs.net
O
+a>'eil;;(l&at<;3@0.P<<E
p>r)2N;+a<N;+a<@r32N;)-2<N;)-2<-r32Nr)2E
-hile;)B0<
F
)--E
p)->tr32E
i(;p?0<
F
3@@E
p3@>tr)2E
O
i(;pB0<
p@>r32-p)E
else
p@>r32-p)@p3E
pl&t;2<E
O
1et'h;<E
'l&se1raph;<E
O
v&i, mai+;<
F
i+t 1,>DETECT,1m,err&r'&,eE
'lrs'r;<E
SRIRAM ENGINEERING
COLLEGE

PERUMALPATTU-602024
www.jntuworld.com
www.jntuworld.com
www.jwjobs.net
pri+t(;IJ+ e+ter the 3ra,iusI<E
s'a+(;IK(I,Lr3<E
pri+t(;IJ+ e+ter the )ra,iusI<E
s'a+(;IK(I,Lr)<E
'lrs'r;<E
i+it1raph;L1,,L1m,II<E
set*.'&l&r;S<E
li+e;1etma33;<M2,0,1etma33;<M2,1etma33;<<E
li+e;0,1etma3);<M2,1etma33;<,1etma3);<M2<E
3'>1etma33;<M2E
)'>1etma3);<M2E
*rell;<E
O
SRIRAM ENGINEERING
COLLEGE

PERUMALPATTU-602024
www.jntuworld.com
www.jntuworld.com
www.jwjobs.net
OUTPUT:
E+ter the C ra,ius: 200
E+ter the D ra,ius: 2P0
SRIRAM ENGINEERING
COLLEGE

PERUMALPATTU-602024
www.jntuworld.com
www.jntuworld.com
www.jwjobs.net
RESULT:
Thus the pr&1ram (&r implementation of Bresenhams ellipse drawing Algorithm is
successfully compiled and executed.
E*!N": 2 LIINE, CIRCLE AND ELLIPSE ATTRIBUTES
DATE:
AIM: T& stu,) a+, !mpleme+t &( #i+e attri*utes *) usi+1 &pe+1l.
ALGORITHM:
STEP \$:This pr&1ram ,em&+strates 1e&metri' primitives a+, their attri*utes.
Usi+1 Ope+G# Graphi's #i*rar) t& per(&rm pr&perties &( &utput primitives.
!+'lu,e all hea,er (ile su'h as 1lut.h,st,li*.h et'.,
STEP 2:Usi+1 1lverte32( t& spe'i() p&i+t ,li+e a+, p&l)1&+ verti'es -ithi+ 1l*e1i+ a+, 1le+,
!+itialiUe the pr&perties &( li+e ;'&l&r a+, sha,e<
STEP %:Glsha,em&,el spe'i(ies a s)m*&li' value represe+ti+1 a sha,i+1 te'h+iTue. A''epte, values are
G#/8#AT a+, G#/"MOOT.
STEP 4:Gl'lear;G#/CO#OR-\$U88ER-\$!T< 'lear *u((er t& preset values a+, i+,i'ati+1 the *u((er
'urre+tl) e+a*le, (&r '&l&r -riti+1.
STEP 5:Gle+a*le;< t& spe'i() the e+a*le &r ,isa*le G# 'apa*ilit).
Gli+estipple;Gli+t (a't&r,Patter+< t& spe'i() thr li+e stipple patter+.
STEP &:"et the vie-p&rt 1lvie-p&rt;3,),-,h<
Glmatri3m&,e;G#/pr&7e'ti&+< spe'i()i+1 -hi'h matri3 sta'. is the tar1et (&r su*seTue+t matri3
&perati&+.
STEP ':Gll&a,i,e+tit) (u+'ti&+ repla'es the 'urre+t matri3 -ith the i,e+ti() matri3.
Gl&rth&2, t& spe'i() the 2, &rth&1raphi' vie-i+1 re1i&+
STEP (:!+itialiUe the 1lut li*rar)
!+itialiUe the ,ispla) m&,e G#UT/"!%G#E;*it mas. t& sele't si+1le *u((ere, -i+,&-s< a+,
G#UT/RG\$;*it mas. t& sele't the r1*<
STEP ):Usi+1 !+put eve+ts t& per(&rm (&ll&-i+1 &perati&+
1lutReshape8u+';< - -hat a'ti&+s t& *e ta.e+ -he+ the -i+,&- is resiUe,, m&ve, &r e3p&se,.
SRIRAM ENGINEERING
COLLEGE

PERUMALPATTU-602024
www.jntuworld.com
www.jntuworld.com
www.jwjobs.net
1lutVe)*&ar,8u+';< - li+.s a .e) -ith a r&uti+e that is i+v&.e, -he+ a .e) is presse,. 1lut"pe'ial8u+';<
- 82, 82, 86 et'.
1lutM&use8u+';< - li+.s a m&use *utt&+ -ith a r&uti+e that is i+v&.e, -he+ the m&use *utt&+ is
presse,Mrelease,.
STEP \$0:"t&p the pr&1ram.
PROGRAM:
Ai+'lu,e ?1lut.hB
Ai+'lu,e ?st,li*.hB
A,e(i+e ,ra-O+e#i+e;32,)2,32,)2< 1l\$e1i+;G#/#!%E"<E
1l0erte32( ;;32<,;)2<<E 1l0erte32( ;;32<,;)2<<E 1lE+,;<E
v&i, i+it;v&i,<
F
1lClearC&l&r ;0.0, 0.0, 0.0, 0.0<E
1l"ha,eM&,el ;G#/8#AT<E
O
v&i, ,ispla);v&i,<
F
i+t iE
1lClear ;G#/CO#OR/\$U88ER/\$!T<E
MN sele't -hite (&r all li+es NM
1lC&l&r6( ;2.0, 2.0, 2.0<E
MN i+ 2st r&-, 6 li+es, ea'h -ith a ,i((ere+t stipple NM
1lE+a*le ;G#/#!%E/"T!PP#E<E

1l#i+e"tipple ;2, 030202<E MN ,&tte, NM
,ra-O+e#i+e ;P0.0, 22P.0, 2P0.0, 22P.0<E
1l#i+e"tipple ;2, 030088<E MN ,ashe, NM
,ra-O+e#i+e ;2P0.0, 22P.0, 2P0.0, 22P.0<E
1l#i+e"tipple ;2, 032C4S<E MN ,ashM,&tM,ash NM
,ra-O+e#i+e ;2P0.0, 22P.0, 6P0.0, 22P.0<E
MN i+ 2+, r&-, 6 -i,e li+es, ea'h -ith ,i((ere+t stipple NM
1l#i+e5i,th ;P.0<E
1l#i+e"tipple ;2, 030202<E MN ,&tte, NM
,ra-O+e#i+e ;P0.0, 200.0, 2P0.0, 200.0<E
1l#i+e"tipple ;2, 030088<E MN ,ashe, NM
,ra-O+e#i+e ;2P0.0, 200.0, 2P0.0, 200.0<E
1l#i+e"tipple ;2, 032C4S<E MN ,ashM,&tM,ash NM
SRIRAM ENGINEERING
COLLEGE

PERUMALPATTU-602024
www.jntuworld.com
www.jntuworld.com
www.jwjobs.net
,ra-O+e#i+e ;2P0.0, 200.0, 6P0.0, 200.0<E
1l#i+e5i,th ;2.0<E
MN i+ 6r, r&-, 6 li+es, -ith ,ashM,&tM,ash stipple NM
MN as part &( a si+1le '&++e'te, li+e strip NM
1l#i+e"tipple ;2, 032C4S<E MN ,ashM,&tM,ash NM
1l\$e1i+ ;G#/#!%E/"TR!P<E
(&r ;i > 0E i ? SE i@@<
1l0erte32( ;P0.0 @ ;;G#(l&at< i N P0.0<, SP.0<E
1lE+, ;<E
MN i+ 4th r&-, 6 i+,epe+,e+t li+es -ith same stipple NM
(&r ;i > 0E i ? 6E i@@< F
,ra-O+e#i+e ;P0.0 @ ;;G#(l&at< i N P0.0<, P0.0,
P0.0 @ ;;G#(l&at<;i@2< N P0.0<, P0.0<E
O
MN i+ Pth r&-, 2 li+e, -ith ,ashM,&tM,ash stipple NM
MN a+, a stipple repeat (a't&r &( P NM
1l#i+e"tipple ;P, 032C4S<E MN ,ashM,&tM,ash NM
,ra-O+e#i+e ;P0.0, 2P.0, 6P0.0, 2P.0<E
1lDisa*le ;G#/#!%E/"T!PP#E<E
1l8lush ;<E
O
v&i, reshape ;i+t -, i+t h<
F
1l0ie-p&rt ;0, 0, ;G#siUei< -, ;G#siUei< h<E
1lMatri3M&,e ;G#/PROWECT!O%<E
1l#&a,!,e+tit) ;<E
1luOrth&2D ;0.0, ;G#,&u*le< -, 0.0, ;G#,&u*le< h<E
O
v&i, .e)*&ar,;u+si1+e, 'har .e), i+t 3, i+t )<
F
s-it'h ;.e)< F
'ase 2S:
e3it;0<E
*rea.E
O
O
i+t mai+;i+t ar1', 'harNN ar1v<
F
1lut!+it;Lar1', ar1v<E
1lut!+itDispla)M&,e ;G#UT/"!%G#E X G#UT/RG\$<E
1lut!+it5i+,&-"iUe ;400, 2P0<E
1lut!+it5i+,&-P&siti&+ ;200, 200<E
1lutCreate5i+,&- ;ar1vG0H<E
i+it ;<E
1lutDispla)8u+';,ispla)<E
1lutReshape8u+';reshape<E
1lutVe)*&ar,8u+';.e)*&ar,<E
1lutMai+#&&p;<E
SRIRAM ENGINEERING
COLLEGE

PERUMALPATTU-602024
www.jntuworld.com
www.jntuworld.com
www.jwjobs.net
retur+ 0E
O
OUTPUT:
SRIRAM ENGINEERING
COLLEGE

PERUMALPATTU-602024
www.jntuworld.com
www.jntuworld.com
www.jwjobs.net
RESULT:
Thus the pr&1ram (&r impleme+tati&+ &( #i+e, 'ir'le a+, ellipse Attri*utes are
su''ess(ull) '&mpile, a+, e3e'ute,.
E* N":% 0-1 T2O DIMENSIONAL TRANSFORMATIONS
DATE: TRANSLATION,ROTATION,SCALING,REFLECTION,SHEAR
AIM: T& stu,) a+, !mpleme+t 2D Tra+s(&rmati&+
ALGORITHM:
STEP \$:"tart the pr&1ram
STEP 2:!+'lu,e the reTuire, varia*les a+, the 1raphi's m&,e spe'i()i+1 the path.
STEP %:Get the verti'es a+, 1et the '&&r,i+ates i+ ea'h 'ase.
Get the &pti&+s the+ usi+1 the suite, 'ase t& (&ll&- tra+s(&rmati&+.
STEP 4:!( the &pti&+2 1et the t3 a+, t) '&&r,i+ate the+ per(&rm the tra+slati&+ &perati&+.
P&i+tGiH>p&i+tGiH@t3 a+, p&i+tGi@2H>p&i+tGi@2H@t)
STEP 5:Repeat the &perati&+ (&r all the '&&r,i+ates.
STEP &:!( &pti&+2 1et the s3 a+, s) '&&r,i+ates the+ per(&rm the s'ali+1 &perati&+ p&i+tGiH>p&i+tGiHNs3E
P&i+tGi@2H>p&i+tGi@2HNs)E
STEP ':Repeat the &perati&+ (&r all '&&r,i+ates.
SRIRAM ENGINEERING
COLLEGE

PERUMALPATTU-602024
www.jntuworld.com
www.jntuworld.com
www.jwjobs.net
STEP (:Displa) the &utput result. !( the &pti&+ is 6 1et the r&tati&+ a+1le;r< the+ per(&rm the r&tati&+
&perati&+ a+, (i+, the r2>r2N;6.24<M;2Y0<
P&i+tGiH>p&i+tGiHN'&s;r2<-p&i+tGi@2HNsi+;r2<
P&i+tGi@2H>p&i+tGiHNsi+;r2<-p&i+tGi@2HN'&s;r2<
STEP ):Repeat the &perati&+ (&r all the '&&r,i+ates
STEP \$0:Displa) the &utput result
STEP \$\$:!( the &pti&+ is 4 per(&rm the re(le'ti&+ &perati&+ su'h that
TempGiH>p&i+tGiH
P&i+tGiH>p&i+tGi@2H
P&i+tGi@2H>tempGiH
Repeat the &perati&+ (&r all the '&&r,i+ates.
STEP \$2:i( the &pti&+ is P 1et the sheari+1 '&&r,i+ates a*&ut 3 a3is as sh3 a+, per(&rm the sheari+1
&perati&+ su'h as p&i+tGiH>p&i+tGiH@;sh3Np&i+tGi@2H<E
P&i+tGi@2H>p&i+tGi@2H
Repeat the steps (&r all '&&r,i+ates
STEP \$%:!( &pti&+ 6 e3it the pr&1ram
STEP \$4!:"t&p the pr&1ram.
PROGRAM:
Ai+'lu,e?st,i&.hB
Ai+'lu,e?'&+i&.hB
Ai+'lu,e?math.hB
Ai+'lu,e?1raphi's.hB
v&i, mai+;<
F
i+t 1,>DETECT,1mE
(l&at t3,t),s3,s),thetaE
i+t 3G20H,)G20H,i,+,'h,3(,)(,3r,)r,a+1leE
,&
F
SRIRAM ENGINEERING
COLLEGE

PERUMALPATTU-602024
www.jntuworld.com
www.jntuworld.com
www.jwjobs.net
'lrs'r;<E
pri+t(;IJtJtJtTra+s(&rmati&+ &( a+ O*7e'tI<E
pri+t(;IJ+2.Tra+s(&ramti&+ J+2."'ali+1J+6."'ali+( &( a+ 8i3e,
p&i+tJ+4.R&tai&+J+P.R&tati&+ &( a+ (i3e, p&i+tJ+6.E3itI<E
pri+t(;IJ+ E+ter )&ur 'h&i'eI<E
s'a+(;IK,I,L'h<E
i(;'h>>6<
e3it;6<E
pri+t(;IJ+ E+ter the t&tal +um*er &( verti'esI<E
s'a+(;IK,I,L+<E
pri+t(;IE+ter the '& &r,i+ates:I<E
(&r;i>2Ei?>+Ei@@<
s'a+(;IK,K,I,L3GiH,L)GiH<E
i+it1raph;L1,,L1m,II<E
(&r;i>2Ei?+Ei@@<
li+e;3GiH,)GiH,3Gi@2H,)Gi@2H<E
li+e;3G+H,)G+H,3G2H,)G2H<E
1et'h;<E
'l&se1raph;<E
s-it'h;'h<
F
'ase 2:
pri+t(;IJ+ e+ter the tra+slati&+ ve't&rI<E
s'a+(;IK(K(K(I,Lt3,Lt)<E
(&r;i>2Ei?>+Ei@@<
F
SRIRAM ENGINEERING
COLLEGE

PERUMALPATTU-602024
www.jntuworld.com
www.jntuworld.com
www.jwjobs.net
3GiH>3GiH@t3E
)GiH>)GiH@t)E
O
*rea.E
'ase 2:
pri+t(;IJ+ e+ter the s'ali+1 ve't&rI<E
s'a+(;IK(K(K(I,Ls3,Ls)<E
(&r;i>2Ei?+Ei@@<
F
3GiH>3GiHNs3E
)GiH>)GiHNs)E
O
*rea.E
'ase 6:
pri+t(;IJ+ e+ter the (i3e, p&i+tI<E
s'a+(;IK,K,K,I,Ls3,Ls)<E
(&r;i>2Ei?+Ei@@<
F
3GiH>3GiHNs3@;2-s3<N3(E
)GiH>)GiHNs)@;2-s)<N)(E
O
*rea.E
'ase 4:
pri+t(;IJ+ e+ter the r&tati&+ a+1leI<E
s'a+(;IK,I,a+1le<E
SRIRAM ENGINEERING
COLLEGE

PERUMALPATTU-602024
www.jntuworld.com
www.jntuworld.com
www.jwjobs.net
theta>;6.24M2Y0<Na+1leE
(&r;i>2Ei?+Ei@@<
F
3GiH>3GiHN'&s;theta<@)GiHNsi+;theta<E
)GiH>-3GiHNsi+;theta<@)GiHN'&s;theta<E
O
*rea.E
'ase P:
pri+t(;IJ+ e+ter the pil&t +um*erI<E
s'a+(;IK,K,I,L3r,L)r<E
pri+t(;IJ+ e+ter the r&tati&+ a+1leI<E
s'a+(;IK,I,a+1le<E
theta>;6.24M2Y0<N;(l&at<a+1leE
(&r;i>2Ei?>+Ei@@<
F
3GiH>3r@;3GiH-3r<N'&s;theta<@;)GiH-)r<Nsi+;theta<E
)GiH>)r@;3GiH-3r<Nsi+;theta<@;)GiH-)r<N'&s;theta<E
O
*rea.E
,e(ault:
e3it;0<E
O
i+it1raph;L1,,L1m,II<E
pri+t(;IJ+ A(ter tra+s(&rmati&+I<E
(&r;i>2Ei?+Ei@@<
SRIRAM ENGINEERING
COLLEGE

PERUMALPATTU-602024
www.jntuworld.com
www.jntuworld.com
www.jwjobs.net
li+e;3GiH,)GiH,3Gi@2H,)Gi@2H<E
li+e;3G+H,)G+H,3G2H,)G2H<E
1et'h;<E
'l&se1raph;<E
O
-hile;'hZ>6<E
O
OUTPUT:
Tra+s(&rmati&+ &( a+ O*7e't
2. Tra+s(&rmati&+
2. "'ali+1
6. "'ali+1 &( a (i3e, p&i+t
4. R&tati&+
P. R&tati&+ &( a (i3e, p&i+te3it
E+ter )&ur 'h&i'e: 2
SRIRAM ENGINEERING
COLLEGE

PERUMALPATTU-602024
www.jntuworld.com
www.jntuworld.com
www.jwjobs.net
E+ter the t&tal +& &( verti'es:6
E+ter the '&-&r,i+ates:20 20
0 P0
P0 0
E+ter the tra+slati&+ ve't&r: 20 20
20 20
A(ter tra+s(&rmati&+:
Tra+s(&rmati&+ &( a+ O*7e't
2. Tra+s(&rmati&+
2. "'ali+1
6. "'ali+1 &( a (i3e, p&i+t
4. R&tati&+
P. R&tati&+ &( a (i3e, p&i+te3it
E+ter )&ur 'h&i'e: 2
E+ter the t&tal +& &( verti'es:6
E+ter the '&-&r,i+ates:20 20
0 P0
P0 0
SRIRAM ENGINEERING
COLLEGE

PERUMALPATTU-602024
www.jntuworld.com
www.jntuworld.com
www.jwjobs.net
E+ter the s'ali+1 ve't&r: 20 20
20 20
A(ter tra+s(&rmati&+:
Tra+s(&rmati&+ &( a+ O*7e't
2. Tra+s(&rmati&+
2. "'ali+1
6. "'ali+1 &( a (i3e, p&i+t
4. R&tati&+
P. R&tati&+ &( a (i3e, p&i+te3it
SRIRAM ENGINEERING
COLLEGE

PERUMALPATTU-602024
www.jntuworld.com
www.jntuworld.com
www.jwjobs.net
E+ter )&ur 'h&i'e: 6
E+ter the t&tal +& &( verti'es:6
E+ter the '&-&r,i+ates:20 20
0 P0
P0 0
E+ter the (i3e, p&i+t:20 20 60
A(ter tra+s(&rmati&+:
Tra+s(&rmati&+ &( a+ O*7e't
2. Tra+s(&rmati&+
2. "'ali+1
6. "'ali+1 &( a (i3e, p&i+t
4. R&tati&+
SRIRAM ENGINEERING
COLLEGE

PERUMALPATTU-602024
www.jntuworld.com
www.jntuworld.com
www.jwjobs.net
P. R&tati&+ &( a (i3e, p&i+te3it
E+ter )&ur 'h&i'e: 6
E+ter the t&tal +& &( verti'es:4
E+ter the '&-&r,i+ates:20 20
0 P0
P0 0
E+ter the r&tati+1 a+1le:20
A(ter tra+s(&rmati&+:
Tra+s(&rmati&+ &( a+ O*7e't
2. Tra+s(&rmati&+
2. "'ali+1
6. "'ali+1 &( a (i3e, p&i+t
4. R&tati&+
P. R&tati&+ &( a (i3e, p&i+te3it
E+ter )&ur 'h&i'e: 6
E+ter the t&tal +& &( verti'es:P
SRIRAM ENGINEERING
COLLEGE

PERUMALPATTU-602024
www.jntuworld.com
www.jntuworld.com
www.jwjobs.net
E+ter the '&-&r,i+ates:20 20
0 P0
P0 0
E+ter the (i3e, p&i+t: 0 20
0 20
E+ter the r&tati+1 a+1le:20
RESULT:
Thus the pr&1ram (&r impleme+tati&+ &( T-& ,ime+si&+al Tra+s(&rmati&+s (&r
Tra+s(&rmati&+, "'ali+1 a+, R&tati&+ are su''ess(ull) '&mpile, a+, e3e'ute,.
E* N":% 031 REFLECTION OF AN OB4ECT
P"g-.:
SRIRAM ENGINEERING
COLLEGE

PERUMALPATTU-602024
www.jntuworld.com
www.jntuworld.com
www.jwjobs.net
Ai+'lu,e ?st,i&.hB
Ai+'lu,e?'&+i&.hB
Ai+'lu,e?math.hB
Ai+'lu,e?1raphi's.hB
i+t 30>620,)0>240E
i+t tG6HG2H>F200,200,2P0,P0,P0,P0OE
v&i, mai+;<
F
i+t 1,>DETECT,1m,'hE
i+it1raph;L1,,L1m,I I<E
'lrs'r;<E
,&
F
'lear,evi'e;<E
1&t&3);2,2<E
pri+t(;IJtJt re(le'ti&+ &( a+ &*7e'tJtJtI<E
pri+t(;IJ+2.&ri1i+alJ+2.re(le'ti&+J+6.e3itI<E
pri+t(;IJ+ e+ter ur 'h&i'e:I<E
s'a+(;IK,I,L'h<E
i(;'h>>2<
F
'lear,evi'e;<E
tria+1le;0,2,2,2<E
1et'h;<E
O
SRIRAM ENGINEERING
COLLEGE

PERUMALPATTU-602024
www.jntuworld.com
www.jntuworld.com
www.jwjobs.net
i(;'h>>2<
re(le't;<E
O
-hile;'hZ>6<E
O
tria+1le;i+t i,i+t 7,i+t r3,i+t r)<
F
li+e;30,0,30,2N)0<E
li+e;0,)0,2N30,)0<E
li+e;30@tG0HGiHNr3,)0-tG0HG7HNr),30@tG2HGiHNr3,)0-tG2HG7HNr)<E
li+e;30@tG2HGiHNr3,)0-tG2HG7HNr),30@tG2HGiHNr3,)0-tG2HG7HNr)< E
li+e;30@tG2HGiHNr3,)0-tG2HG7HNr),30@tG0HGiHNr3,)0-tG0HG7HNr)< E
retur+E
O
re(le't;<
F
i+t 'hE
pri+t(;IJ+ 2.3 asesJ+ 2.) a3isJ+ 6.&r1i+J+4.)>3J+P.)>-3I<E
pri+t(;Ie+ter ur 'h&i'eI<E
s'a+(;IK,I,L'h<E
'lear,evi'e;<E
tria+1le;0,2,2,2<E
i(;'h>>2<
tria+1le;0,2,2,-2<E
SRIRAM ENGINEERING
COLLEGE

PERUMALPATTU-602024
www.jntuworld.com
www.jntuworld.com
www.jwjobs.net
i(;'h>>2<
tria+1le;0,2,-2,2<E
i(;'h>>6<
tria+1le;0,2,-2,-2<E
i(;'h>>4<
tria+1le;2,0,2,2<E
i(;'h>>P<
tria+1le;2,0,-2,-2<E
1et'h;<E
retur+E
O
SRIRAM ENGINEERING
COLLEGE

PERUMALPATTU-602024
www.jntuworld.com
www.jntuworld.com
www.jwjobs.net
OUTPUT:
Re(le'ti&+ &( a+ &*7e't
Ori1i+al
Re(le'ti&+
E3it
E+ter )&ur 'h&i'e:2
Re(le'ti&+ &( a+ &*7e't
2.Ori1i+al
2.Re(le'ti&+
6.E3it
E+ter )&ur 'h&i'e:2
3 4 a3is
) 4 a3is
&ri1i+
)>3
)>-3 E+ter ur 'h&i'e : 2
SRIRAM ENGINEERING
COLLEGE

PERUMALPATTU-602024
www.jntuworld.com
www.jntuworld.com
www.jwjobs.net
Re(le'ti&+ &( a+ &*7e't
2.Ori1i+al
2.Re(le'ti&+
6.E3it
E+ter )&ur 'h&i'e:2
2.3 4 a3is
2.) 4 a3is
6.&ri1i+
4.)>3
P.)>-3 E+ter ur 'h&i'e : 2
Re(le'ti&+ &( a+ &*7e't
2.Ori1i+al
2.Re(le'ti&+
6.E3it
E+ter )&ur 'h&i'e:2
2.3 4 a3is
2.) 4 a3is
6.&ri1i+
4.)>3
SRIRAM ENGINEERING
COLLEGE

PERUMALPATTU-602024
www.jntuworld.com
www.jntuworld.com
www.jwjobs.net
P.)>-3 E+ter ur 'h&i'e : 6
Re(le'ti&+ &( a+ &*7e't
2.Ori1i+al
2.Re(le'ti&+
6.E3it
E+ter )&ur 'h&i'e:2
2.3 4 a3is
2.) 4 a3is
6.&ri1i+
4.)>3
P.)>-3 E+ter ur 'h&i'e : 4
Re(le'ti&+ &( a+ &*7e't
SRIRAM ENGINEERING
COLLEGE

PERUMALPATTU-602024
www.jntuworld.com
www.jntuworld.com
www.jwjobs.net
2.Ori1i+al
2.Re(le'ti&+
6.E3it
E+ter )&ur 'h&i'e:2
2.3 4 a3is
2.) 4 a3is
6.&ri1i+
4.)>3
P.)>-3 E+ter ur 'h&i'e : P
SRIRAM ENGINEERING
COLLEGE

PERUMALPATTU-602024
www.jntuworld.com
www.jntuworld.com
www.jwjobs.net
RESULT:
Thus the pr&1ram (&r impleme+tati&+ &( T-& ,ime+si&+al Tra+s(&rmati&+s (&r
Re(le'ti&+ are su''ess(ull) '&mpile, a+, e3e'ute,.
E* N":%051 SHEARING OF AN OB4ECT
P"g-.:
Ai+'lu,e?st,i&.hB
Ai+'lu,e?'&+i&.hB
Ai+'lu,e?1raphi's.hB
v&i, mai+;<
F
i+t 1,>DETECT,1m,3G20H,)G20H,32G20H,)2G20H,i,+,s,'hE
'lrs'r;<E
pri+t(;IJtJtJt"EAR TRA%"8ORMAT!O%J+I<E
pri+t(;IJtJtJtJ-------------------I<E
pri+t(;IJ+E+ter the t&tal +um*er &( veti'es:I<E
s'a+(;IJ+K,I,L+<E
pri+t(;IJ+E+ter the '&-&r,ei+atesI<E
(&r;i>2Ei?>+Ei@@<
s'a+(;IK,K,I,L3GiH,L)GiH<E
i+it1raph;L1,,L1m,I I<E
start:
'lear,evi'e;<E
SRIRAM ENGINEERING
COLLEGE

PERUMALPATTU-602024
www.jntuworld.com
www.jntuworld.com
www.jwjobs.net
(&r;i>2Ei?>+Ei@@<
F
32GiH>3GiHE
)2GiH>)GiHE
O
(&r;i>2Ei?+Ei@@<
li+e;32GiH,)2GiH,32Gi@2H,)2Gi@2H<E
li+e;32G+H,)2G+H,32G2H,)2G2H<E
1&t&3);2,2<E
pri+t(;IJ+2.C-"EARJ+2.D-"EARJ+6.EC!TI<E
pri+t(;IJ+E+ter )&ur 'h&i'eI<E
s-it'h;'h<
F
'ase 2:
1&t&3);2,P<E
pri+t(;IJ+E+ter the sheari+1 ,ista+'eI<E
s'a+(;IK,I,Ls<E
(&r;i>0Ei?+M2Ei@@<
32GiH@>sE
*rea.E
'ase 2:
1&t&3);2,P<E
pri+t(;IJ+E+ter the sheari+1 ,ista+'eI<E
s'a+(;IK,I,Ls<E
(&r;i>2Ei?>+M2Ei@@<
SRIRAM ENGINEERING
COLLEGE

PERUMALPATTU-602024
www.jntuworld.com
www.jntuworld.com
www.jwjobs.net
)2GiH@>sE
*rea.E
'ase 6:
e3it;0<E
*rea.E
,e(ault:
pri+t(;IJ+E+ter a vali, 'h&i'eI<E
1et'h;<E
1&t& startE
*rea.E
O
(&r;i>2Ei?+Ei@@<
li+e;32GiH,)2GiH,32Gi@2H,)2Gi@2H<E
li+e;32G+H,)G+H,32G2H,)2G2H<E
1et'h;<E
1&t& startE
O
SRIRAM ENGINEERING
COLLEGE

PERUMALPATTU-602024
www.jntuworld.com
www.jntuworld.com
www.jwjobs.net
OUTPUT:
"EAR TRA%"8ORMAT!O%
E+ter the t&tal +um*er &( 0erti'es: 6
E+ter the '&-&r,i+ates:20 20
0 P0
P0 0
2.C-"EAR
2.D-"EAR
6.EC!T
E+ter )&ur 'h&i'e:2
E+ter the sheari+1 ,ista+'e: P0
SRIRAM ENGINEERING
COLLEGE

PERUMALPATTU-602024
www.jntuworld.com
www.jntuworld.com
www.jwjobs.net
2.C-"EAR
2.D-"EAR
6.EC!T
E+ter )&ur 'h&i'e:2
E+ter the sheari+1 ,ista+'e: P0
SRIRAM ENGINEERING
COLLEGE

PERUMALPATTU-602024
www.jntuworld.com
www.jntuworld.com
www.jwjobs.net
RESULT:
Thus the pr&1ram (&r impleme+tati&+ &( T-& ,ime+si&+al Tra+s(&rmati&+s (&r "heari+1
&( a+ &*7e't is su''ess(ull) '&mpile, a+, e3e'ute,.
SRIRAM ENGINEERING
COLLEGE

PERUMALPATTU-602024
www.jntuworld.com
www.jntuworld.com
www.jwjobs.net
SRIRAM ENGINEERING
COLLEGE

PERUMALPATTU-602024
www.jntuworld.com
www.jntuworld.com
www.jwjobs.net
E* N": COHEN SUTHERLAND 2D
DATE: LINE CLIPPING AND 2INDO2ING
AIM:
T& stu,) a+, !mpleme+t C&he+ "utherla+, 2D li+e 'lippi+1 a+, 5i+,&-i+1
ALOGRITHM:
pr&'e,ure C&he+"utherla+,#i+eClipA+,Dra-;
30,)0,32,)2,3mi+,3ma3,)mi+,)ma3 : real E value: i+te1er<E
F C&he+-"utherla+, 'lippi+1 al1&rithm (&r li+e P0>;32,)0< t& P2>;32,)2<
a+, 'lip re'ta+1le -ith ,ia1&+al (r&m ;3mi+,)mi+< t& ;3ma3,)ma3<.O
t)pe
e,1e > ;#E8T,R!GT,\$OTTOM,TOP<E
&ut'&,e > set &( e,1eE
var
a''ept,,&+e : *&&lea+E
&ut'&,e0,&ut'&,e2,&ut'&,eOut : &ut'&,eE
FOut'&,es (&r P0,P2, a+, -hi'hever p&i+t lies &utsi,e the 'lip re'ta+1leO
3,) : realE
pr&'e,ure C&mpOutC&,e;3,): realE var '&,e:&ut'&,e<E
FC&mpute &ut'&,e (&r the p&i+t ;3,)< O
*e1i+
'&,e :> GHE
i( ) B )ma3 the+ '&,e :> GTOPH
else i( ) ? )mi+ the+ '&,e :> G\$OTTOMHE
i( 3 B 3ma3 the+ '&,e :> '&,e @GR!GTH
else i( 3 ? 3mi+ the+ '&,e :> '&,e @G#E8TH
e+,E
*e1i+
a''ept :> (alseE ,&+e :> (alseE
C&mpOutC&,e ;30,)0,&ut'&,e0<E C&mpOutC&,e ;32,)2,&ut'&,e2<E
repeat
i(;&ut'&,e0>GH< a+, ;&ut'&,e2>GH< the+ FTrivial a''ept a+, e3itO
*e1i+ a''ept :> trueE ,&+e:>true e+,
else i( ;&ut'&,e0N&ut'&,e2< ?B GH the+
,&+e :> true F#&1i'al i+terse'ti&+ is true, s& trivial re7e't a+, e3i
t.O
else
F8aile, *&th tests, s& 'al'ulate the li+e se1me+t t& 'lipE
(r&m a+ &utsi,e p&i+t t& a+ i+terse'ti&+ -ith 'lip e,1e.O
*e1i+
FAt least &+e e+,p&i+t is &utsi,e the 'lip re'ta+1leE pi'. it.O
i( &ut'&,e0 ?B GH the+
&ut'&,eOut :> &ut'&,e0 else &ut'&,eOut :> &ut'&,e2E
F%&- (i+, i+terse'ti&+ p&i+tE
use (&rmulas )>)0@sl&peN;3-30<,3>30@;2Msl&pe<N;)-)0<.O
SRIRAM ENGINEERING
COLLEGE

PERUMALPATTU-602024
www.jntuworld.com
www.jntuworld.com
www.jwjobs.net
i( TOP i+ &ut'&,eOut the+
*e1i+ FDivi,e li+e at t&p &( 'lip re'ta+1leO
3 :> 30 @ ;32 - 30< N ;)ma3 - )0< M ;)2 - )0<E
) :> )ma3
e+,
i( \$OTTOM i+ &ut'&,eOut the+
*e1i+ FDivi,e li+e at *&tt&m &( 'lip re'ta+1leO
3 :> 30 @ ;32 - 30< N ;)mi+ - )0< M ;)2 - )0<E
) :> )ma3
e+,
else i( R!GT i+ &ut'&,eOut the+
*e1i+ FDivi,e li+e at ri1ht e,1e &( 'lip re'ta+1leO
) :> )0 @ ;)2 - )0< N ;3ma3 - 30< M ;32 - 30<E
3 :> 3ma3
e+,
else i( #E8T i+ &ut'&,eOut the+
*e1i+ FDivi,e li+e at le(t e,1e &( 'lip re'ta+1leO
) :> )0 @ ;)2 - )0< N ;3mi+ - 30< M ;32 - 30<E
3 :> 3mi+
e+,E
F%&- -e m&ve &utsi,e p&i+t t& i+terse'ti&+ p&i+t t& 'lip,
a+, 1et rea,) (&r +e3t pass.O
i( ;&ut'&,eOut > &ut'&,e0< the+
*e1i+
30 :> 3E )0 :> )E C&mpOutC&,e;30,)0,&ut'&,e0<
e+,
else
*e1i+
32 :> 3E )2 :> )E C&mpOutC&,e;32,)2,&ut'&,e2<E
e+,
e+, Fsu*,ivi,eO
u+til ,&+eE
i( a''ept the+ Mi,p&i+t#i+eReal;30,)0,32,)2,value< F0ersi&+ (&r real '&&r,i+
atesO
e+,E FC&he+"utherla+,#i+eClipA+,Dra-O
SRIRAM ENGINEERING
COLLEGE

PERUMALPATTU-602024
www.jntuworld.com
www.jntuworld.com
www.jwjobs.net
P"g-.:
Ai+'lu,e?st,i&.hB
Ai+'lu,e?1raphi's.hB
Ai+'lu,e?'&+i&.hB
t)pe,e( u+si1+e, i+t &ut'&,eE
e+um F TOP>032, \$OTTOM>032, R!GT>034, #E8T>03Y OE
v&i, li+e'lip;30,)0,32,)2,3-mi+,)-mi+,3-ma3,)-ma3 <
(l&at 30,)0,32,)2,3-mi+,)-mi+,3-ma3,)-ma3E
F
i+t 1,,1mE
&ut'&,e '&,e0,'&,e2,'&,e&utE
i+t a''ept > 0, ,&+e>0E
'&,e0 > 'al'&,e;30,)0,3-mi+,)-mi+,3-ma3,)-ma3<E
'&,e2 > 'al'&,e;32,)2,3-mi+,)-mi+,3-ma3,)-ma3<E
,&F
i(;Z;'&,e0 X '&,e2<<
F a''ept >2 E ,&+e >2E O
else
i(;'&,e0 L '&,e2< ,&+e > 2E
else
F
(l&at 3,)E
'&,e&ut > '&,e0 [ '&,e0 : '&,e2E
i(;'&,e&ut L TOP<
SRIRAM ENGINEERING
COLLEGE

PERUMALPATTU-602024
www.jntuworld.com
www.jntuworld.com
www.jwjobs.net
F
3 > 30 @ ;32-30<N;)-ma3-)0<M;)2-)0<E
) > )-ma3E
O
else
i(; '&,e&ut L \$OTTOM<
F
3 > 30 @ ;32-30<N;)-mi+-)0<M;)2-)0<E
) > )-mi+E
O
else
i( ; '&,e&ut L R!GT<
F
) > )0@;)2-)0<N;3-ma3-30<M;32-30<E
3 > 3-ma3E
O
else
F
) > )0 @ ;)2-)0<N;3-mi+-30<M;32-30<E
3 > 3-mi+E
O
i(; '&,e&ut >> '&,e0<
F
30 > 3E )0 > )E
'&,e0>'al'&,e;30,)0,3-mi+,)-mi+,3-ma3,)-ma3<E
SRIRAM ENGINEERING
COLLEGE

PERUMALPATTU-602024
www.jntuworld.com
www.jntuworld.com
www.jwjobs.net
O
else
F
32 > 3E )2 > )E
'&,e2 > 'al'&,e;32,)2,3-mi+,)-mi+,3-ma3,)-ma3<E
O
O
O -hile; ,&+e >> 0<E
i(;a''ept< li+e;30,)0,32,)2<E
re'ta+1le;3-mi+,)-mi+,3-ma3,)-ma3<E
1et'h;<E
O
i+t 'al'&,e ;3,),3-mi+,)-mi+,3-ma3,)-ma3<
(l&at 3,),3-mi+,)-mi+,3-ma3,)-ma3E
F
i+t '&,e >0E
i(;)B )-ma3<
'&,e X>TOPE
else i(; )?)-mi+<
'&,e X> \$OTTOME
else i(;3 B 3-ma3<
'&,e X> R!GTE
else i( ; 3? 3-mi+<
'&,e X> #E8TE
retur+;'&,e<E
SRIRAM ENGINEERING
COLLEGE

PERUMALPATTU-602024
www.jntuworld.com
www.jntuworld.com
www.jwjobs.net
O
mai+;<
F
(l&at 32,)2,32,)2,3-mi+,)-mi+,3-ma3,)-ma3E
i+t 1,>DETECT,1mE
'lrs'r;<E
i+it1raph;L1,,L1m,I':JJt'JJ*1iI<E
pri+t(;IJ+J+JtE+ter the '&-&r,i+ates &( #i+e :I<E
pri+t(;IJ+J+JtC2 D2 : I<E
s'a+(;IK( K(I,L32,L)2<E
pri+t(;IJ+J+JtC2 D2 : I<E
s'a+(;IK( K(I,L32,L)2<E
pri+t(;IJ+JtE+ter the '&/&r,i+ates &( -i+,&- :J+ I<E
pri+t(;IJ+Jt3-mi+ , )-mi+ : I<E
s'a+(;IK( K(I,L3-mi+,L)-mi+<E
pri+t(;IJ+Jt3-ma3 , )-ma3 : I<E
s'a+(;IK( K(I,L3-ma3,L)-ma3<E
'lrs'r;<E
li+e;32,)2,32,)2<E
re'ta+1le;3-mi+,)-mi+,3-ma3,)-ma3<E
1et'h;<E
'lrs'r;<E
li+e'lip;32,)2,32,)2,3-mi+,)-mi+,3-ma3,)-ma3 <E
1et'h;<E
'l&se1raph;<E
SRIRAM ENGINEERING
COLLEGE

PERUMALPATTU-602024
www.jntuworld.com
www.jntuworld.com
www.jwjobs.net
O
OUTPUT:
E+ter the C0-&r,i+ates &( li+e
32,)2: 2P0 200
32,)2: 600 4P0
E+ter the C&-&r,i+ates &( -i+,&-
3-mi+,)-mi+: 2P0 2Y0
3-ma3,)-ma3: 420 6R0
\$e(&re Clippi+1
A(ter Clippi+1
SRIRAM ENGINEERING
COLLEGE

PERUMALPATTU-602024
www.jntuworld.com
www.jntuworld.com
www.jwjobs.net
RESULT:
Thus the pr&1ram (&r impleme+tati&+ &( C&he+ "utherla+, 2-D #i+e, Clippi+1 a+,
5i+,&-i+1 are su''ess(ull) '&mpile, a+, e3e'ute,.
DATE:
AIM: T& stu,) a+, impleme+t "utherla+, 4 &,1ema+ p&l)1&+ 'lippi+1 usi+1 TC.
ALGORITHM:
t)pe
verte3 > p&i+tE Fp&i+t h&l, real 3,)O
e,1e > arra)G2..2H &( verte3E
verte3Arra) > arra)G2..MACH &( verte3E FMAC is a ,e'lare, '&+sta+tO
pr&'e,ure "utherla+,&,1ma+P&l)1&Clip ;
i+0erte3Arra) : verte3Arra)E F!+put verte3 arra)O
var &ut0erte3Arra) : verte3Arra)E FOutput verte3 arra)O
i+#e+1th : i+te1erE F%um*er &( e+tries i+ i+0erte3Arra)O
var &ut#e+1th : i+te1erE F%um*er &( e+tries i+ &ut0erte3Arra)O
'lip\$&u+,ar) : e,1e<E FE,1e &( 'lip p&l)1&+O
var
s,p, F"tart, e+, p&i+t &( 'urre+t p&l)1&+ e,1eO
i : verte3E F!+terse'ti&+ p&i+t -ith a 'lip *&u+,ar)O
7 : i+te1erE F0erte3 l&&p '&u+terO
pr&'e,ure Output;
+e-0erte3 : verte3E
var &ut#e+1th : i+te1erE var &ut0erte3Arra) : verte3Arra)<E
FA,,s +e-0erte3 t& &ut0erte3Arra) a+, the+ up,ates &ut#e+1th O
*e1i+
...
e+,E
(u+'ti&+ !+si,e;test0erte3 : verte3E 'lip\$&u+,ar) : e,1e<:*&&lea+E
FChe'.s -hether the verte3 lies i+si,e the 'lip e,1e &r +&tO
*e1i+
SRIRAM ENGINEERING
COLLEGE

PERUMALPATTU-602024
www.jntuworld.com
www.jntuworld.com
www.jwjobs.net
E# NO! SUTHERLAND HODGEMAN POL6GON CLIPPING
...
e+,E
pr&'e,ure !+terse't;(irst,se'&+,:verte3E 'lip\$&u+,ar):e,1eE
var i+terse'tPt:verte3<E
FClips p&l)1&+ e,1e ;(irst,se'&+,< a1ai+st 'lip\$&u+,ar), &utputs the
+e- p&i+tO
*e1i+
...
e+,E
*e1i+
&ut#e+1th :> 0E
s :> i+0erte3Arra)Gi+#e+1thHE
F"tart -ith the last verte3 i+ i+0erte3Arra)O
(&r 7 :> 2 t& i+#e+1th ,&
*e1i+
p :> i+0erte3Arra)G7HE F%&- s a+, p '&rresp&+, t& the verti'es i+
8i1. 6.4YO
i( !+si,e;p,'lip\$&u+,ar)< the+ FCases 2 a+, 4O
i( !+si,e;s, 'lip\$&u+,ar)< the+
Output;p, &ut#e+1th, &ut0erte3Arra)< FCase 2O
else
*e1i+
!+terse't;s, p, 'lip\$&u+,ar), i<E
Output;i, &ut#e+1th, &ut0erte3Arra)<E
Output;p, &ut#e+1th, &ut0erte3Arra)<
e+,
else FCases 2 a+, 6O
i( !+si,e;s, 'lip\$&u+,ar)< the+ FCases 2O
*e1i+
!+terse't;s, p, 'lip\$&u+,ar), i<E
Output;i, &ut#e+1th, &ut0erte3Arra)<
e+,E F%& a'ti&+ (&r 'ase 6O
s :> p FA,va+'e t& +e3t pair &( verti'esO
e+, F(&rO
end; {SutherlandHodgmanPolygonClip
SRIRAM ENGINEERING
COLLEGE

PERUMALPATTU-602024
www.jntuworld.com
www.jntuworld.com
www.jwjobs.net
P"g-.:
Ai+'lu,e?i&stream.hB
Ai+'lu,e?1raphi's.hB
Ai+'lu,e?'&+i&.hB
A,e(i+e r&u+,;a<;;i+t<;a@0.P<<
i+t .E
(l&at 3mi+,)mi+,3ma3,)ma3,arrG20H,mE
v&i, 'lipl;(l&at 32,(l&at )2,(l&at 32,(l&at )2<
F
i(;32-32<
m>;)2-)2<M;32-32<E
else
m>200000E
i(;32B>3mi+ LL 32B>3mi+<
F
arrG.H>32E
SRIRAM ENGINEERING
COLLEGE

PERUMALPATTU-602024
www.jntuworld.com
www.jntuworld.com
www.jwjobs.net
arrG.@2H>)2E
.@>2E
O
i(;32?3mi+ LL 32B>3mi+<
F
arrG.H>3mi+E
arrG.@2H>)2@mN;3mi+-32<E
arrG.@2H>32E
arrG.@6H>)2E
.@>4E
O
i(;32B>3mi+ LL 32?3mi+<
F
arrG.H>3mi+E
arrG.@2H>)2@mN;3mi+-32<E
.@>2E
O
O
v&i, 'lipt;(l&at 32,(l&at )2,(l&at 32,(l&at )2<
F
i(;)2-)2<
m>;32-32<M;)2-)2<E
else
m>200000E
i(;)2?>)ma3 LL )2?>)ma3<
SRIRAM ENGINEERING
COLLEGE

PERUMALPATTU-602024
www.jntuworld.com
www.jntuworld.com
www.jwjobs.net
F
arrG.H>32E
arrG.@2H>)2E
.@>2E
O
i(;)2B)ma3 LL )2?>)ma3<
F
arrG.H>32@mN;)ma3-)2<E
arrG.@2H>)ma3E
arrG.@2H>32E
arrG.@6H>)2E
.@>4E
O
i(;)2?>)ma3 LL )2B)ma3<
F
arrG.H>32@mN;)ma3-)2<E
arrG.@2H>)ma3E
.@>2E
O
O
v&i, 'lipr;(l&at 32,(l&at )2,(l&at 32,(l&at )2<
F
i(;32-32<
m>;)2-)2<M;32-32<E
else
SRIRAM ENGINEERING
COLLEGE

PERUMALPATTU-602024
www.jntuworld.com
www.jntuworld.com
www.jwjobs.net
m>200000E
i(;32?>3ma3 LL 32?>3ma3<
F
arrG.H>32E
arrG.@2H>)2E
.@>2E
O
i(;32B3ma3 LL 32?>3ma3<
F
arrG.H>3ma3E
arrG.@2H>)2@mN;3ma3-32<E
arrG.@2H>32E
arrG.@6H>)2E
.@>4E
O
i(;32?>3ma3 LL 32B3ma3<
F
arrG.H>3ma3E
arrG.@2H>)2@mN;3ma3-32<E
.@>2E
O
O
v&i, 'lip*;(l&at 32,(l&at )2,(l&at 32,(l&at )2<
F
i(;)2-)2<
SRIRAM ENGINEERING
COLLEGE

PERUMALPATTU-602024
www.jntuworld.com
www.jntuworld.com
www.jwjobs.net
m>;32-32<M;)2-)2<E
else
m>200000E
i(;)2B>)mi+ LL )2B>)mi+<
F
arrG.H>32E
arrG.@2H>)2E
.@>2E
O
i(;)2?)mi+ LL )2B>)mi+<
F
arrG.H>32@mN;)mi+-)2<E
arrG.@2H>)mi+E
arrG.@2H>32E
arrG.@6H>)2E
.@>4E
O
i(;)2B>)mi+ LL )2?)mi+<
F
arrG.H>32@mN;)mi+-)2<E
arrG.@2H>)mi+E
.@>2E
O
O
v&i, mai+;<
SRIRAM ENGINEERING
COLLEGE

PERUMALPATTU-602024
www.jntuworld.com
www.jntuworld.com
www.jwjobs.net
F
i+t 1,river>DETECT,1m&,eE
i+t +,p&l)G20HE
(l&at 3i,)i,3(,)(,p&l))G20HE
'lrs'r;<E
'&ut??I C&&r,i+ates &( re'ta+1ular 'lip -i+,&- :J+ 3mi+,)mi+ :IE
'i+BB3mi+BB)mi+E
'&ut??I3ma3,)ma3 :IE
'i+BB3ma3BB)ma3E
'&ut??IJ+J+ P&l)1&+ t& *e 'lippe,: J+ %& &( si,es :IE
'i+BB+E
'&ut??IE+ter the '&&r,i+ates:IE
(&r;i+t i>0Ei?2N+Ei@@<
'i+BBp&l))GiHE
p&l))GiH>p&l))G0HE
p&l)Gi@2H>p&l))G2HE
(&r;i>0Ei?2N+@2Ei@@<
p&l)GiH>r&u+,;p&l))GiH<E
i+it1raph;L1,river,L1m&,e,I':JJt'JJ*1iI<E
set'&l&r;RED<E
re'ta+1le;3mi+,)ma3,3ma3,)mi+<E
'&ut??IJtJt U%C#!PPED PO#DGO%IE
set'&l&r;5!TE<E
(illp&l);+,p&l)<E
1et'h;<E
SRIRAM ENGINEERING
COLLEGE

PERUMALPATTU-602024
www.jntuworld.com
www.jntuworld.com
www.jwjobs.net
'lear,evi'e;<E
.>0E
(&r;i>0Ei?2N+Ei@>2<
'lipl;p&l))GiH,p&l))Gi@2H,p&l))Gi@2H,p&l))Gi@6H<E
+>.M2E
(&r;i>0Ei?.Ei@@<
p&l))GiH>arrGiHE
p&l))GiH>p&l))G0HE
p&l))Gi@2H>p&l))G2HE
.>0E
(&r;i>0Ei?2N+Ei@>2<
'lipt;p&l))GiH,p&l))Gi@2H,p&l))Gi@2H,p&l))Gi@6H<E
+>.M2E
(&r;i>0Ei?.Ei@@<
p&l))GiH>arrGiHE
p&l))GiH>p&l))G0HE
p&l))Gi@2H>p&l))G2HE
.>0E
(&r;i>0Ei?2N+Ei@>2<
'lipr;p&l))GiH,p&l))Gi@2H,p&l))Gi@2H,p&l))Gi@6H<E
+>.M2E
(&r;i>0Ei?.Ei@@<
p&l))GiH>arrGiHE
p&l))GiH>p&l))G0HE
p&l))Gi@2H>p&l))G2HE
SRIRAM ENGINEERING
COLLEGE

PERUMALPATTU-602024
www.jntuworld.com
www.jntuworld.com
www.jwjobs.net
.>0E
(&r;i>0Ei?2N+Ei@>2<
'lip*;p&l))GiH,p&l))Gi@2H,p&l))Gi@2H,p&l))Gi@6H<E
(&r;i>0Ei?.Ei@@<
p&l)GiH>r&u+,;arrGiH<E
i(;.<
(illp&l);.M2,p&l)<E
set'&l&r;RED<E
re'ta+1le;3mi+,)ma3,3ma3,)mi+<E
'&ut??IJt C#!PPED PO#DGO%IE
1et'h;<E
'l&se1raph;<E
O
OUTPUT:
C& &r,i+ates &( re1ular 'lip -i+,&-:
Cmi+,)mi+:2P0 2Y0
Cma3,)ma3:200 260
P&l)1&+ t& *e 'lippe,:
%& &( si,es:4
E+ter the '&-&r,i+ates:20 20
60 4P
P0 60
SRIRAM ENGINEERING
COLLEGE

PERUMALPATTU-602024
www.jntuworld.com
www.jntuworld.com
www.jwjobs.net
S0 Y0
U+'lippe, 5i+,&-:
C7i//e8 2i98"::
RESULT:
Thus the pr&1ram (&r impleme+tati&+ &( "utherla+, &,1ema+ P&l)1&+ Clippi+1 is
su''ess(ull) '&mpile, a+, e3e'ute,.
DATE: TRANSLATION, ROTATION, SCALING
AIM: T& stu,) a+, !mpleme+t 6D Tra+s(&rmati&+
ALGORITHM:
STEP \$:"tart the pr&1ram
STEP 2:!+'lu,e the reTuire, varia*les a+, the 1raphi's m&,e spe'i()i+1 the path.
STEP %:Get the verti'es a+, 1et the '&&r,i+ates i+ ea'h 'ase.
STEP 4:Get the &pti&+s the+ usi+1 the suite, 'ase t& (&ll&- tra+s(&rmati&+.
STEP 5:!( the &pti&+2 1et the t3 ,t) a+, tU '&&r,i+ate the+ per(&rm the tra+slati&+ &perati&+.
SRIRAM ENGINEERING
COLLEGE

PERUMALPATTU-602024
www.jntuworld.com
www.jntuworld.com
www.jwjobs.net
E* N": THREE DIMENSIONAL TRANSFORMATIONS
STEP &:Repeat the &perati&+ (&r all the '&&r,i+ates.
STEP ':!( &pti&+2 1et the s3 ,s) a+, sU '&&r,i+ates the+ per(&rm the s'ali+1 &perati&+
STEP (:Repeat the &perati&+ (&r all '&&r,i+ates.
STEP ):Displa) the &utput result. !( the &pti&+ is 6 1et the r&tati&+ a+1le;r< the+ per(&rm the r&tati&+
&perati&+ a+, (i+, the r2>r2N;6.24<M;2Y0<
STEP \$0!T& per(&rm r&tati&+ use s-it'h 'ase,'ase2 (&r 3 ,ire'ti&+
r&tati&+,'ase 2 (&r ) ,ire'ti&+ r&tati&+ a+, 'ase 6 (&r U
,ire'ti&+ r&tati&+ ,'ase 4 (&r e3it &perati&+.
!+ 3 ,ire'ti&+ r&tati&+, per(&rm the &perati&+
!+ ) ,ire'ti&+ r&tati&+, per(&rm the &perati&+
!+ U ,ire'ti&+ r&tati&+, per(&rm the &perati&+
STEP \$\$. Displa) the &utput result
STEP \$2:!( &pti&+ 4 e3it the pr&1ram
STEP \$%:"t&p the pr&1ram.
PROGRAM:
Ai+'lu,e?st,i&.hB
Ai+'lu,e?'&+i&.hB
Ai+'lu,e?1raphi's.hB
Ai+'lu,e?math.hB
i+t ma33,ma3),mi,3,mi,)E
SRIRAM ENGINEERING
COLLEGE

PERUMALPATTU-602024
www.jntuworld.com
www.jntuworld.com
www.jwjobs.net
v&i, a3is;<
F
1et'h;<E
'lear,evi'e;<E
li+e;mi,3,0,mi,3,ma3)<E
li+e;0,mi,),ma33,mi,)<E
O
v&i, mai+;<
F
i+t 1,,1m,3,),U,&,32,32,)2,)2E
,ete't1raph;L1,,L1m<E
i+it1raph;L1,,L1m,I,:JJt'JJ*1iI<E
set(illst)le;0,1etma3'&l&r;<<E
ma33>1etma33;<E
ma3)>1etma3);<E
mi,3>ma33M2E
mi,)>ma3)M2E
a3is;<E
*ar6,;mi,3@200,mi,)-2P0,mi,3@60,mi,)-200,20,2<E
pri+t(;IJE+ter the tra+slati&+ (a't&rI<E
s'a+(;IK,K,I,L3,L)<E
a3is;<E
pri+t(;IA(ter tra+slati&+I<E
*ar6,;mi,3@200,mi,)-2P0,mi,3@60,mi,)-200,20,2<E
*ar6,;mi,3@3@200,mi,)-;)@2P0<,mi,3@3@60,mi,)-;)@200<,20,2<E
SRIRAM ENGINEERING
COLLEGE

PERUMALPATTU-602024
www.jntuworld.com
www.jntuworld.com
www.jwjobs.net
a3is;<E
*ar6,;mi,3@200,mi,)-2P0,mi,3@60,mi,)-200,20,2<E
pri+t(;IE+ter the s'ali+1 (a't&rI<E
s'a+(;IK,K,K,I,L3,L),LU<E
a3is;<E
pri+t(;IA(ter s'ali+1I<E
*ar6,;mi,3@200,mi,)-2P0,mi,3@60,mi,)-200,20,2<E
*ar6,;mi,3@;3N200<,mi,)-;)N2P0<,mi,3@;3N60<,mi,)-;)N200<,20NU,2<E
a3is;<E
*ar6,;mi,3@200,mi,)-2P0,mi,3@60,mi,)-200,20,2<E
pri+t(;IE+ter the r&tati&+ a+1leI<E
s'a+(;IK,I,L&<E
32>P0N'&s;&N6.24M2Y0<-200Nsi+;&N6.24M2Y0<E
)2>P0Nsi+;&N6.24M2Y0<@200N'&s;&N6.24M2Y0<E
32>60N'&s;&N6.24M2Y0<-R0Nsi+;&N6.24M2Y0<E
)2>60Nsi+;&N6.24M2Y0<@R0N'&s;&N6.24M2Y0<E
a3is;<E
pri+t(;IA(ter r&tati+1 a*&ut \-a3isI<E
*ar6,;mi,3@200,mi,)-2P0,mi,3@60,mi,)-200,20,2<E
*ar6,;mi,3@32,mi,)-)2,mi,3@32,mi,)-)2,20,2<E
a3is;<E
pri+t(;IA(ter r&tati+1 a*&ut 3-a3isI<E
*ar6,;mi,3@200,mi,)-2P0,mi,3@60,mi,)-200,20,2<E
*ar6,;mi,3@200,mi,)-32,mi,3@60,mi,)-32,20,2<E
a3is;<E
SRIRAM ENGINEERING
COLLEGE

PERUMALPATTU-602024
www.jntuworld.com
www.jntuworld.com
www.jwjobs.net
pri+t(;IA(ter r&tati+1 a*&ut D-a3isI<E
*ar6,;mi,3@200,mi,)-2P0,mi,3@60,mi,)-200,20,2<E
*ar6,;mi,3@32,mi,)-2P0,mi,3@32,mi,)-200,20,2<E
1et'h;<E
'l&se1raph;<E
O
OUTPUT:
E+ter the tra+slati&+ (a't&r: 20 20 20 20
SRIRAM ENGINEERING
COLLEGE

PERUMALPATTU-602024
www.jntuworld.com
www.jntuworld.com
www.jwjobs.net
A(ter tra+slati&+:
E+ter s'ali+1 (a't&r: 20 20 60
A;te s5-7i9g:
SRIRAM ENGINEERING
COLLEGE

PERUMALPATTU-602024
www.jntuworld.com
www.jntuworld.com
www.jwjobs.net
E+ter r&tati&+ a+1le: 20
A(ter R&tai&+ a*&ut U-a3is:
A(ter R&tai&+ a*&ut 3-a3is:
SRIRAM ENGINEERING
COLLEGE

PERUMALPATTU-602024
www.jntuworld.com
www.jntuworld.com
www.jwjobs.net
A(ter R&tai&+ a*&ut )-a3is:
RESULT:
Thus the pr&1ram (&r impleme+tati&+ &( Three Dime+si&+al tra+s(&rmati&+s, "'ali+1 a+,
R&tati&+ are su''ess(ull) '&mpile, a+, e3e'ute,.
SRIRAM ENGINEERING
COLLEGE

PERUMALPATTU-602024
www.jntuworld.com
www.jntuworld.com
www.jwjobs.net
divide&conquermethod.
Aim:Writeaprogramforfindingthemaximum&minimumusingdivide&
conquermethod.
Theory:
DivideandConquer Algorithms: These algorithms have the following outline: To
solveaproblem,divideitintosubproblems.Recursivelysolvethesubproblems.Finally
glue the resulting solutions together to obtain the solution to the original problem.
Progress here is measured by how much smaller the sub problems are compared to the
originalproblem.
Finding Maximum/minimum numbersinalist:
Suppose we wish to find the minimum and maximum items in a list of numbers. How
many comparisonsdoesittake?
AlgorithmStraightMaxMin(a,n,max,min)
{
max=min=a[1]
fori=2tondo
{
if(a[i]>max)thenmax=a[i]
if(a[i]<min)thenmin=a[i]
}
}
that the complexity will be proportional to the number of comparisons. To find the
maximum, we see fromthe forloopthat N1comparisonswill be made. Thealgorithm
forfindingtheminimumfollowsinasimilarway.Ifwe findtheminimumafterfinding
the maximum, it is enough to find the minimum among the remaining N1 numbers
(assuming there are at least 2 numbers otherwise, maximum and minimum will be the
same).Soto findthe maximum and minimumofN numbers,we need(N1)+(N2)=
2N 3 comparisons. Now, let us see whether we can develop an algorithm whose
complexityisbetterthanthatofsolution1usingthedivideandconquermethod.
Thiswillbethebasicprincipleofthealgorithmtofindthemaximumandminimumofan
array of numbers. The main idea of using the divideandconquer strategy is described
below:
DividearrayAintotwosubpartsA1andA2suchthatA1andA2together
form A.
Find the maximumand minimum of each by recursive application of the
algorithm.
The maximum and minimum of A can be computed from the maximum
andminimumof A1andA2bymakingtwocomparisons.
Programforfindingthemaximum&minimumusing
www.jntuworld.com
www.jntuworld.com
www.jwjobs.net
Algorithm MINMAX(A,L,U,mx,mn)
(Aisanarrayrangingbetween L andUmx andmn willhavethemaximum
andminimumvaluerespectivelywhentheprocedureterminates)
varl1,l2,U1,U2:integer(LocalVariables)
mx1,mx2,mn1,mn2:integer(LocalVariables)
{
If(U==L)thenmax=min=a[L]
elseIf (UL+1)=2 then
{
ifA[L]>A[U]thenmx:=A[L](maximum)
mn:=A[U](minimum )
else
mx:=A[U](maximum)
mn:=A[L](minimum)
}
else
{
//SplitAintotwohalvesA1andA2withlowerandupperindicestobel1,U1 andl2,
U2respectively
call MINMAX(A,l1,U1, mx1,mn1)
call MINMAX(A,l2,U2,mx2,mn2)
mx :=maximum(mx1,mx2)(maximumreturnsthemaximumofthetwo
values)
mn :=minimum(mn1,mn2)(minimumreturnstheminimumofthetwo
values)
}
}
Input:
22 13 5 8 15 60 17 31 47
Output:
Maximum:60
Minimum:8
Conclusion:
Thus we saw using StraightMaxMin algorithm, we need (N1) + (N2) = 2N 3
comparisons.Ifwe solvethesameproblemusingdivideandconquertechnique number
ofcomparisonsarereducedto3n/22.Thissaves25%comparisons.
www.jntuworld.com
www.jntuworld.com
www.jwjobs.net
method.
Aim: Write a program to implement knapsack problem using greedy
method.
Theory:
Greedy algorithms are simple and straightforward. They are shortsighted in their
approachinthesensethattheytakedecisionsonthebasisofinformationathandwithout
easytoimplementandmostofthetimequiteefficient.Manyproblemscannotbesolved
correctlybygreedyapproach.Greedyalgorithmsareusedtosolveoptimizationproblems
GreedyApproach
GreedyAlgorithmworksbymaking thedecision thatseemsmostpromising atany
momentitneverreconsidersthisdecision,whateversituationmayariselater.
Asanexampleconsidertheproblemof "MakingChange".
Coinsavailableare:
dollars(100cents)
quarters(25cents)
dimes(10cents)
nickels(5cents)
pennies(1cent)
Problem Make a change of a given amount using the smallest possible number of
coins.
InformalAlgorithm
Startwithnothing.
ateverystagewithoutpassingthegivenamount.
Example Makeachangefor2.89(289cents)heren=2.89andthesolutioncontains2
dollars(200cents)3quarters(75cents),1dime(10cents)and4pennies(4cents).The
algorithm is greedy because at every stage it chooses the largest coin without worrying
hasbeenincludedinthesolutionset,itremainsthere.
Toconstructthesolutioninanoptimalway.Algorithmmaintainstwosets.Onecontains
chosenitemsandtheothercontainsrejecteditems.
Thegreedyalgorithmconsistsoffour(4)function.
1. Afunctionthatcheckswhetherchosensetofitemsprovideasolution.
2. Afunctionthatchecksthefeasibilityofaset.
Programtoimplement knapsackproblemusinggreedy
www.jntuworld.com
www.jntuworld.com
www.jwjobs.net
3. Theselectionfunctiontellswhichofthecandidatesisthemostpromising.
4. An objective function, which does not appear explicitly, gives the value of a
solution.
StructureGreedyAlgorithm
Initiallythesetofchosenitemsisemptyi.e.,solutionset.
Ateachstep
o IFthesetwouldnolongerbefeasible
rejectitemsunderconsideration(andisneverconsideragain).
o ELSEIFsetisstillfeasibleTHEN
Definitionsoffeasibility
A feasible set (of candidates) is promising if it can be extended to produce not
merelya solution,butanoptimalsolution totheproblem.Inparticular,theempty
setisalwayspromisingwhy?(becauseanoptimalsolutionalwaysexists)
A greedy strategy usually progresses in a topdown fashion, making one greedy choice
afteranother,reducingeachproblemtoasmallerone.
GreedyChoiceProperty
The "greedychoice property" and "optimal substructure" are two ingredients in the
problemthatlendtoagreedystrategy.
GreedyChoiceProperty
It says that a globally optimal solution can be arrived at by making a locally optimal
choice.
KnapsackProblem
Statement Wearegivennobjectsandaknapsackorbag.Objectihasaweightw
i
and
theknapsackhasacapacitym.
Therearetwoversionsofproblem
I. Fractionalknapsackproblem
Thesetupissame,wecantakefractionsofitems,meaningthattheitemscan
bebrokenintosmallerpiecessothatwemaydecidetocarryonlyafractionof
x
i
of item i, where 0 x
i
1. If a fraction x
i
of object i is placed into the
knapsack,thenaprofitp
i
x
i
isearned.
The objective is to obtain a filling of the knapsack that maximizes the total
profitearned.
www.jntuworld.com
www.jntuworld.com
www.jwjobs.net
Theideaistocalculateforeachobjecttheratioof value/cost,andsortthemaccordingto
So,forourexample:
v(weight)={4,2,2,1,10}
c(profit)={12,1,2,1,4}
r={1/3,2,1,1,5/2}
possibleof1.
Wecanchooseobjectslikethis:
Filledthebagwithobjectsworth15.48\$.
II. 01knapsackproblem
Thesetupisthesame,buttheitemsmaynotbebrokenintosmallerpieces,so
we may decide either totake an item or to leave it (binary choice), but may
nottakeafractionofanitem.
Algorithmfractionalknapsack(w,v,W)
{
fori=1ton
dox[i]=0
weight=0
whileweight<W
doi=bestremainingitem
if weight+w[i]W
then x[i]=1
weight=weight+w[i]
else
x[i]=(w weight)/w[i]
weight=W
return x
}
www.jntuworld.com
www.jntuworld.com
www.jwjobs.net
ProgramSnippets:
intn=5/*Thenumberofobjects*/
intc[10]={12,1,2,1,4}/*c[i]isthe*COST*oftheith object
i.e.whatYOUPAYtotaketheobject*/
intv[10]={4,2,2,1,10}/*v[i]isthe*VALUE*oftheithobject
i.e.whatYOUGETfortakingtheobject*/
intW=15/*Themaximumweightyoucantake*/
voidsimple_fill()
{
intcur_w
floattot_v
inti,maxi
intused[10]
for(i=0i<n++i)
used[i]=0/*Notusedtheithobjectyet*/
cur_w=W
while(cur_w>0)
{/*whilethere'sstillroom*/
/*Findthebestobject*/
maxi =1
for(i=0i<n++i)
if((used[i]==0)&&((maxi==1)||((float)v[i]/c[i]>
(float)v[maxi]/c[maxi])))
maxi=i
used[maxi]=1/*markthemaxithobjectasused*/
cur_w =c[maxi]/*withtheobjectinthebag,Icancarryless*/
tot_v+=v[maxi]
if(cur_w>=0)
%d.\n",maxi+1,v[maxi],c[maxi],cur_w)
else{
+(float)cur_w/c[maxi])*100),v[maxi],c[maxi],maxi+1)
tot_v =v[maxi]
tot_v+=(1+(float)cur_w/c[maxi])*v[maxi]
}
}
printf("Filledthebagwithobjectsworth%.2f\$.\n",tot_v)
}
Output:
Filledthebagwithobjectsworth15.48\$.
www.jntuworld.com
www.jntuworld.com
www.jwjobs.net
Conclusion:
PerformanceAnalysis
i
/w
i,
thenthewhilelooptakesa
timein O(n)
Therefore,thetotaltimeincludingthesortisin O(nlogn).
Ifwekeeptheitemsinheapwithlargestv
i
/w
i
attheroot.Then
creatingtheheaptakesO(n)time
whileloop now takes O(log n) time (since heap property must be restored after
theremovalofroot)
Althoughthisdatastructuredoesnotaltertheworstcase,itmaybefasterifonlyasmall
numberofitemsareneededtofilltheknapsack.
Onevariantofthe01knapsackproblemiswhenorderofitemsaresortedbyincreasing
weightisthesameastheirorderwhensortedbydecreasingvalue.
The optimal solution to this problem is to sort by the value of the item in decreasing
order. Then pick up the most valuable item which also has a least weight. First, if its
weightislessthanthetotalweightthatcanbecarried.Thendeductthetotalweightthat
can be carried by the weight of the item just pick. The second item to pick is the most
valuable item among those remaining. Keep follow the same strategy until we cannot
carrymoreitem(duetoweight).
www.jntuworld.com
www.jntuworld.com
www.jwjobs.net
method.
Aim:Writeaprogramforfindingminimumcostspanningtreeusinggreedy
method.
Theory:
Aspanningtreeofagraphisanytreethatincludeseveryvertexinthegraph.Littlemore
formally,aspanningtreeofagraphG isasubgraphofGthatisatreeandcontainsall
theverticesofG.Anedgeofaspanningtreeiscalledabranchanedgeinthegraphthat
is not in the spanning tree is called a chord. We construct spanning tree whenever we
want to find a simple, cheap and yet efficient way to connect a set of terminals
(computers, cites, factories, etc.). Spanning trees are important because of following
reasons.
Spanning trees construct a sparse sub graph that tells a lot about the original
graph.
Spanningtreesaveryimportantindesigningefficientroutingalgorithms.
Some hardproblems(e.g.,Steinertreeproblem andtravelingsalesmanproblem)
canbesolvedapproximatelybyusingspanningtrees.
Spanningtreeshavewideapplicationsinmanyareas,suchasnetworkdesign,etc.
Herearesomeexamples:
AgraphG: Three(ofthemanypossible)spanningtreesfromgraphG:
AweightedgraphG: TheminimumspanningtreefromweightedgraphG:
Figure6.1:GraphGanditsspanningtrees
ToexplainfurtherupontheMinimumSpanningTree(MST),andwhatitappliesto,let's
consideracoupleofrealworldexamples:
Programfor findingminimumcostspanningtreeusinggreedy
www.jntuworld.com
www.jntuworld.com
www.jwjobs.net
1. One practical application of a MST would be in the design of a network. For
instance,agroupof individuals,whoareseparatedby varyingdistances,wishto
beconnectedtogetherinatelephonenetwork. AlthoughMSTcannotdoanything
leastcostly pathswithnocyclesinthisnetwork,therebyconnectingeveryoneata
minimumcost.
2. Another useful application of MST would be finding airline routes. The vertices
ofthegraphwouldrepresentcities,andtheedgeswouldrepresentroutesbetween
thecities. Obviously,thefurtheronehastotravel,themoreitwillcost,soMST
canbeappliedtooptimizeairlineroutesbyfindingtheleastcostlypathswithno
cycles.
ToexplainhowtofindaMinimumSpanningTree,wewilllookattwoalgorithms:the
KruskalalgorithmandthePrimalgorithm. Bothalgorithmsdifferintheirmethodology,
but both eventually end up with the MST. Kruskal's algorithm uses edges, and Prims
algorithmusesvertexconnectionsindeterminingtheMST.
Kruskal'sAlgorithm:
Thisisagreedyalgorithm.Agreedyalgorithmchoosessomelocaloptimum(ie.picking
anedgewiththeleastweightinaMST).
Kruskal's algorithm works as follows: Take a graph with 'n' vertices, keep adding the
shortest(leastcost)edge,whileavoidingthecreationofcycles,until(n1)edges have
whichtheedgesarechosen,inthiscase,doesnotmatter.DifferentMSTsmayresult,but
they will all have the same total cost, which will always be the minimum cost)
AlgorithmKruskalMST
Input:Aweighted,connectedandundirectedgraph G=(V,E).
Output:Aminimalspanningtreeof G.
Step1: f = T .
Step2: whileTcontainslessthan n1edgesdo
Chooseanedge(v,w)from Eofthesmallestweight.
Delete(v,w)from E.
endwhile
Prim'sAlgorithm:
This algorithm builds the MST one vertex at a time. It starts at any vertex in a graph
(vertexA,forexample),andfindstheleastcostvertex(vertexB,forexample)connected
to the start vertex. Now, from either 'A' or 'B', it will find the next least costly vertex
connection,withoutcreatingacycle(vertexC,forexample).Now,from either'A','B',or
'C',itwillfindthenextleastcostlyvertexconnection,withoutcreatingacycle,andsoon
it goes. Eventually, all the vertices will be connected, without any cycles, and an MST
willbetheresult.(NOTE:Twoormoreedgesmayhavethesamecost,sowhenthereisa
www.jntuworld.com
www.jntuworld.com
www.jwjobs.net
choicebytwoormoreverticesthatisexactlythesame,thenonewillbechosen,andan
MSTwillstillresult)
Anytwoverticesinatreeareconnectedbyauniquepath.
LetTbeaspanningtreeofagraphG,andletebeanedge
ofGnotinT.TheT+econtain auniquecycle.
Greediness: It is easy to see that this algorithm has the property that each edge is
examined at most once. Algorithms, like this one, which examine each entity at most
once and decide its fate once and for all during that examination, are called greedy
algorithms. The obvious advantage of greedy approach is that we do not have to spend
timereexaminingentities.
Figure6.2:GraphG
ApplyingKruskal'sAlgorithm onthegraphshowninfigure6.2
Usingtheabovegraph,herearethestepstotheMST,usingKruskal'sAlgorithm:
5. N2toN6 costis4rejectbecauseitformsacircuit
8. N3toN7 costis6rejectbecauseitformsacircuit
9. N4toN8 costis6rejectbecauseitformsacircuit
10. N4toN7 costis7rejectbecauseitformsacircuit
We stop here, because n 1 edges have been added. We are left with the minimum
spanningtree,withatotalweightof23.
www.jntuworld.com
www.jntuworld.com
www.jwjobs.net
ProgramSnippets:
#defineN6/*numberofvertices*/
#defineM15/*numberofedgesingraph*/
intU[N]
/*functionprototypes*/
voidmakeset(inti)
intfind(inti)
voidmerge(intp,intq)
intequal(intp,intq)
voidinitial(intn)
voidtest_univ(void)
voidpause(void)/*usedmainlyfortestpurposes*/
/*functiondefinitions*/
intmain()
{intW[N][N]={0,2,4,1,3,2,/*weightedgraph*/
2,0,6,4,5,1,
4,6,0,4,2,1,
1,4,4,0,5,4,
3,5,2,5,0,6,
2,1,1,4,6,0}
intE[M][3]/*completesetofedges*/
intF[N1][3]/*setofedgesinmin.span.tree*/
intnum_edges=0/*numofedgesinmin.span.tree*/
intnext_edge=0/*nextedgenotyetconsidered*/
intweight=0 /*minimalspanningtreeweight*/
inta,b,c,i,j,k/*counter/placeholdervariables*/
/*initializesetofedges*/
k=0
for(i=0i<Ni++)
for(j=0j<Nj++)
if(j>i)
{E[k][0]=i/*firstvertexofedge*/
E[k][1]=j/*secondvertexofedge*/
E[k][2]=W[i][j]/*weightofedge*/
k++
}
/*displaysetofedgesbeforesort*/
for(i=0i<Mi++)
{for(j=0j<3j++)
printf("%3d",E[i][j])
printf("\n")
}
pause()
www.jntuworld.com
www.jntuworld.com
www.jwjobs.net
/*sortsetofedgesinnondecreasingorderbyweightbubblesort*/
for(i=M1i>0i)
for(j=0j<ij++)
if(E[j][2]>E[j+1][2])
{a=E[j][0]
b=E[j][1]
c=E[j][2]
E[j][0]=E[j+1][0]
E[j][1]=E[j+1][1]
E[j][2]=E[j+1][2]
E[j+1][0]=a
E[j+1][1]=b
E[j+1][2]=c
}
/*displaysetofedgesaftersort*/
for(i=0i<Mi++)
{for(j=0j<3j++)
printf("%3d",E[i][j])
printf("\n")
}
/*createndisjointsubsets*/
initial(N)
/*initializesetofedgesinmin.span.treetoempty*/
for(i=0i<N1i++)
for(j=0j<3j++)
F[i][j]=1/*'1'denotes'empty'*/
test_univ()
/*findminimalspanningtree*/
while(num_edges<N1)
{a=E[next_edge][0]
b=E[next_edge][1]
i=find(a)
j=find(b)
if(!equal(i,j))
{merge(i,j)
F[num_edges][0]=E[next_edge][0]
F[num_edges][1]=E[next_edge][1]
F[num_edges][2]=E[next_edge][2]
num_edges++
test_univ()
}
www.jntuworld.com
www.jntuworld.com
www.jwjobs.net
next_edge++
}
/*displayedgescomprisingminimalspanningtree*/
printf("\nMinimalSpanningTreeEdges:\n")
printf("F=(")
for(i=0i<N1i++)
{printf("(V%d,V%d)",F[i][0],F[i][1])
if(i<N2)
printf(",")
weight=weight+F[i][2]
}
printf(")\n")
printf("MinimalSpanningTreeWeight=%d\n",weight)
return(0)
}
/***************makeset()***************/
voidmakeset(inti)
{U[i]=i
}
/***************find()***************/
intfind(inti)
{intj
j=i
while(U[j]!=j)
j=U[j]
return(j)
}
/***************merge()***************/
voidmerge(intp,intq)
{if(p<q)
U[q]=p
else
U[p]=q
}
/***************equal()***************/
intequal(intp,intq)
{if(p==q)
return(1)
else
return(0)
}
/***************initial()***************/
voidinitial(intn)
www.jntuworld.com
www.jntuworld.com
www.jwjobs.net
{inti
for(i=0i<ni++)
makeset(i)
}
/***************test()***************/
voidtest_univ(void)
/*testuniversevalues*/
{inti
printf("\nThedisjointsubsetsare:\n")
for(i=0i<Ni++)
printf("%3d",U[i])
printf("\n")
}
/***************pause()***************/
voidpause(void)
{inti
printf("PressENTERtocontinue...\n")
i=getchar()
}
Conclusion:
PerformanceanalysisofKruskalsalgorithm:
Creationof thepriorityqueue
Ifthereareeedges,itiseasytoseethatittakesO(eloge)timetoinserttheedges
intoapartiallyorderedtree
EachdeleteminoperationtakesO(loge)timeintheworstcase.Thusfindingand
deletingleastcostedges,overthewhileiterationscontributeO(loge)intheworst
case.
Thetotaltimeforperformingallthemergeandfindis:O(eloge) .
www.jntuworld.com
www.jntuworld.com
www.jwjobs.net
usingdynamicprogramming.
Aim: Write a program to implement traveling salesperson problem using
dynamicprogramming.
Theory:
canbestatedverysimply:
A salesman spends his time visiting n cities (or nodes) cyclically. In one tour he visits
eachcity justonce,and finishesupwhere hestarted.Inwhatordershould hevisitthem
tominimizethedistancetraveled?
ManyTSP'saresymmetricthatis,foranytwocitiesAandB,thedistancefromAtoB
isthesameasthatfromBtoA.Inthiscase youwillgetexactlythesametourlength if
you reverse the order in which they are visited so there is no need to distinguish
betweenatouranditsreverse,andyoucanleaveoffthearrowsonthetourdiagram.
If there are only 2 cities then the problem is trivial, since only one tour is possible. For
DifferenttoursforanncityasymmetricTSP.Toseewhythisisso,pickanycityasthe
firstthentherearen1choicesforthesecondcityvisited,n2choicesforthethird,and
soon.Forthesymmetriccasetherearehalfasmanydistinctsolutions(n1)!/2forann
cityTSP.Ineithercasethe numberofsolutionsbecomesextremely large forlarge n,so
thatanexhaustivesearchisimpractical.
Figure8.1GraphG
ForthegraphGshowninfigure8.1,distancematrix:
D={02 9 10,
10 64,
15708,
63120}
Letg(i,S)bethelengthofshortestpathstartingatvertexi,goingthroughallverticesinS,
andterminatingatvertex1.
g(2,)=c21=1
g(3,)=c31=15
g(4,)=c41=6
1
4
2
3
Programtoimplementtravelingsalespersonproblem
www.jntuworld.com
www.jntuworld.com
www.jwjobs.net
k=1,considersetsof1element:
Set{2}: g(3,{2})=c32+g(2, )=c32+c21=7+1=8p(3,{2})=2
g(4,{2})=c42+g(2, )=c42+c21=3+1=4p(4,{2})=2
Set{3}: g(2,{3})=c23+g(3, )=c23+c31=6+15=21p(2,{3})=3
g(4,{3})=c43+g(3, )=c43+c31=12+15=27p(4,{3})=3
Set{4}: g(2,{4})=c24+g(4,)=c24+c41=4+6=10p(2,{4}) =4
g(3,{4})=c34+g(4,)=c34+c41=8+6=14p(3,{4})=4
k=2,considersetsof2elements:
Set{2,3}: g(4,{2,3})=min{c42+g(2,{3}),c43+g(3,{2})}=min{3+21,12+8}=
min{24,20}=20
p(4,{2,3}=3
Set{2,4}: g(3,{2,4}) = min {c32 + g(2,{4}), c34 + g(4,{2})} = min {7+10, 8+4}=
min{17,12}=12
p(3,{2,4}=4
Set{3,4}: g(2,{3,4})=min{c23+g(3,{4}),c24+g(4,{3})}=min{6+14,4+27}=
min{20,31}=20
p(2,{3,4}=3
Lengthofanoptimaltour:
f=g(1,{2,3,4})=min{c12+g(2,{3,4}),c13+g(3,{2,4}),c14+g(4,{2,3})}=min{2+
20,9+12,10+20}=min{22,21,30}=21
Successorofnode1:p(1,{2,3,4})=3
Successorofnode3:p(3,{2,4})=4
Successorofnode4:p(4,{2})=2
OptimalTSPtour:1 3 4 2 1
AlgorithmTSP
Input:Numberofcitiesnandarrayofcostsc(i,j)i,j=1,..n(Webeginfromcitynumber1)
Output:Vectorofcitiesandtotalcost.
(*startingvalues*)
C=0
cost=0
visits=0
e=1(*e=pointerofthevisitedcity)
(*determinationofroundandcost)
forr=1ton1do
chooseofpointerjwith
minimum=c(e,j)=min{c(e,k)visits(k)=0andk=1,..,n}
cost=cost+minimum
e=j
C(r)=j
endrloop
C(n)=1
cost=cost+c(e,1)
www.jntuworld.com
www.jntuworld.com
www.jwjobs.net
ProgramSnippets:
voidtravel(intn,constnumberW[][],indexP[][],number&minlength)
{
indexi,j,k
numberD[1..n][subsetofV{v1}]
for(i=2i<=ni++)
D[i][]=W[i][1]
for(k=1k<=n 2k++)
for(allsubsetsA V {v1} V {v1}containingkvertices)
for(isuchthati 1andviisnotinA){
D[i][A]=minimum(W[i][j]+D[j][A {vj}])
j:[vj A
P[i][A]=valueofjthatgavetheminimum
}
D[1][V {v1}]=minimum(W[1][j]+D[j][V{v1,vj}])
2jn
P[1][V {v1}]=valueofjthatgavetheminimum
minlength=D[1][V {v1}]
}
Conclusion:
Timecomplexity
LetNbethenumberofg(i,S)thathavetobecomputed
N =
Totaltime=O(n
2
2
n
)
Thisisbetterthanenumeratingalln!differenttours
2
2
0
2 ) 1 (
2
) 1 (
-
-
=
- =

-
-

n
n
k
n
k
n
n
www.jntuworld.com
www.jntuworld.com
www.jwjobs.net
dynamicprogramming.
Aim: Write a program to find shortest path for multistage graph using
dynamicprogramming.
Theory:
Definition:multistagegraphG(V,E)
1ik
If<u,v>E,thenuViandvVi+1forsomeI,1i<k
|V1|=|Vk|=1,ands(source)V1andt(sink)Vk
c(i,j)=costofedge<i,j>
Findaminimumcostpathfromstot
Figure9.1(5stagegraph)
ThevertexsinV
1
iscalledthesourcethevertextinV
K
iscalledthesink.Gisusually
assumedtobeaweightedgraph.Thecostofapathfromnodevtonodewissumofthe
costsof edges in the path. The "multistage graph problem" is to find the minimum cost
pathfromstot.EachsetV
i
iscalledastageinthegraph.
Manyproblemscanbeformulatedasmultistagegraphproblem
Anexample:resourceallocationproblem
nunitsofresourcearetobeallocatedtorprojects
N(i,j)=netprofitwhenjunitsofresourceallocatedtoprojecti
V(i,j) = vertex representing the state in which a total of j units have
Programto findshortestpathformultistagegraph using
www.jntuworld.com
www.jntuworld.com
www.jwjobs.net
DPformulation
Everystotpathistheresultofasequenceofk2decisions
Theprincipleofoptimalityholds(Why?)
p(i,j)=aminimumcostpathfromvertexjinVitovertext
cost(i,j)=costofpathp(i,j)
cost(k1,j)=c(j,t)if<j,t> E,otherwise
Thencomputingcost(k2,j)forallj Vk2
Thencomputingcost(k3,j)forallj Vk3

Finallycomputingcost(1,s)
Tofindtheshortestpath of multistagegraph showninfigure9.1.
Stage5
cost(5,12)=0.0
Stage4
cost(4,9)=min{4+cost(5,12)}=4
cost(4,10)=min{2+cost(5,12)}=2
cost(4,11)=min{5+cost(5,12)}=5
Stage3
cost(3,6)=min{6+cost(4,9),5+cost(4,10)}=7
cost(3,7)=min{4+cost(4,9),3+cost(4,10)}=5
cost(3,8)=min{5+cost(4,10),6+cost(4,11)}=7
Stage2
cost(2,2)=min{4+cost(3,6),2+cost(3,7),1+cost(3,8)}=7
cost(2,3)=min{2+cost(3,6),7+cost(3,7)}=9
cost(2,4)=min{11+cost(3,8)}=18
cost(2,5)=min{11+cost(3,7),8+cost(3,8)}=15
Stage1
cost(1,1) = min {9+cost(2,2), 7+cost(2,3), 3+cost(2,4),
2+cost(2,5)}=16
Recordingthepath
d(i,j) = value of l (l is a vertex) that minimizes c(j,l)+cost(i+1,l) in
equation(5.5)
InFigure9.1
d(3,6)=10d(3,7)=10d(3,8)=10
d(2,2)=7d(2,3)=6d(2,4)=8d(2,5)=8
)} , 1 ( cos ) , ( { min ) , ( cos
,
1
l i t l j c j i t
E l j
V l
i
+ + =
> <

+
www.jntuworld.com
www.jntuworld.com
www.jwjobs.net
d(1,1)=2
Whenlettingtheminimumcostpath1,v2,v3,,vk1,t,
v2=d(1,1)=2
v3=d(2,d(1,1))=7
v4=d(3,d(2,d(1,1)))=d(3,7)=10
Sothesolution(minimumcostpath)is1 2 7 10 12
anditscostis16
Algorithm /ProgramSnippets:
Voidmultistage(graphG,intk,intn,intp[] )
//TheinputisakstagegraphG=(V,E)withnverticesindexedinorder
//ofstages.Eisasetofedgesandc[i][j]isthecostof<i,j>.
//p[1:k]isaminimumcostpath.
{
floatcost[MAXSIZE]intd[MAXSIZE],r
cost[n]=0.0
for(intj=n1j>=1j){//Computecost[j].
letrbeavertexsuchthat<j,r>isanedge
ofGandc[j][r]+cost[r]isminimum
cost[j]=c[j][r]+cost[r]
d[j]=r
}
//Findaminimumcostpath.
p[1]=1p[k]=n
for(j=2j<=k1j++)p[j]=d[p[j1]]
}
Conclusion:
Timecomplexity
www.jntuworld.com
www.jntuworld.com
www.jwjobs.net
method.
Aim: Write a program to implement 8queens problem using backtrack
method.
Theory:
Backtracking is kind of solving a problem by trial and error. However, it is a well
organizedtrialanderror.Wemakesurethatwenevertrythesamethingtwice.Wealso
make sure that if the problem is finite we will eventually try all possibilities (assuming
thereisenoughcomputingpowertotryall possibilities).
Generalmethod
Usefultechniqueforoptimizingsearchundersomeconstraints
Express the desired solution as an ntuple (x1, . . . , xn) where each xi 2 Si, Si
beingafiniteset
Thesolutionisbasedonfindingoneormorevectorsthatmaximize,minimize,or
satisfyacriterionfunctionP(x1,...,xn)
Sortinganarraya[n]
Findanntuplewheretheelementxiistheindexofith smallestelementina
Criterionfunctionisgivenbya[xi]_a[xi+1]for1_i<n
SetSiisafinitesetofintegersintherange[1,n]
Bruteforceapproach
LetthesizeofsetSibemi
Therearem=m1m2mnntuplesthatsatisfythecriterionfunctionP
In brute force algorithm, you have to form all the m ntuples to determine the
optimalsolutions
Backtrackapproach
Requireslessthanmtrialstodeterminethesolution
Formasolution(partialvector)andcheckateverystepifthishasanychanceof
success
Ifthesolutionatanypointseemsnotpromising,ignoreit
Ifthepartialvector(x1,x2,...,xi)doesnotyieldanoptimalsolution,ignore
mi+1mnpossibletestvectors
evenwithoutlookingatthem
All the solutions require a set of constraints divided into two categories: explicit and
implicitconstraints
Definition1Explicitconstraintsarerulesthatrestricteachxitotakeonvaluesonlyfrom
agivenset.
ExplicitconstraintsdependontheparticularinstanceIofproblembeingsolved
Alltuplesthatsatisfytheexplicitconstraintsdefineapossiblesolutionspacefor
I
Examplesofexplicitconstraints
*x
i
>=0,orallnonnegativerealnumbers
*x
i
={0,1}
*l
i
<=x
i
<=u
i
Definition 2 Implicit constraints are rules that determine which of the tuples in the
solutionspaceofIsatisfythecriterionfunction.
Program to implement8queensproblemusing backtrack
www.jntuworld.com
www.jntuworld.com
www.jwjobs.net
Implicitconstraintsdescribethewayinwhichthexismustrelatetoeachother.
Determineproblemsolutionbysystematicallysearchingthesolutionspaceforthegiven
probleminstance
Useatreeorganizationforsolutionspace
8queensproblem
Place eight queens on an 8 8 chessboard so that no queen attacks another
queen
*Identifydatastructurestosolvetheproblem
*Firstpass:Definethechessboardtobean88array
solutiontobean8tuple(x1,...,x8), wherexiisthecolumnforithqueen
Identifyexplicitconstraints
*Explicitconstraintsusing8tupleformulationareSi={1,2,3,4,5,6,7,
8},1 <=i<=8
Solutionspaceof8
8
8tuples
Identifyimplicitconstraints
* No two xi can be the same, or all the queens must be in different
columns
Allsolutionsarepermutationsofthe8tuple(1,2,3,4,5,6,7,8)
Reducesthesizeofsolutionspacefrom8
8
to8!tuples
Notwoqueenscanbeonthesamediagonal
Thesolutionaboveisexpressedasan8tupleas4,6,8,2,7,1,3,5
1 2 3 4 5 6 7 8
1 Q
2 Q
3 Q
4 Q
5 Q
6 Q
7 Q
8 Q
Figure10.1(OneSolutionto8queensproblem)
The8QueensProblem:
Givenisachessboard.Achessboardhas8x8 fields.Isitpossibletoplace8queenson
thisboard,sothatnotwoqueenscanattackeachother?
ThenQueensproblem:
Given isa boardofnbynsquares.Is it possible toplacenqueens(thatbehaveexactly
likechessqueens)onthisboard,withouthavinganyoneofthemattackany otherqueen?
1. Anotherexampleofaproblemthatcanbesolvedwith backtracking:
o Place 8 queens on a 8x8 chess board so that no queen attack each other
(findallsolutions)
www.jntuworld.com
www.jntuworld.com
www.jwjobs.net
52
ExamplesSolutions:
Figure10.2(Solutionsto8queensproblem)
Algorithm: TheBacktrackingAlgorithmfornQueens
Problem: Position nqueensonachessboardsothatnotwoareinthesamerow,column,
ordiagonal.
Inputs:positiveintegern.
Outputs:allpossiblewaysnqueenscanbeplacedonan nx nchessboardsothatnotwo
queensthreateneachother.Eachoutputconsistsofan arrayofintegerscolindexedfrom
1 ton,wherecol[i] isthecolumnwherethequeenintheithrowisplaced.
ProgramSnippets:
include<stdio.h>
defineTRUE1
defineFALSE0
int*board
intmain()
{
board=(int*)calloc(8+1,sizeof(int))
board++
//heregoestheuserinputnoofqueensdoesntmatterinthiscode
intcol
intqueens=2//examplefromuserinput
for(col=1col<=8col++)
placeQueens(1,col,queens)
}
voidplaceQueens(introw,intcol,intqueens)
{
inti
for(i=1i<rowi++)
{
www.jntuworld.com
www.jntuworld.com
www.jwjobs.net
if((board[i]==col)||((row+col)==(i+board[i]))||((rowcol)==(iboard[i])))
{
check=FALSE
}
else
check=TRUE
}
if(check==TRUE)
{
board[row]=col
if(row==8)
{
for(i=1i<=queensi++)
printf("(%d,%d)",i,board[i])
printf("\n")
}
else
{
for(i=1i<=8i++)
placeQueens(row+1,i)
}
}
}
www.jntuworld.com
www.jntuworld.com
www.jwjobs.net