def radialAssign(g, sink=False): global visited visited = list() graph = g.copy() # graph = g # print graph gSorted = list() visited = list() # Toposort goes HERE: GA = GraphAnalysis(graph) gSorted = GA.topologicalSort() if len(gSorted) < 1: gSorted.append(graph.getNodeList()[0]) gSorted[0].PosX = 0 gSorted[0].PosY = 0 # root.PosX=0 # root.PosY=0 d = False if graph.Type == 1: d = True # print graph visited.append(gSorted[0]) # TODO: Add support for multiple roots __recRadial(graph, gSorted[0], 360, 0, 1, d) # print graph return graph
def tikGraph(graph, l = None): analysis = GraphAnalysis(graph) n = analysis.numberOfNodes() #scaleFactor = 1 / log(n) scaleFactor = 0.9 #print n, scaleFactor #exit() output = "" output += "% Tik output from JumanG\n" output += "\\documentclass[class=minimal,border=0pt]{article}\n" output += "\\usepackage{tikz}\n" output += "\\pagestyle{empty}\n" output += "\\usepackage{verbatim}\n" output += "\\begin{document}\n" #output += "\\resizebox{100}{100}{\n" output += "\\begin{tikzpicture}[scale="+"{0:.2f}".format(scaleFactor)+", transform shape]\n" output += "\t\\tikzstyle{every node} = [circle, fill=gray!30, minimum size = 1.8cm]\n" for n in graph.getNodeList(): cleanedName = n.Name cleanedName = cleanedName.replace("_","") x = n.PosX y = n.PosY output+="\t\\node ("+cleanedName+") at ("+"{0:.2f}".format(n.PosX)+", "+"{0:.2f}".format(n.PosY)+") {"+cleanedName+"};\n" for n1 in graph.getNodeList(): for (n2, t) in graph.AdjacencyList[n1]: cleanedN = n1.Name cleanedN = cleanedN.replace("_", "") cleanedN2 = n2.Name cleanedN2 = cleanedN2.replace("_","") if t.Type==1: output+="\t\\draw [->] ("+cleanedN+")--("+cleanedN2+");\n" else: output+="\t\\draw [-] ("+cleanedN+")--("+cleanedN2+");\n" output += "\\end{tikzpicture}\n" #output += "}\n" output += "\\end{document}\n" if l: f=open(l, "w") f.write(output) else: print output
def arrange(g): global toAdjust graph = g.copy() GA = GraphAnalysis(graph) nodepairs = GA.topologicalSort(True) #(_,lengthOfGraph) = nodepairs[-1] #armLength = 3 / log(lengthOfGraph) nodes = list() nodes.append(list()) most = 0 current = 0 layerCount = {} for (n, l) in nodepairs: layerCount[l] = layerCount.get(l, 0) + 1 if l > current: nodes.append(list()) nodes[l].append(n) for layer in layerCount: count = layerCount[layer] if count > most: most = count # Here, we try to minimize edge crossings between the nodes if len(nodes) > 2: # We keep track of two layers. Our current layer and the layer above it. layer1 = nodes[0] layer2 = nodes[1] for i in xrange(2, len(nodes)): layer3 = nodes[i] layer1Size = len(layer1) layer2Size = len(layer2) layer3Size = len(layer3) #print "layer 1 size: ", layer1Size #print "layer 2 size: ", layer2Size weightedLayer2Values = [] for nodej in xrange(len(layer2)): nodejWeight = 0 nodejCount = 0 node2 = layer2[nodej] for nodei in xrange(len(layer1)): node1 = layer1[nodei] isAdjacent = graph.isAdjacent(node1, node2) if isAdjacent: nodejCount += 1 nodejWeight += nodei * layer1Size for nodek in xrange(len(layer3)): node3 = layer3[nodek] isAdjacent = graph.isAdjacent(node2, node3) if isAdjacent: nodejCount += 1 nodejWeight += nodek * layer3Size normalizedWeight = nodejWeight / nodejCount #print node2.Name, "normalizedWeight:",normalizedWeight, nodejWeight, nodejCount weightedLayer2Values.append((node2, normalizedWeight)) sorted_by_second = sorted(weightedLayer2Values, key=lambda tup: tup[1]) for nodej in xrange(len(layer2)): layer2[nodej] = sorted_by_second[nodej][0] layer1 = layer2 layer2 = layer3 layout(nodes, most) return graph
def __init__(self, infile): self.Parser = DotParser() self.Graph = self.Parser.readFile(infile) self.Analysis = GraphAnalysis(self.Graph) self.State = NBODY