0

I having problem getting jgrapht or jgraph or applet to visualize this graph correctly? Can I use this graph library to visualize similarly to the picture beneath? U would be x and V would be Y in the code for example. I'm using the demo versions that used directed graphs to do same in this example. Not sure if I should use a jgAdapter or jgxAdapter? Currently getting blank applet for either.

public class GraphDemo  extends JApplet{

     private static final long serialVersionUID = 2202072534703043194L;
        private static final Dimension DEFAULT_SIZE = new Dimension(530, 320);



        private JGraphXAdapter<String, DefaultEdge> jgxAdapter;

    public static void main(String[] args) {



                JGraphAdapterDemo applet = new JGraphAdapterDemo();
                applet.init();

                JFrame frame = new JFrame();
                frame.getContentPane().add(applet);
                frame.setTitle("JGraphT Adapter to JGraph Demo");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.pack();
                frame.setVisible(true);
            }

    public void init()
    { 
        UndirectedGraph<String, DefaultEdge> g = 
                new SimpleGraph<String, DefaultEdge>(DefaultEdge.class);

        jgxAdapter = new JGraphXAdapter<String, DefaultEdge>(g);

        getContentPane().add(new mxGraphComponent(jgxAdapter));
        resize(DEFAULT_SIZE);

        String x1 = "x1";
        String x2 = "x2";
        String x3 = "x3";

        String y1 = "y1";
        String y2 = "y2";
        String y3 = "y3";
        String y4 = "y5";

        g.addVertex(x1);
        g.addVertex(x2);
        g.addVertex(x3);

        g.addVertex(y1);
        g.addVertex(y2);
        g.addVertex(y3);
        g.addVertex(y4);

        g.addEdge(x1, y1);
        g.addEdge(x1, y2);

        g.addEdge(x2, y1);
        g.addEdge(x2, y4);

        g.addEdge(x3, y2);
        g.addEdge(x3, y3);

        Set<String> p1 = new HashSet<String>(Arrays.asList(x1, x2, x3));
        Set<String> p2 = new HashSet<String>(Arrays.asList(y1, y2, y3, y4));

        HopcroftKarpBipartiteMatching<String, DefaultEdge> alg = 
            new HopcroftKarpBipartiteMatching<String, DefaultEdge>(g, p1, p2);

        Set<DefaultEdge> match = alg.getMatching();

        mxCircleLayout layout = new mxCircleLayout(jgxAdapter);
        layout.execute(jgxAdapter.getDefaultParent());

        System.out.println(g.toString());
        System.out.println(match);
    }
}

enter image description here

4

1 回答 1

0

基于来自http://jgrapht.org/visualizations.html的示例和我已经在J​​ava JGrapht Bipartite graph中发布的一些方法:

import java.awt.Color;
import java.awt.Dimension;
import java.awt.geom.Rectangle2D;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;

import javax.swing.JApplet;

import org.jgraph.JGraph;
import org.jgraph.graph.DefaultGraphCell;
import org.jgraph.graph.GraphConstants;
import org.jgrapht.Graph;
import org.jgrapht.ListenableGraph;
import org.jgrapht.VertexFactory;
import org.jgrapht.ext.JGraphModelAdapter;
import org.jgrapht.graph.DefaultEdge;
import org.jgrapht.graph.ListenableUndirectedGraph;

/**
 * Based on the example from http://jgrapht.org/visualizations.html
 */
public class BipartiteGraphVisualizationTest extends JApplet {
    private static final Color     DEFAULT_BG_COLOR = Color.decode( "#FAFBFF" );
    private static final Dimension DEFAULT_SIZE = new Dimension( 800, 600 );

    // 
    private JGraphModelAdapter<String, DefaultEdge> m_jgAdapter;

    /**
     * @see java.applet.Applet#init().
     */
    public void init(  ) {
        // create a JGraphT graph
        ListenableGraph<String, DefaultEdge> g = 
            new ListenableUndirectedGraph<String, DefaultEdge>( DefaultEdge.class );

        // create a visualization using JGraph, via an adapter
        m_jgAdapter = new JGraphModelAdapter<String, DefaultEdge>( g );

        JGraph jgraph = new JGraph( m_jgAdapter );

        adjustDisplaySettings( jgraph );
        getContentPane(  ).add( jgraph );
        resize( DEFAULT_SIZE );

        List<String> vertices0 = new ArrayList<String>();
        List<String> vertices1 = new ArrayList<String>();
        fillGraph(g, vertices0, vertices1);

        positionVertices(vertices0, vertices1);
    }

    private void positionVertices(List<String> vertices0, List<String> vertices1)
    {
        int dy0 = DEFAULT_SIZE.height / (vertices0.size() + 2);
        int y0 = dy0;
        for (String v0 : vertices0)
        {
            positionVertexAt(v0, 100, y0);
            y0+=dy0;
        }
        int dy1 = DEFAULT_SIZE.height / (vertices1.size() + 2);
        int y1 = dy1;
        for (String v1 : vertices1)
        {
            positionVertexAt(v1, 600, y1);
            y1+=dy1;
        }
    }


    public static void fillGraph(Graph<String, DefaultEdge> graph, 
        List<String> vertices0, List<String> vertices1)
    {
        VertexFactory<String> vertexFactory = new VertexFactory<String>()
        {
            int n = 0;
            @Override
            public String createVertex()
            {
                String s = String.valueOf(n);
                n++;
                return s;
            }
        };
        int numVertices0 = 10;
        int numVertices1 = 15;
        int numEdges = 20;
        generateGraphNoIsolatedVertices(graph, 
            numVertices0, numVertices1, numEdges, 
            vertexFactory, vertices0, vertices1);
    }


    private void adjustDisplaySettings( JGraph jg ) {
        jg.setPreferredSize( DEFAULT_SIZE );

        Color  c        = DEFAULT_BG_COLOR;
        String colorStr = null;

        try {
            colorStr = getParameter( "bgcolor" );
        }
         catch( Exception e ) {}

        if( colorStr != null ) {
            c = Color.decode( colorStr );
        }

        jg.setBackground( c );
    }


    private void positionVertexAt( Object vertex, int x, int y ) {
        DefaultGraphCell cell = m_jgAdapter.getVertexCell( vertex );
        Map              attr = cell.getAttributes(  );
        Rectangle2D        b    = GraphConstants.getBounds( attr );

        GraphConstants.setBounds( attr, new Rectangle2D.Double( x, y, b.getWidth(), b.getHeight() ) );

        Map cellAttr = new HashMap(  );
        cellAttr.put( cell, attr );
        m_jgAdapter.edit( cellAttr, null, null, null);
    }


    // Creates a bipartite graph with the given numbers
    // of vertices and edges without isolated vertices
    public static <V, E> void generateGraphNoIsolatedVertices(
        Graph<V, E> graph, int numVertices0, int numVertices1, int numEdges,
        final VertexFactory<V> vertexFactory, 
        List<V> vertices0, List<V> vertices1)
    {
        int minNumEdges = Math.max(numVertices0, numVertices0);
        if (numEdges < minNumEdges)
        {
            System.out.println("At least " + minNumEdges + " are required to " +
                "connect each of the " + numVertices0 + " vertices " +
                "to any of the " + numVertices1 + " vertices");
            numEdges = minNumEdges;
        }

        for (int i = 0; i < numVertices0; i++)
        {
            V v = vertexFactory.createVertex();
            graph.addVertex(v);
            vertices0.add(v);
        }
        for (int i = 0; i < numVertices1; i++)
        {
            V v = vertexFactory.createVertex();
            graph.addVertex(v);
            vertices1.add(v);
        }

        // Connect each vertex of the larger set with
        // a random vertex of the smaller set
        Random random = new Random(0);
        List<V> larger = null;
        List<V> smaller = null;


        if (numVertices0 > numVertices1)
        {
            larger = new ArrayList<V>(vertices0);
            smaller = new ArrayList<V>(vertices1);
        }
        else
        {
            larger = new ArrayList<V>(vertices1);
            smaller = new ArrayList<V>(vertices0);
        }
        List<V> unmatched = new ArrayList<V>(smaller);
        for (V vL : larger)
        {
            int i = random.nextInt(unmatched.size());
            V vS = unmatched.get(i);
            unmatched.remove(i);
            if (unmatched.size() == 0)
            {
                unmatched = new ArrayList<V>(smaller);
            }
            graph.addEdge(vL, vS);
        }

        // Create the remaining edges between random vertices
        while (graph.edgeSet().size() < numEdges)
        {
            int i0 = random.nextInt(vertices0.size());
            V v0 = vertices0.get(i0);
            int i1 = random.nextInt(vertices1.size());
            V v1 = vertices1.get(i1);
            graph.addEdge(v0, v1);
        }

    }

}
于 2014-03-28T09:53:44.587 回答