All Downloads are FREE. Search and download functionalities are using the official Maven repository.

edu.umd.cs.findbugs.graph.StronglyConnectedComponents Maven / Gradle / Ivy

The newest version!
/*
 * Generic graph library
 * Copyright (C) 2000,2003-2005 University of Maryland
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

// $Revision: 1.17 $

package edu.umd.cs.findbugs.graph;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.Set;
import java.util.TreeSet;

/**
 * Algorithm to find strongly connected components in a graph. Based on
 * algorithm in Cormen et. al., Introduction to Algorithms, p. 489.
 */
public class StronglyConnectedComponents, EdgeType extends GraphEdge, VertexType extends GraphVertex> {

    private final ArrayList> m_stronglyConnectedSearchTreeList;

    private VertexChooser m_vertexChooser;

    /**
     * Constructor.
     */
    public StronglyConnectedComponents() {
        m_stronglyConnectedSearchTreeList = new ArrayList<>();
        m_vertexChooser = null;
    }

    /**
     * Specify a VertexChooser object to restrict which vertices are considered.
     * This is useful if you only want to find strongly connected components
     * among a particular category of vertices.
     */
    public void setVertexChooser(VertexChooser vertexChooser) {
        m_vertexChooser = vertexChooser;
    }

    /**
     * Find the strongly connected components in given graph.
     *
     * @param g
     *            the graph
     * @param toolkit
     *            a GraphToolkit, used to create temporary graphs used by the
     *            algorithm
     */
    public void findStronglyConnectedComponents(GraphType g, GraphToolkit toolkit) {

        // Perform the initial depth first search
        DepthFirstSearch initialDFS = new DepthFirstSearch<>(g);
        if (m_vertexChooser != null) {
            initialDFS.setVertexChooser(m_vertexChooser);
        }
        initialDFS.search();

        // Create a transposed graph
        Transpose t = new Transpose<>();
        GraphType transpose = t.transpose(g, toolkit);

        // Create a set of vertices in the transposed graph,
        // in descending order of finish time in the initial
        // depth first search.
        VisitationTimeComparator comparator = new VisitationTimeComparator<>(
                initialDFS.getFinishTimeList(), VisitationTimeComparator.DESCENDING);
        Set descendingByFinishTimeSet = new TreeSet<>(comparator);
        Iterator i = transpose.vertexIterator();
        while (i.hasNext()) {
            descendingByFinishTimeSet.add(i.next());
        }

        // Create a SearchTreeBuilder for transposed DFS
        SearchTreeBuilder searchTreeBuilder = new SearchTreeBuilder<>();

        // Now perform a DFS on the transpose, choosing the vertices
        // to visit in the main loop by descending finish time
        final Iterator vertexIter = descendingByFinishTimeSet.iterator();
        DepthFirstSearch transposeDFS = new DepthFirstSearch(
                transpose) {
            @Override
            protected VertexType getNextSearchTreeRoot() {
                while (vertexIter.hasNext()) {
                    VertexType vertex = vertexIter.next();
                    if (visitMe(vertex)) {
                        return vertex;
                    }
                }
                return null;
            }
        };
        if (m_vertexChooser != null) {
            transposeDFS.setVertexChooser(m_vertexChooser);
        }
        transposeDFS.setSearchTreeCallback(searchTreeBuilder);
        transposeDFS.search();

        // The search tree roots of the second DFS represent the
        // strongly connected components. Note that we call copySearchTree()
        // to make the returned search trees relative to the original
        // graph, not the transposed graph (which would be very confusing).
        Iterator> j = searchTreeBuilder.searchTreeIterator();
        while (j.hasNext()) {
            m_stronglyConnectedSearchTreeList.add(copySearchTree(j.next(), t));
        }
    }

    /**
     * Make a copy of given search tree (in the transposed graph) using vertices
     * of the original graph.
     *
     * @param tree
     *            a search tree in the transposed graph
     * @param t
     *            the Transpose object which performed the transposition of the
     *            original graph
     */
    private SearchTree copySearchTree(SearchTree tree, Transpose t) {
        // Copy this node
        SearchTree copy = new SearchTree<>(t.getOriginalGraphVertex(tree.getVertex()));

        // Copy children
        Iterator> i = tree.childIterator();
        while (i.hasNext()) {
            SearchTree child = i.next();
            copy.addChild(copySearchTree(child, t));
        }

        return copy;
    }

    /**
     * Returns an iterator over the search trees containing the vertices of each
     * strongly connected component.
     *
     * @return an Iterator over a sequence of SearchTree objects
     */
    public Iterator> searchTreeIterator() {
        return m_stronglyConnectedSearchTreeList.iterator();
    }

    /**
     * Iterator for iterating over sets of vertices in strongly connected
     * components.
     */
    private class SCCSetIterator implements Iterator> {
        private final Iterator> m_searchTreeIterator;

        public SCCSetIterator() {
            m_searchTreeIterator = searchTreeIterator();
        }

        @Override
        public boolean hasNext() {
            return m_searchTreeIterator.hasNext();
        }

        @Override
        public Set next() {
            SearchTree tree = m_searchTreeIterator.next();
            TreeSet set = new TreeSet<>();
            tree.addVerticesToSet(set);
            return set;
        }

        @Override
        public void remove() {
            throw new UnsupportedOperationException();
        }
    }

    /**
     * Returns an iterator over the sets of vertices of each strongly connected
     * component.
     *
     * @return an Iterator over a sequence of Set objects
     */
    public Iterator> setIterator() {
        return new SCCSetIterator();
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy