本文整理汇总了C#中AdjacencyGraph.TopologicalSort方法的典型用法代码示例。如果您正苦于以下问题:C# AdjacencyGraph.TopologicalSort方法的具体用法?C# AdjacencyGraph.TopologicalSort怎么用?C# AdjacencyGraph.TopologicalSort使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类AdjacencyGraph
的用法示例。
在下文中一共展示了AdjacencyGraph.TopologicalSort方法的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Render
public override void Render(Context context, TextWriter result)
{
// init modules context
ModulesContext modules = DotLiquidModules.ContextExtractor.GetOrAddModulesContext(context);
// allow module name to be supplied as a variable, makes sense when you supply modules in the Model
object evalName = context[_moduleName];
string modName = evalName != null ? Convert.ToString(evalName) : _moduleName;
// remember modules that were already loaded
Dictionary<string, bool> alreadyLoaded = new Dictionary<string, bool>();
foreach (string moduleName in modules.ModuleIndex.Keys)
{
alreadyLoaded[moduleName] = true;
}
// add module to context, get its dependencies in graph
AdjacencyGraph<Module, Edge<Module>> dependencyGraph = new AdjacencyGraph<Module, Edge<Module>>(true);
AddModuleToContextByName(modName, modules, context, dependencyGraph);
// add dependency tree into context's dependency list
foreach (Module module in dependencyGraph.TopologicalSort())
{
if (!alreadyLoaded.ContainsKey(module.ModuleName))
{
modules.DependencyOrder.Add(module);
}
}
}
示例2: SortByEvaluationOrder
/// <summary>
/// Sorts the collection by evaluation order, using a topological sort.
/// </summary>
/// <param name="calcs">The collection to sort.</param>
/// <returns>The sorted collection.</returns>
public static IEnumerable<CalculationControl> SortByEvaluationOrder(this IEnumerable<CalculationControl> calcs)
{
AdjacencyGraph<CalculationControl, SEdge<CalculationControl>> graph = new AdjacencyGraph<CalculationControl, SEdge<CalculationControl>>();
graph.AddVertexRange(calcs);
foreach (var target in calcs)
{
foreach (var calc in calcs)
{
if (target.CalculationExpression.Contains("{%" + calc.Name + "%}") || Regex.IsMatch(target.CalculationExpression, @"{%[^\.]*." + calc.Name + ".Total%}"))
{
graph.AddEdge(new SEdge<CalculationControl>(calc, target));
}
}
}
return graph.TopologicalSort();
}
示例3: DependencySort
/// <summary>
/// Sort the specified resources in dependency order (most-dependent-first).
/// </summary>
/// <param name="resources">The resources to examine.</param>
/// <returns>
/// A read-only list of <see cref="Resource"/>s in dependency order.
/// </returns>
public static IReadOnlyList<Resource> DependencySort(IReadOnlyCollection<Resource> resources)
{
if (resources == null)
{
throw new ArgumentNullException(nameof(resources));
}
try
{
Dictionary<string, Resource> resourcesById = resources.ToDictionary(resource => resource.ResourceId);
AdjacencyGraph<Resource, Edge<Resource>> resourceDependencies = new AdjacencyGraph<Resource, Edge<Resource>>();
foreach (string resourceId in resourcesById.Keys)
{
Resource resource = resourcesById[resourceId];
if (!resourceDependencies.AddVertex(resource))
continue; // Already processed.
if (resource.DependsOn != null)
{
foreach (string dependsOnResourceId in resource.DependsOn)
{
Resource dependsOnResource;
if (!resourcesById.TryGetValue(dependsOnResourceId, out dependsOnResource))
{
throw new TemplateParserException($"Resource '{resourceId}' depends on non-existent resource '{dependsOnResourceId}'.");
}
resourceDependencies.AddEdge(new Edge<Resource>(resource, dependsOnResource));
}
}
}
return resourceDependencies.TopologicalSort().ToList();
}
catch (NonAcyclicGraphException ex)
{
throw new TemplateParserException("The template contains a circular dependency.", ex);
}
}
示例4: ExecuteTasks
public async Task<bool> ExecuteTasks(Resource resource)
{
var tasks = new ResourceTask[]
{
new UpdateStreamListTask(),
new UpdatePackageFileTask(),
new BuildAssemblyTask()
};
var graph = new AdjacencyGraph<string, SEdge<string>>();
// add vertices and edges
foreach (var task in tasks)
{
graph.AddVertex(task.Id);
graph.AddEdgeRange(task.DependsOn.Select(a => new SEdge<string>(task.Id, a)));
}
var runTasks = graph.TopologicalSort().Reverse().Select(a => tasks.First(b => b.Id == a)).Where(a => a.NeedsExecutionFor(resource));
if (runTasks.FirstOrDefault() != null)
{
this.Log().Info("Running tasks on {0}: {1}.", resource.Name, string.Join(", ", runTasks.Select(a => a.Id)));
}
foreach (var task in runTasks)
{
if (!await task.Process(resource))
{
this.Log().Warn("Task {0} failed.", task.Id);
return false;
}
}
return true;
}
示例5: ShortestPhrase
/// <summary>
/// Time complexity n + logn?
/// http://bigocheatsheet.com/
///
/// Used a directed graph acyclic
/// http://en.wikipedia.org/wiki/Directed_acyclic_graph
///
/// The use topological sorting to determine order
/// https://quickgraph.codeplex.com/wikipage?title=Topological%20Sort
/// http://en.wikipedia.org/wiki/Topological_sorting#Algorithms
/// </summary>
/// <param name="logins"></param>
/// <returns></returns>
public static int ShortestPhrase(string[] logins)
{
var graph = new AdjacencyGraph<int, SEdge<int>>();
foreach (var item in logins)
{
for (int i = 0; i < item.Length; i++)
{
var number = Convert.ToInt32(item.Substring(i, 1));
if (!graph.ContainsVertex(number))
{
graph.AddVertex(number);
}
if (i + 1 == item.Length)
break;
var next = Convert.ToInt32(item.Substring(i + 1, 1));
if (!graph.ContainsEdge(number, next))
{
graph.AddEdge(new SEdge<int>(number, next));
}
}
}
var shortestPath = graph.TopologicalSort(); // O(log(n))
var builder = new StringBuilder();
shortestPath.ForEach(item => builder.Append(item));
return Convert.ToInt32(builder.ToString());
}
示例6: DetermineServiceStartupOrder
/// <summary>
/// Determines the ideal service startup order of the services.
/// </summary>
/// <returns>
/// An ordered collection of services. Where the order indicates the ideal startup order.
/// </returns>
private List<KernelService> DetermineServiceStartupOrder()
{
var graph = new AdjacencyGraph<ServiceVertex, Edge<ServiceVertex>>();
{
var typedCollection = new Dictionary<Type, ServiceVertex>();
foreach (var pair in m_Services)
{
var vertex = new ServiceVertex(pair.Value);
graph.AddVertex(vertex);
typedCollection.Add(pair.Key, vertex);
}
// The edges point from a dependency to the dependent vertex
foreach (var pair in typedCollection)
{
var target = pair.Value;
if (target.HasDependencies)
{
var dependencies = m_Connections[target.Service];
foreach (var dependent in dependencies)
{
Debug.Assert(typedCollection.ContainsKey(dependent.Applied.GetType()), "Missing a service type.");
var source = typedCollection[dependent.Applied.GetType()];
graph.AddEdge(new Edge<ServiceVertex>(source, target));
}
}
}
}
return graph.TopologicalSort()
.Select(vertex => vertex.Service)
.ToList();
}