本文整理汇总了C++中PUNGraph::BegNI方法的典型用法代码示例。如果您正苦于以下问题:C++ PUNGraph::BegNI方法的具体用法?C++ PUNGraph::BegNI怎么用?C++ PUNGraph::BegNI使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类PUNGraph
的用法示例。
在下文中一共展示了PUNGraph::BegNI方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: 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;
}
示例2: GetEigenVectorCentr
void GetEigenVectorCentr(const PUNGraph& Graph, TIntFltH& EigenH, const double& Eps, const int& MaxIter) {
const int NNodes = Graph->GetNodes();
EigenH.Gen(NNodes);
for (TUNGraph::TNodeI NI = Graph->BegNI(); NI < Graph->EndNI(); NI++) {
EigenH.AddDat(NI.GetId(), 1.0/NNodes);
IAssert(NI.GetId() == EigenH.GetKey(EigenH.Len()-1));
}
TFltV TmpV(NNodes);
double diff = TFlt::Mx;
for (int iter = 0; iter < MaxIter; iter++) {
int j = 0;
for (TUNGraph::TNodeI NI = Graph->BegNI(); NI < Graph->EndNI(); NI++, j++) {
TmpV[j] = 0;
for (int e = 0; e < NI.GetOutDeg(); e++) {
TmpV[j] += EigenH.GetDat(NI.GetOutNId(e)); }
}
double sum = 0;
for (int i = 0; i < TmpV.Len(); i++) {
EigenH[i] = TmpV[i];
sum += EigenH[i];
}
for (int i = 0; i < EigenH.Len(); i++) {
EigenH[i] /= sum; }
if (fabs(diff-sum) < Eps) { break; }
//printf("\tdiff:%f\tsum:%f\n", fabs(diff-sum), sum);
diff = sum;
}
}
示例3: 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;
}
示例4: ave_path_length
double ave_path_length (PUNGraph p) {
TVec<TInt> v;
double tot_lengths = 0.0;
for (TUNGraph::TNodeI n = p->BegNI(); n != p->EndNI(); n++) {
v = v + n.GetId();
}
// cerr << "vlen: " << v.Len() << endl;
TBreathFS<PUNGraph> b(p);
double tot_pairs = 0.0;
while (v.Len () > 0) {
TInt last = v[v.Len()-1];
b.DoBfs (last, true, true);
for (TVec<TInt>::TIter i = v.BegI(); (*i) != last; i++) {
int length;
length = b.GetHops (last, (*i));
if (length == length) {
tot_lengths += length;
tot_pairs += 1;
}
}
// cerr << "tps: " << tot_pairs << ", last: " << last << ", beg: " << v[*(v.BegI())] << endl;
v.Del(v.Len()-1);
}
// cerr << "paths: " << tot_lengths << " " << tot_pairs << " " << (tot_lengths/tot_pairs) << endl;
return tot_lengths / tot_pairs;
}
示例5: GetGroupFarnessCentr
double GetGroupFarnessCentr(const PUNGraph& Graph, const TIntH& GroupNodes) {
TIntH* NDistH = new TIntH[GroupNodes.Len()];
for (int i = 0; i<GroupNodes.Len(); i++){
NDistH[i](Graph->GetNodes());
TSnap::GetShortPath<PUNGraph>(Graph, GroupNodes.GetDat(i), NDistH[i], true, TInt::Mx);
}
int min, dist, sum = 0, len = 0;
for (PUNGraph::TObj::TNodeI NI = Graph->BegNI(); NI < Graph->EndNI(); NI++){
if (NDistH[0].IsKey(NI.GetId()))
min = NDistH[0].GetDat(NI.GetId());
else
min = -1;
for (int j = 1; j<GroupNodes.Len(); j++){
if (NDistH[j].IsKey(NI.GetId()))
dist = NDistH[j].GetDat(NI.GetId());
else
dist = -1;
if ((dist < min && dist != -1) || (dist > min && min == -1))
min = dist;
}
if (min>0){
sum += min;
len++;
}
}
if (len > 0) { return sum / double(len); }
else { return 0.0; }
}
示例6: GetEigenVectorCentr
void GetEigenVectorCentr(const PUNGraph& Graph, TIntFltH& NIdEigenH, const double& Eps, const int& MaxIter) {
const int NNodes = Graph->GetNodes();
NIdEigenH.Gen(NNodes);
// initialize vector values
for (TUNGraph::TNodeI NI = Graph->BegNI(); NI < Graph->EndNI(); NI++) {
NIdEigenH.AddDat(NI.GetId(), 1.0 / NNodes);
IAssert(NI.GetId() == NIdEigenH.GetKey(NIdEigenH.Len() - 1));
}
TFltV TmpV(NNodes);
for (int iter = 0; iter < MaxIter; iter++) {
int j = 0;
// add neighbor values
for (TUNGraph::TNodeI NI = Graph->BegNI(); NI < Graph->EndNI(); NI++, j++) {
TmpV[j] = 0;
for (int e = 0; e < NI.GetOutDeg(); e++) {
TmpV[j] += NIdEigenH.GetDat(NI.GetOutNId(e));
}
}
// normalize
double sum = 0;
for (int i = 0; i < TmpV.Len(); i++) {
sum += (TmpV[i] * TmpV[i]);
}
sum = sqrt(sum);
for (int i = 0; i < TmpV.Len(); i++) {
TmpV[i] /= sum;
}
// compute difference
double diff = 0.0;
j = 0;
for (TUNGraph::TNodeI NI = Graph->BegNI(); NI < Graph->EndNI(); NI++, j++) {
diff += fabs(NIdEigenH.GetDat(NI.GetId()) - TmpV[j]);
}
// set new values
j = 0;
for (TUNGraph::TNodeI NI = Graph->BegNI(); NI < Graph->EndNI(); NI++, j++) {
NIdEigenH.AddDat(NI.GetId(), TmpV[j]);
}
if (diff < Eps) {
break;
}
}
}
示例7: GetDegreeCentralization
double GetDegreeCentralization(const PUNGraph& Graph) {
int MaxDeg = -1;
int N = Graph->GetNodes();
int Sum = 0;
if (Graph->GetNodes() > 1 && (double(N - 2.0)*double(N - 1)) > 0) {
for (TUNGraph::TNodeI NI = Graph->BegNI(); NI < Graph->EndNI(); NI++) {
int deg = NI.GetDeg();
if (deg > MaxDeg) {
MaxDeg = deg;
}
}
for (TUNGraph::TNodeI NI = Graph->BegNI(); NI < Graph->EndNI(); NI++) {
Sum += MaxDeg - NI.GetDeg();
}
return double(Sum) / (double(N - 2.0)*double(N - 1));
}
else { return 0.0; }
}
示例8: 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();
}
示例9: CmtyCMN
static double CmtyCMN(const PUNGraph& Graph, TCnComV& CmtyV) {
TCNMQMatrix QMatrix(Graph);
// maximize modularity
while (QMatrix.MergeBestQ()) { }
// reconstruct communities
THash<TInt, TIntV> IdCmtyH;
for (TUNGraph::TNodeI NI = Graph->BegNI(); NI < Graph->EndNI(); NI++) {
IdCmtyH.AddDat(QMatrix.CmtyIdUF.Find(NI.GetId())).Add(NI.GetId());
}
CmtyV.Gen(IdCmtyH.Len());
for (int j = 0; j < IdCmtyH.Len(); j++) {
CmtyV[j].NIdV.Swap(IdCmtyH[j]);
}
return QMatrix.Q;
}
示例10: GetAsstyCor
double GetAsstyCor(const PUNGraph& Graph) {
TIntFltH deg(Graph->GetNodes()), deg_sq(Graph->GetNodes());
for (TUNGraph::TNodeI NI = Graph->BegNI(); NI < Graph->EndNI(); NI++) {
deg.AddDat(NI.GetId()) = NI.GetOutDeg();
deg_sq.AddDat(NI.GetId()) = NI.GetOutDeg() * NI.GetOutDeg();
}
double m = Graph->GetEdges(), num1 = 0.0, num2 = 0.0, den1 = 0.0;
for (TUNGraph::TEdgeI EI = Graph->BegEI(); EI < Graph->EndEI(); EI++) {
double t1 = deg.GetDat(EI.GetSrcNId()).Val, t2 = deg.GetDat(EI.GetDstNId()).Val;
num1 += t1 * t2;
num2 += t1 + t2;
den1 += deg_sq.GetDat(EI.GetSrcNId()).Val + deg_sq.GetDat(EI.GetDstNId()).Val;
}
num1 /= m;
den1 /= (2.0 * m);
num2 = (num2 / (2.0 * m)) * (num2 / (2.0 * m));
return (num1 - num2) / (den1 - num2);
}
示例11: GenRewire
/// Rewire the network. Keeps node degrees as is but randomly rewires the edges.
/// Use this function to generate a random graph with the same degree sequence
/// as the OrigGraph.
/// See: On the uniform generation of random graphs with prescribed degree
/// sequences by R. Milo, N. Kashtan, S. Itzkovitz, M. E. J. Newman, U. Alon
/// URL: http://arxiv.org/abs/cond-mat/0312028
PUNGraph GenRewire(const PUNGraph& OrigGraph, const int& NSwitch, TRnd& Rnd) {
const int Nodes = OrigGraph->GetNodes();
const int Edges = OrigGraph->GetEdges();
PUNGraph GraphPt = TUNGraph::New();
TUNGraph& Graph = *GraphPt;
Graph.Reserve(Nodes, -1);
TExeTm ExeTm;
// generate a graph that satisfies the constraints
printf("Randomizing edges (%d, %d)...\n", Nodes, Edges);
TIntPrSet EdgeSet(Edges);
for (TUNGraph::TNodeI NI = OrigGraph->BegNI(); NI < OrigGraph->EndNI(); NI++) {
const int NId = NI.GetId();
for (int e = 0; e < NI.GetOutDeg(); e++) {
if (NId <= NI.GetOutNId(e)) { continue; }
EdgeSet.AddKey(TIntPr(NId, NI.GetOutNId(e)));
}
Graph.AddNode(NI.GetId());
}
// edge switching
uint skip=0;
for (uint swps = 0; swps < 2*uint(Edges)*uint(NSwitch); swps++) {
const int keyId1 = EdgeSet.GetRndKeyId(Rnd);
const int keyId2 = EdgeSet.GetRndKeyId(Rnd);
if (keyId1 == keyId2) { skip++; continue; }
const TIntPr& E1 = EdgeSet[keyId1];
const TIntPr& E2 = EdgeSet[keyId2];
TIntPr NewE1(E1.Val1, E2.Val1), NewE2(E1.Val2, E2.Val2);
if (NewE1.Val1 > NewE1.Val2) { Swap(NewE1.Val1, NewE1.Val2); }
if (NewE2.Val1 > NewE2.Val2) { Swap(NewE2.Val1, NewE2.Val2); }
if (NewE1!=NewE2 && NewE1.Val1!=NewE1.Val2 && NewE2.Val1!=NewE2.Val2 && ! EdgeSet.IsKey(NewE1) && ! EdgeSet.IsKey(NewE2)) {
EdgeSet.DelKeyId(keyId1); EdgeSet.DelKeyId(keyId2);
EdgeSet.AddKey(TIntPr(NewE1));
EdgeSet.AddKey(TIntPr(NewE2));
} else { skip++; }
if (swps % Edges == 0) {
printf("\r %uk/%uk: %uk skip [%s]", swps/1000u, 2*uint(Edges)*uint(NSwitch)/1000u, skip/1000u, ExeTm.GetStr());
if (ExeTm.GetSecs() > 2*3600) { printf(" *** Time limit!\n"); break; } // time limit 2 hours
}
}
printf("\r total %uk switchings attempted, %uk skiped [%s]\n", 2*uint(Edges)*uint(NSwitch)/1000u, skip/1000u, ExeTm.GetStr());
for (int e = 0; e < EdgeSet.Len(); e++) {
Graph.AddEdge(EdgeSet[e].Val1, EdgeSet[e].Val2); }
return GraphPt;
}
示例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: FastCorePeriphery
int FastCorePeriphery(PUNGraph& Graph, TIntIntH& out) {
TIntIntH nodes;
double Z=0;
for (TUNGraph::TNodeI NI = Graph->BegNI(); NI < Graph->EndNI(); NI++) { // Calculate and store the degrees of each node.
int deg = NI.GetDeg();
int id = NI.GetId();
Z += deg;
nodes.AddDat(id,deg);
}
Z = Z/2;
nodes.SortByDat(false); // Then sort the nodes in descending order of degree, to get a list of nodes {v1, v2, . . . , vn}.
double Zbest = 99999900000000000;
int kbest = 0;
int br=0;
for (int k=0; k<nodes.Len(); k++) {
br++;
Z = Z + br - 1 - nodes[k];
if (Z < Zbest) { // or <=
Zbest = Z;
kbest = br;
}
}
int cp = 0;
br = 0;
for (THashKeyDatI<TInt, TInt> it = nodes.BegI(); !it.IsEnd(); it++) {
if (br < kbest)
cp = 1;
else
cp = 0;
out.AddDat(it.GetKey(), cp);
br++;
}
return kbest;
}
示例14: CommunityGirvanNewman
// Maximum modularity clustering by Girvan-Newman algorithm (slow)
// Girvan M. and Newman M. E. J., Community structure in social and biological networks, Proc. Natl. Acad. Sci. USA 99, 7821–7826 (2002)
double CommunityGirvanNewman(PUNGraph& Graph, TCnComV& CmtyV) {
TIntH OutDegH;
const int NEdges = Graph->GetEdges();
for (TUNGraph::TNodeI NI = Graph->BegNI(); NI < Graph->EndNI(); NI++) {
OutDegH.AddDat(NI.GetId(), NI.GetOutDeg());
}
double BestQ = -1; // modularity
TCnComV CurCmtyV;
CmtyV.Clr();
TIntV Cmty1, Cmty2;
while (true) {
CmtyGirvanNewmanStep(Graph, Cmty1, Cmty2);
const double Q = _GirvanNewmanGetModularity(Graph, OutDegH, NEdges, CurCmtyV);
//printf("current modularity: %f\n", Q);
if (Q > BestQ) {
BestQ = Q;
CmtyV.Swap(CurCmtyV);
}
if (Cmty1.Len()==0 || Cmty2.Len() == 0) { break; }
}
return BestQ;
}
示例15: MaxCPGreedyBetter
// Maximum Domination Problem
void MaxCPGreedyBetter(const PUNGraph& Graph, const int k, TIntH& GroupNodes) {
// buildup cpntainer of group nodes
const int n = Graph->GetNodes();
int *NNodes = new int[n]; // container of neighbouring nodes
int NNodes_br = 0;
TIntH Nodes; // nodes sorted by vd
double gc = 0, gc0 = 0;
int addId = 0, addIdPrev = 0;
for (TUNGraph::TNodeI NI = Graph->BegNI(); NI < Graph->EndNI(); NI++){
Nodes.AddDat(NI.GetId(), NI.GetDeg());
}
Nodes.SortByDat(false);
int br = 0;
while (br < k) {
for (THashKeyDatI<TInt, TInt> NI = Nodes.BegI(); NI < Nodes.EndI(); NI++){
if ((NI.GetDat() <= (int)gc0))
break;
gc = NI.GetDat() - Intersect(Graph->GetNI(NI.GetKey()), NNodes, NNodes_br);
if (gc>gc0){
gc0 = gc;
addId = NI.GetKey();
}
}
if (addId != addIdPrev) {
GroupNodes.AddDat(br, addId);
br++;
gc0 = 0;
int nn = addId;
bool nnnew = true;
for (int j = 0; j<NNodes_br; j++)
if (NNodes[j] == nn){
nnnew = false;
j = NNodes_br;
}
if (nnnew){
NNodes[NNodes_br] = nn;
NNodes_br++;
}
for (int i = 0; i<Graph->GetNI(addId).GetDeg(); i++) {
int nn = Graph->GetNI(addId).GetNbrNId(i);
bool nnnew = true;
for (int j = 0; j<NNodes_br; j++) {
if (NNodes[j] == nn){
nnnew = false;
j = NNodes_br;
}
}
if (nnnew){
NNodes[NNodes_br] = nn;
NNodes_br++;
}
}
addIdPrev = addId;
Nodes.DelKey(addId);
}
else {
br = k;
}
printf("%i,", br);
}
delete NNodes;
}