简体   繁体   English

使用jgrapht使用聚类创建图

[英]Creating graph with clusters using jgrapht

Is there any way to create a graph with clusters using jgrapht? 有什么方法可以使用jgrapht创建带有聚类的图? Example graph with two clusters "process #1" and "process #2": 具有两个集群“进程1”和“进程2”的示例图:

在此处输入图片说明

Expected dot file content : 预期的点文件内容:

digraph G {
subgraph cluster_0 {
style=filled;
color=lightgrey;
node [style=filled,color=white];
a0 -> a1 -> a2 -> a3;
label = "process #1";
}

subgraph cluster_1 {
node [style=filled];
b0 -> b1 -> b2 -> b3;
label = "process #2";
color=blue
}
start -> a0;
start -> b0;
a1 -> b3;
b2 -> a3;
a3 -> a0;
a3 -> end;
b3 -> end;

start [shape=Mdiamond];
end [shape=Msquare];
}

Your graph design is a bit peculiar. 您的图形设计有些特殊。 When I visually look at your graph, I see a base graph, and 2 clustered groups of nodes . 当我目视观察您的图时,会看到一个基础图和2个群集的节点组 When I look at your DOT file, I see 2 subgraphs, and an 'outer' graph that refers to specific nodes in your subgraph. 当我查看您的DOT文件时,我看到2个子图,以及一个“外部”图,它引用了您的子图中的特定节点。 Note that a subgraph is not a cluster. 请注意,子图不是聚类。

Your question seems to have 2 parts: (1) can you create a graph with clusters using jgrapht and (2) can you create the DOT file in your example. 您的问题似乎包括两部分:(1)您可以使用jgrapht创建带有聚类的图吗?(2)您可以在示例中创建DOT文件。 The answers are: (1) yes, (2) no, at least not out of the box, since DOTExporter does not support 'subgraphs'. 答案是:(1)是,(2)否,至少不是开箱即用,因为DOTExporter不支持“子图”。

There are a few different ways you can create clustered graphs. 您可以通过几种不同的方法来创建聚类图。

  1. Create a regular graph with edges and vertices, and maintain a separate List<Set<V>> to store your clusters. 创建具有边和顶点的正则图,并维护一个单独的List<Set<V>>以存储您的聚类。 You could visualize the subgraph induced by a particular cluster using the AsSubgraph class. 您可以使用AsSubgraph类可视化由特定群集诱导的子图。
  2. JGraphT support vertices of any type. JGraphT支持任何类型的顶点。 So a vertex can be a graph as well! 因此,顶点也可以是图形! You could create a graph where each vertex is a graph by itself; 您可以创建一个图形,其中每个顶点本身就是一个图形。 edges between these vertices represent relations between these special vertices. 这些顶点之间的边表示这些特殊顶点之间的关系。 We did something like this in the BlockCutpointGraph implementation. 我们在BlockCutpointGraph实现中做了类似的BlockCutpointGraph

If you want to export your graph in a similar fashion as your example DOT file, you'll have to do some work. 如果要以与示例DOT文件类似的方式导出图形,则必须做一些工作。 You could implement your own custom DOTExporter, or modify the existing one. 您可以实现自己的自定义DOTExporter,或修改现有的DOTExporter。 Perhaps an easy alternative (not the cleanest) is to do something along the following lines: 也许一个简单的替代方法(不是最干净的方法)是按照以下方式进行操作:

  1. Create a regular graph with vertices and edges (in your case all nodes and arcs in your graph). 创建具有顶点和边线的常规图形(在您的情况下,图形中的所有节点和弧线)。
  2. Create your induced subgraph clusters (in your case, a subgraph for process1 and another one for process 2. For this you can use the AsSubgraph class 创建诱导子图集群(在本例中,为进程1创建一个子图,为过程2创建另一个。为此,您可以使用AsSubgraph
  3. Create a graph which does not contain any of the arcs internally present in your clusters. 创建一个图形,该图形不包含集群内部存在的任何弧。 For this you could use the MaskSubgraph class. 为此,您可以使用MaskSubgraph类。
  4. Export the graphs you created in steps (2) and (3) using the DOTExporter class. 使用DOTExporter类导出在步骤(2)和(3)中创建的DOTExporter
  5. As a final step, you must merge the exported graphs into a single file. 最后一步,您必须将导出的图形合并到一个文件中。 I would use the graph from step (3) as the 'base' graph, and insert the other graphs using the subgraph keywork. 我将使用步骤(3)中的图作为“基础”图,并使用subgraph键盘插入其他图。

Using your example: 使用您的示例:

  1. Base graph from step (3): 步骤(3)中的基础图:
    digraph G {
      start -> a0;
      start -> b0;
      a1 -> b3;
      b2 -> a3;
      a3 -> a0;
      a3 -> end;
      b3 -> end;

      start [shape=Mdiamond];
      end [shape=Msquare];
    }
  1. One of the induced subgraphs from step (2): 来自步骤(2)的归纳子图之一:
    digraph cluster_0 {
        style=filled;
        color=lightgrey;
        node [style=filled,color=white];
        a0 -> a1 -> a2 -> a3;
        label = "process #1";
    }

Here you have to substitute digraph by subgraph and insert this graph into the base graph to obtain: 在这里,您必须用subgraph替换digraph subgraph然后将此图插入基础图以获得:

digraph G {
  subgraph cluster_0 {
    style=filled;
    color=lightgrey;
    node [style=filled,color=white];
    a0 -> a1 -> a2 -> a3;
    label = "process #1";
  }

  start -> a0;
  start -> b0;
  a1 -> b3;
  b2 -> a3;
  a3 -> a0;
  a3 -> end;
  b3 -> end;

  start [shape=Mdiamond];
  end [shape=Msquare];
}

Obviously you must repeat this for the remaining clusters. 显然,您必须对其余群集重复此操作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM