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

MAXGEN
  • 735
  • 1
  • 10
  • 21

1 Answers1

0

Based on the example from http://jgrapht.org/visualizations.html and some methods that I already posted in Java 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);
        }

    }

}
Community
  • 1
  • 1
Marco13
  • 53,703
  • 9
  • 80
  • 159
  • I been working with your example and integrating my data. I ran across two problems, I have a large set of data and I also realized that my data does have isolated vertices in order to be correct. Is your function from before, be similar implementing in this case? Also I guess I can make the screen size bigger to deal with large sets? – MAXGEN Mar 28 '14 at 21:08
  • When you already HAVE a predefined data set, then you don't have to use the "generateGraph..." method. Any beyond that: I provided large and perfectly running examples answering exactly your questions. If you are looking for someone who writes a complete application for you, then you should should not look on a Q/A site like stackoverflow, but on a job exchange site. – Marco13 Mar 28 '14 at 21:12
  • Either way thank you for spending your time and answering. Do you recommend any? Do you have a reference to any of them? – MAXGEN Mar 28 '14 at 21:45
  • What tough about this is bipartite requires additional edges for matches and if you don't have random data, you need an algorithm on finding the matches. Trying work through that now, I think that's why jgrapht implements there own, specifically for matching and make a edge set. – MAXGEN Mar 28 '14 at 22:41
  • Computing a matching and inserting edges to connect vertices is not really related to each other. Maybe you should describe what you indend to do on a higher level of abstraction. – Marco13 Mar 29 '14 at 00:21