简体   繁体   English

如何使用Jung 2在图中用概率标记边缘

[英]How to label edges with probabilities in graph using Jung 2

I am fairly new to Java and Jung. 我是Java和Jung的新手。 I am writing a program where I need to add probabilities on edges of the event occurrence(means probability of the event that data will flow from first node to other). 我正在编写一个程序,需要在事件发生的边缘上添加概率(即数据将从第一个节点流到另一个节点的事件的概率)。 I am a little confuse that will Max-Flow do the trick for me or do I need to use some other option or there is no option to do it within Jung and in that case do I need to write it on my own? 我有点困惑,Max-Flow会帮我这个忙吗,还是我需要使用其他选项,或者在Jung内没有其他选择,在这种情况下,我需要自己编写吗? Any help in this regard will be appreciated. 在这方面的任何帮助将不胜感激。

regards, waqas 问候,瓦卡斯

Do you intend to set the edge weights to represent the probabilities of certain events? 您是否打算设置边缘权重来表示某些事件的概率? The Max-Flow algorithm will use the "capacities" you assign to each edge to find the path of maximum flow from the source vertex to the sink vertex. 最大流量算法将使用您分配给每个边的“容量”来查找从源顶点到宿顶点的最大流量的路径。 What exactly are you trying to do here? 您到底想在这里做什么?

I'm not very sure what your final aim is, so I'll try my best to help out. 我不太确定您的最终目标是什么,所以我会尽力帮助您。

You can first represent the probabilities by defining a custom Edge and Edge Factory classes. 首先可以通过定义自定义Edge和Edge Factory类来表示概率。 What I did was: 我所做的是:

0. Imports: 0.进口:

import org.apache.commons.collections15.Factory;

1. Add in your custom classes. 1.添加自定义类。 They custom edge class might be something like: 他们自定义的边缘类可能类似于:

public static class MyEdge {

    private int flow;
    private int capacity;

    private String name;
    private int eIndex;

    public MyEdge(String name, int eIndex) {
        this.name = name;
        this.eIndex = eIndex;
    }

    public int getCapacity() {
        return this.capacity;
    }

    public void setCapacity(int edgeCapacity) {
        this.capacity = edgeCapacity;
    }

    public int getFlow() {
        return this.flow;
    }

    public void setFlow(int edgeFlow) {
        this.flow = edgeFlow;
    }

    public String toString() {
        return this.name;
    }

}

The custom edge factory is what actually creates your edges each time you draw them on the canvas graphically, it might look like: 每次在画布上以图形方式绘制边缘时,自定义边缘工厂实际上就是创建边缘的过程,它看起来像:

public static class MyEdgeFactory implements Factory {

    private static int defaultFlow = 0;
    private static int defaultCapacity = 0;
    private int edgeCount;

    private MyEdgeFactory() {            

    }

    public MyEdge create() {
        String name = "E" + edgeCount;
        MyEdge e = new MyEdge(name, edgeCount);
        edgeCount++;
        e.setFlow(defaultFlow);
        e.setCapacity(defaultCapacity);
        return e;
    }    

}

2. Tell your visualization viewer how to display the edge labels; 2.告诉可视化查看器如何显示边缘标签; you'll need to add this in wherever you're creating your graph and VisualizationViewer object (vv): 您需要将其添加到要创建图形和VisualizationViewer对象(vv)的位置:

vv.getRenderContext().setEdgeLabelTransformer(new Transformer() {
    public String transform(MyEdge e) {
        return (e.toString() + " " + e.getFlow() + "/" + e.getCapacity());
    }
});

Now everytime you create an edge, it's label will be of the form "E0 0/0", "E1 0/0" and so on. 现在,每次创建边时,其标签的形式将为“ E0 0/0”,“ E1 0/0”,依此类推。

I'll be posting detailed tutorials and code on my blog soon so you could watch that space if you're going to be spending significant time on whatever project you're working on. 我将很快在我的博客上发布详细的教程和代码,因此,如果您要花大量时间在正在进行的任何项目上,那么您可以关注这个空间。

Look at the way you're calling setEdgeLabelTransformer , you need to pass it a new Transformer() , like I've done in my code snippet numbered 2 . 看一下您调用setEdgeLabelTransformer的方式,您需要将其传递给新的Transformer() ,就像我在编号为2的代码段中所做的那样。

When you pass a new ToStringLabeller() , you're telling the viewer to label using the toString() method of the edge object. 当您传递新的ToStringLabeller()时 ,您是在告诉查看者使用边缘对象的toString()方法进行标记。 You'll need to pass a custom Transformer instead, just correct your code to look like mine and you'll be fine. 您需要传递一个自定义的Transformer,只需更正您的代码使其看起来像我的代码就可以了。

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

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