本文整理汇总了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");
}
示例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");
}
示例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");
}
示例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();
}
示例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();
}
示例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();
}
示例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();
}
示例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;
}
示例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();
}
示例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();
}
示例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();
}
示例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();
}
示例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");
}
示例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");
}
示例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");
}