本文整理汇总了C++中tngraph::TNodeI::GetId方法的典型用法代码示例。如果您正苦于以下问题:C++ TNodeI::GetId方法的具体用法?C++ TNodeI::GetId怎么用?C++ TNodeI::GetId使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类tngraph::TNodeI
的用法示例。
在下文中一共展示了TNodeI::GetId方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: GetSngVec
void GetSngVec(const PNGraph& Graph, TFltV& LeftSV, TFltV& RightSV) {
const int Nodes = Graph->GetNodes();
TFltVV LSingV, RSingV;
TFltV SngValV;
if (Nodes < 500) {
// perform full SVD
TFltVV AdjMtx(Nodes+1, Nodes+1);
TIntH NodeIdH;
// create adjecency matrix
for (TNGraph::TNodeI NodeI = Graph->BegNI(); NodeI < Graph->EndNI(); NodeI++) {
NodeIdH.AddKey(NodeI.GetId()); }
for (TNGraph::TNodeI NodeI = Graph->BegNI(); NodeI < Graph->EndNI(); NodeI++) {
const int NodeId = NodeIdH.GetKeyId(NodeI.GetId()) + 1;
for (int e = 0; e < NodeI.GetOutDeg(); e++) {
const int DstNId = NodeIdH.GetKeyId(NodeI.GetOutNId(e)) + 1; // no self edges
if (NodeId != DstNId) AdjMtx.At(NodeId, DstNId) = 1;
}
}
try { // can fail to converge but results seem to be good
TSvd::Svd1Based(AdjMtx, LSingV, SngValV, RSingV); }
catch(...) {
printf("\n***No SVD convergence: G(%d, %d)\n", Nodes, Graph->GetEdges()); }
} else { // Lanczos
TNGraphMtx GraphMtx(Graph);
TSparseSVD::LanczosSVD(GraphMtx, 1, 8, ssotFull, SngValV, LSingV, RSingV);
}
TFlt MxSngVal = TFlt::Mn;
int ValN = 0;
for (int i = 0; i < SngValV.Len(); i++) {
if (MxSngVal < SngValV[i]) { MxSngVal = SngValV[i]; ValN = i; } }
LSingV.GetCol(ValN, LeftSV);
RSingV.GetCol(ValN, RightSV);
IsAllValVNeg(LeftSV, true);
IsAllValVNeg(RightSV, true);
}
示例2: DirectedModularity
double DirectedModularity(PNGraph& graph, std::vector<int>& communities) {
if (graph->GetNodes() != communities.size()) {
throw std::logic_error("Number of nodes does not match community size.");
}
int num_edges = graph->GetEdges();
double score = 0.0;
int num_unique = 10;
std::map<int, double> outdeg_sums;
std::map<int, double> indeg_sums;
for (TNGraph::TNodeI node = graph->BegNI(); node < graph->EndNI(); node++) {
int comm = communities[node.GetId()];
outdeg_sums[comm] += node.GetOutDeg();
indeg_sums[comm] += node.GetInDeg();
}
for (auto& kv : outdeg_sums) {
score -= (kv.second / num_edges) * indeg_sums[kv.first];
}
for (TNGraph::TNodeI node = graph->BegNI(); node < graph->EndNI(); node++) {
int node_ID = node.GetId();
for (int e = 0; e < node.GetOutDeg(); ++e) {
int nbr = node.GetOutNId(e);
if (communities[node_ID] == communities[nbr]) {
score += 1.0;
}
}
}
return score / num_edges;
}
示例3: SaveLoadEdgeList
// Save and load directed, undirected and multi-graphs as list of edges, where nodes are ids
void SaveLoadEdgeList() {
const int NNodes = 500;
const int NEdges = 2000;
const char *FName = "demo.graph.dat";
const char *Desc = "Randomly generated graph for input/output.";
PNGraph GOut, GIn;
GOut = GenRndGnm<PNGraph>(NNodes, NEdges);
// Output node IDs as numbers
SaveEdgeList(GOut, FName, Desc);
// Load edge list
GIn = LoadEdgeList<PNGraph>(FName);
// Verify all nodes exist in input and output graphs
THashSet<TInt> OutNIdH, InNIdH;
for (TNGraph::TNodeI NI = GOut->BegNI(); NI < GOut->EndNI(); NI++) {
// Nodes that do not have edges are left off during input
if (NI.GetDeg() > 0) {
OutNIdH.AddKey(NI.GetId());
}
}
for (TNGraph::TNodeI NI = GIn->BegNI(); NI < GIn->EndNI(); NI++) {
InNIdH.AddKey(NI.GetId());
}
PrintGStats<PNGraph>("EdgeList - Out", GOut);
PrintGStats<PNGraph>("EdgeList - In", GIn);
}
示例4: IOGViz
// Save directed, undirected and multi-graphs in GraphVizp .DOT format
void IOGViz() {
const int NNodes = 500;
const int NEdges = 2000;
const char *FName1 = "demo1.dot.dat", *FName2 = "demo2.dot.dat";
const char *Desc = "Randomly generated GgraphVizp for input/output.";
PNGraph GOut; // Can be PNEGraph or PUNGraph
GOut = GenRndGnm<PNGraph>(NNodes, NEdges);
SaveGViz(GOut, FName1);
// Output node IDs as numbers
TIntStrH NIdLabelH;
// Generate labels for random graph
for (TNGraph::TNodeI NI = GOut->BegNI(); NI < GOut->EndNI(); NI++) {
NIdLabelH.AddDat(NI.GetId(), TStr::Fmt("Node%d", NI.GetId()));
}
SaveGViz(GOut, FName2, Desc, NIdLabelH);
PrintGStats("IOGViz - In", GOut);
}
示例5: SaveCascades
void TNetInfBs::SaveCascades(const TStr& OutFNm) {
TFOut FOut(OutFNm);
// write nodes to file
for (TNGraph::TNodeI NI = GroundTruth->BegNI(); NI < GroundTruth->EndNI(); NI++) {
FOut.PutStr(TStr::Fmt("%d,%d\r\n", NI.GetId(), NI.GetId())); // nodes
}
FOut.PutStr("\r\n");
// write cascades to file
for (int i=0; i<CascV.Len(); i++) {
TCascade &C = CascV[i];
int j = 0;
for (THash<TInt, THitInfo>::TIter NI = C.NIdHitH.BegI(); NI < C.NIdHitH.EndI(); NI++, j++) {
if (j > 0)
FOut.PutStr(TStr::Fmt(",%d,%f", NI.GetDat().NId.Val, NI.GetDat().Tm.Val));
else
FOut.PutStr(TStr::Fmt("%d,%f", NI.GetDat().NId.Val, NI.GetDat().Tm.Val));
}
if (C.Len() >= 1)
FOut.PutStr(TStr::Fmt("\r\n"));
}
}
示例6: OnlyD3CEdgesNoBack
void OnlyD3CEdgesNoBack(PNGraph& dir_graph, PNGraph& d3c_graph) {
// Add all of the nodes into the new graph
for (TNGraph::TNodeI node = dir_graph->BegNI(); node < dir_graph->EndNI();
node++) {
int curr_node = node.GetId();
d3c_graph->AddNode(curr_node);
}
for (TNGraph::TNodeI node = dir_graph->BegNI(); node < dir_graph->EndNI();
node++) {
int curr_node = node.GetId();
auto curr_node_it = dir_graph->GetNI(curr_node);
for (int out_edge = 0; out_edge < curr_node_it.GetOutDeg(); ++out_edge) {
int out_node = curr_node_it.GetOutNId(out_edge);
for (int in_edge = 0; in_edge < curr_node_it.GetInDeg(); ++in_edge) {
int in_node = curr_node_it.GetInNId(in_edge);
if (dir_graph->IsEdge(out_node, in_node) && out_node != in_node) {
if (!d3c_graph->IsEdge(in_node, out_node) &&
!d3c_graph->IsEdge(curr_node, in_node) &&
!d3c_graph->IsEdge(out_node, curr_node)) {
if (!d3c_graph->IsEdge(out_node, in_node)) { d3c_graph->AddEdge(out_node, in_node); }
if (!d3c_graph->IsEdge(in_node, curr_node)) { d3c_graph->AddEdge(in_node, curr_node); }
if (!d3c_graph->IsEdge(curr_node, out_node)) { d3c_graph->AddEdge(curr_node, out_node); }
}
}
}
}
}
}
示例7: OnlyD3CEdges
void OnlyD3CEdges(PNGraph& dir_graph, PNGraph& d3c_graph, bool recip_edges) {
// Add all of the nodes into the new graph
for (TNGraph::TNodeI node = dir_graph->BegNI(); node < dir_graph->EndNI();
node++) {
int curr_node = node.GetId();
d3c_graph->AddNode(curr_node);
}
for (TNGraph::TNodeI node = dir_graph->BegNI(); node < dir_graph->EndNI();
node++) {
int curr_node = node.GetId();
auto curr_node_it = dir_graph->GetNI(curr_node);
for (int out_edge = 0; out_edge < curr_node_it.GetOutDeg(); ++out_edge) {
int out_node = curr_node_it.GetOutNId(out_edge);
for (int in_edge = 0; in_edge < curr_node_it.GetInDeg(); ++in_edge) {
int in_node = curr_node_it.GetInNId(in_edge);
if (out_node == in_node && !recip_edges) { continue; }
if (dir_graph->IsEdge(out_node, in_node) || recip_edges) {
if (!d3c_graph->IsEdge(out_node, in_node)) {
d3c_graph->AddEdge(out_node, in_node);
}
if (!d3c_graph->IsEdge(in_node, curr_node)) {
d3c_graph->AddEdge(in_node, curr_node);
}
if (!d3c_graph->IsEdge(curr_node, out_node)) {
d3c_graph->AddEdge(curr_node, out_node);
}
}
}
}
}
#ifdef _VERBOSE_
std::cout << "Original graph edge count: " << dir_graph->GetEdges() << std::endl
<< "D3C graph edge count: " << d3c_graph->GetEdges() << std::endl;
#endif
}
示例8: Print
void TGraphCascade::Print(const TIntV& SortV) {
printf("graph start:\n");
if (SortV.Empty()) {
for (TNGraph::TNodeI NI = Graph.BegNI(); NI < Graph.EndNI(); NI++) {
printf("%s %d %d\n", NodeIdNmH.GetDat(NI.GetId()).CStr(), NI.GetId(), NodeNmIdH.GetDat(NodeIdNmH.GetDat(NI.GetId())).Val);
}
} else {
for (int NodeN = 0; NodeN < SortV.Len(); NodeN++) {
printf("%s %d\n", NodeIdNmH.GetDat(SortV[NodeN]).CStr(), SortV[NodeN].Val);
}
}
printf("graph end\n");
}
示例9: TakeSig
void TGraphKey::TakeSig(const PNGraph& Graph, const int& MnSvdGraph, const int& MxSvdGraph) {
const int Edges = Graph->GetEdges();
Nodes = Graph->GetNodes();
VariantId = 0;
SigV.Gen(2+Nodes, 0);
// degree sequence
TIntPrV DegV(Nodes, 0);
for (TNGraph::TNodeI NodeI = Graph->BegNI(); NodeI < Graph->EndNI(); NodeI++) {
DegV.Add(TIntPr(NodeI.GetInDeg(), NodeI.GetOutDeg()));
}
DegV.Sort(false);
SigV.Add(TFlt(Nodes));
SigV.Add(TFlt(Edges));
for (int i = 0; i < DegV.Len(); i++) {
SigV.Add(DegV[i].Val1());
SigV.Add(DegV[i].Val2());
}
// singular values signature
// it turns out that it is cheaper to do brute force isomorphism
// checking than to calculate SVD and then check isomorphism
if (Nodes >= MnSvdGraph && Nodes < MxSvdGraph) {
// perform full SVD
TFltVV AdjMtx(Nodes+1, Nodes+1);
TFltV SngValV;
TFltVV LSingV, RSingV;
TIntH NodeIdH;
// create adjecency matrix
for (TNGraph::TNodeI NodeI = Graph->BegNI(); NodeI < Graph->EndNI(); NodeI++) {
NodeIdH.AddKey(NodeI.GetId());
}
for (TNGraph::TNodeI NodeI = Graph->BegNI(); NodeI < Graph->EndNI(); NodeI++) {
const int NodeId = NodeIdH.GetKeyId(NodeI.GetId()) + 1;
for (int e = 0; e < NodeI.GetOutDeg(); e++) {
const int DstNId = NodeIdH.GetKeyId(NodeI.GetOutNId(e)) + 1; // no self edges
if (NodeId != DstNId) AdjMtx.At(NodeId, DstNId) = 1;
}
}
try { // can fail to converge but results seem to be good
TSvd::Svd(AdjMtx, LSingV, SngValV, RSingV);
} catch(...) {
printf("\n***No SVD convergence: G(%d, %d): SngValV.Len():%d\n", Nodes(), Graph->GetEdges(), SngValV.Len());
}
// round singular values
SngValV.Sort(false);
for (int i = 0; i < SngValV.Len(); i++) {
SigV.Add(TMath::Round(SngValV[i], RoundTo));
}
}
//printf("SIG:\n"); for (int i = 0; i < SigV.Len(); i++) { printf("\t%f\n", SigV[i]); }
SigV.Pack();
}
示例10: GetSngVals
void GetSngVals(const PNGraph& Graph, const int& SngVals, TFltV& SngValV) {
const int Nodes = Graph->GetNodes();
IAssert(SngVals > 0);
if (Nodes < 100) {
// perform full SVD
TFltVV AdjMtx(Nodes+1, Nodes+1);
TFltVV LSingV, RSingV;
TIntH NodeIdH;
// create adjecency matrix
for (TNGraph::TNodeI NodeI = Graph->BegNI(); NodeI < Graph->EndNI(); NodeI++) {
NodeIdH.AddKey(NodeI.GetId()); }
for (TNGraph::TNodeI NodeI = Graph->BegNI(); NodeI < Graph->EndNI(); NodeI++) {
const int NodeId = NodeIdH.GetKeyId(NodeI.GetId()) + 1;
for (int e = 0; e < NodeI.GetOutDeg(); e++) {
const int DstNId = NodeIdH.GetKeyId(NodeI.GetOutNId(e)) + 1; // no self edges
if (NodeId != DstNId) AdjMtx.At(NodeId, DstNId) = 1;
}
}
try { // can fail to converge but results seem to be good
TSvd::Svd1Based(AdjMtx, LSingV, SngValV, RSingV); }
catch(...) {
printf("\n***No SVD convergence: G(%d, %d)\n", Nodes, Graph->GetEdges()); }
} else {
// Lanczos
TNGraphMtx GraphMtx(Graph);
int CalcVals = int(2*SngVals);
//if (CalcVals > Nodes) { CalcVals = int(2*Nodes); }
//if (CalcVals > Nodes) { CalcVals = Nodes; }
//while (SngValV.Len() < SngVals && CalcVals < 10*SngVals) {
try {
if (SngVals > 4) {
TSparseSVD::SimpleLanczosSVD(GraphMtx, 2*SngVals, SngValV, false); }
else { TFltVV LSingV, RSingV; // this is much more precise, but also much slower
TSparseSVD::LanczosSVD(GraphMtx, SngVals, 3*SngVals, ssotFull, SngValV, LSingV, RSingV); }
}
catch(...) {
printf("\n ***EXCEPTION: TRIED %d GOT %d values** \n", 2*SngVals, SngValV.Len()); }
if (SngValV.Len() < SngVals) {
printf(" ***TRIED %d GOT %d values** \n", CalcVals, SngValV.Len()); }
// CalcVals += SngVals;
//}
}
SngValV.Sort(false);
//if (SngValV.Len() > SngVals) {
// SngValV.Del(SngVals, SngValV.Len()-1); }
//else {
// while (SngValV.Len() < SngVals) SngValV.Add(1e-6); }
//IAssert(SngValV.Len() == SngVals);
}
示例11: TakeGraph
// renumbers nodes
void TGraphKey::TakeGraph(const PNGraph& Graph) {
TIntH NodeIdH;
for (TNGraph::TNodeI NI = Graph->BegNI(); NI < Graph->EndNI(); NI++) {
NodeIdH.AddKey(NI.GetId()); }
Nodes = Graph->GetNodes();
EdgeV.Gen(Nodes, 0);
for (TNGraph::TNodeI NI = Graph->BegNI(); NI < Graph->EndNI(); NI++) {
const int NewNId = NodeIdH.GetKeyId(NI.GetId());
for (int i = 0; i < NI.GetOutDeg(); i++) {
EdgeV.Add(TIntPr(NewNId, NodeIdH.GetKeyId(NI.GetOutNId(i))));
}
}
EdgeV.Sort(true);
EdgeV.Pack();
}
示例12: IOMatlabSparseMtx
// Saves and loads a graph in a MATLAB sparse matrix format
void IOMatlabSparseMtx() {
const int NNodes = 500;
const int NEdges = 2000;
const char *FName = "demo.matlab.dat";
PNGraph GOut, GIn;
GOut = GenRndGnm<PNGraph>(NNodes, NEdges);
SaveMatlabSparseMtx(GOut, FName);
GIn = TNGraph::New();
GIn->Reserve(NNodes, NEdges);
// Read-in Matlab-file
FILE *F = fopen(FName, "r");
while (! feof(F)) {
int Src, Dst, Edge;
fscanf(F, "%d %d %d\n", &Src, &Dst, &Edge);
Src--; Dst--; // SNAP graphs start at Node Ids of 0
if (not GIn->IsNode(Src)) {
GIn->AddNode(Src);
}
if (not GIn->IsNode(Dst)) {
GIn->AddNode(Dst);
}
GIn->AddEdge(Src, Dst);
}
fclose(F);
// Verify all nodes exist in input and output graphs (and no more)
THashSet<TInt> OutNIdH, InNIdH;
for (TNGraph::TNodeI NI = GOut->BegNI(); NI < GOut->EndNI(); NI++) {
// Nodes that do not have edges are left off during input
if (NI.GetDeg() > 0) {
OutNIdH.AddKey(NI.GetId());
}
}
for (TNGraph::TNodeI NI = GIn->BegNI(); NI < GIn->EndNI(); NI++) {
InNIdH.AddKey(NI.GetId());
}
PrintGStats("Matlab - Out", GOut);
PrintGStats("Matlab - In", GIn);
}
示例13: KeepAtMostOneChildPerNode
void TIncrementalClustering::KeepAtMostOneChildPerNode(PNGraph& G, TQuoteBase *QB, TDocBase *DB) {
TIntSet::TIter EndNode = AffectedNodes.EndI();
for (TIntSet::TIter NodeId = AffectedNodes.BegI(); NodeId < EndNode; NodeId++) {
TNGraph::TNodeI Node = G->GetNI(NodeId.GetKey());
TQuote SourceQuote;
if (QB->GetQuote(Node.GetId(), SourceQuote)) {
TInt NodeDegree = Node.GetOutDeg();
if (NodeDegree > 1) {
TFlt MaxScore = 0;
TInt MaxNodeId = 0;
TIntV NodeV;
// first pass: check to see if we are pointing to any old nodes - if so, they get higher
// priority over the new ones for edge selection.
bool ContainsOldNode = false;
for (int i = 0; i < NodeDegree; ++i) {
if (!NewQuotes.IsKey(Node.GetOutNId(i))) {
ContainsOldNode = true;
}
}
// modified edge selection: filter out new nodes if old ones exist.
for (int i = 0; i < NodeDegree; ++i) {
TInt CurNode = Node.GetOutNId(i);
NodeV.Add(CurNode);
TQuote DestQuote;
if (QB->GetQuote(CurNode, DestQuote)) {
TFlt EdgeScore = 0;
if (!ContainsOldNode || !NewQuotes.IsKey(Node.GetOutNId(i))) {
EdgeScore = ComputeEdgeScore(SourceQuote, DestQuote, DB);
}
if (EdgeScore > MaxScore) {
MaxScore = EdgeScore;
MaxNodeId = CurNode;
}
}
}
// remove all other edges, backwards to prevent indexing fail
for (int i = 0; i < NodeV.Len(); i++) {
if (NodeV[i] != MaxNodeId) {
G->DelEdge(Node.GetId(), NodeV[i]);
}
}
//printf("Out degree: %d out of %d\n", Node.GetOutDeg(), NodeDegree.Val);
}
}
}
fprintf(stderr, "finished deleting edges\n");
}
示例14: GetSubGraph
TIntNNet TMultimodalGraphImplB::GetSubGraph(const TIntV ModeIds) const {
TIntNNet SubGraph = TIntNNet();
for (THash<TInt,TInt>::TIter CurI = NodeToModeMapping.BegI(); CurI < NodeToModeMapping.EndI(); CurI++) {
if (ModeIds.IsIn(CurI.GetDat())) {
SubGraph.AddNode(CurI.GetKey(), CurI.GetDat());
}
}
for (int ModeIdx1 = 0; ModeIdx1 < ModeIds.Len(); ModeIdx1++) {
int ModeId1 = ModeIds.GetVal(ModeIdx1);
for (int ModeIdx2 = 0; ModeIdx2 < ModeIds.Len(); ModeIdx2++) {
int ModeId2 = ModeIds.GetVal(ModeIdx2);
TPair<TInt,TInt> ModeIdsKey = GetModeIdsKey(ModeId1, ModeId2);
if (!Graphs.IsKey(ModeIdsKey)) { continue; }
const TNGraph& Graph = Graphs.GetDat(ModeIdsKey);
for (TNGraph::TNodeI it = Graph.BegNI(); it < Graph.EndNI(); it++) {
for (int e = 0; e < it.GetOutDeg(); e++) {
SubGraph.AddEdge(it.GetId(), it.GetOutNId(e));
}
}
}
}
printf("Number of nodes in SubGraph: %d...\n", SubGraph.GetNodes());
printf("Number of edges in SubGraph: %d...\n", SubGraph.GetEdges());
return SubGraph;
}
示例15: GetAllNodes
void TempMotifCounter::GetAllNodes(TIntV& nodes) {
nodes = TIntV();
for (TNGraph::TNodeI it = static_graph_->BegNI();
it < static_graph_->EndNI(); it++) {
nodes.Add(it.GetId());
}
}