2

I seen examples of jgraph and jgrapht and there easy to follow but now sure how I would go about using the CompleteBipartiteGraph? How is the syntax to be used to instantiate the graph?

http://jgrapht.org/javadoc/

http://jgrapht.org/javadoc/org/jgrapht/generate/CompleteBipartiteGraphGenerator.html

MAXGEN
  • 735
  • 1
  • 10
  • 21
  • It looks like it's pretty straightforward to use. (This impression may be wrong, of course, but) it seems like you just create an instance of this generator, and then call the `generateGraph` with an (initially empty) graph that receives the vertices and edges that are created by the given factories. Could you point out what you tried so far and where you are stuck? – Marco13 Mar 26 '14 at 18:19
  • Let me ask you this. I have a list of each vertex and corresponding vertex to that respective vertex. So one side of the graph should have multiple lines going to multiple vertexes on the right. Is this concept and structure correct for graphing bipartite? What data structure should I use, array list? So it would look like this.....V -> V1, V2, V3. – MAXGEN Mar 26 '14 at 19:13
  • I did not understand your question. Using this generator with parameters (5,3) should create a complete bipartite graph like in the example (upper right image) at http://en.wikipedia.org/wiki/Complete_bipartite_graph ... – Marco13 Mar 26 '14 at 19:54
  • I was more thinking of this. https://en.wikipedia.org/wiki/Bipartite_graph Could I still use this generator? – MAXGEN Mar 26 '14 at 22:32
  • Maybe this might be easier? [Bipartite Solver for Java](https://github.com/dberm22/Bipartite-Solver) – dberm22 Apr 17 '15 at 17:19

1 Answers1

1

In response to the question "Could I still use this generator?" from the comment: You could still use it to create a complete bipartite graph, and then randomly remove some edges.

But a more straightforward approach would be to simply generate two sets of vertices and insert some random edges between them. In fact, this is so easy that I have to assume that there are additional constraints that you did not mention until now. I inserted another method where it is made sure that the bipartite graph does not contain isolated vertices (my crystal ball told be to do so...)

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

import org.jgrapht.Graph;
import org.jgrapht.UndirectedGraph;
import org.jgrapht.VertexFactory;
import org.jgrapht.graph.DefaultEdge;
import org.jgrapht.graph.SimpleGraph;

public class BipartiteGraphTest
{
    public static void main(String[] args)
    {
        UndirectedGraph<String, DefaultEdge> graph =
            new SimpleGraph<String, DefaultEdge>(DefaultEdge.class);

        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;
        generateGraph(graph, numVertices0, numVertices1, numEdges, vertexFactory);

        System.out.println(graph);
    }

    // Creates a bipartite graph with the given numbers 
    // of vertices and edges
    public static <V, E> void generateGraph(Graph<V, E> graph,
        int numVertices0, int numVertices1, int numEdges,
        final VertexFactory<V> vertexFactory)
    {
        List<V> vertices0 = new ArrayList<V>();
        for (int i = 0; i < numVertices0; i++)
        {
            V v = vertexFactory.createVertex();
            graph.addVertex(v);
            vertices0.add(v);
        }
        List<V> vertices1 = new ArrayList<V>();
        for (int i = 0; i < numVertices1; i++)
        {
            V v = vertexFactory.createVertex();
            graph.addVertex(v);
            vertices1.add(v);
        }

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

    }

    // 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);
        }

    }


}
Marco13
  • 53,703
  • 9
  • 80
  • 159
  • Thank you. I'm going to work on this. Its a direct approach using the library and probably what i will go with. I'm parsing a drugbank and making the drug IDS which are strings show the relationship between each others enzymes, they are also have strings for there IDs. So the graph doesn't string compare I"m going to have to do that myself and Then iterate like your doing and make vertexes and edges? – MAXGEN Mar 27 '14 at 00:00
  • @MAXGEN If this was a question, you should try to rephrase it. At least, I did not understand it. – Marco13 Mar 27 '14 at 09:14
  • I open another question if you can help and it might make more sense. https://stackoverflow.com/questions/22703918/java-jgraph-applet-visualize-bipartite-graph – MAXGEN Mar 28 '14 at 04:03