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

发布于 2024-10-07 12:23:18 字数 185 浏览 1 评论 0原文

我对 Java 和 Jung 相当陌生。我正在编写一个程序,我需要在事件发生的边缘添加概率(意味着数据从第一个节点流向其他节点的事件的概率)。我有点困惑,Max-Flow 会为我解决这个问题,还是我需要使用其他选项,或者在 Jung 中没有选项可以做到这一点,在这种情况下,我需要自己编写它吗?在这方面的任何帮助将不胜感激。

问候, 瓦卡斯

I am fairly new to Java and 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? Any help in this regard will be appreciated.

regards,
waqas

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

流殇 2024-10-14 12:23:18

您是否打算设置边权重来表示某些事件的概率?最大流算法将使用您分配给每条边的“容量”来查找从源顶点到汇顶点的最大流路径。你到底想在这里做什么?

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?

dawn曙光 2024-10-14 12:23:18

我不太确定你的最终目标是什么,所以我会尽力帮助你。

您可以首先通过定义自定义边缘和边缘工厂类来表示概率。我所做的是:

0。进口:

import org.apache.commons.collections15.Factory;

1。添加您的自定义类。他们的自定义边缘类可能类似于:

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;
    }

}

自定义边缘工厂是每次您以图形方式在画布上绘制边缘时实际创建边缘的东西,它可能看起来像:

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。告诉您的可视化查看器如何显示边缘标签;您需要在创建图形和 VisualizationViewer 对象 (vv) 的任何位置添加此内容:

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

现在,每次创建边时,它的标签都将采用“E0 0/0”形式, “E1 0/0”等等。

我将在我的博客 很快,如果您要在您正在从事的任何项目上花费大量时间,您就可以观看该空间。

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. What I did was:

0. Imports:

import org.apache.commons.collections15.Factory;

1. Add in your custom classes. 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; you'll need to add this in wherever you're creating your graph and VisualizationViewer object (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.

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.

笑脸一如从前 2024-10-14 12:23:18

看看您调用 setEdgeLabelTransformer 的方式,您需要向其传递一个 new Transformer(),就像我在编号为 2 的代码片段中所做的那样< /强>。

当您传递新的 ToStringLabeller() 时,您是在告诉查看器使用边缘对象的 toString() 方法进行标记。您需要传递一个自定义 Transformer,只需更正您的代码以使其看起来像我的那样就可以了。

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.

When you pass a new ToStringLabeller(), you're telling the viewer to label using the toString() method of the edge object. You'll need to pass a custom Transformer instead, just correct your code to look like mine and you'll be fine.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文