当前位置: 首页>>代码示例>>C#>>正文


C# Graph.FindCycles方法代码示例

本文整理汇总了C#中Graph.FindCycles方法的典型用法代码示例。如果您正苦于以下问题:C# Graph.FindCycles方法的具体用法?C# Graph.FindCycles怎么用?C# Graph.FindCycles使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在Graph的用法示例。


在下文中一共展示了Graph.FindCycles方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: MultipleCyclesDirected

        public void MultipleCyclesDirected()
        {
            var graph = new Graph<int>(true);
            var vertex1 = graph.AddVertex(1);
            var vertex2 = graph.AddVertex(2);
            var vertex3 = graph.AddVertex(3);
            var vertex4 = graph.AddVertex(4);
            var vertex5 = graph.AddVertex(5);

            graph.AddEdge(vertex1, vertex2);
            graph.AddEdge(vertex2, vertex3);
            graph.AddEdge(vertex3, vertex1);

            graph.AddEdge(vertex4, vertex5);
            graph.AddEdge(vertex5, vertex4);

            var cycles = graph.FindCycles(true);

            Assert.AreEqual(2, cycles.Count, "There are two cycles");

            IList<Vertex<int>> cycle1 = cycles[0];
            IList<Vertex<int>> cycle2 = cycles[1];

            Assert.IsTrue(((cycle1.Count == 3) && (cycle2.Count == 2)) || ((cycle1.Count == 2) && (cycle2.Count == 3)), "Wrong number of items in the cycles");

            var index = (cycle1.Count == 3) ? 0 : 1;
            Assert.IsTrue(cycles[index].Any(v => v.Data == 1), "Vertex 1 missing from the cycle");
            Assert.IsTrue(cycles[index].Any(v => v.Data == 2), "Vertex 2 missing from the cycle");
            Assert.IsTrue(cycles[index].Any(v => v.Data == 3), "Vertex 3 missing from the cycle");

            index = (cycle1.Count == 2) ? 0 : 1;
            Assert.IsTrue(cycles[index].Any(v => v.Data == 4), "Vertex 1 missing from the cycle");
            Assert.IsTrue(cycles[index].Any(v => v.Data == 5), "Vertex 2 missing from the cycle");
        }
开发者ID:GTuritto,项目名称:ngenerics,代码行数:34,代码来源:FindCycles.cs

示例2: ExcludeSingleItems

        public void ExcludeSingleItems()
        {
            var graph = new Graph<int>(true);
            graph.AddVertex(1);

            var cycles = graph.FindCycles();

            Assert.AreEqual(0, cycles.Count, "Wrong number of cycles");
        }
开发者ID:GTuritto,项目名称:ngenerics,代码行数:9,代码来源:FindCycles.cs

示例3: IncludeSingleItems

        public void IncludeSingleItems()
        {
            var graph = new Graph<int>(true);
            graph.AddVertex(1);

            var cycles = graph.FindCycles(false);

            Assert.AreEqual(1, cycles.Count, "Wrong number of cycles");
            Assert.AreEqual(1, cycles[0].Length, "Wrong number of nodes in the cycle");
            Assert.AreEqual(1, cycles[0][0].Data, "Wrong number of nodes in the cycle");
        }
开发者ID:GTuritto,项目名称:ngenerics,代码行数:11,代码来源:FindCycles.cs

示例4: DisconnectedWithACycle

        public void DisconnectedWithACycle()
        {
            var edges = new Dictionary<int, int[]>
            {
                { 1, new int[]{} },
                { 2, new[]{3} },
                { 3, new[]{2} }
            };
            var graph = new Graph<int>(new[] { 1, 2, 3 }, i => edges[i]);

            graph.FindCycles().First().SetEquals(new[] { 2, 3 }).ShouldBeTrue();
        }
开发者ID:ryansroberts,项目名称:cassette,代码行数:12,代码来源:Graph.cs

示例5: Disconnected

        public void Disconnected()
        {
            var edges = new Dictionary<int, int[]>
            {
                { 1, new int[]{} },
                { 2, new int[]{} },
                { 3, new int[]{} }
            };
            var graph = new Graph<int>(new[] { 1, 2, 3 }, i => edges[i]);

            graph.FindCycles().ShouldBeEmpty();
        }
开发者ID:ryansroberts,项目名称:cassette,代码行数:12,代码来源:Graph.cs

示例6: BackReferenceDoesNotCauseCycle

        public void BackReferenceDoesNotCauseCycle()
        {
            var edges = new Dictionary<int, int[]>
            {
                { 1, new[] { 2 } },
                { 2, new[] { 3 } },
                { 3, new int[] { } },
                { 4, new[] { 1 } }
            };
            var graph = new Graph<int>(new[] { 1, 2, 3, 4 }, i => edges[i]);

            graph.FindCycles().ShouldBeEmpty();
        }
开发者ID:ryansroberts,项目名称:cassette,代码行数:13,代码来源:Graph.cs

示例7: DiamondWithCycle

        public void DiamondWithCycle()
        {
            var edges = new Dictionary<int, int[]>
            {
                { 1, new[] { 2, 3 } },
                { 2, new[] { 4 } },
                { 3, new[] { 4 } },
                { 4, new[] { 1 } }
            };
            var graph = new Graph<int>(new[] { 1, 2, 3, 4 }, i => edges[i]);

            var cycle = graph.FindCycles().First();
            cycle.SetEquals(new[] { 1, 2, 3, 4 }).ShouldBeTrue();
        }
开发者ID:ryansroberts,项目名称:cassette,代码行数:14,代码来源:Graph.cs

示例8: SortAssetsByDependency

 internal void SortAssetsByDependency()
 {
     if (IsSorted) return;
     // Graph topological sort, based on references between assets.
     var assetsByFilename = Assets.ToDictionary(
         a => a.SourceFile.FullPath,
         StringComparer.OrdinalIgnoreCase
     );
     var graph = new Graph<IAsset>(
         Assets,
         asset => asset.References
             .Where(reference => reference.Type == AssetReferenceType.SameBundle)
             .Select(reference => assetsByFilename[reference.Path])
     );
     var cycles = graph.FindCycles().ToArray();
     if (cycles.Length > 0)
     {
         var details = string.Join(
             Environment.NewLine,
             cycles.Select(
                 cycle => "[" + string.Join(", ", cycle.Select(a => a.SourceFile.FullPath)) + "]"
             )
         );
         throw new InvalidOperationException("Cycles detected in asset references:" + Environment.NewLine + details);
     }
     assets.Clear();
     assets.AddRange(graph.TopologicalSort());
     IsSorted = true;
 }
开发者ID:stucampbell,项目名称:cassette,代码行数:29,代码来源:Bundle.cs

示例9: MoreComplexGraphWithNoCycles

        public void MoreComplexGraphWithNoCycles()
        {
            var edges = new Dictionary<int, int[]>
            {
                { 1, new int[] { } },
                { 2, new int[] { } },
                { 3, new[] { 1, 2, 5 } },
                { 4, new[] { 1, 2, 6 } },
                { 5, new[] { 2 } },
                { 6, new[] { 2, 3, 7, 5 } },
                { 7, new[] { 3 } },
                { 8, new[] { 3 } }
            };
            var graph = new Graph<int>(new[] { 1, 2, 3, 4, 5, 6, 7, 8 }, i => edges[i]);

            var result = graph.FindCycles().ToArray();
            result.ShouldBeEmpty();
        }
开发者ID:ryansroberts,项目名称:cassette,代码行数:18,代码来源:Graph.cs

示例10: TwoDisconnectedCycles

        public void TwoDisconnectedCycles()
        {
            var edges = new Dictionary<int, int[]>
            {
                { 1, new[] { 2 } },
                { 2, new[] { 1 } },
                { 3, new[] { 4 } },
                { 4, new[] { 3 } }
            };
            var graph = new Graph<int>(new[] { 1, 2, 3, 4 }, i => edges[i]);

            var cycles = graph.FindCycles().ToArray();
            cycles[0].SetEquals(new[] { 1, 2 }).ShouldBeTrue();
            cycles[1].SetEquals(new[] { 3, 4 }).ShouldBeTrue();
        }
开发者ID:ryansroberts,项目名称:cassette,代码行数:15,代码来源:Graph.cs

示例11: PartialCycle2

        public void PartialCycle2()
        {
            var edges = new Dictionary<int, int[]>
            {
                { 1, new[]{4} },
                { 2, new[]{4} },
                { 3, new[]{4,6} },
                { 4, new[]{5} },
                { 5, new int[]{} },
                {6,new[]{5}}
            };
            var graph = new Graph<int>(new[] { 1, 2, 3, 4, 5,6 }, i => edges[i]);

            graph.FindCycles().ShouldBeEmpty();
        }
开发者ID:ryansroberts,项目名称:cassette,代码行数:15,代码来源:Graph.cs

示例12: PartialCycle

        public void PartialCycle()
        {
            var edges = new Dictionary<int, int[]>
            {
                { 1, new[]{2} },
                { 2, new[]{3} },
                { 3, new[]{1} },
                { 4, new[]{1} }
            };
            var graph = new Graph<int>(new[] { 1, 2, 3, 4 }, i => edges[i]);

            graph.FindCycles().First().SetEquals(new[] { 1, 2, 3 }).ShouldBeTrue();
        }
开发者ID:ryansroberts,项目名称:cassette,代码行数:13,代码来源:Graph.cs

示例13: MultipleCyclesUndirected

        public void MultipleCyclesUndirected()
        {
            var graph = new Graph<int>(false);
            var vertex1 = graph.AddVertex(1);
            var vertex2 = graph.AddVertex(2);
            var vertex3 = graph.AddVertex(3);
            var vertex4 = graph.AddVertex(4);
            var vertex5 = graph.AddVertex(5);

            graph.AddEdge(vertex1, vertex2);
            graph.AddEdge(vertex2, vertex3);
            graph.AddEdge(vertex3, vertex1);

            graph.AddEdge(vertex4, vertex5);

            var cycles = graph.FindCycles(true);

            Assert.AreEqual(1, cycles.Count, "There are two cycles");

            IList<Vertex<int>> cycle1 = cycles[0];

            Assert.IsTrue((cycle1.Count == 3) || (cycle1.Count == 2), "Wrong number of items in the cycles");

            Assert.IsTrue(cycles[0].Any(v => v.Data == 1), "Vertex 1 missing from the cycle");
            Assert.IsTrue(cycles[0].Any(v => v.Data == 2), "Vertex 2 missing from the cycle");
            Assert.IsTrue(cycles[0].Any(v => v.Data == 3), "Vertex 3 missing from the cycle");
        }
开发者ID:GTuritto,项目名称:ngenerics,代码行数:27,代码来源:FindCycles.cs

示例14: SingleCycleWithManyEdgesUndirected

        public void SingleCycleWithManyEdgesUndirected()
        {
            var graph = new Graph<int>(false);
            var vertex1 = graph.AddVertex(1);
            var vertex2 = graph.AddVertex(2);
            var vertex3 = graph.AddVertex(3);
            var vertex4 = graph.AddVertex(4);

            graph.AddEdge(vertex1, vertex2);
            graph.AddEdge(vertex2, vertex3);
            graph.AddEdge(vertex3, vertex1);
            graph.AddEdge(vertex3, vertex4);
            graph.AddEdge(vertex4, vertex1);
            graph.AddEdge(vertex4, vertex2);

            var cycles = graph.FindCycles(true);

            Assert.AreEqual(1, cycles.Count, "There is a single cycle");

            IList<Vertex<int>> cycle1 = cycles[0];
            Assert.IsTrue(cycle1.Any(v => v.Data == 1), "Vertex 1 missing from the cycle");
            Assert.IsTrue(cycle1.Any(v => v.Data == 2), "Vertex 2 missing from the cycle");
            Assert.IsTrue(cycle1.Any(v => v.Data == 3), "Vertex 3 missing from the cycle");
            Assert.IsTrue(cycle1.Any(v => v.Data == 3), "Vertex 4 missing from the cycle");
            Assert.AreEqual(4, cycle1.Count, "There should be four items in the cycle");
        }
开发者ID:GTuritto,项目名称:ngenerics,代码行数:26,代码来源:FindCycles.cs

示例15: SingleCycle

        public void SingleCycle()
        {
            var graph = new Graph<int>(true);
            var vertex1 = graph.AddVertex(1);
            var vertex2 = graph.AddVertex(2);
            var vertex3 = graph.AddVertex(3);

            graph.AddEdge(vertex1, vertex2);
            graph.AddEdge(vertex2, vertex3);
            graph.AddEdge(vertex3, vertex1);

            var cycles = graph.FindCycles(true);

            Assert.AreEqual(1, cycles.Count, "There is a single cycle");

            IList<Vertex<int>> cycle1 = cycles[0];
            Assert.IsTrue(cycle1.Any(v => v.Data == 1), "Vertex 1 missing from the cycle");
            Assert.IsTrue(cycle1.Any(v => v.Data == 2), "Vertex 2 missing from the cycle");
            Assert.IsTrue(cycle1.Any(v => v.Data == 3), "Vertex 3 missing from the cycle");
            Assert.AreEqual(3, cycle1.Count, "There should be three items in the cycle");
        }
开发者ID:GTuritto,项目名称:ngenerics,代码行数:21,代码来源:FindCycles.cs


注:本文中的Graph.FindCycles方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。