http://jgrapht.org/visualizations.htmlの例と、 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);
}
}
}