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


C# Graph.TopologicalSort方法代码示例

本文整理汇总了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();
        }
开发者ID:GTuritto,项目名称:ngenerics,代码行数:16,代码来源:TopologicalSort.cs

示例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;
 }
开发者ID:stucampbell,项目名称:cassette,代码行数:29,代码来源:Bundle.cs

示例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);
        }
开发者ID:havok,项目名称:ngenerics,代码行数:27,代码来源:GraphExamples.cs

示例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]);
        }
开发者ID:ryansroberts,项目名称:knapsack,代码行数:11,代码来源:ReferenceBuilder.cs

示例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);
        }
开发者ID:GTuritto,项目名称:ngenerics,代码行数:21,代码来源:TopologicalSort.cs

示例6: ExceptionUndirectedGraph

 public void ExceptionUndirectedGraph()
 {
     var graph = new Graph<int>(false);
     graph.TopologicalSort();
 }
开发者ID:GTuritto,项目名称:ngenerics,代码行数:5,代码来源:TopologicalSort.cs

示例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;

      }
    }
开发者ID:ggrov,项目名称:tacny,代码行数:61,代码来源:DeadVarElim.cs

示例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();
        }
开发者ID:ryansroberts,项目名称:knapsack,代码行数:13,代码来源:UnresolvedModule.cs


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