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


C# IGraph.Weight方法代码示例

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


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

示例1: Relax

        /// <summary>
        /// The graph shortest path relax procedure.
        /// Inputs:
        ///     u, v: vertices such that there is an edge (u, v).
        /// Result:
        ///     The value of shortest[v] might decrease, and if it does, then pred[v] becomes u.
        /// </summary>
        public void Relax(IGraph dag, int u, int v)
        {
            var uPath = (dag.Weight(u, v) ?? double.PositiveInfinity) + Shortest[u];

            if (uPath < Shortest[v])
            {
                Shortest[v] = uPath;
                Predecessors[v] = u;
            }
        }
开发者ID:ioab,项目名称:AU,代码行数:17,代码来源:GraphExtenstion.cs

示例2: FindNegativeWeightCycle

        /// <summary>
        /// The Find-Negative-Weight-Cycle() procedure to determine whether a graph has a negative-weight cycle,
        /// and how to construct one if it does.
        /// </summary>
        /// <param name="graph">A directed graph containing a set V of n vertices and a set E of m directed edges with arbitrary weights on which
        /// the BellmanFord() procedure has already been run.</param>
        /// <returns>
        /// Either a list of vertices in a negative-weight cycle, in order,
        /// or an empty list if the graph has no negative-weight cycles.
        /// </returns>
        public List<int> FindNegativeWeightCycle(IGraph directedGraph)
        {
            foreach (var u in directedGraph.Vertices)
                foreach (var v in directedGraph[u])
                    if (Shortest[u] + directedGraph.Weight(u, v) < Shortest[v])
                    {
                        int vTemp = v;
                        bool[] visited = new bool[directedGraph.N];         // Initialized by default to false //

                        int x = v;
                        while (!visited[x])
                        {
                            visited[x] = true;
                            x = (int)Graph.Predecessors[x];
                        }
                        vTemp = (int)Graph.Predecessors[x];

                        var cycle = new List<int>() { x };
                        while (vTemp != x)
                        {
                            cycle.Insert(0, vTemp);
                            vTemp = (int)Graph.Predecessors[vTemp];
                        }
                        return cycle;
                    }

            /**
             *  No negative edges, return empty list .. (P.104).
             */
            return new List<int>();
        }
开发者ID:ioab,项目名称:AU,代码行数:41,代码来源:BellmanFord.cs


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