开发者

How to label edges with probabilities in graph using Jung 2

I am fairly new to Java and Jung. I am writing a program where I need to add probabi开发者_JAVA百科lities 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


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. 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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜