DFS algorithm implementation at Objective C -
DFS algorithm implementation at Objective C -
i trying implement objective c realization of algorithm. here implementation of it:
@implementation dfsalgorithm -(void)dfs:(graph*)g andstartingposition:(int)s{ [self performdfs:g andposition:s]; } -(void)markedarrayinit:(int)capacity{ //0 unmarked vertices //1 form marked ones self.marked=[[nsmutablearray alloc]initwithcapacity:capacity]; for(int i=0;i<[self.marked count];i++) [self.marked replaceobjectatindex:i withobject:[nsnumber numberwithint:0]]; } -(void)performdfs:(graph *)g andposition:(int)v{ [self markedarrayinit:(int)[g numberofvertices]]; [self.marked replaceobjectatindex:v withobject:[nsnumber numberwithint:1]]; (nsnumber *vertex in [g.vertices objectatindex:v]){ if(1==[self ismarked:v atgraph:g]){ nslog(@"%d",(int)vertex); [self performdfs:g andposition:(int)vertex]; } } } -(int)ismarked:(int)v atgraph:(graph *)g{ homecoming [self.marked objectatindex:v]; } @end
however, don't understand why next error occurs:
[__nsarraym replaceobjectatindex:withobject:]: index 0 beyond bounds empty array'
how can correctly initialize marked array?
thank you.
an nsmutablearray
created empty, capacity value pass hint implementation how big expect array become.
therefore replaceobjectatindex:withobject:
not work you, array empty have no objects replace.
instead utilize addobject:
capacity
times.
objective-c algorithm
Comments
Post a Comment