public MinCreateTreeUseKruskal(WeightGraph weightGraph) { _weightGraph = weightGraph; _result = new List <WeightEdge>(); //cc检测是否联通 List <WeightEdge> list = new List <WeightEdge>(); for (int i = 0; i < _weightGraph.V; i++) { foreach (var w in _weightGraph.GetAllContiguousEdge(i)) { if (i > w) { continue; } int weight = _weightGraph.GetWeight(i, w); list.Add(new WeightEdge(i, w, weight)); } } list.Sort(); UnionFindAggregate uf = new UnionFindAggregate(_weightGraph.V); foreach (var e in list) { if (uf.IsConnected(e.V, e.W)) { continue; } _result.Add(e); uf.Union(e.V, e.W); } }
public Edmonds_Karp(WeightGraph weightGraph, int s, int t) { _weightGraph = weightGraph; _s = s; _t = t; if (!weightGraph.Directed) { throw new Exception("directed graph can be supported"); } if (_weightGraph.V < 2) { throw new Exception("vertex must be more than 2"); } _weightGraph.ValidateNumber(s); _weightGraph.ValidateNumber(t); if (s == t) { throw new Exception("source point and sink point can't be same"); } _residualQuantityGraph = new WeightGraph(_weightGraph.V, true); for (int v = 0; v < _weightGraph.V; v++) { foreach (var w in _weightGraph.GetAllContiguousEdge(v)) { int weight = _weightGraph.GetWeight(v, w); _residualQuantityGraph.AddEdge(v, w, weight); _residualQuantityGraph.AddEdge(w, v, 0); } } while (true) { List <int> path = GetArgumentingPath(); if (path.Count == 0) { break; } int f = int.MaxValue; for (int i = 1; i < path.Count; i++) { int v = path[i - 1]; int w = path[i]; f = Math.Min(f, _residualQuantityGraph.GetWeight(v, w)); } MaxFlow += f; for (int i = 1; i < path.Count; i++) { int v = path[i - 1]; int w = path[i]; int weight = _residualQuantityGraph.GetWeight(v, w); int residualQuantity = _residualQuantityGraph.GetWeight(w, v); _residualQuantityGraph.SetWeight(v, w, weight - f); _residualQuantityGraph.SetWeight(w, v, residualQuantity + f); } } }
public Floyed(WeightGraph weightGraph) { this._weightGraph = weightGraph; _dirs = new int[_weightGraph.V][]; _pre = new int[_weightGraph.V, _weightGraph.V]; for (int i = 0; i < _weightGraph.V; i++) { for (int j = 0; j < _weightGraph.V; j++) { _pre[i, j] = i; } } for (int i = 0; i < _weightGraph.V; i++) { _dirs[i] = new int[_weightGraph.V]; Array.Fill(_dirs[i], int.MaxValue); } for (int v = 0; v < _weightGraph.V; v++) { foreach (var w in _weightGraph.GetAllContiguousEdge(v)) { _dirs[v][w] = _weightGraph.GetWeight(v, w); } } //Floyed for (int t = 0; t < _weightGraph.V; t++) { for (int v = 0; v < _weightGraph.V; v++) { for (int w = 0; w < _weightGraph.V; w++) { if (_dirs[v][t] != Int32.MaxValue && _dirs[t][w] != Int32.MaxValue && _dirs[v][t] + _dirs[t][w] < _dirs[v][w]) { _dirs[v][w] = _dirs[v][t] + _dirs[t][w]; _pre[v, w] = _pre[t, w]; } } } } for (int i = 0; i < _weightGraph.V; i++) { if (_dirs[i][i] < 0) { IsHaveNegativeCircle = true; } } }
public WeightGraph Clone() { WeightGraph g = (WeightGraph)this.MemberwiseClone(); g.Adj = new Dictionary <int, int> [V]; for (int i = 0; i < V; i++) { g.Adj[i] = new Dictionary <int, int>(); foreach (var kv in this.Adj[i]) { g.Adj[i].Add(kv.Key, kv.Value); } } return(g); }
public Dijkstra(WeightGraph weightGraph, int s) { this._weightGraph = weightGraph; this._s = s; _pre = new int[_weightGraph.V]; _visited = new bool[_weightGraph.V]; _dirs = new int[_weightGraph.V]; for (int i = 0; i < _weightGraph.V; i++) { _dirs[i] = int.MaxValue; } _dirs[_s] = 0; while (true) { int curDis = Int32.MaxValue; int cur = -1; for (int i = 0; i < _weightGraph.V; i++) { if (!_visited[i] && _dirs[i] < curDis) { curDis = _dirs[i]; cur = i; } } if (cur == -1) { break; } _visited[cur] = true; foreach (var w in _weightGraph.GetAllContiguousEdge(cur)) { if (!_visited[w] && _dirs[w] > _dirs[cur] + _weightGraph.GetWeight(cur, w)) { _dirs[w] = _dirs[cur] + _weightGraph.GetWeight(cur, w); _pre[w] = cur; } } } }
public BinaryPartGraphMatch(IAdjacency iAdjacency) { _iAdjacency = iAdjacency; BinaryPartitionDetection binaryPartitionDetection = new BinaryPartitionDetection(iAdjacency); if (!binaryPartitionDetection.IsBinaryPartition) { throw new Exception("you must use two partite graph"); } int[] colors = binaryPartitionDetection.Colors; WeightGraph weightGraph = new WeightGraph(iAdjacency.V + 2, true); for (int v = 0; v < iAdjacency.V; v++) { if (colors[v] == 0) { weightGraph.AddEdge(iAdjacency.V, v, 1); } if (colors[v] == 1) { weightGraph.AddEdge(v, iAdjacency.V + 1, 1); } foreach (var w in iAdjacency.GetAllContiguousEdge(v)) { if (colors[v] == 0) { weightGraph.AddEdge(v, w, 1); } else if (colors[v] == 1) { weightGraph.AddEdge(w, v, 1); } } } Edmonds_Karp edmondsKarp = new Edmonds_Karp(weightGraph, iAdjacency.V, iAdjacency.V + 1); MaxMatch = edmondsKarp.MaxFlow; IsPerfectMatch = MaxMatch * 2 == iAdjacency.V; }
public Bellman_Ford(WeightGraph weightGraph, int s) { this._weightGraph = weightGraph; this._s = s; _pre = new int[_weightGraph.V]; _dirs = new int[_weightGraph.V]; for (int i = 0; i < _weightGraph.V; i++) { _dirs[i] = Int32.MaxValue; } _dirs[s] = 0; for (int i = 0; i < _weightGraph.V - 1; i++) { for (int j = 0; j < _weightGraph.V; j++) { foreach (var w in _weightGraph.GetAllContiguousEdge(j)) { if (_dirs[j] != int.MaxValue && _dirs[j] + _weightGraph.GetWeight(j, w) < _dirs[w]) { _dirs[w] = _dirs[j] + _weightGraph.GetWeight(j, w); _pre[w] = j; } } } } for (int j = 0; j < _weightGraph.V; j++) { foreach (var w in _weightGraph.GetAllContiguousEdge(j)) { if (_dirs[j] != int.MaxValue && _dirs[j] + _weightGraph.GetWeight(j, w) < _dirs[w]) { IsHaveCircle = true; } } } }
public MinCreateTreeUsePrim(WeightGraph weightGraph) { this._weightGraph = weightGraph; _result = new List <WeightEdge>(); //cc 的联通分量个数==1 bool[] visited = new bool[_weightGraph.V]; visited[0] = true; for (int i = 0; i < _weightGraph.V - 1; i++) { WeightEdge min = new WeightEdge(-1, -1, int.MaxValue); for (int j = 0; j < visited.Length; j++) { if (visited[j] == false) { continue; } foreach (var w in _weightGraph.GetAllContiguousEdge(j)) { if (visited[w]) { continue; } int weight = _weightGraph.GetWeight(j, w); WeightEdge cur = new WeightEdge(j, w, weight); if (cur.CompareTo(min) < 0) { min = cur; } } } _result.Add(min); visited[min.W] = true; visited[min.V] = true; } }
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][])); }