本文整理汇总了C#中Graph.TopologicalSort方法的典型用法代码示例。如果您正苦于以下问题:C# Graph.TopologicalSort方法的具体用法?C# Graph.TopologicalSort怎么用?C# Graph.TopologicalSort使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Graph
的用法示例。
在下文中一共展示了Graph.TopologicalSort方法的8个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Exception
public void Exception()
{
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);
graph.AddEdge(vertex1, vertex2);
graph.AddEdge(vertex2, vertex3);
graph.AddEdge(vertex2, vertex4);
graph.AddEdge(vertex3, vertex4);
graph.AddEdge(vertex4, vertex1);
graph.TopologicalSort();
}
示例2: 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;
}
示例3: TopologicalSortExample
public void TopologicalSortExample()
{
// Create a new directed graph
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);
// Add some edges
graph.AddEdge(vertex1, vertex2);
graph.AddEdge(vertex2, vertex3);
graph.AddEdge(vertex2, vertex4);
graph.AddEdge(vertex3, vertex4);
// Retrieve the vertices from the graph in topological order
var vertices = graph.TopologicalSort();
// There will be 4 items in the list
Assert.AreEqual(vertices.Count, 4);
// And the topological order of the sample graph is vertex1, vertex2, vertex3, and vertex4.
Assert.AreSame(vertices[0], vertex1);
Assert.AreSame(vertices[1], vertex2);
Assert.AreSame(vertices[2], vertex3);
Assert.AreSame(vertices[3], vertex4);
}
示例4: OrderModulesByDependency
IEnumerable<Module> OrderModulesByDependency(IEnumerable<Module> modules)
{
var modulesByPath = modules.ToDictionary(m => m.Path, StringComparer.OrdinalIgnoreCase);
var graph = new Graph<string>(
modules.Select(m => m.Path),
path => modulesByPath[path].References
);
return graph.TopologicalSort().Select(path => modulesByPath[path]);
}
示例5: Simple
public void Simple()
{
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);
graph.AddEdge(vertex1, vertex2);
graph.AddEdge(vertex2, vertex3);
graph.AddEdge(vertex2, vertex4);
graph.AddEdge(vertex3, vertex4);
var vertices = graph.TopologicalSort();
Assert.AreEqual(vertices.Count, 4);
Assert.AreSame(vertices[0], vertex1);
Assert.AreSame(vertices[1], vertex2);
Assert.AreSame(vertices[2], vertex3);
Assert.AreSame(vertices[3], vertex4);
}
示例6: ExceptionUndirectedGraph
public void ExceptionUndirectedGraph()
{
var graph = new Graph<int>(false);
graph.TopologicalSort();
}
示例7: ComputeLiveVariables
public static void ComputeLiveVariables(Implementation impl) {
Contract.Requires(impl != null);
Microsoft.Boogie.Helpers.ExtraTraceInformation("Starting live variable analysis");
Graph<Block> dag = new Graph<Block>();
dag.AddSource(cce.NonNull(impl.Blocks[0])); // there is always at least one node in the graph
foreach (Block b in impl.Blocks) {
GotoCmd gtc = b.TransferCmd as GotoCmd;
if (gtc != null) {
Contract.Assume(gtc.labelTargets != null);
foreach (Block/*!*/ dest in gtc.labelTargets) {
Contract.Assert(dest != null);
dag.AddEdge(dest, b);
}
}
}
IEnumerable<Block> sortedNodes;
if (CommandLineOptions.Clo.ModifyTopologicalSorting) {
sortedNodes = dag.TopologicalSort(true);
} else {
sortedNodes = dag.TopologicalSort();
}
foreach (Block/*!*/ block in sortedNodes) {
Contract.Assert(block != null);
HashSet<Variable/*!*/>/*!*/ liveVarsAfter = new HashSet<Variable/*!*/>();
// The injected assumption variables should always be considered to be live.
foreach (var v in impl.InjectedAssumptionVariables.Concat(impl.DoomedInjectedAssumptionVariables))
{
liveVarsAfter.Add(v);
}
if (block.TransferCmd is GotoCmd) {
GotoCmd gotoCmd = (GotoCmd)block.TransferCmd;
if (gotoCmd.labelTargets != null) {
foreach (Block/*!*/ succ in gotoCmd.labelTargets) {
Contract.Assert(succ != null);
Contract.Assert(succ.liveVarsBefore != null);
liveVarsAfter.UnionWith(succ.liveVarsBefore);
}
}
}
List<Cmd> cmds = block.Cmds;
int len = cmds.Count;
for (int i = len - 1; i >= 0; i--) {
if (cmds[i] is CallCmd) {
Procedure/*!*/ proc = cce.NonNull(cce.NonNull((CallCmd/*!*/)cmds[i]).Proc);
if (InterProcGenKill.HasSummary(proc.Name)) {
liveVarsAfter =
InterProcGenKill.PropagateLiveVarsAcrossCall(cce.NonNull((CallCmd/*!*/)cmds[i]), liveVarsAfter);
continue;
}
}
Propagate(cmds[i], liveVarsAfter);
}
block.liveVarsBefore = liveVarsAfter;
}
}
示例8: OrderScriptsByDependency
Resource[] OrderScriptsByDependency(Resource[] resources)
{
var resourcesByPath = resources.ToDictionary(resource => resource.Path, StringComparer.OrdinalIgnoreCase);
// Create a graph where each node is a resource path
// and directed edges represent references between resources.
var graph = new Graph<string>(
resourcesByPath.Keys,
path => resourcesByPath[path].References
);
var sortedPaths = graph.TopologicalSort();
return sortedPaths.Select(path => resourcesByPath[path]).ToArray();
}