Upload
ritajit-majumdar
View
2
Download
0
Embed Size (px)
DESCRIPTION
It contains certain algorithms and their solutions
Citation preview
Algorithm Assignment
Ritajit Majumdar
Mtech 1st semesterComputer Science and Engineering
Class Roll: 1
Exam Roll: 97/CSM/140001Registration No: 0029169 of 2008-2009
March 26, 2015
Contents
1 Problem 1 11.1 Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11.2 Output . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2
2 Problem 2 32.1 Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32.2 Output . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
3 Problem 3 43.1 Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 43.2 Output . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
4 Problem 4 64.1 Visualization . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 64.2 Remarks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8
5 Problem 5 85.1 Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 85.2 Output . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10
6 Problem 6 106.1 Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 106.2 Output . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14
1 Problem 1
Given n-points in the plane and the sequence of h points, write a program to determine if these hpoints are the points generating the convex hull in counter-clockwise order.
1.1 Code
from as t import l i t e r a l e v a l
def convex hu l l ( po in t s ) :# sor t po in t s l e x i c o g r a p h i c a l l y# tak ing s e t to remove d u p l i c a t e spo in t s = sor t ed ( s e t ( po in t s ) )
i f l en ( po in t s ) <= 1 :return po in t s
# 2d cros s product o f OA and OB vec t o r s# p o s i t i v e va lue i f OAB makes a counter−c l o c kw i s e turn# nega t i v e f o r c l o c kw i s e turn , zero f o r c o l l i n e a rdef 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 ] )
# bu i l d upper h u l lupper = [ ]for p in r eve r s ed ( po in t s ) :
while l en ( upper ) >= 2 and c r o s s ( upper [−2] , upper [−1] ,p ) <= 0 :upper . pop ( )
upper . append (p)
# bu i l d lower h u l llower = [ ]for p in po in t s :
while l en ( lower ) >= 2 and c r o s s ( lower [−2] , lower [−1] ,p ) <= 0 :lower . pop ( )
lower . append (p)
# concatenat ion o f lower and upper h u l l# l a s t po in t o f each l i s t i s omit ted because i t i s repea ted# at the beg inn ing o f o therreturn lower [ : −1 ] + upper [ : −1 ]
#to take the input po in t sdef ge t coo rd s ( ) :
print ” Enter a l i s t o f po in t s . For example ( 0 , 0 ) , ( 0 , 1 ) , ( 1 , 1 ) , ( 1 , 0 ) ”po in t s = raw input ( )
try :return l i s t ( l i t e r a l e v a l ( po in t s ) )
except SyntaxError :
1
print ”The coo rd ina t e s should be entered in p r e s c r i b e d form”
#conv e x hu l l ( [ ( i /10 , i %10) f o r i in range (100 ) ] )print ” Enter the s e t o f n po in t s ”po in t s = ge t coo rd s ( )print ”The po in t s you gave are ”print po in t sh u l l = convex hu l l ( po in t s )print ”\nThe convex h u l l f o r the po in t s you provided : ”print h u l lprint ”\nEnter the h u l l po in t s h to be checked ”h = ge t coo rd s ( )print ”The po in t s you provided as h u l l po in t s ”print hi s p r e s e n t = Fal sei f l en ( h u l l ) == len (h ) :
i s p r e s e n t = Truefor i in range ( l en ( h u l l ) ) :
i f h [ i ] not in h u l l :i s p r e s e n t = Fal sebreak
i f i s p r e s e n t == True :print ”\nThe po in t s you provided are h u l l po in t s \
in counter−c l o ckw i s e order ”else :
print ”\nThe po in t s you provided are not h u l l po in t s \in counter−c l o ckw i s e order ”
1.2 Output
A snapshot of the output is attached below.
2
2 Problem 2
Write a program for finding the convex hull in two-dimensional space using divide and conquerstrategy.
2.1 Code
from numpy . random import randimport matp lo t l i b . pyplot as p l tfrom operator import i t emge t t e rfrom numpy import concatenate
print ”The po in t s are generated randomly”raw input ( ” Press ente r to generate the convex h u l l ” )po in t s = rand (50 ,2 )
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 ] )
def mergeHull ( l e f t h u l l , r i g h t h u l l ) :l p o i n t s = l e f t h u l l . s o r t ( key=i t emge t t e r ( 0 ) )r p o i n t s = r i g h t h u l l . s o r t ( key=i t emge t t e r ( 0 ) )
idx1 = l p o i n t s [ l en ( l p o i n t s ) ] . index ( )idx2 = r p o i n t s [ 0 ] . index ( )
while c r o s s ( l p o i n t s [ idx2 ] , r p o i n t s [ idx1 ] , r p o i n t s [ idx1 +1]) <= 0 :idx1 += 1
while c r o s s ( r p o i n t s [ idx1 ] , l p o i n t s [ idx2 ] , l p o i n t s [ idx2 −1]) >= 0 :idx2 −= 1
return l p o i n t s [ : idx1 ] + r p o i n t s [ idx2 : ]
def ConvexHull ( po in t s ) :po in t s = sor t ed ( s e t ( po in t s ) )i f l en ( po in t s ) <= 1 :
return po in t selse :
mid = len ( po in t s )/2l e f t h u l l = ConvexHull ( po in t s [ : mid ] )r i g h t h u l l = ConvexHull ( po in t s [ mid : ] )
return mergeHull ( l e f t h u l l , r i g h t h u l l )
h u l l = ConvexHull ( po in t s )p l t . p l o t ( po in t s [ : , 0 ] , po in t s [ : , 1 ] , ’ o ’ )for s in h u l l . s i m p l i c e s :
p l t . p l o t ( po in t s [ s , 0 ] , po in t s [ s , 1 ] , ’ k− ’ )p l t . p l o t ( po in t s [ h u l l . v e r t i c e s , 0 ] , po in t s [ h u l l . v e r t i c e s , 1 ] , ’ r−− ’ , lw=2)p l t . p l o t ( po in t s [ h u l l . v e r t i c e s [ 0 ] , 0 ] , po in t s [ h u l l . v e r t i c e s [ 0 ] , 1 ] , ’ ro ’ )
3
p l t . t i t l e ( ’ Convex Hul l ’ )#p l t . show ()p l t . s a v e f i g ( ’ / Users /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 plotof the output is attached below.
3 Problem 3
Implement the divide and conquer closest pair algorithm in language of your choice.
3.1 Code
from math import sqrt , powfrom as t import l i t e r a l e v a l
def d i s t ance ( a , b ) :return s q r t (pow( a [0]−b [ 0 ] , 2 ) + pow( a [1]−b [ 1 ] , 2 ) )
def bruteMin ( points , cur r ent=f l o a t ( ” i n f ” ) ) :
4
i f l en ( po in t s ) < 2 :return cur r ent
else :head = po in t s [ 0 ]del po in t s [ 0 ]newMin = min ( [ d i s t ance ( head , x ) for x in po in t s ] )newCurrent = min ( [ newMin , cur rent ] )return bruteMin ( points , newCurrent )
def divideMin ( po in t s ) :h a l f = l en ( so r t ed ( po in t s ) )/2minimum = min ( [ bruteMin ( po in t s [ : h a l f ] ) , bruteMin ( po in t s [ h a l f : ] ) ] )nearLine = f i l t e r (lambda x : x [ 0 ] > h a l f − minimum and \
x [ 0 ] < h a l f + minimum , po in t s )return round (min ( [ bruteMin ( nearLine ) ,minimum ] ) , 2 )
def ge t coo rd s ( ) :print ” Enter a l i s t o f po in t s . \
For example ( 0 , 0 ) , ( 0 , 1 ) , ( 1 , 1 ) , ( 1 , 0 ) ”po in t s = raw input ( )
try :return l i s t ( l i t e r a l e v a l ( po in t s ) )
except SyntaxError :print ”The coo rd ina t e s should be entered in p r e s c r i b e d form”
print ” Enter the s e t o f n po in t s ”po in t s = ge t coo rd s ( )print ”The po in t s you gave are ”print po in t s
minimum = divideMin ( po in t s )print ”\nThe minimum d i s t ance between the po in t s you provided i s ”print minimum
3.2 Output
The snapshot of the output is attached below.
5
4 Problem 4
Find a visualization of an algorithm for the closest pair problem on the web.
4.1 Visualization
Visualization of closest pair problem is available at the site http://alvie.algoritmica.org/alvie3/visualizations. This website contains visualization for many algorithms, closest pair being one ofthem. However, in the visualization page, the video is not ready for playing. From this page, weneed to go to downloads page http://alvie.algoritmica.org/alvie3/downoads.
In this page, all the videos, which are in .swf format, are available for both viewing and down-loading. At the closest pair algorithm, clicking on the view option leads us to the url Visulaizationof closest pair.
The visualization shows all the steps of the algorithm as they occur. It is not possible to give allthe snapshots. So I am only attaching 4 screenshots which will hopefully give an essence of how thealgorithm works.
6
7
4.2 Remarks
These screenshots hopefully provides an essence of how the algorithm works and the major stepsof its implementation. Every step is shown in the video. For better understanding, refer to the urlprovided.
5 Problem 5
Implement the maximum flow algorithm in a network with a language of your choice. Consider bothdirect and reverse flow direction.
5.1 Code
class Edge ( ob j e c t ) :def i n i t ( s e l f , u , v ,w) :
s e l f . source = us e l f . s ink = vs e l f . capac i ty = w
def r e p r ( s e l f ) :return ”%s −> %s : %s ” % ( s e l f . source , s e l f . s ink , s e l f . capac i ty )
class FlowNetwork ( ob j e c t ) :def i n i t ( s e l f ) :
s e l f . adj = {}s e l f . f l ow = {}
def add vertex ( s e l f , ve r tex ) :
8
s e l f . adj [ ve r tex ] = [ ]
def ge t edge s ( s e l f , v ) :return s e l f . adj [ v ]
def add edge ( s e l f , u , v ,w=0):i f u == v :
raise ValueError ( ”Same ver tex . Edge not p o s s i b l e ” )edge = Edge (u , v ,w)r edge = Edge (v , u , 0 )edge . r edge = r edger edge . r edge = edges e l f . adj [ u ] . append ( edge )s e l f . adj [ v ] . append ( r edge )s e l f . f l ow [ edge ] = 0s e l f . f l ow [ r edge ] = 0
def f i nd path ( s e l f , source , s ink , path ) :i f source == s ink :
return pathfor edge in s e l f . g e t edge s ( source ) :
r e s i d u a l = edge . capac i ty − s e l f . f l ow [ 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 nd path ( edge . s ink , s ink , 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 , source , s ink ) :path = s e l f . f i nd path ( source , s ink , [ ] )while path != None :
r e s i d u a l s = [ edge . capac i ty − s e l f . f l ow [ edge ] for edge in path ]f low = min ( r e s i d u a l s )for edge in path :
s e l f . f l ow [ edge ] += f lows e l f . f l ow [ edge . r edge ] −= flow
path = s e l f . f i nd path ( source , s ink , [ ] )return sum( s e l f . f low [ edge ] for edge in s e l f . g e t edge s ( source ) )
i f name == ’ ma in ’ :g = FlowNetwork ( )[ g . add vertex ( v ) for v in ” sopqrt ” ]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 edge ( ’ q ’ , ’ t ’ , 2 )print ”\nThe graph i s s −− o −− p −− q −− r −− t ”print ”The source i s s and the s ink i s t ”print ”The f low through edges i s : ”print ” s − o −−> 3”print ” s − p −−> 3”print ”o − p −−> 2”print ”o − q −−> 3”print ”p − r −−> 2”print ” r − t −−> 3”print ”q − r −−> 4”print ”q − t −−> 2\n”print ”The maximum f low through t h i s network i s ” , g . max flow ( ’ s ’ , ’ t ’ )
5.2 Output
The snapshot of the output is attached below.
6 Problem 6
Write a program that accepts mouse clicks in a window and draws the convex hull of the pointsclicked.
6.1 Code
There are two different programs - one to implement the gui and the other to implement the convexhull
ConvexHull.py
def convexHull ( p o i n t s L i s t ) :lowerHul l = [ ]upperHull = [ ]i f l en ( p o i n t s L i s t ) <= 0 :
10
return p o i n t s L i s t#have to s o r t l i s t f i r s tp o i n t s L i s t . s o r t ( key=lambda tup : tup [ 0 ] )n = len ( p o i n t s L i s t )j = 0#then c a l c u l a t e lower h u l lfor idx in range (n ) :
#check ing f o r counter c l ockwi se−nesswhile l en ( lowerHul l ) >= 2 and \
c r o s s ( lowerHul l [ j − 2 ] , lowerHul l [ j − 1 ] , p o i n t s L i s t [ idx ] ) <= 0 :lowerHul l . pop ( )j −= 1
lowerHul l . append ( p o i n t s L i s t [ idx ] )j += 1
j = 0idx = n − 1#then upper h u l l from reve r s e o f p o i n t sL i s tfor i in range (n ) :
while l en ( upperHull ) >= 2 and \c r o s s ( upperHull [ j − 2 ] , upperHull [ j − 1 ] , p o i n t s L i s t [ idx ] ) <= 0 :
upperHull . pop ( )j −= 1
upperHull . append ( p o i n t s L i s t [ idx ] )idx −= 1j += 1
#pop l ow e r hu l l b/c i t con ta ins the 1 s t po in t then combine wi th upper and re turnl owerHul l . pop ( )return l owerHul l + upperHull
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 ] )
launch.py
import pygamefrom pygame . l o c a l s import ∗from convexHull import convexHull
class Button :def i n i t ( s e l f , button message , c oo rd ina t e s ) :
s e l f . capt ion = ” ”+button messages e l f . btn width = 90s e l f . b tn he ight = 30s e l f . r e c t = pygame . Rect ( coo rd ina t e s [ 0 ] , c oo rd ina t e s [ 1 ] , s e l f . btn width , s e l f . b tn he ight )s e l f . s u r f a c e = pygame . Sur face ( 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 lack ’ )pygame . f ont . i n i t ( )
11
s e l f . f on t = pygame . f ont . Font ( ’ f r e e s a n s b o l d . t t f ’ , 14)s e l f . update ( )
def pres s ed ( s e l f , mouse ) :# i f mouse r i g h t or l e f t i s w i th in the bu t toni 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 . bottomright [ 0 ] and mouse [ 1 ] < s e l f . r e c t . bottomright [ 1 ] :return True
return False
def draw ( s e l f , d i s p l a y s u r f a c e ) :d i s p l a y s u r f a c e . b l i t ( s e l f . su r f ace , s e l f . r e c t )
def update ( s e l f ) :w = s e l f . r e c t . widthh = s e l f . r e c t . he ight
# f i l l the but ton backgrounds e l f . s u r f a c e . f i l l ( s e l f . b g c o l o r )# render the cap t ion and re turn a r e c t an g l ec a p t i o n s u r f = s e l f . f ont . render ( s e l f . capt ion , True , s e l f . f g c o l o r , s e l f . b g c o l o r )c a p t i o n r e c t = c a p t i o n s u r f . g e t r e c t ( )# i n f l a t e in p lace , moves the t e x t to a more p l e a s i n g spo t in the bu t tonc a p t i o n r e c t . i n f l a t e i p (−10 , −17)# commits the cap t ions e l f . s u r f a c e . b l i t ( c a p t i o n s u r f , c a p t i o n r e c t )
# draw border f o r normal bu t tonpygame . draw . r e c t ( s e l f . su r f ace , pygame . Color (b ’ b lack ’ ) , pygame . Rect ( ( 0 , 0 , w, h ) ) , 1)pygame . draw . l i n e ( s e l f . su r f ace , pygame . Color (b ’ white ’ ) , (1 , 1 ) , (w − 2 , 1 ) )pygame . draw . l i n e ( s e l f . su r f ace , pygame . Color (b ’ white ’ ) , (1 , 1 ) , (1 , h − 2) )pygame . draw . l i n e ( s e l f . su r f ace , pygame . Color (b ’ darkgray ’ ) , (1 , h − 1) , (w − 1 , h − 1) )pygame . draw . l i n e ( s e l f . su r f ace , pygame . Color (b ’ darkgray ’ ) , (w − 1 , 1 ) , (w − 1 , h − 1) )pygame . draw . l i n e ( s e l f . su r f ace , pygame . Color (b ’ gray ’ ) , (2 , h − 2) , (w − 2 , h − 2) )pygame . draw . l i n e ( s e l f . su r f ace , pygame . Color (b ’ gray ’ ) , (w − 2 , 2 ) , (w − 2 , h − 2) )
class App :def i n i t ( s e l f ) :
s e l f . button width = 20s e l f . but ton spac ing = 100s e l f . b u t t o n o f f s e t = 0s e l f . but ton r ight most = 840s e l f . m s g d i s p l a y t o p l e f t = 20 , 25s e l f . running = Trues e l f . d i s p l a y s u r f = Nones e l f . s i z e = s e l f . weight , s e l f . he ight = 960 , 720s e l f . b g c o l o r = Nones e l f . p o i n t c o l o r = Nones 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 , 0s e l f . f o n t o b j = Nones e l f . po in t s = [ ]s e l f . ch po in t s = [ ]s e l f . msg = ” Cl i ck po in t s then ’ Get Hul 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 on r i g h t mo s t − s e l f . b u t t o n o f f s e t , s e l f . bu t t on w id th ) )#s e l f . b u t t o n o f f s e t = s e l f . bu t t on spac ings e l f . b t n r e s e t = Button ( ” Reset ” , ( s e l f . but ton r ight most − s e l f . b u t t o n o f f s e t , s e l f . button width ) )s e l f . b u t t o n o f f s e t += s e l f . button spac ings e l f . btn get convex = Button ( ”Get Hul l ” , ( s e l f . but ton r ight most − s e l f . b u t t o n o f f s e t , s e l f . button width ) )
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 ont . 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 on 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 (255 , 255 , 255)s e l f . h u l l c o l o r = pygame . Color (b ’ red ’ )s e l f . d i s p l a y s u r f = pygame . d i sp l ay . 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 sp l ay . set mode ( (1024 , 600) , HWSURFACE|DOUBLEBUF|RESIZABLE)s e l f . running = True
def on event ( s e l f , event ) :i f event . type == QUIT:
s e l f . running = Falsee l i f event . type == MOUSEBUTTONUP and event . button in (1 , 2 , 3 ) :
# i f event i s button , make h u l l or r e s e t screen , e l s e add to po in t l i s ti f s e l f . btn get convex . pre s s ed ( event . pos ) :
s e l f . msg = ”Convex Hul l ”del s e l f . ch po in t s [ : ]s e l f . ch po in t s = convexHull ( s e l f . po in t s )
e l i f s e l f . b t n r e s e t . p re s s ed ( event . pos ) :del s e l f . po in t s [ : ]del s e l f . ch po in t s [ : ]s e l f . msg = ” Cl i ck po in t s then ’ Get Hul l ’ ”
else :s e l f . po in t s . append ( event . pos )s e l f . msg = ”x , y : ” + s t r ( event . pos )
def on loop ( s e l f ) :s e l f . d i s p l a y s u r f . f i l l ( s e l f . b g c o l o r )s e l f . btn get convex . 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 out the r e gu l a r coord ina te do t s i f popu la tedfor coord in s e l f . po in 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 out the convex h u l l coord ina te do t s i f popu la tedfor coord in s e l f . ch po in t s :
13
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 . h u l l c o l o r , coord , 3 , 0)# draws the edges to show the convex h u l l i f popu la tedi f l en ( s e l f . ch po in t s ) > 0 :
pygame . draw . l i n e s ( s e l f . d i s p l a y s u r f , s e l f . h u l l c o l o r , True , s e l f . ch po int s , 1)
# message d i s p l a y windowmsg sur f a c e ob j = s e l f . f o n t o b j . render ( s e l f . msg , False , s e l f . p o i n t c o l o r )msg rec t ob j = msg su r f a c e ob j . g e t r e c t ( )msg rec t ob j . t o p l e f t = ( s e l f . m s g d i s p l a y t o p l e f t [ 0 ] , s e l f . m s g d i s p l a y t o p l e f t [ 1 ] )s e l f . d i s p l a y s u r f . b l i t ( msg sur face ob j , msg rec t ob j )
def on render ( s e l f ) :pygame . d i sp l ay . update ( )
def on cleanup ( s e l f ) :pygame . qu i t ( )
def execute ( s e l f ) :s e l f . o n i n i t ( )while s e l f . running :
for event in pygame . event . get ( ) :s e l f . on event ( event )
s e l f . on loop ( )s e l f . on render ( )
s e l f . on c leanup ( )
i f name == ” main ” :l e t s g o = App( )l e t s g o . execute ( )
6.2 Output
Two snapshots of the output are provided below.
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