示例#1
0
        static void Main2()
        {
            // Setup structure
            ObjectStructure o = new ObjectStructure();

            o.Attach(new ConcreteElementA());
            o.Attach(new ConcreteElementB());

            // Create visitor objects
            ConcreteVisitor1 v1 = new ConcreteVisitor1();
            ConcreteVisitor2 v2 = new ConcreteVisitor2();

            // Structure accepting visitors
            o.Accept(v1);
            o.Accept(v2);

            // Wait for user
            Console.ReadKey();

            cGraph g = new cGraph();

            g.AddVertex("A");
            g.AddVertex("B");
            g.AddVertex("C");
            g.AddVertex("D");
            g.AddEdge(0, 1);
            g.AddEdge(1, 2);
            g.AddEdge(2, 3);
            //g.AddEdge(3, 4);

            Console.WriteLine("Breadth-First:");
            g.BreadthFirstSearch();
            Console.WriteLine();
            Console.WriteLine("Depth-First:");
            g.DepthFirstSearch();
            Console.WriteLine();
            g.TopologicalSort();
            Console.WriteLine();

            g = new cGraph();

            g.AddVertex("CS1");
            g.AddVertex("CS2");
            g.AddVertex("Data Structures");
            g.AddVertex("Operating Systems");
            g.AddVertex("Algorithms");
            g.AddVertex("Assembly Language");

            g.AddEdge(0, 1);
            g.AddEdge(1, 2);
            g.AddEdge(2, 3);

            g.AddEdge(1, 5);
            g.AddEdge(2, 4);

            Console.WriteLine("Breadth-First:");
            g.BreadthFirstSearch();
            Console.WriteLine();
            Console.WriteLine("Depth-First:");
            g.DepthFirstSearch();
            Console.WriteLine();
            g.TopologicalSort();
            Console.WriteLine();

            g = new cGraph();
            g.AddVertex("A");
            g.AddVertex("B");
            g.AddVertex("C");
            g.AddVertex("D");
            g.AddVertex("E");
            g.AddVertex("F");
            g.AddVertex("G");
            g.AddVertex("H");
            g.AddVertex("I");
            g.AddVertex("J");
            g.AddVertex("K");
            g.AddVertex("L");
            g.AddVertex("M");

            g.AddEdge(0, 1);
            g.AddEdge(1, 2);
            g.AddEdge(2, 3);
            g.AddEdge(0, 4);
            g.AddEdge(4, 5);
            g.AddEdge(5, 6);
            g.AddEdge(0, 7);
            g.AddEdge(7, 8);
            g.AddEdge(8, 9);
            g.AddEdge(0, 10);
            g.AddEdge(10, 11);
            g.AddEdge(11, 12);


            Console.WriteLine("Breadth-First:");
            g.BreadthFirstSearch();
            Console.WriteLine();
            Console.WriteLine("Depth-First:");
            g.DepthFirstSearch();
            Console.WriteLine();
            g.TopologicalSort();
            Console.WriteLine();


            g = new cGraph(cGraph.cGraphTypeEnum.undirected);
            g.AddVertex("A");
            g.AddVertex("B");
            g.AddVertex("C");
            g.AddVertex("D");
            g.AddVertex("E");
            g.AddVertex("F");
            g.AddVertex("G");


            g.AddEdge(0, 1);
            g.AddEdge(0, 2);
            g.AddEdge(0, 3);
            g.AddEdge(1, 2);
            g.AddEdge(1, 3);
            g.AddEdge(1, 4);
            g.AddEdge(2, 3);
            g.AddEdge(2, 5);
            g.AddEdge(3, 5);
            g.AddEdge(3, 4);
            g.AddEdge(3, 6);
            g.AddEdge(4, 5);
            g.AddEdge(4, 6);
            g.AddEdge(5, 6);


            Console.WriteLine("Minimum Spanning Tree:");
            Console.WriteLine();
            g.Mst();
            Console.WriteLine();


            g = new cGraph(cGraph.cGraphTypeEnum.directed | cGraph.cGraphTypeEnum.weigthed);
            g.AddVertex("A");
            g.AddVertex("B");
            g.AddVertex("C");
            g.AddVertex("D");
            g.AddVertex("E");
            g.AddVertex("F");
            g.AddVertex("G");

            g.AddEdge(0, 1, 2);
            g.AddEdge(0, 3, 1);
            g.AddEdge(1, 3, 3);
            g.AddEdge(1, 4, 10);
            g.AddEdge(2, 5, 5);
            g.AddEdge(2, 0, 4);
            g.AddEdge(3, 2, 2);
            g.AddEdge(3, 5, 8);
            g.AddEdge(3, 4, 2);
            g.AddEdge(3, 6, 4);
            g.AddEdge(4, 6, 6);
            g.AddEdge(6, 5, 1);

            Console.WriteLine("Shortest paths:");
            Console.WriteLine();
            g.Path();
            Console.WriteLine();
            Console.WriteLine("Breadth-First:");
            g.BreadthFirstSearch();
            Console.WriteLine();
            Console.WriteLine("Depth-First:");
            g.DepthFirstSearch();
            Console.WriteLine();
            g.TopologicalSort();
            Console.WriteLine("Finished.");
            Console.ReadKey();
        }
示例#2
0
        static void Main2()
        {
            // Setup structure
            ObjectStructure o = new ObjectStructure();
            o.Attach(new ConcreteElementA());
            o.Attach(new ConcreteElementB());

            // Create visitor objects
            ConcreteVisitor1 v1 = new ConcreteVisitor1();
            ConcreteVisitor2 v2 = new ConcreteVisitor2();

            // Structure accepting visitors
            o.Accept(v1);
            o.Accept(v2);

            // Wait for user
            Console.ReadKey();

            cGraph g = new cGraph();

            g.AddVertex("A");
            g.AddVertex("B");
            g.AddVertex("C");
            g.AddVertex("D");
            g.AddEdge(0, 1);
            g.AddEdge(1, 2);
            g.AddEdge(2, 3);
            //g.AddEdge(3, 4);

            Console.WriteLine("Breadth-First:");
            g.BreadthFirstSearch();
            Console.WriteLine();
            Console.WriteLine("Depth-First:");
            g.DepthFirstSearch();
            Console.WriteLine();
            g.TopologicalSort();
            Console.WriteLine();

            g = new cGraph();

            g.AddVertex("CS1");
            g.AddVertex("CS2");
            g.AddVertex("Data Structures");
            g.AddVertex("Operating Systems");
            g.AddVertex("Algorithms");
            g.AddVertex("Assembly Language");

            g.AddEdge(0, 1);
            g.AddEdge(1, 2);
            g.AddEdge(2, 3);

            g.AddEdge(1, 5);
            g.AddEdge(2, 4);

            Console.WriteLine("Breadth-First:");
            g.BreadthFirstSearch();
            Console.WriteLine();
            Console.WriteLine("Depth-First:");
            g.DepthFirstSearch();
            Console.WriteLine();
            g.TopologicalSort();
            Console.WriteLine();

            g = new cGraph();
            g.AddVertex("A");
            g.AddVertex("B");
            g.AddVertex("C");
            g.AddVertex("D");
            g.AddVertex("E");
            g.AddVertex("F");
            g.AddVertex("G");
            g.AddVertex("H");
            g.AddVertex("I");
            g.AddVertex("J");
            g.AddVertex("K");
            g.AddVertex("L");
            g.AddVertex("M");

            g.AddEdge(0, 1);
            g.AddEdge(1, 2);
            g.AddEdge(2, 3);
            g.AddEdge(0, 4);
            g.AddEdge(4, 5);
            g.AddEdge(5, 6);
            g.AddEdge(0, 7);
            g.AddEdge(7, 8);
            g.AddEdge(8, 9);
            g.AddEdge(0, 10);
            g.AddEdge(10, 11);
            g.AddEdge(11, 12);

            Console.WriteLine("Breadth-First:");
            g.BreadthFirstSearch();
            Console.WriteLine();
            Console.WriteLine("Depth-First:");
            g.DepthFirstSearch();
            Console.WriteLine();
            g.TopologicalSort();
            Console.WriteLine();

            g = new cGraph(cGraph.cGraphTypeEnum.undirected);
            g.AddVertex("A");
            g.AddVertex("B");
            g.AddVertex("C");
            g.AddVertex("D");
            g.AddVertex("E");
            g.AddVertex("F");
            g.AddVertex("G");

            g.AddEdge(0, 1);
            g.AddEdge(0, 2);
            g.AddEdge(0, 3);
            g.AddEdge(1, 2);
            g.AddEdge(1, 3);
            g.AddEdge(1, 4);
            g.AddEdge(2, 3);
            g.AddEdge(2, 5);
            g.AddEdge(3, 5);
            g.AddEdge(3, 4);
            g.AddEdge(3, 6);
            g.AddEdge(4, 5);
            g.AddEdge(4, 6);
            g.AddEdge(5, 6);

            Console.WriteLine("Minimum Spanning Tree:");
            Console.WriteLine();
            g.Mst();
            Console.WriteLine();

            g = new cGraph(cGraph.cGraphTypeEnum.directed | cGraph.cGraphTypeEnum.weigthed);
            g.AddVertex("A");
            g.AddVertex("B");
            g.AddVertex("C");
            g.AddVertex("D");
            g.AddVertex("E");
            g.AddVertex("F");
            g.AddVertex("G");

            g.AddEdge(0, 1, 2);
            g.AddEdge(0, 3, 1);
            g.AddEdge(1, 3, 3);
            g.AddEdge(1, 4, 10);
            g.AddEdge(2, 5, 5);
            g.AddEdge(2, 0, 4);
            g.AddEdge(3, 2, 2);
            g.AddEdge(3, 5, 8);
            g.AddEdge(3, 4, 2);
            g.AddEdge(3, 6, 4);
            g.AddEdge(4, 6, 6);
            g.AddEdge(6, 5, 1);

            Console.WriteLine("Shortest paths:");
            Console.WriteLine();
            g.Path();
            Console.WriteLine();
            Console.WriteLine("Breadth-First:");
            g.BreadthFirstSearch();
            Console.WriteLine();
            Console.WriteLine("Depth-First:");
            g.DepthFirstSearch();
            Console.WriteLine();
            g.TopologicalSort();
            Console.WriteLine("Finished.");
            Console.ReadKey();
        }