Professional Documents
Culture Documents
solveralgorithm
MoSIGM1
Group2
Presentedby
AveekMukherjee
DinaraSeilkhanova
UdhayanVenugopal
ValentinPruliere
WaqasImtiaz
Contents:
TRAPI:MazeGeneration
MazeGenerationAlgorithm:
Session2:WorkSession
Trap2:MazeSolvingAlgorithm
Algorithmtosolvethemaze
Trap4:ComplexityAnalysis
SpaceComplexitytostorethemaze
TimeComplexityinsolvingthemaze
MoSIGM2|Group2|RandomMazeGenerationandSolvingAlgorithmAnalysis
1
TRAPI:MazeGeneration
MazeGenerationAlgorithm:
Ourinputisaroomwithwidthandheightofmandnrespectively.Wecan
createamazewithinthisroombyrecursivelydividingeachroom.Wedividea
roomalongthewidthiftheroomislargerinwidththanitsheight,andalong
theheightotherwise.Theratioofsplitischosenrandomly,byourrandom
numbergenerator.Nowforeachroomwecanrepeatthesplittingprocessuntil
thesumofwidthandheightofeachroomislessthan200pixels(thisvalueis
arbitrarilychosenbyus).(Figure1).
Figure1Splittingtheroomof10*12dimensioninto4randomrooms
Session2:WorkSession
Our maze structure is represented as a binary tree, each node containing the
information about the height and width of the space created by recursive
division.Ourmazedatastructureslooklikethis:
typedefstruct_cell{
intheight
intwidth
}cell
typedefstruct_tree{
MoSIGM2|Group2|RandomMazeGenerationandSolvingAlgorithmAnalysis
2
cell*room
struct_tree*first_child
struct_tree*second_child
struct_tree*parent
}tree
typedefstruct_maze{
tree*tr
tree*start
tree*end
}maze
PSEUDOCODE:
typeMaze:
typeTree:
root:Tree
current:Room
left:Tree
right:Tree
parent:Tree
typeRoom:width:integer
height:integer
Mazemaze_random(width:int,height:int):
Mazem
m.root.current.height=height
m.root.current.width=width
recursive_split(m.root)
returnm
MoSIGM2|Group2|RandomMazeGenerationandSolvingAlgorithmAnalysis
3
//For the following function we use the random(x,y) method, which returns a
//numbern,x<n<y
voidrecursive_split(Treet):
if(t.current.height+t.current.width<=200):
return
if(t.current.height>t.current.width){
//splitalongheight
new_height=random(0,t.current.height)
t.left.current.height=new_height
t.left.current.width=t.current.width
t.right.current.height=t.current.heightnew_height
t.right.current.width=t.current.width
}else{
//Similarlysplitalongwidth
}
//splitthechildren
recursive_split(t.left)
recursive_split(t.right)
Trap2:MazeSolvingAlgorithm
Algorithmtosolvethemaze
Solving the maze in this case means finding the path between the desired leaf
nodes of the tree. Our group proposes an algorithm which is based on
traversingthroughthenodestoseekforthelastcommonancestor.
Implementation
:
1. Findthelevelofthefirstnode,letscallitd1.
2. Findthelevelofthesecondnode,letscallitd2.
3. Move the pointer from the node that has the biggest level to position
wherelevelsoftheothernodeandthispointerwillbethesame.
MoSIGM2|Group2|RandomMazeGenerationandSolvingAlgorithmAnalysis
4
4. Now both node and pointer are in the same level in tree. Move them
simultaneouslyuntiltheymeeteachother.
5. Thenodewheretheymeetisthelongestcommonancestor
PSEUDOCODE:
//FindthedepthoftherootofTreeswithinthetreeofthemaze
intFind_depth(Mazem,Trees):
intdepth=0
while(s!=m.root)
s=s.parent
depth++
returndepth
//Wewillprintapathfromstarttotheend
voidprint_path(Mazem,Trees,Treee):
intd1=Find_depth(m,start)
intd2=Find_depth(m,end)
intshift=|d1d2|
Treestart=s
Treeend=e
Treelca
if(d1>d2):
//shiftthepointerfromstart
while(shift>0):
//printlinebetweenstartandparent
start=start.parent
shift=shift1
elseif(d1<d2):
//shift the pointer from end and keep printing the lines between
//succesiveends
MoSIGM2|Group2|RandomMazeGenerationandSolvingAlgorithmAnalysis
5
while(start!=end):
//shiftbothpointersstartandend,wheretheymeet
//printlinebetweenstartandparent
start=start.parent
//printlinebetweenendandparent
end=end.parent
remaining_shift=remaining_shift1
Trap4:TimeSpaceComplexityAnalysis
SpaceComplexitytostorethemaze
Everytimewe introducearoomwe areessentiallycreatingasinglenodeinthe
tree.At anyinstance oftime,thetotalnumberofleafnodesofthetreegivesthe
totalnumberofrooms inour designatedmaze.Incase,ifwesplitanyroomthat
resultsincreating two new leaf nodesas thechild ofthatroom.Ultimately,we
aredealingwithafullpledgedBinaryTree.
Thetotalnumberofnodesinthetreeare
1+2+4+2h
=2h+1
1
.
h
So, ifwetakethetotalnumberofleafnodes(rooms)inourmazeasn,n=2
.
h+1
2
2
The total number of nodes in the tree ,
2 1 = n
1
. We have n
1 , space
complexity.
2
Therefore
spacecomplexityisofO(n
)
.
TimeComplexityinsolvingthemaze
Thelengthofthepathissumoflengthsfromfirstnodetocommonancestorand
secondnodetocommonancestor.
Lets computethe timerequiredtofindapathbetweentwonodesatheightd1
andd2respectively.
MoSIGM2|Group2|RandomMazeGenerationandSolvingAlgorithmAnalysis
6
MoSIGM2|Group2|RandomMazeGenerationandSolvingAlgorithmAnalysis
7