def createGraph(dictuniq): g = SparseGraph(len(dictuniq), False, dtype = numpy.int32) mts = dictuniq.keys() for mt in mts: if mt.strip() != '': relmts = [k for k in mts if k.strip() != '' and k in mt and k != mt] relmts.sort(key=len, reverse=True) currentGrandchildren = set([]) for relmt in relmts: if relmt not in currentGrandchildren: g.addEdge(dictuniq[mt], dictuniq[relmt]) currentGrandchildren = currentGrandchildren.union([k for k in mts if k.strip() != '' and k in relmt]) return g
def construct_graph(samples, S): """ Constructs an undirected graph using the Another Python Graph Library (apgl) SparseGraph class. samples are the samples used to construct the graph S is the number of states This method returns the constructed graph object """ graph = SparseGraph(S) # loop through all samples # for each state transition # make the adjacency cell 1 for sample in samples: if graph[sample.state, sample.nextstate] != 1: graph.addEdge(sample.state, sample.nextstate) return graph
#----- Connect vertices # This just runs through and connects the vertices # This is pretty complicated, and you don't need to worry about it. The code here should be right becuase the red edges are draen correctly edgeCheck=[] for row in range((rowLength-1)): for column in range(colLength-1): x=column+1 y=row+1 vertex1=(((colLength-1)*row)+column) vertex2=(((colLength-1)*row)+column)+1 if((((colLength-1)*row)+column)<numVertices and edgeCheck.count([vertex2,vertex1])==0 and vertex1%(colLength-1)<(colLength-2)): if(not(obstacles.count([x,y])!=0 and obstacles.count([x,(y-1)])!=0)): #print "v1: " + str(vertex1) + "v2: " + str(vertex2) graph.addEdge(vertex1,vertex2, edge=1) vertex2=(((colLength-1)*row)+column)+(colLength-1) if((((colLength-1)*row)+column)<numVertices and edgeCheck.count([vertex2,vertex1])==0 and vertex2<numVertices): if(not(obstacles.count([x,y])!=0 and obstacles.count([(x-1),y])!=0)): #print "v1: " + str(vertex1) + "v2: " + str(vertex2) graph.addEdge(vertex1,vertex2, edge=1) #do the diagonals vertex2=(((colLength-1)*row)+column)+(colLength) if(obstacles.count([x,y])==0 and (((colLength-1)*row)+column)<numVertices and edgeCheck.count([vertex2,vertex1])==0 and vertex1%(colLength-1)<(colLength-1) and vertex1%(rowLength-1)<(rowLength-1) and vertex2<numVertices and vertex1%(colLength-1)!=(colLength-2)): graph.addEdge(vertex1,vertex2, edge=math.sqrt(2)) vertex1=(((colLength-1)*row)+column) vertex2=(((colLength-1)*row)+column)+(colLength-2)