本文整理汇总了C++中TPt::GetNI方法的典型用法代码示例。如果您正苦于以下问题:C++ TPt::GetNI方法的具体用法?C++ TPt::GetNI怎么用?C++ TPt::GetNI使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类TPt
的用法示例。
在下文中一共展示了TPt::GetNI方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: Dijkstra
//! Given pGraph with data about edge weights, computes the distance of the shortest paths from sourceNode
//! and returns the result in the nodes of pDAGGraph.
//! Updates the edges if bUpdateEdges is set to true. Default is false. In that case only the node data is updated with the shortest distance to sourceNode.
//! @note Requires initial values for the nodes of pDAGGraph (edges are not needed)
void Dijkstra(const TPt<TNodeEDatNet<TFlt, TFlt>>& pGraph, int sourceNode, double dThreshold, TPt<TNodeEDatNet<TFlt, TFlt>>& pDAGGraph, bool bUpdateEdges = false)
{
double logThreshold = log(dThreshold);
if(dThreshold==0)
logThreshold=-DBL_MAX;
// List of visited nodes
std::map<int, bool> visitedNodes;
// Stores the edge vertices to build the final DAG
std::map<int, int> mapPrevious;
std::priority_queue<std::pair<int,double>, std::vector<std::pair<int,double>>, Order> nodesToVisit;
// Distance from source node to itself is 0
pDAGGraph->SetNDat(sourceNode, 0);
nodesToVisit.push(std::make_pair(sourceNode,0));
// Beginning of the loop of Dijkstra algorithm
while(!nodesToVisit.empty())
{
// Find the vertex in queue with the smallest distance and remove it
int iParentID = -1;
while (!nodesToVisit.empty() && visitedNodes[iParentID = nodesToVisit.top().first])
nodesToVisit.pop();
if (iParentID == -1) break;
// mark the vertex with the shortest distance
visitedNodes[iParentID]=true;
auto parent = pGraph->GetNI(iParentID);
int numChildren = parent.GetOutDeg();
for(int i = 0; i < numChildren; ++i)
{
int iChildID = parent.GetOutNId(i);
// Accumulate the shortest distance from source
double alt = pDAGGraph->GetNDat(iParentID) - log(parent.GetOutEDat(i).Val);
if(alt >= logThreshold)
{
auto it = visitedNodes.find(iChildID);
if (alt < pDAGGraph->GetNDat(iChildID) && it->second == false)
{
//1. update distance
//2. update the predecessor
//3. push new shortest rank of chidren nodes
pDAGGraph->SetNDat(iChildID, alt);
mapPrevious[iChildID]= iParentID;
nodesToVisit.push(std::make_pair(iChildID,alt));
}
}
}
}
if(bUpdateEdges)
for(auto it=mapPrevious.begin(); it!= mapPrevious.end(); ++it)
{
pDAGGraph->AddEdge(it->second, it->first);
pDAGGraph->SetEDat(it->second,it->first, pGraph->GetEDat(it->second,it->first));
}
}