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


C++ TPt::SetNDat方法代码示例

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


在下文中一共展示了TPt::SetNDat方法的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的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));
		}
}
开发者ID:guorongYu,项目名称:Belief-Propagation,代码行数:64,代码来源:Utilities.cpp

示例2: while

// Test update node data
TEST(TNodeEdgeNet, UpdateNodeData) {
  int NNodes = 10000;
  int NEdges = 100000;

  TPt <TNodeEdgeNet<TInt, TInt> > Net;
  TPt <TNodeEdgeNet<TInt, TInt> > Net1;
  TPt <TNodeEdgeNet<TInt, TInt> > Net2;
  int i;
  int n;
  int NCount;
  int x,y;

  Net = TNodeEdgeNet<TInt, TInt>::New();
  EXPECT_EQ(1,Net->Empty());

  // create the nodes
  for (i = 0; i < NNodes; i++) {
    Net->AddNode(i,i+5);
  }
  EXPECT_EQ(0,Net->Empty());
  EXPECT_EQ(NNodes,Net->GetNodes());

  // create random edges
  NCount = NEdges;
  while (NCount > 0) {
    x = (long) (drand48() * NNodes);
    y = (long) (drand48() * NNodes);
    n = Net->AddEdge(x, y);
    NCount--;
  }

  EXPECT_EQ(NEdges,Net->GetEdges());

  EXPECT_EQ(0,Net->Empty());
  EXPECT_EQ(1,Net->IsOk());

  for (i = 0; i < NNodes; i++) {
    EXPECT_EQ(1,Net->IsNode(i));
  }

  EXPECT_EQ(0,Net->IsNode(NNodes));
  EXPECT_EQ(0,Net->IsNode(NNodes+1));
  EXPECT_EQ(0,Net->IsNode(2*NNodes));

  // test node data
  for (TNodeEdgeNet<TInt, TInt>::TNodeI NI = Net->BegNI(); NI < Net->EndNI(); NI++) {
    EXPECT_EQ(NI.GetId()+5, Net->GetNDat(NI.GetId()));
  }

  // update node data, node ID + 10
  for (TNodeEdgeNet<TInt, TInt>::TNodeI NI = Net->BegNI(); NI < Net->EndNI(); NI++) {
    Net->SetNDat(NI.GetId(), NI.GetId()+10);
  }

  // test node data
  for (TNodeEdgeNet<TInt, TInt>::TNodeI NI = Net->BegNI(); NI < Net->EndNI(); NI++) {
    EXPECT_EQ(NI.GetId()+10, Net->GetNDat(NI.GetId()));
  }
}
开发者ID:Aleyasen,项目名称:Alaki,代码行数:60,代码来源:test-TNodeEdgeNet.cpp

示例3: SetNodeData

// Test set node data
void SetNodeData() {
  int NNodes = 10000;
  int NEdges = 100000;

  TPt <TNodeEDatNet<TInt, TInt> > Net;
  TPt <TNodeEDatNet<TInt, TInt> > Net1;
  TPt <TNodeEDatNet<TInt, TInt> > Net2;
  int i;
  int n;
  int NCount;
  int x,y;
  bool t;
  int NodeId;
  int NodeDat;
  int Value;
  bool ok;

  Net = TNodeEDatNet<TInt, TInt>::New();
  t = Net->Empty();

  // create the nodes
  for (i = 0; i < NNodes; i++) {
    Net->AddNode(i);
  }
  t = Net->Empty();
  n = Net->GetNodes();

  // create random edges
  NCount = NEdges;
  while (NCount > 0) {
    x = (long) (drand48() * NNodes);
    y = (long) (drand48() * NNodes);
    // Net->GetEdges() is not correct for the loops (x == y),
    // skip the loops in this test
    if (x != y  &&  !Net->IsEdge(x,y)) {
      n = Net->AddEdge(x, y);
      NCount--;
    }
  }
  PrintNStats("SetNodeData:Net", Net);

  // set node data, square of node ID
  for (TNodeEDatNet<TInt, TInt>::TNodeI NI = Net->BegNI(); NI < Net->EndNI(); NI++) {
    NodeId = NI.GetId();
    NodeDat = NI.GetId()*NI.GetId();
    Net->SetNDat(NodeId, NodeDat);
  }

  // read and test node data
  ok = true;
  for (TNodeEDatNet<TInt, TInt>::TNodeI NI = Net->BegNI(); NI < Net->EndNI(); NI++) {
    NodeDat = Net->GetNDat(NI.GetId());
    Value = NI.GetId()*NI.GetId();
    if (NodeDat != Value) {
      ok = false;
    }
  }
  printf("network SetNodeData:Net, status %s\n", (ok == true) ? "ok" : "ERROR");
}
开发者ID:Accio,项目名称:snap,代码行数:60,代码来源:demo-TNodeEDatNet.cpp

示例4: RandomGraphInitialization

// before generating DAG
void RandomGraphInitialization(TPt<TNodeEDatNet<TFlt, TFlt>> &pGraph)
{
	srand(time(NULL));
	for (auto EI = pGraph->BegEI(); EI < pGraph->EndEI(); EI++)
		pGraph->SetEDat(EI.GetSrcNId(), EI.GetDstNId(), (double) rand() / RAND_MAX);
	for (auto NI = pGraph->BegNI(); NI < pGraph->EndNI(); NI++)
		pGraph->SetNDat(NI.GetId(), 0.0);
}
开发者ID:guorongYu,项目名称:Belief-Propagation,代码行数:9,代码来源:Utilities.cpp

示例5: SortNodeData

// Test node data sorting
void SortNodeData() {
  int NNodes = 10000;
  int NEdges = 100000;

  TPt <TNodeEDatNet<TInt, TInt> > Net;
  TPt <TNodeEDatNet<TInt, TInt> > Net1;
  TPt <TNodeEDatNet<TInt, TInt> > Net2;
  int i;
  int n;
  int NCount;
  int x,y;
  bool t;
  int NodeId;
  int NodeDat;
  bool ok;
  bool Sorted;
  int Min;
  int Value;

  Net = TNodeEDatNet<TInt, TInt>::New();
  t = Net->Empty();

  // create the nodes
  for (i = 0; i < NNodes; i++) {
    Net->AddNode((i*13) % NNodes);
  }
  t = Net->Empty();
  n = Net->GetNodes();

  // create random edges
  NCount = NEdges;
  while (NCount > 0) {
    x = (long) (drand48() * NNodes);
    y = (long) (drand48() * NNodes);
    // Net->GetEdges() is not correct for the loops (x == y),
    // skip the loops in this test
    if (x != y  &&  !Net->IsEdge(x,y)) {
      n = Net->AddEdge(x, y);
      NCount--;
    }
  }
  PrintNStats("SortNodeData:Net", Net);

  // add data to nodes, square of node ID % NNodes
  for (TNodeEDatNet<TInt, TInt>::TNodeI NI = Net->BegNI(); NI < Net->EndNI(); NI++) {
    NodeId = NI.GetId();
    NodeDat = (NI.GetId()*NI.GetId()) % NNodes;
    Net->SetNDat(NodeId, NodeDat);
  }

  // test node data
  ok = true;
  for (TNodeEDatNet<TInt, TInt>::TNodeI NI = Net->BegNI(); NI < Net->EndNI(); NI++) {
    NodeDat = Net->GetNDat(NI.GetId());
    Value = (NI.GetId()*NI.GetId()) % NNodes;
    if (NodeDat != Value) {
      ok = false;
    }
  }
  printf("network SortNodeData:Net, status1 %s\n", (ok == true) ? "ok" : "ERROR");

  // test sorting of node IDs (unsorted)
  Min = -1;
  Sorted = true;
  for (TNodeEDatNet<TInt, TInt>::TNodeI NI = Net->BegNI(); NI < Net->EndNI(); NI++) {
    Value = NI.GetId();
    if (Min > Value) {
      Sorted = false;
    }
    Min = Value;
  }
  printf("network SortNodeData:Net, status2 %s\n", (Sorted == false) ? "ok" : "ERROR");

  // sort the nodes by node IDs (sorted)
  Net->SortNIdById();

  // test sorting of node IDs
  Min = -1;
  Sorted = true;
  for (TNodeEDatNet<TInt, TInt>::TNodeI NI = Net->BegNI(); NI < Net->EndNI(); NI++) {
    Value = NI.GetId();
    if (Min > Value) {
      Sorted = false;
    }
    Min = Value;
  }
  printf("network SortNodeData:Net, status3 %s\n", (Sorted == true) ? "ok" : "ERROR");

  // test sorting of node data (unsorted)
  Min = -1;
  Sorted = true;
  for (TNodeEDatNet<TInt, TInt>::TNodeI NI = Net->BegNI(); NI < Net->EndNI(); NI++) {
    Value = Net->GetNDat(NI.GetId());
    if (Min > Value) {
      Sorted = false;
    }
    Min = Value;
  }
  printf("network SortNodeData:Net, status4 %s\n", (Sorted == false) ? "ok" : "ERROR");
//.........这里部分代码省略.........
开发者ID:Accio,项目名称:snap,代码行数:101,代码来源:demo-TNodeEDatNet.cpp

示例6: InitializationBeforePropagation

// before belief propagation
void InitializationBeforePropagation(TPt<TNodeEDatNet<TFlt, TFlt>>& pGraph)
{

	for (auto NI = pGraph->BegNI(); NI < pGraph->EndNI(); NI++)
		pGraph->SetNDat(NI.GetId(), 0.0);
}
开发者ID:guorongYu,项目名称:Belief-Propagation,代码行数:7,代码来源:Utilities.cpp


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