Ejemplo n.º 1
0
        static void Main(string[] args)
        {
            DGraph graph  = new DGraph();
            NodeD  testV1 = new NodeD("Sea");
            NodeD  testV2 = new NodeD("Chi");
            NodeD  testV3 = new NodeD("SF");

            graph.AddNode(testV1);
            graph.AddNode(testV2);
            //graph.AddNode(testV3);
            graph.AddEdge(testV1, testV2, 150);
            //graph.AddEdge(testV2, testV3, 250);

            string[] travel = new string[] { "Sea", "Chi" };
            //List<Node> nodes = graph.GetNodes();
            //foreach (var item in nodes)
            //{
            //    Console.WriteLine(item.Value.ToString());
            //}
            List <DEdge> edges = graph.GetNeighbors(testV1);

            foreach (DEdge item in edges)
            {
                Console.WriteLine(item);
            }
            Console.WriteLine(GetEdge(graph, travel));
        }
Ejemplo n.º 2
0
        static void Main(string[] args)
        {
            Graphs graph  = new Graphs();
            Node   testV1 = new Node("Test1");
            Node   testV2 = new Node("Test2");

            graph.AddNode(testV1);
            graph.AddNode(testV2);
            graph.AddEdge(testV1, testV2, 4);

            List <Node> ordered = graph.BreadFirst(testV1);

            foreach (Node item in ordered)
            {
                Console.Write($"{item.Value} => ");
            }

            DGraph dictGraph = new DGraph();
            NodeD  node1     = new NodeD("Sea");
            NodeD  node2     = new NodeD("Chi");

            dictGraph.AddNode(node1);
            dictGraph.AddNode(node2);
            dictGraph.AddEdge(node1, node2, 150);
            List <DEdge> edges = dictGraph.GetNeighbors(node1);

            foreach (DEdge item in edges)
            {
                Console.Write($"{item} => ");
            }
            Console.ReadLine();
        }
Ejemplo n.º 3
0
        public static string GetEdge(DGraph graph, string[] cities)
        {
            NodeD city1 = new NodeD(cities[0]);
            NodeD city2 = new NodeD(cities[1]);

            if (graph.AdjList.ContainsKey(city1) && graph.AdjList.ContainsKey(city2))
            {
                List <DEdge> neighbors = graph.GetNeighbors(city1);
                foreach (var v in neighbors)
                {
                    if (v.Start == city1 && v.End == city2 || v.Start == city1 && v.End == city2)
                    {
                        return($"True, ${v.Weight}");
                    }
                }
                neighbors = graph.GetNeighbors(city2);
                foreach (var v in neighbors)
                {
                    if (v.Start == city1 && v.End == city2 || v.End == city1 && v.Start == city2)
                    {
                        return($"True, ${v.Weight}");
                    }
                }
            }

            return("False, $0 ");
        }