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

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

The newest version!
/*
 * (C) Copyright 2013-2023, by Alexey Kudinkin 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 java.io.*;
import java.util.*;

/**
 * An algorithm which computes a  spanning
 * tree of a given connected graph. In the case of disconnected graphs it would rather derive a
 * spanning forest.
 *
 * @param  the graph edge type
 */
public interface SpanningTreeAlgorithm
{
    /**
     * Computes a spanning tree.
     *
     * @return a spanning tree
     */
    SpanningTree getSpanningTree();

    /**
     * A spanning tree.
     *
     * @param  the graph edge type
     */
    interface SpanningTree
        extends Iterable
    {
        /**
         * Returns the weight of the spanning tree.
         * 
         * @return weight of the spanning tree
         */
        double getWeight();

        /**
         * Set of edges of the spanning tree.
         * 
         * @return edge set of the spanning tree
         */
        Set getEdges();

        /**
         * Returns an iterator over the edges in the spanning tree.
         * 
         * @return iterator over the edges in the spanning tree.
         */
        @Override
        default Iterator iterator()
        {
            return getEdges().iterator();
        }
    }

    /**
     * Default implementation of the spanning tree interface.
     *
     * @param  the graph edge type
     */
    class SpanningTreeImpl
        implements SpanningTree, Serializable
    {
        private static final long serialVersionUID = 402707108331703333L;

        private final double weight;
        private final Set edges;

        /**
         * Construct a new spanning tree.
         *
         * @param edges the edges
         * @param weight the weight
         */
        public SpanningTreeImpl(Set edges, double weight)
        {
            this.edges = edges;
            this.weight = weight;
        }

        @Override
        public double getWeight()
        {
            return weight;
        }

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

        @Override
        public String toString()
        {
            return "Spanning-Tree [weight=" + weight + ", edges=" + edges + "]";
        }
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy