本文整理汇总了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;
}
}
示例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>();
}