2012-02-09 110 views
7

我想使用graphviz为了绘制给定的图形所有最大的派系。因此,我希望在同一个最大派系中的节点将被视觉封装在一起(这意味着我希望一个大圆圈将围绕它们)。我知道集群选项存在 - 但在我目前看到的所有示例中 - 每个节点仅在一个集群中。在最大派系情况下,一个节点可以在多个派系中。 有没有一个选项可以用graphviz显示这个? 如果没有,是否有任何其他工具用于此任务(最好使用python api)。 谢谢。Graphviz - 绘制最大派系

回答

12

取茶,这会是很长:)

我画这个与networkx,但主要步骤可以很容易地转移到graphviz的。

的计划如下:
一)找到最大派系(以防万一,最大派系是没有必要的最大派系);
b)绘制图形并记住绘图程序使用的顶点坐标;
c)取派系的坐标,计算围绕它的圆的中心和半径;
d)绘制圆和颜色相同颜色的派系的verteces(对于2个和更多的maxcliques交集,这是不可能的)。

关于c):我懒得弄清楚紧密的圈子,但有一段时间可以轻松完成。相反,我计算了“近似圆”:我将该集团最长边的长度作为半径,并乘以1.3。实际上,通过这种方法,一些节点可能被排除在外,因为只有sqrt(3)商人保证每个人都在。但是,采用sqrt(3)会使该圈太大(再次,它不紧)。作为中锋,我占据了最大的优势。

import networkx as nx 
from math import * 
import matplotlib.pylab as plt 
import itertools as it 

def draw_circle_around_clique(clique,coords): 
    dist=0 
    temp_dist=0 
    center=[0 for i in range(2)] 
    color=colors.next() 
    for a in clique: 
     for b in clique: 
      temp_dist=(coords[a][0]-coords[b][0])**2+(coords[a][1]-coords[b][2])**2 
      if temp_dist>dist: 
       dist=temp_dist 
       for i in range(2): 
        center[i]=(coords[a][i]+coords[b][i])/2 
    rad=dist**0.5/2 
    cir = plt.Circle((center[0],center[1]), radius=rad*1.3,fill=False,color=color,hatch=hatches.next()) 
    plt.gca().add_patch(cir) 
    plt.axis('scaled') 
    # return color of the circle, to use it as the color for vertices of the cliques 
    return color 

global colors, hatches 
colors=it.cycle('bgrcmyk')# blue, green, red, ... 
hatches=it.cycle('/\|-+*') 

# create a random graph 
G=nx.gnp_random_graph(n=7,p=0.6) 
# remember the coordinates of the vertices 
coords=nx.spring_layout(G) 

# remove "len(clique)>2" if you're interested in maxcliques with 2 edges 
cliques=[clique for clique in nx.find_cliques(G) if len(clique)>2] 

#draw the graph 
nx.draw(G,pos=coords) 
for clique in cliques: 
    print "Clique to appear: ",clique 
nx.draw_networkx_nodes(G,pos=coords,nodelist=clique,node_color=draw_circle_around_clique(clique,coords)) 

plt.show() 

让我们看看我们得到:

>> Clique to appear: [0, 5, 1, 2, 3, 6] 
>> Clique to appear: [0, 5, 4] 

图片: Circled max cliques

3个maxcliques又如:

>> Clique to appear: [1, 4, 5] 
>> Clique to appear: [2, 5, 4] 
>> Clique to appear: [2, 5, 6] 

Circled max cliques

0

我不认为你可以这样做。聚类是通过子图完成的,这些子图预计是独立的图,与其他子图不重叠。

你可以改变可视化;如果您想象某个派系的成员是某个集合S的成员,那么您可以简单地添加一个节点S并添加将每个成员链接到S节点的定向或虚线边缘。如果S节点被赋予不同的形状,那么应该清楚哪些节点在哪个派系中。

如果你真的想要,你可以给连接成员的边缘连接到它们的团体节点高权重,这应该使它们在图形上靠得很近。

请注意,集团节点之间永远不会存在边缘;这将表明两个派系之间的关系最为密切,这只意味着他们实际上是一个大集团,而不是两个独立的集团。

0

实现起来会有点困难,但这里是一个如何绘制重叠集的例子。

  • Riche,N.H。; Dwyer,T。;“Untangling Euler Diagrams”,IEEE Transactions on Visualization and Computer Graphics,vol.16,no.6,pp.1090-1099,Nov.-Dec。 2010 DOI:10.1109/TVCG.2010.210PDF

enter image description here