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


C# IGraph.Exists方法代码示例

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


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

示例1: TopologicalSort

        /// <summary>
        /// The topological sort procedure for DAG.
        /// </summary>
        /// <param name="dag">a directed acyclic graph (DAG) with vertices numbered 1 to n.</param>
        /// <returns>
        /// A linear order of the vertices such that u appears before v in the linear order,
        /// if (u, v) is an edge in the graph.
        /// </returns>
        public static List<int> TopologicalSort(IGraph dag)
        {
            // the initialization (to 0) is done by default //
            int[] inDegree = new int[dag.N];

            var linear = new List<int>();

            var next = new Stack<int>();

            foreach (var u in dag.Vertices)
                foreach (var v in dag[u])
                {
                    if (dag.Exists(u, v))
                        inDegree[v]++;
                }

            for (int i = 0; i < inDegree.Length; ++i)
            {
                if (inDegree[i] == 0)
                    next.Push(i);
            }

            while (next.Count > 0)
            {
                var u = next.Pop();
                linear.Add(u);

                foreach (var v in dag.Vertices)
                {
                    if (dag.Exists(u, v))
                        inDegree[v]--;

                    if (!linear.Contains(v) && !next.Contains(v) && inDegree[v] == 0)
                        next.Push(v);
                }
            }

            return linear;
        }
开发者ID:ioab,项目名称:AU,代码行数:47,代码来源:DagAlgorithms.cs

示例2: ShortestPath

        /// <summary>
        /// The DAG shortest path procedure.
        /// </summary>
        /// <param name="dag">a weighted directed acyclic graph containing a set V of n vertices and a set E of m directed edges.</param>
        /// <param name="sourceVertix">a source vertex in V.</param>
        /// <remarks>
        /// For each non-source vertex v in V, shortest[v] is the weight sp(s,v) of a shortest path from s to v and pred(v) is the vertex
        /// preceding v on some shortest path.
        /// For the source vertex s, shortest(s) = 0 and pred(s) = NULL.
        /// If there is no path from s to v, then shortest[v] = infinity, and pred(v) = NULL.
        /// </remarks>
        public void ShortestPath(IGraph dag, int sourceVertix)
        {
            var linear = TopologicalSort(dag);

            Graph.Shortest = new double[dag.N];
            Graph.Predecessors = new int?[dag.N];

            for (int i = 0; i < Shortest.Length; ++i)
            {
                Shortest[i] = double.PositiveInfinity;
                Graph.Predecessors[i] = null;
            }

            Shortest[sourceVertix] = 0;

            foreach (var u in linear)
            {
                foreach (var v in dag.Vertices)
                    if (dag.Exists(u, v))
                        Graph.Relax(dag, u, v);
            }
        }
开发者ID:ioab,项目名称:AU,代码行数:33,代码来源:DagAlgorithms.cs


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