Java Code Examples for org.jgrapht.graph.SimpleGraph#addVertex()

The following examples show how to use org.jgrapht.graph.SimpleGraph#addVertex() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: NetworkVertex.java    From Rails with GNU General Public License v2.0 6 votes vote down vote up
/**
 * replaces one vertex by another for a network graph
 * copies all edges
 */
public static boolean replaceVertex(SimpleGraph<NetworkVertex, NetworkEdge> graph,
        NetworkVertex oldVertex, NetworkVertex newVertex) {
    // add new vertex
    graph.addVertex(newVertex);
    // replace old edges
    Set<NetworkEdge> oldEdges = graph.edgesOf(oldVertex);
    for (NetworkEdge oldEdge:oldEdges) {
        NetworkEdge newEdge = NetworkEdge.replaceVertex(oldEdge, oldVertex, newVertex);
        if (newEdge.getSource() == newVertex) {
            graph.addEdge(newVertex, newEdge.getTarget(), newEdge);
        } else {
            graph.addEdge(newEdge.getSource(), newVertex, newEdge);
        }
    }
    // remove old vertex
    return graph.removeVertex(oldVertex);
}
 
Example 2
Source File: CompoundFactory.java    From audiveris with GNU Affero General Public License v3.0 5 votes vote down vote up
/**
 * Browse through the provided isolated sections and return a list of compound
 * instances, one for each set of connected sections.
 * <p>
 * This method does not use the sections neighboring links, it is thus rather slow but usable
 * when these links are not yet set.
 *
 * @param sections    the sections to browse
 * @param constructor specific compound constructor
 * @return the list of compound instances created
 */
public static List<SectionCompound> buildCompounds (Collection<Section> sections,
                                                    CompoundConstructor constructor)
{
    // Build a temporary graph of all sections with "touching" relations
    List<Section> list = new ArrayList<>(sections);

    ///Collections.sort(list, Section.byAbscissa);
    SimpleGraph<Section, Touching> graph = new SimpleGraph<>(Touching.class);

    // Populate graph with all sections as vertices
    for (Section section : list) {
        graph.addVertex(section);
    }

    // Populate graph with relations
    for (int i = 0; i < list.size(); i++) {
        Section one = list.get(i);

        for (Section two : list.subList(i + 1, list.size())) {
            if (one.touches(two)) {
                graph.addEdge(one, two, new Touching());
            }
        }
    }

    // Retrieve all the clusters of sections (sets of touching sections)
    ConnectivityInspector<Section, Touching> inspector = new ConnectivityInspector<>(graph);
    List<Set<Section>> sets = inspector.connectedSets();
    logger.debug("sets: {}", sets.size());

    List<SectionCompound> compounds = new ArrayList<>();

    for (Set<Section> set : sets) {
        compounds.add(buildCompound(set, constructor));
    }

    return compounds;
}