本文整理汇总了C++中PUNGraph::GetNI方法的典型用法代码示例。如果您正苦于以下问题:C++ PUNGraph::GetNI方法的具体用法?C++ PUNGraph::GetNI怎么用?C++ PUNGraph::GetNI使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类PUNGraph
的用法示例。
在下文中一共展示了PUNGraph::GetNI方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: GetEgonet
PUNGraph GetEgonet(const PUNGraph& Graph, const int CtrNId, int& ArndEdges) {
PUNGraph NewGraphPt = TUNGraph::New();
TUNGraph& NewGraph = *NewGraphPt;
NewGraph.AddNode(CtrNId);
const TUNGraph::TNodeI& CtrNode = Graph->GetNI(CtrNId);
for (int i = 0; i < CtrNode.GetInDeg(); ++i) {
NewGraph.AddNode(CtrNode.GetInNId(i));
}
ArndEdges = 0;
for (int i = 0; i < CtrNode.GetInDeg(); ++i) {
int NbrNId = CtrNode.GetInNId(i);
const TUNGraph::TNodeI& NbrNode = Graph->GetNI(NbrNId);
for (int j = 0; j < NbrNode.GetInDeg(); ++j) {
int NbrNbrNId = NbrNode.GetInNId(j);
if (NewGraph.IsNode(NbrNbrNId)) {
if (!NewGraph.IsEdge(NbrNId, NbrNbrNId)) {
NewGraph.AddEdge(NbrNId, NbrNbrNId);
}
} else {
ArndEdges++;
}
}
}
return NewGraphPt;
}
示例2: GetGroupDegreeCentr
double GetGroupDegreeCentr(const PUNGraph& Graph, const PUNGraph& Group) {
int deg;
TIntH NN;
for (TUNGraph::TNodeI NI = Group->BegNI(); NI < Group->EndNI(); NI++) {
deg = Graph->GetNI(NI.GetId()).GetDeg();
for (int i = 0; i<deg; i++) {
if (Group->IsNode(Graph->GetNI(NI.GetId()).GetNbrNId(i)) == 0)
NN.AddDat(Graph->GetNI(NI.GetId()).GetNbrNId(i), NI.GetId());
}
}
return (double)NN.Len();
}
示例3: GetGroupDegreeCentr0
double GetGroupDegreeCentr0(const PUNGraph& Graph, const TIntH& GroupNodes) {
int deg;
TIntH NN;
for (int i = 0; i<GroupNodes.Len(); i++) {
deg = Graph->GetNI(GroupNodes.GetDat(i)).GetDeg();
for (int j = 0; j < deg; j++) {
if (GroupNodes.IsKey(Graph->GetNI(GroupNodes.GetDat(i)).GetNbrNId(j)) == 0)
NN.AddDat(Graph->GetNI(GroupNodes.GetDat(i)).GetNbrNId(j), GroupNodes.GetDat(i));
}
}
return (double)NN.Len();
}
示例4: ComputeKCore
int ComputeKCore(const PUNGraph& G) {
int cnt = 0;
for(TUNGraph::TNodeI NI = G->BegNI(); NI < G->EndNI(); NI++)
cnt = max(cnt, NI.GetOutDeg());
THashSet <TInt> D[cnt+1];
THash <TInt, TInt> deg;
for(TUNGraph::TNodeI NI = G->BegNI(); NI < G->EndNI(); NI++) {
TInt tmp = NI.GetOutDeg() - G->IsEdge(NI.GetId(), NI.GetId() );
D[tmp.Val].AddKey(NI.GetId());
deg.AddDat(NI.GetId()) = tmp;
}
int max_k = 0;
for(int num_iters = 0;num_iters < G->GetNodes(); num_iters++)
for(int i = 0; i < cnt; i++)
if(D[i].Empty() == 0) {
max_k = max(max_k, i);
TInt a = *(D[i].BegI());
D[i].DelKey(a);
deg.AddDat(a.Val) = -1; // Hope overwriting works
TUNGraph::TNodeI NI = G->GetNI(a.Val);
for(int e = 0; e < NI.GetOutDeg(); e++) {
TInt b = NI.GetOutNId(e);
if(deg.GetDat(b) >= 0) {
int Id = deg.GetKeyId(b);
D[deg[Id].Val].DelKey(b);
deg[Id] = deg[Id] - 1; //Hope the overwriting works
D[deg[Id]].AddKey(b);
}
}
break;
}
return max_k;
}
示例5: GetConductance
double TAGMUtil::GetConductance(const PUNGraph& Graph, const TIntSet& CmtyS, const int Edges) {
const int Edges2 = Edges >= 0 ? 2*Edges : Graph->GetEdges();
int Vol = 0, Cut = 0;
double Phi = 0.0;
for (int i = 0; i < CmtyS.Len(); i++) {
if (! Graph->IsNode(CmtyS[i])) {
continue;
}
TUNGraph::TNodeI NI = Graph->GetNI(CmtyS[i]);
for (int e = 0; e < NI.GetOutDeg(); e++) {
if (! CmtyS.IsKey(NI.GetOutNId(e))) {
Cut += 1;
}
}
Vol += NI.GetOutDeg();
}
// get conductance
if (Vol != Edges2) {
if (2 * Vol > Edges2) {
Phi = Cut / double (Edges2 - Vol);
}
else if (Vol == 0) {
Phi = 0.0;
}
else {
Phi = Cut / double(Vol);
}
} else {
if (Vol == Edges2) {
Phi = 1.0;
}
}
return Phi;
}
示例6: GetNbhCom
void TAGMUtil::GetNbhCom(const PUNGraph& Graph, const int NID, TIntSet& NBCmtyS) {
TUNGraph::TNodeI NI = Graph->GetNI(NID);
NBCmtyS.Gen(NI.GetDeg());
NBCmtyS.AddKey(NID);
for (int e = 0; e < NI.GetDeg(); e++) {
NBCmtyS.AddKey(NI.GetNbrNId(e));
}
}
示例7: GetSubGraph
// RenumberNodes ... Renumber node ids in the subgraph to 0...N-1
PUNGraph GetSubGraph(const PUNGraph& Graph, const TIntV& NIdV, const bool& RenumberNodes) {
//if (! RenumberNodes) { return TSnap::GetSubGraph(Graph, NIdV); }
PUNGraph NewGraphPt = TUNGraph::New();
TUNGraph& NewGraph = *NewGraphPt;
NewGraph.Reserve(NIdV.Len(), -1);
TIntSet NIdSet(NIdV.Len());
for (int n = 0; n < NIdV.Len(); n++) {
if (Graph->IsNode(NIdV[n])) {
NIdSet.AddKey(NIdV[n]);
if (! RenumberNodes) {
NewGraph.AddNode(NIdV[n]);
}
else {
NewGraph.AddNode(NIdSet.GetKeyId(NIdV[n]));
}
}
}
if (! RenumberNodes) {
for (int n = 0; n < NIdSet.Len(); n++) {
const int SrcNId = NIdSet[n];
const TUNGraph::TNodeI NI = Graph->GetNI(SrcNId);
for (int edge = 0; edge < NI.GetOutDeg(); edge++) {
const int OutNId = NI.GetOutNId(edge);
if (NIdSet.IsKey(OutNId)) {
NewGraph.AddEdge(SrcNId, OutNId);
}
}
}
} else {
for (int n = 0; n < NIdSet.Len(); n++) {
const int SrcNId = NIdSet[n];
const TUNGraph::TNodeI NI = Graph->GetNI(SrcNId);
for (int edge = 0; edge < NI.GetOutDeg(); edge++) {
const int OutNId = NI.GetOutNId(edge);
if (NIdSet.IsKey(OutNId)) {
NewGraph.AddEdge(NIdSet.GetKeyId(SrcNId), NIdSet.GetKeyId(OutNId));
}
}
}
}
return NewGraphPt;
}
示例8: main
int main(int argc, char* argv[]) {
Env = TEnv(argc, argv, TNotify::StdNotify);
Env.PrepArgs(TStr::Fmt("Node Centrality. build: %s, %s. Time: %s", __TIME__, __DATE__, TExeTm::GetCurTm()));
TExeTm ExeTm;
Try
const TStr InFNm = Env.GetIfArgPrefixStr("-i:", "../as20graph.txt", "Input un/directed graph");
const TStr OutFNm = Env.GetIfArgPrefixStr("-o:", "node_centrality.tab", "Output file");
printf("Loading %s...", InFNm.CStr());
PNGraph Graph = TSnap::LoadEdgeList<PNGraph>(InFNm);
//PNGraph Graph = TSnap::GenRndGnm<PNGraph>(10, 10);
//TGraphViz::Plot(Graph, gvlNeato, InFNm+".gif", InFNm, true);
printf("nodes:%d edges:%d\n", Graph->GetNodes(), Graph->GetEdges());
PUNGraph UGraph = TSnap::ConvertGraph<PUNGraph>(Graph); // undirected version of the graph
TIntFltH BtwH, EigH, PRankH, CcfH, CloseH, HubH, AuthH;
//printf("Computing...\n");
printf("Treat graph as DIRECTED: ");
printf(" PageRank... "); TSnap::GetPageRank(Graph, PRankH, 0.85);
printf(" Hubs&Authorities..."); TSnap::GetHits(Graph, HubH, AuthH);
printf("\nTreat graph as UNDIRECTED: ");
printf(" Eigenvector..."); TSnap::GetEigenVectorCentr(UGraph, EigH);
printf(" Clustering..."); TSnap::GetNodeClustCf(UGraph, CcfH);
printf(" Betweenness (SLOW!)..."); TSnap::GetBetweennessCentr(UGraph, BtwH, 1.0);
printf(" Constraint (SLOW!)..."); TNetConstraint<PUNGraph> NetC(UGraph, true);
printf(" Closeness (SLOW!)...");
for (TUNGraph::TNodeI NI = UGraph->BegNI(); NI < UGraph->EndNI(); NI++) {
const int NId = NI.GetId();
CloseH.AddDat(NId, TSnap::GetClosenessCentr<PUNGraph>(UGraph, NId, false));
}
printf("\nDONE! saving...");
FILE *F = fopen(OutFNm.CStr(), "wt");
fprintf(F,"#Network: %s\n", InFNm.CStr());
fprintf(F,"#Nodes: %d\tEdges: %d\n", Graph->GetNodes(), Graph->GetEdges());
fprintf(F,"#NodeId\tDegree\tCloseness\tBetweennes\tEigenVector\tNetworkConstraint\tClusteringCoefficient\tPageRank\tHubScore\tAuthorityScore\n");
for (TUNGraph::TNodeI NI = UGraph->BegNI(); NI < UGraph->EndNI(); NI++) {
const int NId = NI.GetId();
const double DegCentr = UGraph->GetNI(NId).GetDeg();
const double CloCentr = CloseH.GetDat(NId);
const double BtwCentr = BtwH.GetDat(NId);
const double EigCentr = EigH.GetDat(NId);
const double Constraint = NetC.GetNodeC(NId);
const double ClustCf = CcfH.GetDat(NId);
const double PgrCentr = PRankH.GetDat(NId);
const double HubCentr = HubH.GetDat(NId);
const double AuthCentr = AuthH.GetDat(NId);
fprintf(F, "%d\t%f\t%f\t%f\t%f\t%f\t%f\t%f\t%f\t%f\n", NId,
DegCentr, CloCentr, BtwCentr, EigCentr, Constraint, ClustCf, PgrCentr, HubCentr, AuthCentr);
}
fclose(F);
Catch
printf("\nrun time: %s (%s)\n", ExeTm.GetTmStr(), TSecTm::GetCurTm().GetTmStr().CStr());
return 0;
}
示例9: _GirvanNewmanGetModularity
// Connected components of a graph define clusters
// OutDegH and OrigEdges stores node degrees and number of edges in the original graph
double _GirvanNewmanGetModularity(const PUNGraph& G, const TIntH& OutDegH, const int& OrigEdges, TCnComV& CnComV) {
TSnap::GetWccs(G, CnComV); // get communities
double Mod = 0;
for (int c = 0; c < CnComV.Len(); c++) {
const TIntV& NIdV = CnComV[c]();
double EIn=0, EEIn=0;
for (int i = 0; i < NIdV.Len(); i++) {
TUNGraph::TNodeI NI = G->GetNI(NIdV[i]);
EIn += NI.GetOutDeg();
EEIn += OutDegH.GetDat(NIdV[i]);
}
Mod += (EIn-EEIn*EEIn/(2.0*OrigEdges));
}
if (Mod == 0) { return 0; }
else { return Mod/(2.0*OrigEdges); }
}
示例10: AddSpuriousEdges
// network cascade: add spurious edges
// for more details see "Correcting for Missing Data in Information Cascades" by E. Sadikov, M. Medina, J. Leskovec, H. Garcia-Molina. WSDM, 2011
PNGraph AddSpuriousEdges(const PUNGraph& Graph, const PNGraph& Casc, TIntH NIdTmH) {
TIntPrV EdgeV;
for (TNGraph::TNodeI NI = Casc->BegNI(); NI < Casc->EndNI(); NI++) {
TUNGraph::TNodeI GNI = Graph->GetNI(NI.GetId());
const int Tm = NIdTmH.GetDat(NI.GetId());
for (int i=0,j=0; i < GNI.GetOutDeg(); i++) {
const int Dst = GNI.GetOutNId(i);
if (NIdTmH.IsKey(Dst) && Tm<NIdTmH.GetDat(Dst) && ! NI.IsNbhNId(Dst)) {
EdgeV.Add(TIntPr(GNI.GetId(), Dst)); }
}
}
PNGraph NetCasc = TNGraph::New();
*NetCasc = *Casc;
for (int e = 0; e < EdgeV.Len(); e++) {
NetCasc->AddEdge(EdgeV[e].Val1, EdgeV[e].Val2); }
return NetCasc;
}
示例11: GenGeoPrefAttach
/// Generates a random scale-free graph using the Geometric Preferential
/// Attachment model by Flexman, Frieze and Vera.
/// See: A geometric preferential attachment model of networks by Flexman,
/// Frieze and Vera. WAW 2004.
/// URL: http://math.cmu.edu/~af1p/Texfiles/GeoWeb.pdf
PUNGraph GenGeoPrefAttach(const int& Nodes, const int& OutDeg, const double& Beta, TRnd& Rnd) {
PUNGraph G = TUNGraph::New(Nodes, Nodes*OutDeg);
TFltTrV PointV(Nodes, 0);
TFltV ValV;
// points on a sphere of radius 1/(2*pi)
const double Rad = 0.5 * TMath::Pi;
for (int i = 0; i < Nodes; i++) {
TSnapDetail::GetSphereDev(3, Rnd, ValV);
PointV.Add(TFltTr(Rad*ValV[0], Rad*ValV[1], Rad*ValV[2]));
}
const double R2 = TMath::Sqr(log((double) Nodes) / (pow((double) Nodes, 0.5-Beta)));
TIntV DegV, NIdV;
int SumDeg;
for (int t = 0; t < Nodes; t++) {
const int pid = t;
const TFltTr& P1 = PointV[pid];
// add node
if (! G->IsNode(pid)) { G->AddNode(pid); }
// find neighborhood
DegV.Clr(false); NIdV.Clr(false); SumDeg=0;
for (int p = 0; p < t; p++) {
const TFltTr& P2 = PointV[p];
if (TMath::Sqr(P1.Val1-P2.Val1)+TMath::Sqr(P1.Val2-P2.Val2)+TMath::Sqr(P1.Val3-P2.Val3) < R2) {
NIdV.Add(p);
DegV.Add(G->GetNI(p).GetDeg()+1);
SumDeg += DegV.Last();
}
}
// add edges
for (int m = 0; m < OutDeg; m++) {
const int rnd = Rnd.GetUniDevInt(SumDeg);
int sum = 0, dst = -1;
for (int s = 0; s < DegV.Len(); s++) {
sum += DegV[s];
if (rnd < sum) { dst=s; break; }
}
if (dst != -1) {
G->AddEdge(pid, NIdV[dst]);
SumDeg -= DegV[dst];
NIdV.Del(dst); DegV.Del(dst);
}
}
}
return G;
}
示例12: Init
void Init(const PUNGraph& Graph) {
const double M = 0.5/Graph->GetEdges(); // 1/2m
Q = 0.0;
for (TUNGraph::TNodeI NI = Graph->BegNI(); NI < Graph->EndNI(); NI++) {
CmtyIdUF.Add(NI.GetId());
const int OutDeg = NI.GetOutDeg();
if (OutDeg == 0) { continue; }
TCmtyDat& Dat = CmtyQH.AddDat(NI.GetId(), TCmtyDat(M * OutDeg, OutDeg));
for (int e = 0; e < NI.GetOutDeg(); e++) {
const int DstNId = NI.GetOutNId(e);
const double DstMod = 2 * M * (1.0 - OutDeg * Graph->GetNI(DstNId).GetOutDeg() * M);
Dat.AddQ(DstNId, DstMod);
}
Q += -1.0*TMath::Sqr(OutDeg*M);
if (NI.GetId() < Dat.GetMxQNId()) {
MxQHeap.Add(TFltIntIntTr(Dat.GetMxQ(), NI.GetId(), Dat.GetMxQNId())); }
}
MxQHeap.MakeHeap();
}
示例13: RunSICascade2
// simulate SI model cascade using infection probability Beta until the cascade stops or reaches size MxCascSz
PNGraph RunSICascade2(PUNGraph G, const double& Beta, const int& MxCascSz, TIntH& NIdInfTmH) {
PNGraph Casc = TNGraph::New();
const int StartNId = G->GetRndNId();
Casc->AddNode(StartNId);
NIdInfTmH.AddDat(StartNId, NIdInfTmH.Len());
TIntQ Q; Q.Push(StartNId);
while (! Q.Empty()) {
const TUNGraph::TNodeI NI = G->GetNI(Q.Top()); Q.Pop();
for (int i = 0; i < NI.GetOutDeg(); i++) {
if (TInt::Rnd.GetUniDev() < Beta && ! NIdInfTmH.IsKey(NI.GetOutNId(i))) {
Casc->AddNode(NI.GetOutNId(i));
NIdInfTmH.AddDat(NI.GetOutNId(i), NIdInfTmH.Len());
Casc->AddEdge(NI.GetId(), NI.GetOutNId(i));
if (Casc->GetNodes() == MxCascSz) { return Casc; }
Q.Push(NI.GetOutNId(i));
}
}
}
return Casc;
}
示例14: RunSICascade
// simulate SI model cascade using infection probability Beta until the cascade reaches size CascSz
PNGraph RunSICascade(PUNGraph G, const double& Beta, const int& CascSz, TIntH& NIdInfTmH) {
PNGraph Casc = TNGraph::New();
const int StartId = G->GetRndNId();
Casc->AddNode(StartId);
NIdInfTmH.AddDat(StartId, NIdInfTmH.Len());
for (int X = 0; X < 10*CascSz; X++) {
TIntV CascNIdV; Casc->GetNIdV(CascNIdV);
for (int n = 0; n < CascNIdV.Len(); n++) {
const TUNGraph::TNodeI NI = G->GetNI(CascNIdV[n]);
for (int i = 0; i < NI.GetOutDeg(); i++) {
if (Casc->IsNode(NI.GetOutNId(i))) { continue; }
if (TInt::Rnd.GetUniDev() < Beta) {
Casc->AddNode(NI.GetOutNId(i));
NIdInfTmH.AddDat(NI.GetOutNId(i), NIdInfTmH.Len());
Casc->AddEdge(NI.GetId(), NI.GetOutNId(i));
if (Casc->GetNodes() == CascSz) { return Casc; }
}
}
}
}
return Casc;
}
示例15: GetBetweennessCentr
void GetBetweennessCentr(const PUNGraph& Graph, const TIntV& BtwNIdV, TIntFltH& NodeBtwH, const bool& DoNodeCent, TIntPrFltH& EdgeBtwH, const bool& DoEdgeCent) {
if (DoNodeCent) { NodeBtwH.Clr(); }
if (DoEdgeCent) { EdgeBtwH.Clr(); }
const int nodes = Graph->GetNodes();
TIntS S(nodes);
TIntQ Q(nodes);
TIntIntVH P(nodes); // one vector for every node
TIntFltH delta(nodes);
TIntH sigma(nodes), d(nodes);
// init
for (TUNGraph::TNodeI NI = Graph->BegNI(); NI < Graph->EndNI(); NI++) {
if (DoNodeCent) {
NodeBtwH.AddDat(NI.GetId(), 0);
}
if (DoEdgeCent) {
for (int e = 0; e < NI.GetOutDeg(); e++) {
if (NI.GetId() < NI.GetOutNId(e)) {
EdgeBtwH.AddDat(TIntPr(NI.GetId(), NI.GetOutNId(e)), 0);
}
}
}
sigma.AddDat(NI.GetId(), 0);
d.AddDat(NI.GetId(), -1);
P.AddDat(NI.GetId(), TIntV());
delta.AddDat(NI.GetId(), 0);
}
// calc betweeness
for (int k = 0; k < BtwNIdV.Len(); k++) {
const TUNGraph::TNodeI NI = Graph->GetNI(BtwNIdV[k]);
// reset
for (int i = 0; i < sigma.Len(); i++) {
sigma[i] = 0; d[i] = -1; delta[i] = 0; P[i].Clr(false);
}
S.Clr(false);
Q.Clr(false);
sigma.AddDat(NI.GetId(), 1);
d.AddDat(NI.GetId(), 0);
Q.Push(NI.GetId());
while (!Q.Empty()) {
const int v = Q.Top(); Q.Pop();
const TUNGraph::TNodeI NI2 = Graph->GetNI(v);
S.Push(v);
const int VDat = d.GetDat(v);
for (int e = 0; e < NI2.GetOutDeg(); e++) {
const int w = NI2.GetOutNId(e);
if (d.GetDat(w) < 0) { // find w for the first time
Q.Push(w);
d.AddDat(w, VDat + 1);
}
//shortest path to w via v ?
if (d.GetDat(w) == VDat + 1) {
sigma.AddDat(w) += sigma.GetDat(v);
P.GetDat(w).Add(v);
}
}
}
while (!S.Empty()) {
const int w = S.Top();
const double SigmaW = sigma.GetDat(w);
const double DeltaW = delta.GetDat(w);
const TIntV NIdV = P.GetDat(w);
S.Pop();
for (int i = 0; i < NIdV.Len(); i++) {
const int nid = NIdV[i];
const double c = (sigma.GetDat(nid)*1.0 / SigmaW) * (1 + DeltaW);
delta.AddDat(nid) += c;
if (DoEdgeCent) {
EdgeBtwH.AddDat(TIntPr(TMath::Mn(nid, w), TMath::Mx(nid, w))) += c;
}
}
if (DoNodeCent && w != NI.GetId()) {
NodeBtwH.AddDat(w) += delta.GetDat(w) / 2.0;
}
}
}
}