Professional Documents
Culture Documents
Ritajit Majumdar
Mtech 1st semester
Computer Science and Engineering
Class Roll: 1
Exam Roll: 97/CSM/140001
Registration No: 0029169 of 2008-2009
March 26, 2015
Contents
1 Problem 1
1.1 Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
1.2 Output . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
1
1
2
2 Problem 2
2.1 Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
2.2 Output . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
3
3
4
3 Problem 3
3.1 Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
3.2 Output . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
4
4
5
4 Problem 4
4.1 Visualization . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
4.2 Remarks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
6
6
8
5 Problem 5
8
5.1 Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8
5.2 Output . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10
6 Problem 6
10
6.1 Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10
6.2 Output . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14
Problem 1
Given n-points in the plane and the sequence of h points, write a program to determine if these h
points are the points generating the convex hull in counter-clockwise order.
1.1
Code
from a s t import l i t e r a l e v a l
def c o n v e x h u l l ( p o i n t s ) :
# sort points lexicographically
# t a k i n g s e t t o remove d u p l i c a t e s
points = sorted ( set ( points ))
i f l e n ( p o i n t s ) <= 1 :
return p o i n t s
# 2d c r o s s p r o d u c t o f OA and OB v e c t o r s
# p o s i t i v e v a l u e i f OAB makes a counter c l o c k w i s e t u r n
# n e g a t i v e f o r c l o c k w i s e turn , z e r o f o r c o l l i n e a r
def c r o s s ( o , a , b ) :
return ( a [0] o [ 0 ] ) ( b [1] o [ 1 ] ) ( a [1] o [ 1 ] ) ( b [0] o [ 0 ] )
# b u i l d upper h u l l
upper = [ ]
for p in r e v e r s e d ( p o i n t s ) :
while l e n ( upper ) >= 2 and c r o s s ( upper [ 2 ] , upper [ 1 ] , p ) <= 0 :
upper . pop ( )
upper . append ( p )
# b u i l d lower h u l l
lower = [ ]
for p in p o i n t s :
while l e n ( l o w e r ) >= 2 and c r o s s ( l o w e r [ 2 ] , l o w e r [ 1 ] , p ) <= 0 :
l o w e r . pop ( )
l o w e r . append ( p )
# c o n c a t e n a t i o n o f l o w e r and upper h u l l
# l a s t p o i n t o f each l i s t i s o m i t t e d b e c a u s e i t i s r e p e a t e d
# at the beginning of other
return l o w e r [ : 1 ] + upper [ : 1 ]
#t o t a k e t h e i n p u t p o i n t s
def g e t c o o r d s ( ) :
print Enter a l i s t o f p o i n t s . For example ( 0 , 0 ) , ( 0 , 1 ) , ( 1 , 1 ) , ( 1 , 0 )
points = raw input ()
try :
return l i s t ( l i t e r a l e v a l ( p o i n t s ) )
except SyntaxError :
1
1.2
Output
Problem 2
Write a program for finding the convex hull in two-dimensional space using divide and conquer
strategy.
2.1
Code
p l t . t i t l e ( Convex H u l l )
#p l t . show ( )
p l t . s a v e f i g ( / U s e r s / mindSpace / Desktop / ch . png )
2.2
Output
The previous code is written to plot the points and also show the convex hull of the points. The plot
of the output is attached below.
Problem 3
Implement the divide and conquer closest pair algorithm in language of your choice.
3.1
Code
print Enter t he s e t o f n p o i n t s
points = get coords ()
print The p o i n t s you gave a r e
print p o i n t s
minimum = divideMin ( p o i n t s )
print \nThe minimum d i s t a n c e between t he p o i n t s you p r o v i d e d i s
print minimum
3.2
Output
Problem 4
Find a visualization of an algorithm for the closest pair problem on the web.
4.1
Visualization
4.2
Remarks
These screenshots hopefully provides an essence of how the algorithm works and the major steps
of its implementation. Every step is shown in the video. For better understanding, refer to the url
provided.
Problem 5
Implement the maximum flow algorithm in a network with a language of your choice. Consider both
direct and reverse flow direction.
5.1
Code
c l a s s Edge ( o b j e c t ) :
i n i t ( s e l f , u , v ,w ) :
def
s e l f . source = u
s e l f . sink = v
s e l f . capacity = w
def
repr ( self ):
return %s > %s : %s % ( s e l f . s o u r c e , s e l f . s i n k , s e l f . c a p a c i t y )
c l a s s FlowNetwork ( o b j e c t ) :
def
init ( self ):
s e l f . a d j = {}
s e l f . f l o w = {}
def a d d v e r t e x ( s e l f , v e r t e x ) :
8
s e l f . adj [ vertex ] = [ ]
def g e t e d g e s ( s e l f , v ) :
return s e l f . a d j [ v ]
def add edge ( s e l f , u , v ,w=0):
i f u == v :
r a i s e Val ueErro r ( Same v e r t e x . Edge not p o s s i b l e )
edge = Edge ( u , v ,w)
r e d g e = Edge ( v , u , 0 )
edge . r e d g e = r e d g e
r e d g e . r e d g e = edge
s e l f . a d j [ u ] . append ( edge )
s e l f . a d j [ v ] . append ( r e d g e )
s e l f . f l o w [ edge ] = 0
s e l f . flow [ r edge ] = 0
def f i n d p a t h ( s e l f , s o u r c e , s i n k , path ) :
i f s o u r c e == s i n k :
return path
for edge in s e l f . g e t e d g e s ( s o u r c e ) :
r e s i d u a l = edge . c a p a c i t y s e l f . f l o w [ edge ]
i f r e s i d u a l > 0 and edge not in path :
r e s u l t = s e l f . f i n d p a t h ( edge . s i n k , s i n k , path +[ edge ] )
i f r e s u l t != None :
return r e s u l t
def max flow ( s e l f , s o u r c e , s i n k ) :
path = s e l f . f i n d p a t h ( s o u r c e , s i n k , [ ] )
while path != None :
r e s i d u a l s = [ edge . c a p a c i t y s e l f . f l o w [ edge ] for edge in path ]
f l o w = min ( r e s i d u a l s )
for edge in path :
s e l f . f l o w [ edge ] += f l o w
s e l f . f l o w [ edge . r e d g e ] = f l o w
path = s e l f . f i n d p a t h ( s o u r c e , s i n k , [ ] )
return sum ( s e l f . f l o w [ edge ] for edge in s e l f . g e t e d g e s ( s o u r c e ) )
if
name
== m a i n :
g = FlowNetwork ( )
[ g . a d d v e r t e x ( v ) for v in s o p q r t ]
g . add vertex (v)
g . add edge ( s , o , 3 )
g . add edge ( s , p , 3 )
g . add edge ( o , p , 2 )
g . add edge ( o , q , 3 )
g . add edge ( p , r , 2 )
g . add edge ( r , t , 3 )
g . add edge ( q , r , 4 )
9
g . add
print
print
print
print
print
print
print
print
print
print
print
print
5.2
edge ( q , t , 2 )
\nThe graph i s s o p q r t
The s o u r c e i s s and t he s i n k i s t
The f l o w through e d g e s i s :
s o > 3
s p > 3
o p > 2
o q > 3
p r > 2
r t > 3
q r > 4
q t > 2\n
The maximum f l o w through t h i s network i s , g . max flow ( s , t )
Output
Problem 6
Write a program that accepts mouse clicks in a window and draws the convex hull of the points
clicked.
6.1
Code
There are two different programs - one to implement the gui and the other to implement the convex
hull
ConvexHull.py
def c o n v e x H u l l ( p o i n t s L i s t ) :
lowerHull = [ ]
upperHull = [ ]
i f l e n ( p o i n t s L i s t ) <= 0 :
10
return p o i n t s L i s t
#have t o s o r t l i s t f i r s t
p o i n t s L i s t . s o r t ( key=lambda tup : tup [ 0 ] )
n = len ( pointsList )
j = 0
#t h e n c a l c u l a t e l o w e r h u l l
for i d x in range ( n ) :
#c h e c k i n g f o r c o u n t e r c l o c k w i s e n e s s
while l e n ( l o w e r H u l l ) >= 2 and \
c r o s s ( l o w e r H u l l [ j 2 ] , l o w e r H u l l [ j 1 ] , p o i n t s L i s t [ i d x ] ) <= 0
l o w e r H u l l . pop ( )
j = 1
l o w e r H u l l . append ( p o i n t s L i s t [ i d x ] )
j += 1
j = 0
idx = n 1
#t h e n upper h u l l from r e v e r s e o f p o i n t s L i s t
for i in range ( n ) :
while l e n ( u p p e r H u l l ) >= 2 and \
c r o s s ( u p p e r H u l l [ j 2 ] , u p p e r H u l l [ j 1 ] , p o i n t s L i s t [ i d x ] ) <= 0
u p p e r H u l l . pop ( )
j = 1
u p p e r H u l l . append ( p o i n t s L i s t [ i d x ] )
i d x = 1
j += 1
launch.py
import pygame
from pygame . l o c a l s import
from c o n v e x H u l l import c o n v e x H u l l
c l a s s Button :
i n i t ( s e l f , button message , c o o r d i n a t e s ) :
def
s e l f . c a p t i o n = +b u t t o n m e s s a g e
s e l f . b t n w i d t h = 90
s e l f . b t n h e i g h t = 30
s e l f . r e c t = pygame . Rect ( c o o r d i n a t e s [ 0 ] , c o o r d i n a t e s [ 1 ] , s e l f . btn width ,
s e l f . s u r f a c e = pygame . S u r f a c e ( s e l f . r e c t . s i z e )
s e l f . b g c o l o r = pygame . Color ( b l i g h t g r a y )
s e l f . f g c o l o r = pygame . Color ( b b l a c k )
pygame . f o n t . i n i t ( )
11
s e l f . f o n t = pygame . f o n t . Font ( f r e e s a n s b o l d . t t f , 1 4)
s e l f . update ( )
def p r e s s e d ( s e l f , mouse ) :
# i f mouse r i g h t or l e f t i s w i t h i n t h e b u t t o n
i f mouse [ 0 ] > s e l f . r e c t . t o p l e f t [ 0 ] and mouse [ 1 ] > s e l f . r e c t . t o p l e f t [ 1 ] :
i f mouse [ 0 ] < s e l f . r e c t . b o t t o m r i g h t [ 0 ] and mouse [ 1 ] < s e l f . r e c t . b o t
return True
return F a l s e
def draw ( s e l f , d i s p l a y s u r f a c e ) :
display surface . b l i t ( s e l f . surface , s e l f . rect )
def
update ( s e l f ) :
w = s e l f . r e c t . width
h = s e l f . rect . height
# f i l l t h e b u t t o n background
s e l f . surface . f i l l ( s e l f . bg color )
# r e n d e r t h e c a p t i o n and r e t u r n a r e c t a n g l e
c a p t i o n s u r f = s e l f . f o n t . r e n d e r ( s e l f . c a p t i o n , True , s e l f . f g c o l o r , s e l f
caption rect = caption surf . get rect ()
# i n f l a t e i n p l a c e , moves t h e t e x t t o a more p l e a s i n g s p o t i n t h e b u t t o
c a p t i o n r e c t . i n f l a t e i p ( 10 , 17)
# commits t h e c a p t i o n
s e l f . surface . blit ( caption surf , caption rect )
# draw b o r d e r f o r normal b u t t o n
pygame . draw . r e c t ( s e l f . s u r f a c e , pygame . Color ( b b l a c k ) , pygame . Rect ( ( 0 ,
pygame . draw . l i n e ( s e l f . s u r f a c e , pygame . Color ( b white ) , ( 1 , 1 ) , (w 2 ,
pygame . draw . l i n e ( s e l f . s u r f a c e , pygame . Color ( b white ) , ( 1 , 1 ) , ( 1 , h
pygame . draw . l i n e ( s e l f . s u r f a c e , pygame . Color ( b darkgray ) , ( 1 , h 1 ) , (
pygame . draw . l i n e ( s e l f . s u r f a c e , pygame . Color ( b darkgray ) , (w 1 , 1 ) , (
pygame . draw . l i n e ( s e l f . s u r f a c e , pygame . Color ( b gray ) , ( 2 , h 2 ) , (w
pygame . draw . l i n e ( s e l f . s u r f a c e , pygame . Color ( b gray ) , (w 2 , 2 ) , (w
c l a s s App :
init ( self ):
def
s e l f . b u t t o n w i d t h = 20
s e l f . b u t t o n s p a c i n g = 100
self . button offset = 0
s e l f . b u t t o n r i g h t m o s t = 840
s e l f . m s g d i s p l a y t o p l e f t = 2 0 , 25
s e l f . running = True
s e l f . d i s p l a y s u r f = None
s e l f . s i z e = s e l f . weight , s e l f . h e i g h t = 9 6 0 , 720
s e l f . b g c o l o r = None
s e l f . p o i n t c o l o r = None
s e l f . h u l l c o l o r = None
12
s e l f . mouse x , s e l f . mouse y = 0 , 0
s e l f . f o n t o b j = None
s e l f . points = [ ]
s e l f . ch points = [ ]
s e l f . msg = C l i c k p o i n t s then Get H u l l
#s e l f . b t n i n t e r a c t i v e = Button ( I n t e r a c t i v e , ( s e l f . b u t t o n r i g h t m o s t
#s e l f . b u t t o n o f f s e t = s e l f . b u t t o n s p a c i n g
s e l f . b t n r e s e t = Button ( Reset , ( s e l f . b u t t o n r i g h t m o s t s e l f . b u t t o n
s e l f . b u t t o n o f f s e t += s e l f . b u t t o n s p a c i n g
s e l f . b t n g e t c o n v e x = Button ( Get H u l l , ( s e l f . b u t t o n r i g h t m o s t s e l f
def o n i n i t ( s e l f ) :
pygame . i n i t ( )
s e l f . f o n t o b j = pygame . f o n t . Font ( f r e e s a n s b o l d . t t f , 24 )
#s e l f . f o n t o b j = pygame . f o n t . SysFont ( FreeMono . t t f , 24)
s e l f . b g c o l o r = pygame . Color ( 0 , 0 , 0 )
s e l f . p o i n t c o l o r = pygame . Color ( 2 5 5 , 2 5 5 , 255)
s e l f . h u l l c o l o r = pygame . Color ( b re d )
s e l f . d i s p l a y s u r f = pygame . d i s p l a y . set mode ( s e l f . s i z e )
s e l f . d i s p l a y s u r f = pygame . d i s p l a y . set mode ( ( 1 0 2 4 , 6 0 0 ) , HWSURFACE|DOUB
s e l f . running = True
def o n e v e n t ( s e l f , e v e n t ) :
i f e v e n t . type == QUIT :
s e l f . running = F a l s e
e l i f e v e n t . type == MOUSEBUTTONUP and e v e n t . button in ( 1 , 2 , 3 ) :
# i f e v e n t i s b u t t o n , make h u l l or r e s e t s c r e e n , e l s e add t o p o i n t
i f s e l f . b t n g e t c o n v e x . p r e s s e d ( e v e n t . pos ) :
s e l f . msg = Convex H u l l
del s e l f . c h p o i n t s [ : ]
s e l f . c h p o i n t s = convexHull ( s e l f . points )
e l i f s e l f . b t n r e s e t . p r e s s e d ( e v e n t . pos ) :
del s e l f . p o i n t s [ : ]
del s e l f . c h p o i n t s [ : ]
s e l f . msg = C l i c k p o i n t s then Get H u l l
else :
s e l f . p o i n t s . append ( e v e n t . pos )
s e l f . msg = x , y : + s t r ( e v e n t . pos )
def o n l o o p ( s e l f ) :
s e l f . display surf . f i l l ( s e l f . bg color )
s e l f . b t n g e t c o n v e x . draw ( s e l f . d i s p l a y s u r f )
s e l f . b t n r e s e t . draw ( s e l f . d i s p l a y s u r f )
# s e l f . b t n i n t e r a c t i v e . draw ( s e l f . d i s p l a y s u r f )
# draws o u t t h e r e g u l a r c o o r d i n a t e d o t s i f p o p u l a t e d
for coord in s e l f . p o i n t s :
pygame . draw . c i r c l e ( s e l f . d i s p l a y s u r f , s e l f . p o i n t c o l o r , coord , 3 , 0
# draws o u t t h e convex h u l l c o o r d i n a t e d o t s i f p o p u l a t e d
for coord in s e l f . c h p o i n t s :
13
# message d i s p l a y window
m s g s u r f a c e o b j = s e l f . f o n t o b j . r e n d e r ( s e l f . msg , F a l s e , s e l f . p o i n t c o l o
msg rect obj = msg surface obj . get rect ()
msg rect obj . topleft = ( s e l f . msg display top left [ 0 ] , s e l f . msg display
s e l f . display surf . b l i t ( msg surface obj , msg rect obj )
def o n r e n d e r ( s e l f ) :
pygame . d i s p l a y . update ( )
def o n c l e a n u p ( s e l f ) :
pygame . q u i t ( )
def e x e c u t e ( s e l f ) :
s e l f . on init ()
while s e l f . running :
for e v e n t in pygame . e v e n t . g e t ( ) :
s e l f . on event ( event )
s e l f . on loop ()
s e l f . on render ()
s e l f . on cleanup ()
if
6.2
name
== m a i n
l e t s g o = App ( )
l e t s g o . execute ()
Output
14
The above snapshot shows how the points are taken as input via mouse click.
In this snapshot we show how clicking the Get Hull button generates the convex hull.s
15