org.jgrapht.alg.interfaces.VertexColoringAlgorithm Maven / Gradle / Ivy
The newest version!
/*
* (C) Copyright 2017-2023, by Dimitrios Michail 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 graph vertex coloring.
*
* @param the graph vertex type
*/
public interface VertexColoringAlgorithm
{
/**
* Computes a vertex coloring.
*
* @return a vertex coloring
*/
Coloring getColoring();
/**
* A coloring. The colors are between 0 and $n-1$ where $n$ is the number of vertices of the
* graph.
*
* @param the graph vertex type
*/
interface Coloring
{
/**
* Get the number of colors.
*
* @return the number of colors
*/
int getNumberColors();
/**
* Get the color map.
*
* @return the color map
*/
Map getColors();
/**
* Get the color classes. A subset of vertices assigned to the same color is called a color
* class; every such class forms an independent set. This method returns a partitioning of
* the vertices in the graph in disjoint color classes.
*
* @return a list of color classes
*/
List> getColorClasses();
}
/**
* Default implementation of the coloring interface.
*
* @param the graph vertex type
*/
class ColoringImpl
implements Coloring, Serializable
{
private static final long serialVersionUID = -8456580091672353150L;
private final int numberColors;
private final Map colors;
/**
* Construct a new vertex coloring.
*
* @param colors the color map
* @param numberColors the total number of colors used
*/
public ColoringImpl(Map colors, int numberColors)
{
this.numberColors = numberColors;
this.colors = colors;
}
/**
* {@inheritDoc}
*/
@Override
public int getNumberColors()
{
return numberColors;
}
/**
* {@inheritDoc}
*/
@Override
public Map getColors()
{
return colors;
}
/**
* {@inheritDoc}
*/
@Override
public List> getColorClasses()
{
Map> groups = new HashMap<>();
colors.forEach((v, color) -> {
Set g = groups.computeIfAbsent(color, k -> new HashSet<>());
g.add(v);
});
List> classes = new ArrayList<>(numberColors);
classes.addAll(groups.values());
return classes;
}
/**
* {@inheritDoc}
*/
@Override
public String toString()
{
return "Coloring [number-of-colors=" + numberColors + ", colors=" + colors + "]";
}
}
}