尝试使用JGraphT和JGraphX构造和可视化Gomory-Hu-Tree

时间:2019-04-13 12:36:19

标签: java jgrapht jgraphx

我正在尝试使用JGraphTJGraphX构建和可视化Gomory-Hu树。通过阅读教程或其他问题,我设法实现了一个简单的加权图和由此产生的Gomory-Hu树。 现在,我想可视化结果树。我见过使用JGraphX的人,但是我无法让它对我有用。

不幸的是,我的编程技能并不那么出色,因此如果我的任何问题/问题似乎有点愚蠢,我要先感到抱歉:)

我见过其他人使用JGraphXAdapter和Listenable Objects来可视化图形,但是我什至无法导入这些类(“导入...无法解析”)。 另外我的问题是,通过使用JGraphT提供的Gomory-Hu Tree函数/类,我获得了GusfieldGomoryHuCutTree类的实例,但是要使用JGraphX进行可视化,我需要一个可收听的图(至少我认为是这样)

这两个都不起作用

  

导入org.jgrapht.ext.JGraphXAdapter;

     

导入org.jgrapht.graph.ListenableDirectedWeightedGraph;

         public static void main(String[] args)
    { 
        Graph<String, DefaultWeightedEdge> bsp = new SimpleWeightedGraph<>(DefaultWeightedEdge.class); //of that kind is my graph right now

        GusfieldGomoryHuCutTree<String, DefaultWeightedEdge> tree = new GusfieldGomoryHuCutTree(bsp); 

        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                createAndShowGui();
            }
        }); //found at a different question 
    }

    private static void createAndShowGui() {
        JFrame frame = new JFrame("DemoGraph");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        ListenableGraph<String, MyEdge> g = buildGraph();
        JGraphXAdapter<String, MyEdge> graphAdapter = 
                new JGraphXAdapter<String, MyEdge>(g);
        //cant import that Adapter
        mxIGraphLayout layout = new mxCircleLayout(graphAdapter);
        layout.execute(graphAdapter.getDefaultParent());

        frame.add(new mxGraphComponent(graphAdapter));

        frame.pack();
        frame.setLocationByPlatform(true);
        frame.setVisible(true);
    }

    public static class MyEdge extends DefaultWeightedEdge {
        @Override
        public String toString() {
            return String.valueOf(getWeight());
        }
    }

    public static ListenableGraph<String, MyEdge> buildGraph() {

        ListenableDirectedWeightedGraph<String, MyEdge> g = 
                new ListenableDirectedWeightedGraph<String, MyEdge>(MyEdge.class);
        //doesnt work because I can't import that listenable class, want to make my original graph like that
       ...
    }

0 个答案:

没有答案