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

org.jgrapht.alg.interfaces.PartitioningAlgorithm 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 java.io.*;
import java.util.*;
import java.util.stream.*;

/**
 * Algorithm to compute a vertex partitioning of a graph.
 *
 * @param  vertex the graph vertex type
 *
 * @author Alexandru Valeanu
 */
public interface PartitioningAlgorithm
{

    /**
     * Computes a vertex partitioning.
     *
     * @return a vertex partitioning
     */
    Partitioning getPartitioning();

    /**
     * Check if the given vertex partitioning is valid.
     *
     * @param partitioning the input vertex partitioning
     * @return true if the input partitioning is valid, false otherwise
     */
    boolean isValidPartitioning(Partitioning partitioning);

    /**
     * A graph partitioning.
     *
     * @param  the vertex type
     */
    interface Partitioning
        extends Iterable>
    {

        /**
         * Get the number of partitions.
         *
         * @return the number of partitions
         */
        int getNumberPartitions();

        /**
         * Get the index-th partition (0-based).
         *
         * @param index index of the partition to return
         * @return the index-th partition
         * @throws IndexOutOfBoundsException if the index is out of range
         *         (index < 0 || index >= getNumberPartitions())
         */
        Set getPartition(int index);

        /**
         * Get the partitions. This method returns a partitioning of the vertices in the graph into
         * disjoint partitions.
         *
         * @return a list of partitions
         */
        default List> getPartitions()
        {
            final int n = getNumberPartitions();
            List> partitions = new ArrayList<>(n);

            for (int i = 0; i < n; i++) {
                partitions.add(getPartition(i));
            }

            return partitions;
        }
    }

    /**
     * Default implementation of a vertex partition
     *
     * @param  the vertex type
     */
    class PartitioningImpl
        implements Partitioning, Serializable
    {

        private static final long serialVersionUID = 3702471090706836080L;

        /* Partitioning classes */
        private final List> classes;

        /**
         * Construct a new vertex partitioning.
         *
         * @param classes the partition classes
         * @throws NullPointerException if {@code classes} is {@code null}
         */
        public PartitioningImpl(List> classes)
        {
            this.classes = Collections.unmodifiableList(
                Objects.requireNonNull(classes).stream().map(Collections::unmodifiableSet).collect(
                    Collectors.toList()));
        }

        /**
         * Construct a new vertex partitioning.
         *
         * @param vertexToPartitionMap the vertex to partition index map
         * @throws NullPointerException if {@code vertexToPartitionMap} is {@code null}
         */
        public PartitioningImpl(Map vertexToPartitionMap)
        {
            Objects.requireNonNull(vertexToPartitionMap);

            Map> partitionIndexToVertexMap = new HashMap<>();

            for (Map.Entry entry : vertexToPartitionMap.entrySet()) {
                partitionIndexToVertexMap
                    .computeIfAbsent(entry.getValue(), x -> new HashSet<>()).add(entry.getKey());
            }

            this.classes = Collections.unmodifiableList(
                partitionIndexToVertexMap
                    .values().stream().map(Collections::unmodifiableSet)
                    .collect(Collectors.toList()));
        }

        /**
         * {@inheritDoc}
         */
        @Override
        public int getNumberPartitions()
        {
            return classes.size();
        }

        /**
         * {@inheritDoc}
         */
        @Override
        public Set getPartition(int index)
        {
            if (index < 0 || index >= classes.size()) {
                throw new IndexOutOfBoundsException(index + " is not valid");
            }

            return classes.get(index);
        }

        /**
         * {@inheritDoc}
         */
        @Override
        public String toString()
        {
            return "Partition [number-of-partitions=" + getNumberPartitions() + ", partitions="
                + classes + "]";
        }

        /**
         * {@inheritDoc}
         */
        @Override
        public Iterator> iterator()
        {
            return classes.iterator();
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy