Example #1
0
        public static Supergraph CreateSatsumaGraph(this Choosability.Graph g)
        {
            var satsumaGraph = new CustomGraph();

            var nodes = new Dictionary <int, Node>();

            foreach (var v in g.Vertices)
            {
                var node = satsumaGraph.AddNode();
                nodes[v] = node;
            }

            for (int i = 0; i < g.N; i++)
            {
                for (int j = i + 1; j < g.N; j++)
                {
                    if (g.Directed[i, j])
                    {
                        satsumaGraph.AddArc(nodes[i], nodes[j], Directedness.Directed);
                    }
                    else if (g.Directed[j, i])
                    {
                        satsumaGraph.AddArc(nodes[j], nodes[i], Directedness.Directed);
                    }
                    else if (g.Adjacent[i, j])
                    {
                        satsumaGraph.AddArc(nodes[i], nodes[j], Directedness.Undirected);
                    }
                }
            }

            return(satsumaGraph);
        }
Example #2
0
        public override void Solve()
        {
            var pparser = new Pparser(FpatIn);
            int ccity, cwizard;

            pparser.Fetch(out ccity, out cwizard);
            var rgcity   = pparser.FetchN <Nod>(ccity);
            var rgwizard = pparser.FetchN <Nod>(cwizard);

            var graph       = new CustomGraph();
            var mpnodByinod = new Dictionary <long, Nod>();

            foreach (var wizard in rgwizard)
            {
                wizard.kind = Kind.Wizard;
                wizard.node = graph.AddNode();
                mpnodByinod[wizard.node.Id] = wizard;
            }

            foreach (var city in rgcity)
            {
                city.kind = Kind.City;
                city.node = graph.AddNode();
                mpnodByinod[city.node.Id] = city;
            }

            foreach (var wizard in rgwizard)
            {
                foreach (var city in rgcity)
                {
                    if (Dist(wizard, city) <= 50)
                    {
                        graph.AddArc(wizard.node, city.node, Directedness.Directed);
                    }
                }
            }

            var mm = new MaximumMatching(graph, node => mpnodByinod[node.Id].kind == Kind.Wizard);

            mm.Run();

            using (Output)
            {
                Solwrt.WriteLine(mm.Matching.ArcCount());
            }
        }
Example #3
0
        private void CreateGraph(NavigationNode[] navigationNodes)
        {
            Graph = new CustomGraph();

            foreach (NavigationNode navigationNode in navigationNodes)
            {
                navigationNode.Node = Graph.AddNode(navigationNode.transform.position);
            }
        }
        static void Main(string[] args)
        {
            try
            {
                Console.ForegroundColor = ConsoleColor.DarkYellow;
                Console.WriteLine("Implementing the Graph with basic operations");
                Console.ForegroundColor = ConsoleColor.White;

                CustomGraph customGraph = new CustomGraph();

                Console.ForegroundColor = ConsoleColor.Green;
                Console.WriteLine("Pushing the lables A, B, C, D, E to graph");
                Console.WriteLine("Adding edges A-->B, B-->D, D-->C, A-->C, A-->D, B-->E");
                Console.ForegroundColor = ConsoleColor.White;

                customGraph.AddNode("A");
                customGraph.AddNode("B");
                customGraph.AddNode("C");
                customGraph.AddNode("D");
                customGraph.AddNode("E");

                customGraph.AddEdge("A", "B");
                customGraph.AddEdge("B", "D");
                customGraph.AddEdge("D", "C");
                customGraph.AddEdge("A", "C");
                customGraph.AddEdge("A", "D");
                customGraph.AddEdge("B", "E");

                Console.WriteLine();
                Console.ForegroundColor = ConsoleColor.Green;
                Console.WriteLine("Printing nodes connected with edges");
                Console.ForegroundColor = ConsoleColor.White;
                customGraph.Print();

                Console.WriteLine();
                Console.ForegroundColor = ConsoleColor.Green;
                Console.WriteLine("Removing edge A-->B and D-->C");
                Console.ForegroundColor = ConsoleColor.White;
                customGraph.RemoveEdge("A", "B");
                customGraph.RemoveEdge("D", "C");
                customGraph.Print();
                customGraph.AddEdge("A", "B");
                customGraph.AddEdge("D", "C");

                Console.WriteLine();
                Console.ForegroundColor = ConsoleColor.Green;
                Console.WriteLine("Perform the DFS Traversal for node  A using recurssion");
                Console.ForegroundColor = ConsoleColor.White;
                customGraph.DFSTraversalUsingRecurssion("A");

                Console.WriteLine();
                Console.ForegroundColor = ConsoleColor.Green;
                Console.WriteLine("Perform the DFS Traversal for node  A using iteration");
                Console.ForegroundColor = ConsoleColor.White;
                customGraph.DFSTraversaUsingIteration("A");

                Console.WriteLine();
                Console.ForegroundColor = ConsoleColor.Green;
                Console.WriteLine("Perform the DFS Traversal for node  A using iteration");
                Console.ForegroundColor = ConsoleColor.White;
                customGraph.BFSTraversaUsingIteration("B");

                #region Topological Sort
                Console.WriteLine();
                Console.ForegroundColor = ConsoleColor.Green;
                Console.WriteLine("Performing topological sort");
                Console.ForegroundColor = ConsoleColor.White;

                CustomGraph topologicalSort = new CustomGraph();

                topologicalSort.AddNode("P");
                topologicalSort.AddNode("A");
                topologicalSort.AddNode("B");
                topologicalSort.AddNode("X");

                topologicalSort.AddEdge("A", "P");
                topologicalSort.AddEdge("X", "A");
                topologicalSort.AddEdge("X", "B");
                topologicalSort.AddEdge("B", "A");

                List <string> nodes = topologicalSort.TopologicalSort();
                foreach (var item in nodes)
                {
                    Console.WriteLine(item);
                }
                #endregion

                #region Cycle Detection
                Console.WriteLine();
                Console.ForegroundColor = ConsoleColor.Green;
                Console.WriteLine("Checking whether the  graph");
                Console.ForegroundColor = ConsoleColor.White;

                CustomGraph cycleDetection = new CustomGraph();

                cycleDetection.AddNode("A");
                cycleDetection.AddNode("B");
                cycleDetection.AddNode("C");
                cycleDetection.AddNode("D");

                cycleDetection.AddEdge("A", "D");
                cycleDetection.AddEdge("B", "C");
                cycleDetection.AddEdge("C", "B");

                Console.WriteLine(cycleDetection.HasCycle());
                #endregion
            }
            catch (Exception ex)
            {
                Console.ForegroundColor = ConsoleColor.DarkRed;
                Console.WriteLine(ex.Message);
                Console.ForegroundColor = ConsoleColor.White;
            }
        }