简体   繁体   English

如何创建一个点文件

[英]How to create a dot file

I have a program that generate a random graph (DAG), How can I extract the output graph and convert in file dot format In order to visualize it in GraphViz? 我有一个生成随机图(DAG)的程序,如何提取输出图并转换为文件点格式,以便在GraphViz中将其可视化? Or there's another way to do it? 还是有另一种方法? Here's the code ( I have omitted all the dependencies) and a simple generated output 这是代码(我已经省略了所有依赖项)和一个简单的生成的输出

public class DigraphGenerator {
    private static final class Edge implements Comparable<Edge> {
        private final int v;
        private final int w;

        private Edge(int v, int w) {
            this.v = v;
            this.w = w;
        }

        public int compareTo(Edge that) {
            if (this.v < that.v) return -1;
            if (this.v > that.v) return +1;
            if (this.w < that.w) return -1;
            if (this.w > that.w) return +1;
            return 0;
        }
    }

    private DigraphGenerator() { }

    public static Digraph dag(int V, int E) {
        if (E > (long) V*(V-1) / 2) throw new IllegalArgumentException("Too many edges");
        if (E < 0)                  throw new IllegalArgumentException("Too few edges");
        Digraph G = new Digraph(V);
        SET<Edge> set = new SET<Edge>();
        int[] vertices = new int[V];
        for (int i = 0; i < V; i++)
            vertices[i] = i;
        StdRandom.shuffle(vertices);
        while (G.E() < E) {
            int v = StdRandom.uniform(V);
            int w = StdRandom.uniform(V);
            Edge e = new Edge(v, w);
            if ((v < w) && !set.contains(e)) {
                set.add(e);
                G.addEdge(vertices[v], vertices[w]);
            }
        }
        return G;
    }

    public static void main(String[] args) {
        int V = Integer.parseInt(args[0]);
        int E = Integer.parseInt(args[1]);

        StdOut.println("DAG");
        StdOut.println(dag(V, E));
        StdOut.println();
    }

}

Examples of output: 输出示例:

DAG 12 vertices, 10 edges 0: 2 1 1: 2: 3: 4: 8 5: 9 6: 7: 8 4 8: 9: 8 10: 5 11: 5 6 DAG 12个顶点,10个边线0:2 1 1:2:3:3:4:8 5:9 6:7:8 4 8:9:8 10:5 11:5 6

DAG 12 vertices, 10 edges 0: 8 1: 8 2: 5 3: 4: 5 5: 6: 7 9 8 7: 8: 9: 10: 11: 5 7 3 DAG 12个顶点,10个边线0:8 1:8 2:5 3:4:5 5:6:7 9 8 7:8:9:10:11:5 7 3

Something like this should work: 这样的事情应该起作用:

void writeDot(Digraph d){
  try(BufferedWriter out=new BufferedWriter(new OutputStreamWriter(new FileOutputStream("g.dot")))){
    out.write("digraph {"); 
    out.newLine();
    for(Edge e:d.getEdges()){
      out.write(e.v+" -> "+e.w);
      out.newLine();
    }
    out.write("}");
  }
}

Or you can have a look at https://github.com/nidi3/graphviz-java which takes care of generating dot files and calling graphviz and produces directly png images. 或者,您可以查看https://github.com/nidi3/graphviz-java ,它负责生成点文件并调用graphviz并直接生成png图像。

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

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