1

I'm trying to construct and visualize a Gomory-Hu Tree using JGraphT and JGraphX. By reading tutorials or other questions, I've managed to implement a simple weighted graph and a resulting Gomory-Hu Tree. Now I want to visualize the resulting Tree. I've seen people using JGraphX for that, however I can't manage to get that working for me.

Unfortunately, my programming skills aren't that impressive, so I'm sorry in advance if any of my questions/problems seem a little bit dumb :)

I've seen other people using the JGraphXAdapter and Listenable Objects to visualize graphs, but I can't even manage to import these classes ("import ... cannot be resolved"). Also my problem is that by using the Gomory-Hu Tree function/class provided by JGraphT, I get an instance of the class GusfieldGomoryHuCutTree, but to use JGraphX in order to visualize I'd need a Listenable graph (I think so at least).

These two are not working

import org.jgrapht.ext.JGraphXAdapter;

import 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
       ...
    }
Community
  • 1
  • 1
Infty
  • 13
  • 4

0 Answers0