src/VertexColoring.Algorithms/ColoringExtensions.cs
/// <summary>
/// Colors graph using G.I.S (Greedy Independent Sets) algorithm. See remarks in <see cref="ColorGreedyIndependentSets(Graph)"/>.
/// </summary>
/// <param name="graph">Graph to be colored.</param>
/// <param name="adjacency"><paramref name="graph"/> adjacency to save calculations
/// when doing multiple colorings.</param>
/// <returns>Vertex coloring of provided graph.</returns>
Colors graph using G.I.S (Greedy Independent Sets) algorithm. See remarks in .
Colors graph using G . I . S ( Greedy Independent Sets ) algorithm . See remarks in .
public static GraphColoring ColorGreedyIndependentSets(this Graph graph, VertexAdjacency adjacency)
        {
            var maxDegree = adjacency.AdjacentVertices.Max(p => p.Value.Count);
            var color = 1;
            var coloring = new MutableGraphColoring(graph);
            while (coloring.VertexColors.Count < graph.Vertices.Count)
            {
                var induced = adjacency.InducedSubgraphByRemovingVertices(coloring.VertexColors.Keys);
                var inducedAdjacency = induced.Adjacency();
                while (induced.Subgraph.Vertices.Count > 0)
                {
                    var vertex = induced.Subgraph.MinDegreeVertex(inducedAdjacency.VertexDegrees());
                    coloring[vertex] = color;
                    induced = inducedAdjacency.InducedSubgraphByRemovingVertices(inducedAdjacency.AdjacentVertices[vertex].Add(vertex));
                    inducedAdjacency = induced.Adjacency();
                }
                color++;
            }
            return coloring.ToImmutable();
        }
public static GraphColoring ColorGreedyIndependentSets ( this Graph graph , VertexAdjacency adjacency ) { var maxDegree = adjacency . AdjacentVertices . Max ( p => p . Value . Count ) ; var color = 1 ; var coloring = new MutableGraphColoring ( graph ) ; while ( coloring . VertexColors . Count < graph . Vertices . Count ) { var induced = adjacency . InducedSubgraphByRemovingVertices ( coloring . VertexColors . Keys ) ; var inducedAdjacency = induced . Adjacency ( ) ; while ( induced . Subgraph . Vertices . Count > 0 ) { var vertex = induced . Subgraph . MinDegreeVertex ( inducedAdjacency . VertexDegrees ( ) ) ; coloring [ vertex ] = color ; induced = inducedAdjacency . InducedSubgraphByRemovingVertices ( inducedAdjacency . AdjacentVertices [ vertex ] . Add ( vertex ) ) ; inducedAdjacency = induced . Adjacency ( ) ; } color ++ ; } return coloring . ToImmutable ( ) ; }
Colors graph using G.I.S (Greedy Independent Sets) algorithm.
Colors graph using G . I . S ( Greedy Independent Sets ) algorithm .
Vertex coloring of provided graph.
Vertex coloring of provided graph .
Graph
this
adjacency
VertexAdjacency
adjacency to save calculations
when doing multiple colorings.
adjacency to save calculations when doing multiple colorings .
graph
Graph to be colored.
Graph to be colored .