public static void TwoTeams() { var n = Convert.ToInt32(Console.ReadLine()); visited = new bool[n]; used = new int[n]; for (int i = 0; i < n; i++) { var members = Console.ReadLine().Split(' ').Select(x => Int32.Parse(x) - 1).ToList(); members.Remove(members.Last()); AdjacencyList.Add(members); } for (int i = 0; i < n; i++) { if (!visited[i]) { BFS(i); } } var teamFirst = new List <int>(); for (int i = 0; i < used.Length; i++) { if (used[i] % 2 == 0) { teamFirst.Add(i + 1); } } Console.WriteLine(teamFirst.Count); Console.WriteLine(String.Join(" ", teamFirst)); }
/// <summary> /// 求一个图的反图 /// </summary> /// <returns></returns> public IAdjacency Reverse() { if (!Directed) { throw new Exception("directed graph can be support!"); } AdjacencyList reverse = (AdjacencyList)this.Clone(); reverse.Adj = new LinkedList <int> [V]; for (int i = 0; i < V; i++) { reverse.Adj[i] = new LinkedList <int>(); } reverse._indgree = new int[V]; reverse._outDgree = new int[V]; for (int i = 0; i < V; i++) { foreach (var w in this.Adj[i]) { reverse.Adj[w].AddLast(i); reverse._indgree[i]++; reverse._outDgree[w]++; } } return(reverse); }
public GraphNode <T> AddNode(T value) { GraphNode <T> node = new GraphNode <T>(value); AdjacencyList.Add(node, new List <Edge <T> >()); _size++; return(node); }
public IAdjacency Clone() { AdjacencyList clone = (AdjacencyList)this.MemberwiseClone(); clone.Adj = new LinkedList <int> [V]; for (int i = 0; i < this.Adj.Length; i++) { clone.Adj[i] = new LinkedList <int>(); foreach (var v in this.Adj[i]) { clone.Adj[i].AddLast(v); } } return(clone); }
static void Main(string[] args) { AdjacencyList <int> graph1 = new AdjacencyList <int>(); //添加顶点 graph1.AddVertex(1); graph1.AddVertex(2); graph1.AddVertex(3); graph1.AddVertex(4); graph1.AddVertex(5); graph1.AddVertex(6); graph1.AddVertex(10); graph1.AddVertex(7); graph1.AddVertex(8); graph1.AddVertex(9); //添加边 graph1.AddEdge(1, 2); graph1.AddEdge(1, 4); graph1.AddEdge(1, 3); graph1.AddEdge(4, 6); graph1.AddEdge(3, 5); graph1.AddEdge(10, 8); graph1.AddEdge(10, 7); graph1.AddEdge(8, 9); Console.WriteLine("graph1结构为:"); Console.WriteLine(graph1.ToString()); Console.WriteLine("深度优先遍历graph1:"); graph1.DFSTraverse(); Console.WriteLine(); Console.WriteLine("广度优先遍历graph1:"); graph1.BFSTraverse(); int[,] LJmini = new int[6, 6]; LJmini[0, 1] = 1; LJmini[0, 3] = 3; LJmini[3, 4] = 2; LJmini[0, 4] = 5; LJmini[0, 5] = 5; LJmini[1, 2] = 3; LJmini[1, 5] = 2; Console.WriteLine(); Dijkstra(LJmini, 0); Floyd(LJmini); Console.ReadKey(); }
static void Main(string[] args) { Console.Title = "遍历城市网"; string[] cities = new string[] { "北京", "上海", "天津", "重庆", "武汉","深圳","成都","西安"}; AdjacencyList graph = new AdjacencyList(cities); graph.AddEdge(cities[0], cities[1]); graph.AddEdge(cities[0], cities[2]); graph.AddEdge(cities[1], cities[3]); graph.AddEdge(cities[1], cities[4]); graph.AddEdge(cities[2], cities[5]); graph.AddEdge(cities[2], cities[6]); graph.AddEdge(cities[3], cities[7]); graph.AddEdge(cities[4], cities[7]); graph.AddEdge(cities[5], cities[6]); graph.DepthFirstSearch(); Console.ReadLine(); }
private void btnCreate_Click(object sender, EventArgs e) { AdjacencyList a = new AdjacencyList(); //添加顶点 //a.AddVertex("A"); //a.AddVertex("B"); //a.AddVertex("C"); //a.AddVertex("D"); ////添加边 //a.AddEdge("A", "B"); //a.AddEdge("A", "C"); //a.AddEdge("A", "D"); //a.AddEdge("B", "D"); a.AddVertex("V1"); a.AddVertex("V2"); a.AddVertex("V3"); a.AddVertex("V4"); a.AddVertex("V5"); a.AddVertex("V6"); a.AddVertex("V7"); a.AddVertex("V8"); a.AddEdge("V1", "V2"); a.AddEdge("V1", "V3"); a.AddEdge("V2", "V4"); a.AddEdge("V2", "V5"); a.AddEdge("V3", "V6"); a.AddEdge("V3", "V7"); //a.AddEdge("V4", "V8"); a.AddEdge("V5", "V8"); a.AddEdge("V6", "V8"); //a.AddEdge("V7", "V8"); a.DFSTraverse(); rtbList.AppendText(a.ToString()); rtbList.AppendText(a.dfsPath.ToString()); }
static void Main1(string[] args) { #region 显示节点的结构 //AdjacencyList<char> a = new AdjacencyList<char>(); ////添加顶点 //a.AddVertex('A'); //a.AddVertex('B'); //a.AddVertex('C'); //a.AddVertex('D'); ////添加边 //a.AddEdge('A', 'B'); //a.AddEdge('A', 'C'); //a.AddEdge('A', 'D'); //a.AddEdge('B', 'D'); //Console.WriteLine(a.ToString()); #endregion #region 深度优先遍历 //AdjacencyList<string> D = new AdjacencyList<string>(); //D.AddVertex("V1"); //D.AddVertex("V2"); //D.AddVertex("V3"); //D.AddVertex("V4"); //D.AddVertex("V5"); //D.AddVertex("V6"); //D.AddVertex("V7"); //D.AddVertex("V8"); //D.AddEdge("V1", "V2"); //D.AddEdge("V1", "V3"); //D.AddEdge("V2", "V4"); //D.AddEdge("V2", "V5"); //D.AddEdge("V3", "V6"); //D.AddEdge("V3", "V7"); //D.AddEdge("V4", "V8"); //D.AddEdge("V5", "V8"); //D.AddEdge("V6", "V8"); //D.AddEdge("V7", "V8"); //D.DFSTraverse(); #endregion #region 广度优先遍历 AdjacencyList <string> B = new AdjacencyList <string>(); B.AddVertex("V1"); B.AddVertex("V2"); B.AddVertex("V3"); B.AddVertex("V4"); B.AddVertex("V5"); B.AddVertex("V6"); B.AddVertex("V7"); B.AddVertex("V8"); B.AddEdge("V1", "V2"); B.AddEdge("V1", "V3"); B.AddEdge("V2", "V4"); B.AddEdge("V2", "V5"); B.AddEdge("V3", "V6"); B.AddEdge("V3", "V7"); B.AddEdge("V4", "V8"); B.AddEdge("V5", "V8"); B.AddEdge("V6", "V8"); B.AddEdge("V7", "V8"); B.BFSTraverse(); #endregion Console.ReadLine(); }
public void AddVertex(Person obj) { Vertices.Add(obj); AdjacencyList.Add(new List <int>()); }
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][])); }