コード例 #1
0
ファイル: GraphAlgorithims.cs プロジェクト: Nublo/Quoridor
 public static void InsightDFS(int vertex, bool[] used, AdjacencyMatrix graph)
 {
     used[vertex] = true;
     for (int i = 0; i < graph.NumberVertexes; i++)
         if (graph.GetWeigth(vertex, i) != Constants.NotConnected && !used[i])
         {
             used[i] = true;
             InsightDFS(i, used, graph);
         }
     used[vertex] = true;
 }
コード例 #2
0
ファイル: GraphAlgorithims.cs プロジェクト: Nublo/Quoridor
 public static bool DFS(AdjacencyMatrix graph, int vertex1, int vertex2)
 {
     int numberVertexes = graph.NumberVertexes;
     bool[] used = new bool[numberVertexes + 1];
     for (int i = 1; i <= numberVertexes; i++)
     {
         used[i] = false;
     }
     InsightDFS(vertex1, used, graph);
     return used[vertex2];
 }
コード例 #3
0
        static void Main(string[] args)
        {
            var matrix = new AdjacencyMatrix(5);

            matrix.Connect(1, 2);
            matrix.Connect(2, 3);
            matrix.Connect(3, 4);
            matrix.Connect(2, 4);
            matrix.Connect(2, 0);
            matrix.Disconnect(1, 2);

            Console.Write(matrix.ToString());
        }
コード例 #4
0
        public IAdjacency Clone()
        {
            AdjacencyMatrix adjacencyMatrix = (AdjacencyMatrix)this.MemberwiseClone();

            adjacencyMatrix.Adj = new int[V, V];
            for (int i = 0; i < V; i++)
            {
                for (int j = 0; j < V; j++)
                {
                    adjacencyMatrix.Adj[i, j] = this.Adj[i, j];
                }
            }

            return(adjacencyMatrix);
        }
コード例 #5
0
ファイル: GraphAlgorithims.cs プロジェクト: Nublo/Quoridor
 public static bool BFS(AdjacencyMatrix graph, int vertex1, int vertex2)
 {
     Queue<int> queue = new Queue<int>();
     int numberVertexes = graph.NumberVertexes;
     queue.Enqueue(vertex1);
     while (queue.Count != 0)
     {
         int vertex = queue.Dequeue();
         for (int i = 0; i < numberVertexes; i++)
             if (graph.GetWeigth(vertex, i) != Constants.NotConnected)
             {
                 if (i == vertex2)
                 {
                     return true;
                 }
                 queue.Enqueue(i);
             }
     }
     return false;
 }
コード例 #6
0
        /// <summary>
        /// 求一个图的反图
        /// </summary>
        /// <returns></returns>
        public IAdjacency Reverse()
        {
            AdjacencyMatrix reverse = (AdjacencyMatrix)this.Clone();

            reverse._inDgree   = new int[V];
            reverse._outDegree = new int[V];
            reverse.Adj        = new int[V, V];

            for (int i = 0; i < V; i++)
            {
                for (int j = 0; j < V; j++)
                {
                    if (this.Adj[i, j] == 1)
                    {
                        reverse.Adj[j, i] = 1;
                        reverse._inDgree[i]++;
                        reverse._outDegree[j]++;
                    }
                }
            }

            return(reverse);
        }
コード例 #7
0
        //Create a graph
        //      A(0)
        //      |
        //      |
        //      B(1)------H(7)
        //      |         |
        //      |         |
        //      C(2)------E(4)------G(6)
        //      |         |
        //      |         |
        //      D(3)      F(5)
        static void Main(string[] args)
        {
            AdjacencyMatrix aM = new AdjacencyMatrix(8);

            aM.addEdge(0, 1);
            aM.addEdge(1, 2);
            aM.addEdge(1, 7);
            aM.addEdge(2, 4);
            aM.addEdge(7, 4);
            aM.addEdge(4, 6);
            aM.addEdge(2, 3);
            aM.addEdge(4, 5);

            Vertex A = new Vertex('A');
            Vertex B = new Vertex('B');
            Vertex C = new Vertex('C');
            Vertex D = new Vertex('D');
            Vertex E = new Vertex('E');
            Vertex F = new Vertex('F');
            Vertex G = new Vertex('G');
            Vertex H = new Vertex('H');

            Vertex[] vertexList = new Vertex[8] {
                A, B, C, D, E, F, G, H
            };

            //CommonGraph cG = new CommonGraph(aM, vertexList, 8);
            TraversalHelper.AllTraversal(aM, vertexList, 8);

            //    5         4
            //   / \       / \
            //  /   \     /   \
            // v     v   v     v
            //2        0        1
            // \               ^
            //  \             /
            //   \           /
            //     \        /
            //       \     /
            //        v   /
            //          3

            // Create a graph given
            // in the above diagram
            TopologicalSort g = new TopologicalSort(6);

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

            Console.WriteLine("Following is a Topological "
                              + "sort of the given graph");

            // Function Call
            g.TopologicalSorting();

            Console.ReadLine();
        }
コード例 #8
0
        static void MainForGraph(string[] args)
        {
            AdjacencyMatrix adjcencyMatrix = new AdjacencyMatrix("../../../g2.txt");
            AdjacencyList   adjacencyList  = new AdjacencyList("../../../g2.txt");

            Console.WriteLine(adjcencyMatrix);
            Console.WriteLine(adjacencyList);

            DfsGraph dfsMatrix = new DfsGraph(adjcencyMatrix);

            dfsMatrix.PreOrder.ForEach(Console.WriteLine);
            Console.WriteLine("----------------华丽的分割线----------------");
            dfsMatrix.PostOrder.ForEach(Console.WriteLine);
            Console.WriteLine(dfsMatrix.ConnectedComponentCount);
            PrintListArray(dfsMatrix.ConnectedComponents());

            Console.WriteLine("----------------华丽的分割线----------------");

            DfsGraph dfsList = new DfsGraph(adjacencyList);

            dfsList.PreOrder.ForEach(Console.WriteLine);
            Console.WriteLine("----------------华丽的分割线----------------");
            dfsList.PostOrder.ForEach(Console.WriteLine);
            Console.WriteLine(dfsList.ConnectedComponentCount);
            PrintListArray(dfsList.ConnectedComponents());

            //Console.WriteLine("----------------华丽的分割线----------------");
            //SingleSourcePath singleSourcePath=new SingleSourcePath(adjcencyMatrix,0,6);
            //foreach (var i in singleSourcePath.Path())
            //{
            //    Console.Write(i+" ");
            //}

            Console.WriteLine();
            Console.WriteLine("----------------华丽的分割线----------------");
            CircleDetection circleDetection = new CircleDetection(adjcencyMatrix);

            Console.WriteLine(circleDetection.IsHaveCircle);

            Console.WriteLine("----------------华丽的分割线----------------");
            BinaryPartitionDetection binaryPartitionDetection = new BinaryPartitionDetection(adjcencyMatrix);

            Console.WriteLine(binaryPartitionDetection.IsBinaryPartition);

            BfsGraph bfsGraph = new BfsGraph(adjcencyMatrix);

            bfsGraph.Order.ForEach(Console.WriteLine);

            //Console.WriteLine("----------------华丽的分割线----------------");
            //SingleSourcePathUseBfs singleSourcePathUseBfs=new SingleSourcePathUseBfs(adjcencyMatrix,0);
            //foreach (var i in singleSourcePathUseBfs.Path(6))
            //{
            //    Console.Write(i+" ");
            //}

            //Console.WriteLine();
            //Console.WriteLine(singleSourcePathUseBfs.MinDistance(6));


            //Console.WriteLine("----------------华丽的分割线----------------");
            //int[][] grid = new int[4][];//[[1,1,0,0,0],[1,1,0,0,0],[0,0,0,1,1],[0,0,0,1,1]]
            //grid[0] =new int[]{ 1, 1, 0, 0, 0 };
            //grid[1]=new int[]{ 1, 1, 0, 0, 0 };
            //grid[2] = new int[] { 0, 0, 0, 1,1 };
            //grid[3] = new int[] { 0, 0, 0, 1, 1 };
            //Solution solution=new Solution();
            //Console.WriteLine(solution.MaxAreaOfIsland(grid));

            //Console.WriteLine("----------------华丽的分割线----------------");
            //int[][] grid = new int[4][];//[[1,1,0,0,0],[1,1,0,0,0],[0,0,0,1,1],[0,0,0,1,1]]
            //grid[0] = new int[] { 1, 1, 0, 0, 0 };
            //grid[1] = new int[] { 1, 1, 0, 0, 0 };
            //grid[2] = new int[] { 0, 0, 0, 1, 1 };
            //grid[3] = new int[] { 0, 0, 0, 1, 1 };
            //Solution1 solution = new Solution1();
            //Console.WriteLine(solution.MaxAreaOfIsland(grid));

            Console.WriteLine("----------------华丽的分割线----------------");
            Solution3 solution3 = new Solution3();

            int[][] grid = new int[3][]
            {
                new int[3] {
                    0, 0, 0
                },
                new int[3] {
                    1, 1, 0
                },
                new int[3] {
                    1, 1, 0
                }
            };
            Console.WriteLine(solution3.ShortestPathBinaryMatrix(grid));

            Console.WriteLine("----------------华丽的分割线----------------");
            Bulket bulket = new Bulket();

            bulket.WaterPullze().Path().ForEach(t => Console.WriteLine(t[0] + "," + t[1]));

            Console.WriteLine("----------------华丽的分割线----------------");
            FarmerAcrossTheRiver farmerAcrossTheRiver = new FarmerAcrossTheRiver();

            farmerAcrossTheRiver.AcrossTheRiver().ForEach(_ =>
            {
                Console.WriteLine(_[0] + "," + _[1] + "," + _[2] + "," + _[3]);
            });


            Console.WriteLine("----------------华丽的分割线----------------");
            Bridge bridge = new Bridge();

            bridge.FindBridge(adjcencyMatrix);
            bridge.BridgeEdge.ForEach(Console.WriteLine);

            Console.WriteLine("----------------华丽的分割线----------------");
            CutPoint cutPoint = new CutPoint();

            cutPoint.FindCutPoint(adjcencyMatrix);
            foreach (var cutPointCutPoint in cutPoint.CutPoints)
            {
                Console.Write(cutPointCutPoint + " ");
            }


            Console.WriteLine();
            Console.WriteLine("----------------华丽的分割线----------------");
            HamiltonLoop hamiltonLoop = new HamiltonLoop();

            hamiltonLoop.FindHamiltonPath(adjcencyMatrix);
            hamiltonLoop.Path().ForEach(Console.WriteLine);

            Console.WriteLine();
            Console.WriteLine("----------------华丽的分割线----------------");
            HamiltonPath hamiltonPath = new HamiltonPath(adjcencyMatrix, 0);

            foreach (var i in hamiltonPath.Path())
            {
                Console.Write(i + " ");
            }
            Console.WriteLine();

            Console.WriteLine("----------------华丽的分割线----------------");
            // [[1,0,0,0],[0,0,0,0],[0,0,2,-1]]
            DiferencePath diferencePath = new DiferencePath();

            int[][] s = new int[3][] { new int[] { 1, 0, 0, 0 }, new int[] { 0, 0, 0, 0 }, new int[] { 0, 0, 2, -1 } };
            Console.WriteLine(diferencePath.UniquePathsIII(s));

            Console.WriteLine("----------------华丽的分割线----------------");
            EulerLoop eulerLoop = new EulerLoop(adjcencyMatrix);

            eulerLoop.EulerLoopPath().ForEach(Console.WriteLine);


            Console.WriteLine("----------------华丽的分割线----------------");
            WeightGraph             weightGraph             = new WeightGraph("../../../mincreatetree.txt");
            MinCreateTreeUseKruskal minCreateTreeUseKruskal = new MinCreateTreeUseKruskal(weightGraph);

            minCreateTreeUseKruskal.Result().ForEach(Console.WriteLine);

            Console.WriteLine("----------------华丽的分割线----------------");
            MinCreateTreeUsePrim minCreateTreeUsePrim = new MinCreateTreeUsePrim(weightGraph);

            minCreateTreeUsePrim.Result().ForEach(Console.WriteLine);


            Console.WriteLine("----------------华丽的分割线----------------");
            WeightGraph weightGraph1 = new WeightGraph("../../../dijkstra.txt");
            Dijkstra    dijkstra     = new Dijkstra(weightGraph1, 0);

            Console.WriteLine(dijkstra.Distance(4));
            dijkstra.Path(4).ForEach(Console.WriteLine);

            Console.WriteLine("----------------华丽的分割线----------------");
            Bellman_Ford bellmanFord = new Bellman_Ford(weightGraph1, 0);

            Console.WriteLine(bellmanFord.Distance(4));
            bellmanFord.Path(4).ForEach(Console.WriteLine);

            Console.WriteLine("----------------华丽的分割线----------------");
            Floyed floyed = new Floyed(weightGraph1);

            Console.WriteLine(floyed.ShortedLength(0, 4));
            floyed.Path(0, 4).ForEach(Console.WriteLine);

            Console.WriteLine("----------------华丽的分割线----------------");
            IAdjacency adjacency = new AdjacencyList("../../../directed.txt", true);
            DirectedCircleDectection directedCircleDectection = new DirectedCircleDectection(adjacency);

            Console.WriteLine(directedCircleDectection.IsHaveCircle);

            Console.WriteLine("----------------华丽的分割线----------------");
            ToopSort toopSort = new ToopSort(adjacency);

            Console.WriteLine(toopSort.IsHaveCircle);
            toopSort.Result.ForEach(Console.WriteLine);

            Console.WriteLine("----------------华丽的分割线----------------");
            ToopSortByDfs toopSortByDfs = new ToopSortByDfs(adjacency);

            toopSortByDfs.Result.ForEach(Console.WriteLine);

            Console.WriteLine("----------------华丽的分割线----------------");
            IAdjacency stronglyTest = new AdjacencyList("../../../stronglyconnectedweight.txt", true);
            StronglyConnectedWeight stronglyConnectedWeight = new StronglyConnectedWeight(stronglyTest);

            Console.WriteLine(stronglyConnectedWeight.StronglyConnectedWeightCount);
            for (int i = 0; i < stronglyConnectedWeight.StronglyConnectedComponent.Length; i++)
            {
                Console.Write(i + ":");
                foreach (var v in stronglyConnectedWeight.StronglyConnectedComponent[i])
                {
                    Console.Write(v + " ");
                }
                Console.WriteLine();
            }

            Console.WriteLine("----------------华丽的分割线----------------");
            WeightGraph  edmondsGraph = new WeightGraph("../../../edmondskarp.txt", true);
            Edmonds_Karp edmondsKarp  = new Edmonds_Karp(edmondsGraph, 0, 3);

            Console.WriteLine(edmondsKarp.MaxFlow);
            Console.WriteLine(edmondsKarp.FlowInTwoVertex(0, 1));

            Console.WriteLine("----------------华丽的分割线----------------");
            AdjacencyList        twoPartite           = new AdjacencyList("../../../twopartitegraphmatch.txt");
            BinaryPartGraphMatch binaryPartGraphMatch = new BinaryPartGraphMatch(twoPartite);

            Console.WriteLine(binaryPartGraphMatch.MaxMatch);
            Console.WriteLine(binaryPartGraphMatch.IsPerfectMatch);

            Console.WriteLine("----------------华丽的分割线----------------");
            Hungarain hungarain = new Hungarain(twoPartite);

            Console.WriteLine(hungarain.MaxMatch);

            Console.WriteLine("----------------华丽的分割线----------------");
            Lcp4 lcp4 = new Lcp4();

            int[][] test = new int[2][] { new int[] { 1, 0 }, new int[] { 1, 1 } };
            Console.WriteLine(lcp4.domino(2, 3, test));
            Console.WriteLine(lcp4.domino(3, 3, new int[0][]));
        }
コード例 #9
0
ファイル: Field.cs プロジェクト: Nublo/Quoridor
 private void GenerateGraph()
 {
     // 2 ребра у крайних, 3 у стенок, 4 во внутреннем квадрате (для неориентированного графа)
     int numberOfEdges = 4 * 2 + (Dimension - 2) * 3 * 4 + (Dimension - 2) * (Dimension - 2) * 4;
     int numberOfVertexes = Dimension * Dimension;
     int[,] mas = new int[numberOfEdges, 3];
     int k = 0; //индексация для массива вершин
     for (int i = 0; i < numberOfVertexes; i++)
     {
         int[] neighbors = GetNeighbors(i);
         for (int j = 0; j < 4; j++)
             if (neighbors[j] != Constants.NoNeighbor)
             {
                 mas[k, 0] = i;
                 mas[k, 1] = neighbors[j];
                 mas[k, 2] = Constants.EdgeWeight;
                 k++;
             }
     }
     matrix = new AdjacencyMatrix(numberOfVertexes, numberOfEdges, mas);
 }