Search code examples
javajgrapht

Accessing elements in a Set<> of edges from BipartiteMatching


    HopcroftKarpBipartiteMatching<String, DefaultEdge> alg = new HopcroftKarpBipartiteMatching<String, DefaultEdge>(sg, setO, setM1);

    Set<DefaultEdge> match = alg.getMatching();
    System.out.println("Match: " + match);

I have to find the maximum matching of two sets in a simple graph using Jgrapht. My method is HopcroftKarpBipartiteMatching, getMatching(), and it returns a Set<> of edges. My question is, how can you access the elements of a set?

Each element will be an edge made up of two vertices. I need to be able to access and check the two vertices individually but I don't know how. I've tried using an iterator and using the method match.toArray(), but it returns Objects. I'm not sure how to convert the objects into two seperate strings for each of the vertices.

Any help would be awesome, thanks!

Edit: A little clarification


Solution

  • You need to specify the generic type for the iterator.

    Iterator<DefaultEdge> it = match.iterator();
    while (it.hasNext()) {
        DefaultEdge currentEdge = it.next();
        //Do something with your edge...
    }
    

    When using

    Iterator it = match.iterator();
    

    you'll get references to Object instances. But you could cast those to your DefaultEdge type.