コード例 #1
0
        private void ColorNodes(List<Node> orderedNodes)
        {
            orderedNodes.ForEach(x => x.Color = 0);

            var queue = new Queue<Node>();
            orderedNodes.ForEach(queue.Enqueue);

            var bfsColorizer = new BfsGraphColorizer();
            bfsColorizer.Color(queue, orderedNodes);
        }
コード例 #2
0
        public void ColoringTest()
        {
            //http://en.wikipedia.org/wiki/File:4CT_Non-Counterexample_1.svg
            var states = new Graph();
            states.AddNode("9");
            states.AddNode("1");
            states.AddNode("2");
            states.AddNode("3");
            states.AddNode("4");
            states.AddNode("5");
            states.AddNode("6");
            states.AddNode("7");
            states.AddNode("8");
            //TODO: add all states

            states["1"].AddEdge(states["2"]);
            states["1"].AddEdge(states["3"]);
            states["1"].AddEdge(states["4"]);
            states["1"].AddEdge(states["5"]);

            states["2"].AddEdge(states["3"]);
            states["2"].AddEdge(states["5"]);
            states["2"].AddEdge(states["6"]);
            states["2"].AddEdge(states["9"]);

            states["3"].AddEdge(states["4"]);
            states["3"].AddEdge(states["6"]);
            states["3"].AddEdge(states["7"]);

            states["4"].AddEdge(states["5"]);
            states["4"].AddEdge(states["7"]);
            states["4"].AddEdge(states["8"]);

            states["5"].AddEdge(states["8"]);
            states["5"].AddEdge(states["9"]);

            states["6"].AddEdge(states["7"]);
            states["6"].AddEdge(states["9"]);
            states["7"].AddEdge(states["8"]);
            states["8"].AddEdge(states["9"]);

            //TODO: add all edges for adjacent states

            var bfsGraphColorizer = new BfsGraphColorizer();
            bfsGraphColorizer.Color(states.Values);
            Debug.Print("Using BFS algorithm:");
            foreach (var state in states)
                Debug.Print("{0}: {1}", state.Key, state.Value.Color);

            var bruteForceGraphColorizer = new BruteForceGraphColorizer();
            bruteForceGraphColorizer.Color(states);
            Debug.Print("Using Brute Force algorithm:");
            foreach (var state in states)
                Debug.Print("{0}: {1}", state.Key, state.Value.Color);
        }