Esempio 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));
        }
Esempio n. 2
0
 public void DGraph_has_cycle()
 {
     DGraph<int> g = null;
     using (var reader = File.OpenText(@"C:\rajiv\DSinCS\DataStructures\DSTests\graph1.txt"))
         while (!reader.EndOfStream)
         {
             var V = uint.Parse(reader.ReadLine().Trim());
             g = new DGraph<int>(V);
             while (!reader.EndOfStream)
             {
                 var inp = reader.ReadLine().Split(new char[] { ' ' }).Select(int.Parse).ToArray();
                 g.AddEdge(inp[0], inp[1]);
             }
         }
     foreach (var e in g.BfsPathTo(0, 9))
     {
         Console.WriteLine($"{e} ");
     }
     foreach (var e in g.DfsPathTo(0, 4))
     {
         Console.WriteLine($"{e} ");
     }
     Console.WriteLine($"check cycle from a node 7 :{g.HasCycle(7)}");
     Console.WriteLine($"check cycle from a node 0 :{g.HasCycle(0)}");
     foreach (var e in g.DfsPathToNon(0, 4))
     {
         Console.WriteLine($"{e} ");
     }
 }
Esempio n. 3
0
        static void ConnectTest()
        {
            Console.Write("This test displays a connection table for a directed graph\n\n");

            // create the graph
            DGraph tree1 = new DGraph();

            //add nodes
            tree1.AddNode('A');
            tree1.AddNode('C');
            tree1.AddNode('T');
            tree1.AddNode('Z');
            tree1.AddNode('X');
            tree1.AddNode('K');
            tree1.AddNode('Q');
            tree1.AddNode('J');
            tree1.AddNode('M');
            tree1.AddNode('U');

            // add some edges
            tree1.AddEdge('A', 'C');
            tree1.AddEdge('A', 'T');
            tree1.AddEdge('A', 'Z');
            tree1.AddEdge('X', 'C');
            tree1.AddEdge('C', 'X');
            tree1.AddEdge('C', 'K');
            tree1.AddEdge('T', 'Q');
            tree1.AddEdge('K', 'Q');
            tree1.AddEdge('Q', 'J');
            tree1.AddEdge('J', 'M');
            tree1.AddEdge('Z', 'X');
            tree1.AddEdge('U', 'M');
            tree1.AddEdge('K', 'X');

            // uncomment the next line to see your node list, edge list, and edge matrix
            Debug1(tree1);

            Console.Write("The breadth first min tree from A should be: \n");
            Console.Write(" A-Z A-Z A-T A-C Z-X T-Q C-K Q-J J-M  with U unreachable\n");
            Console.Write(" is: " + tree1.BreadthFirst('A') + "\n\n");

            Console.Write("Note that the order of nodes in your output might differ\n");
            Console.Write("what is important which nodes are reached from each start\n");
            Console.Write("The graph connect table should be: \n");
            Console.Write("A: Z X C K Q J M T \n");
            Console.Write("C: K X Q J M\n");
            Console.Write("T: Q J M\n");
            Console.Write("Z: X C K Q J M\n");
            Console.Write("X: C K Q J M\n");
            Console.Write("K: X C Q J M\n");
            Console.Write("Q: J M\n");
            Console.Write("J: M\n");
            Console.Write("M:\n");
            Console.Write("U: M \n\n");

            Console.Write("The graph connect table is: \n");
            Console.Write(tree1.ConnectTable() + "\n\n");

            Console.Write("End of testing connection table\n\n");
        }
Esempio n. 4
0
        public void Find_shortest_path_in_graph()
        {
            using (var reader = File.OpenText(@"C:\rajiv\DSinCS\DataStructures\DSTests\Data\tinyEWD.txt"))
            {
                var V = uint.Parse(reader.ReadLine());
                var g = new DGraph<int>(V);
                reader.ReadLine();
                while (!reader.EndOfStream)
                {
                    var line = reader.ReadLine();
                    if(line == null) { continue; }
                    var inp = line.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
                    var u = int.Parse(inp[0]);
                    var v = int.Parse(inp[1]);
                    var w = float.Parse(inp[2]);
                    g.AddEdge(u, v, w);
                }
                var res = g.ShortestPathTree(0);
                Assert.AreEqual(res[0], 0);
                Assert.AreEqual(res[1], (float)1.05);
                Assert.AreEqual(res[2], (float)0.26);
                Assert.AreEqual(res[3], (float)0.99);
                Assert.AreEqual(res[4], (float)0.38);
                Assert.AreEqual(res[5], (float)0.73);
                Assert.AreEqual(res[6], (float)1.51);
                Assert.AreEqual(res[7], (float)0.6);

            }
        }
Esempio n. 5
0
        public void Run_topologocal_sort()
        {
            var g = new DGraph<int>(6);
            g.AddEdge(2, 3);
            g.AddEdge(3, 1);
            g.AddEdge(5, 2);
            g.AddEdge(4, 0);
            g.AddEdge(4, 1);
            g.AddEdge(5, 0);

            var res = g.TopologicalSort();
            var str = string.Join(",", res);
            Debug.WriteLine(str);
            Assert.AreEqual(str, "4,5,0,2,3,1");
        }
Esempio n. 6
0
        public void Run_topological_sort_2()
        {
            var g = new DGraph<int>(8);
            g.AddEdge(5, 11);
            g.AddEdge(11, 2);
            g.AddEdge(11, 9);
            g.AddEdge(11, 10);
            g.AddEdge(7, 11);
            g.AddEdge(7, 8);
            g.AddEdge(8, 9);
            g.AddEdge(3, 8);
            g.AddEdge(3, 10);

            var res = g.TopologicalSort();
            var str = string.Join(",", res);
            Debug.WriteLine(str);
            Assert.AreEqual(str, "3,7,8,5,11,10,9,2");
        }
Esempio n. 7
0
        public void MST_via_kruskals()
        {
            using (var reader = File.OpenText(@"C:\rajiv\DSinCS\DataStructures\DSTests\Data\tinyEWG.txt"))
            {
                var V = uint.Parse(reader.ReadLine());
                var g = new DGraph<int>(V);
                reader.ReadLine();
                while (!reader.EndOfStream)
                {
                    var line = reader.ReadLine();
                    if (line == null) { continue; }
                    var inp = line.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
                    var u = int.Parse(inp[0]);
                    var v = int.Parse(inp[1]);
                    var w = float.Parse(inp[2]);
                    g.AddEdge(u, v, w);
                }
                var res = g.MST_Kruskal();
                Assert.AreEqual(res.Count, 7);

            }
        }