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

org.jgrapht.alg.interfaces.TreeToPathDecompositionAlgorithm Maven / Gradle / Ivy

The newest version!
/*
 * (C) Copyright 2018-2023, by Alexandru Valeanu and Contributors.
 *
 * JGraphT : a free Java graph-theory library
 *
 * See the CONTRIBUTORS.md file distributed with this work for additional
 * information regarding copyright ownership.
 *
 * This program and the accompanying materials are made available under the
 * terms of the Eclipse Public License 2.0 which is available at
 * http://www.eclipse.org/legal/epl-2.0, or the
 * GNU Lesser General Public License v2.1 or later
 * which is available at
 * http://www.gnu.org/licenses/old-licenses/lgpl-2.1-standalone.html.
 *
 * SPDX-License-Identifier: EPL-2.0 OR LGPL-2.1-or-later
 */
package org.jgrapht.alg.interfaces;

import org.jgrapht.*;
import org.jgrapht.graph.*;
import org.jgrapht.util.*;

import java.io.*;
import java.util.*;
import java.util.stream.*;

/**
 * An algorithm which computes a decomposition into disjoint paths for a given tree/forest
 *
 * @param  the graph vertex type
 * @param  the graph edge type
 */
public interface TreeToPathDecompositionAlgorithm
{
    /**
     * Computes a path decomposition.
     *
     * @return a path decomposition
     */
    PathDecomposition getPathDecomposition();

    /**
     * A path decomposition.
     *
     * @param  the graph vertex type
     * @param  the graph edge type
     */
    interface PathDecomposition
    {
        /**
         * Set of edges of the path decomposition.
         * 
         * @return edge set of the path decomposition
         */
        Set getEdges();

        /**
         * Set of disjoint paths of the decomposition
         *
         * @return list of vertex paths
         */
        Set> getPaths();

        /**
         * @return number of paths in the decomposition
         */
        default int numberOfPaths()
        {
            return getPaths().size();
        }
    }

    /**
     * Default implementation of the path decomposition interface.
     *
     * @param  the graph vertex type
     * @param  the graph edge type
     */
    class PathDecompositionImpl
        implements PathDecomposition, Serializable
    {

        private static final long serialVersionUID = 8468626434814461297L;
        private final Set edges;
        private final Set> paths;

        /**
         * Construct a new path decomposition.
         *
         * @param graph the graph
         * @param edges the edges
         * @param paths the vertex paths
         */
        public PathDecompositionImpl(Graph graph, Set edges, List> paths)
        {
            this.edges = edges;

            Set> arrayUnenforcedSet =
                paths.stream().map(path -> new GraphWalk<>(graph, path, path.size())).collect(
                    Collectors.toCollection(ArrayUnenforcedSet::new));

            this.paths = Collections.unmodifiableSet(arrayUnenforcedSet);
        }

        @Override
        public Set getEdges()
        {
            return edges;
        }

        @Override
        public Set> getPaths()
        {
            return paths;
        }

        @Override
        public String toString()
        {
            return "Path-Decomposition [edges=" + edges + "," + "paths=" + getPaths() + "]";
        }
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy