connectedComponents - SeqAn API Documentation

# fn() connectedComponents
Decomposes an undirected graph into its connected components.

Defined in | <seqan/graph_algorithms.h>
Signature | `TSize connectedComponents(components, g);`

## Parameters

`components` | A property map. Each vertex is mapped to a component id. If two vertices share the same id they are in the same component.
`g` | An undirected graph. Types: Undirected Graph

## Return Values

`TSize` | The number of components.

## Detailed Description

### Examples

A simple example on how to use this function.

    // Build Input.
    Graph<Undirected<> > graph;
    for (unsigned i = 0; i < 5; ++i)
        addVertex(graph);
    addEdge(graph, 0, 1);
    addEdge(graph, 0, 3);
    addEdge(graph, 2, 4);
    String<unsigned> components;
    unsigned numComponents = 0;

    // Call Algorithm.
    numComponents = connectedComponents(g, components);

    // Print Result.
    std::cout << "Number of components: " << numComponents << std::endl;
    std::cout << std::endl << "Vertex -> Component" << std::endl;
    for (unsigned i = 0; i < length(components); ++i)
        std::cout << i << " -> " << components[i] << std::endl;

The output now is:

    Number of components: 2

    Vertex -> Component
    0 -> 0
    1 -> 0
    2 -> 1
    3 -> 0
    4 -> 1

## Data Races

If not stated otherwise, concurrent invocation is not guaranteed to be thread-safe.

#### Dox Sources

    /*!
     * @fn connectedComponents
     *
     * @headerfile <seqan/graph_algorithms.h>
     *
     * @brief Decomposes an undirected graph into its connected components.
     *
     * @signature TSize connectedComponents(components, g);
     *
     * @param[out] components  A property map. Each vertex is mapped to a component
     *                        id. If two vertices share the same id they are in the
     *                        same component.
     * @param[in] g An undirected graph. Types: Undirected Graph
     *
     * @return TSize The number of components.
     *
     * @section Examples
     *
     * A simple example on how to use this function.
     *
     * @code{.cpp}
     * // Build Input.
     * Graph<Undirected<> > graph;
     * for (unsigned i = 0; i < 5; ++i)
     *     addVertex(graph);
     * addEdge(graph, 0, 1);
     * addEdge(graph, 0, 3);
     * addEdge(graph, 2, 4);
     * String<unsigned> components;
     * unsigned numComponents = 0;
     *
     * // Call Algorithm.
     * numComponents = connectedComponents(g, components);
     *
     * // Print Result.
     * std::cout << "Number of components: " << numComponents << std::endl;
     * std::cout << std::endl << "Vertex -> Component" << std::endl;
     * for (unsigned i = 0; i < length(components); ++i)
     *     std::cout << i << " -> " << components[i] << std::endl;
     * @endcode
     *  The output now is:
     *
     * @code{.console}
     * Number of components: 2
     *
     * Vertex -> Component
     * 0 -> 0
     * 1 -> 0
     * 2 -> 1
     * 3 -> 0
     * 4 -> 1
     * @endcode
     */