本文整理汇总了C++中PUNGraph::GetNodes方法的典型用法代码示例。如果您正苦于以下问题:C++ PUNGraph::GetNodes方法的具体用法?C++ PUNGraph::GetNodes怎么用?C++ PUNGraph::GetNodes使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类PUNGraph
的用法示例。
在下文中一共展示了PUNGraph::GetNodes方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: main
int main(int argc, char* argv[]) {
Env = TEnv(argc, argv, TNotify::StdNotify);
Env.PrepArgs(TStr::Fmt("ragm. build: %s, %s. Time: %s", __TIME__, __DATE__, TExeTm::GetCurTm()));
TExeTm ExeTm;
Try
TStr OutFPrx = Env.GetIfArgPrefixStr("-o:", "", "Output Graph data prefix");
const TStr InFNm = Env.GetIfArgPrefixStr("-i:", "../as20graph.txt", "Input edgelist file name");
const TStr LabelFNm = Env.GetIfArgPrefixStr("-l:", "", "Input file name for node names (Node ID, Node label) ");
int OptComs = Env.GetIfArgPrefixInt("-c:", -1, "The number of communities to detect (-1: detect automatically)");
const int MinComs = Env.GetIfArgPrefixInt("-mc:", 5, "Minimum number of communities to try");
const int MaxComs = Env.GetIfArgPrefixInt("-xc:", 100, "Maximum number of communities to try");
const int DivComs = Env.GetIfArgPrefixInt("-nc:", 10, "How many trials for the number of communities");
const int NumThreads = Env.GetIfArgPrefixInt("-nt:", 1, "Number of threads for parallelization");
const double StepAlpha = Env.GetIfArgPrefixFlt("-sa:", 0.3, "Alpha for backtracking line search");
const double StepBeta = Env.GetIfArgPrefixFlt("-sb:", 0.3, "Beta for backtracking line search");
PUNGraph G;
TIntStrH NIDNameH;
if (InFNm.IsStrIn(".ungraph")) {
TFIn GFIn(InFNm);
G = TUNGraph::Load(GFIn);
} else {
G = TAGMUtil::LoadEdgeListStr<PUNGraph>(InFNm, NIDNameH);
}
if (LabelFNm.Len() > 0) {
TSsParser Ss(LabelFNm, ssfTabSep);
while (Ss.Next()) {
if (Ss.Len() > 0) { NIDNameH.AddDat(Ss.GetInt(0), Ss.GetFld(1)); }
}
}
else {
}
printf("Graph: %d Nodes %d Edges\n", G->GetNodes(), G->GetEdges());
TVec<TIntV> EstCmtyVV;
TExeTm RunTm;
TAGMFast RAGM(G, 10, 10);
if (OptComs == -1) {
printf("finding number of communities\n");
OptComs = RAGM.FindComsByCV(NumThreads, MaxComs, MinComs, DivComs, OutFPrx, StepAlpha, StepBeta);
}
RAGM.NeighborComInit(OptComs);
if (NumThreads == 1 || G->GetEdges() < 1000) {
RAGM.MLEGradAscent(0.0001, 1000 * G->GetNodes(), "", StepAlpha, StepBeta);
} else {
RAGM.MLEGradAscentParallel(0.0001, 1000, NumThreads, "", StepAlpha, StepBeta);
}
RAGM.GetCmtyVV(EstCmtyVV);
TAGMUtil::DumpCmtyVV(OutFPrx + "cmtyvv.txt", EstCmtyVV, NIDNameH);
TAGMUtil::SaveGephi(OutFPrx + "graph.gexf", G, EstCmtyVV, 1.5, 1.5, NIDNameH);
Catch
printf("\nrun time: %s (%s)\n", ExeTm.GetTmStr(), TSecTm::GetCurTm().GetTmStr().CStr());
return 0;
}
示例2: TriadGetTestTUNGraph
// Test GetCmnNbrs: the number of neighbors in common
TEST(triad, TestGetCmnNbrs) {
// Test TUNGraph
PUNGraph GraphTUN = TriadGetTestTUNGraph();
for (int i = 0; i < GraphTUN->GetNodes(); i++) {
for (int j = i + 1; j < GraphTUN->GetNodes(); j++) {
VerifyCmnNbrs(i, j, TSnap::GetCmnNbrs(GraphTUN, i, j));
}
}
// Test TNGraph which is same as undirected.
PNGraph GraphTN = TriadGetTestTNGraph();
for (int i = 0; i < GraphTN->GetNodes(); i++) {
for (int j = i + 1; j < GraphTN->GetNodes(); j++) {
VerifyCmnNbrs(i, j, TSnap::GetCmnNbrs(GraphTN, i, j));
}
}
// Test TNEGraph which is same as undirected.
PNEGraph GraphTNE = TriadGetTestTNEGraph();
for (int i = 0; i < GraphTNE->GetNodes(); i++) {
for (int j = i + 1; j < GraphTNE->GetNodes(); j++) {
VerifyCmnNbrs(i, j, TSnap::GetCmnNbrs(GraphTNE, i, j));
}
}
}
示例3: 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;
}
示例4: GetCPMCommunities
/// Clique Percolation method communities
void TCliqueOverlap::GetCPMCommunities(const PUNGraph& G, int MinMaxCliqueSize, TVec<TIntV>& NIdCmtyVV) {
printf("Clique Percolation Method\n");
TExeTm ExeTm;
TVec<TIntV> MaxCliques;
TCliqueOverlap::GetMaxCliques(G, MinMaxCliqueSize, MaxCliques);
// op RS 2012/05/15, commented out next line, a parameter is missing,
// creating a warning on OS X
// printf("...%d cliques found\n");
// get clique overlap matrix (graph)
PUNGraph OverlapGraph = TCliqueOverlap::CalculateOverlapMtx(MaxCliques, MinMaxCliqueSize-1);
printf("...overlap matrix (%d, %d)\n", G->GetNodes(), G->GetEdges());
// connected components are communities
TCnComV CnComV;
TSnap::GetWccs(OverlapGraph, CnComV);
NIdCmtyVV.Clr(false);
TIntSet CmtySet;
for (int c = 0; c < CnComV.Len(); c++) {
CmtySet.Clr(false);
for (int i = 0; i <CnComV[c].Len(); i++) {
const TIntV& CliqueNIdV = MaxCliques[CnComV[c][i]];
CmtySet.AddKeyV(CliqueNIdV);
}
NIdCmtyVV.Add();
CmtySet.GetKeyV(NIdCmtyVV.Last());
NIdCmtyVV.Last().Sort();
}
printf("done [%s].\n", ExeTm.GetStr());
}
示例5: GenAGM
///Generate graph using the AGM model. CProbV = vector of Pc
PUNGraph TAGM::GenAGM(TVec<TIntV>& CmtyVV, const TFltV& CProbV, TRnd& Rnd, const double PNoCom) {
PUNGraph G = TUNGraph::New(100 * CmtyVV.Len(), -1);
printf("AGM begins\n");
for (int i = 0; i < CmtyVV.Len(); i++) {
TIntV& CmtyV = CmtyVV[i];
for (int u = 0; u < CmtyV.Len(); u++) {
if ( G->IsNode(CmtyV[u])) {
continue;
}
G->AddNode(CmtyV[u]);
}
double Prob = CProbV[i];
RndConnectInsideCommunity(G, CmtyV, Prob, Rnd);
}
if (PNoCom > 0.0) { //if we want to connect nodes that do not share any community
TIntSet NIDS;
for (int c = 0; c < CmtyVV.Len(); c++) {
for (int u = 0; u < CmtyVV[c].Len(); u++) {
NIDS.AddKey(CmtyVV[c][u]);
}
}
TIntV NIDV;
NIDS.GetKeyV(NIDV);
RndConnectInsideCommunity(G,NIDV,PNoCom,Rnd);
}
printf("AGM completed (%d nodes %d edges)\n",G->GetNodes(),G->GetEdges());
G->Defrag();
return G;
}
示例6: SetGraph
void TAGMFast::SetGraph(const PUNGraph& GraphPt) {
G = GraphPt;
HOVIDSV.Gen(G->GetNodes());
NodesOk = true;
GraphPt->GetNIdV(NIDV);
// check that nodes IDs are {0,1,..,Nodes-1}
for (int nid = 0; nid < GraphPt->GetNodes(); nid++) {
if (! GraphPt->IsNode(nid)) {
NodesOk = false;
break;
}
}
if (! NodesOk) {
printf("rearrage nodes\n");
G = TSnap::GetSubGraph(GraphPt, NIDV, true);
for (int nid = 0; nid < G->GetNodes(); nid++) {
IAssert(G->IsNode(nid));
}
}
TSnap::DelSelfEdges(G);
PNoCom = 1.0 / (double) G->GetNodes();
DoParallel = false;
if (1.0 / PNoCom > sqrt(TFlt::Mx)) { PNoCom = 0.99 / sqrt(TFlt::Mx); } // to prevent overflow
NegWgt = 1.0;
}
示例7: 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; }
}
示例8: GetEigVec
// to get first few eigenvectors
void GetEigVec(const PUNGraph& Graph, const int& EigVecs, TFltV& EigValV, TVec<TFltV>& EigVecV) {
const int Nodes = Graph->GetNodes();
// Lanczos
TUNGraphMtx GraphMtx(Graph);
int CalcVals = int(2*EigVecs);
if (CalcVals > Nodes) { CalcVals = Nodes; }
TFltVV EigVecVV;
//while (EigValV.Len() < EigVecs && CalcVals < 10*EigVecs) {
try {
TSparseSVD::Lanczos(GraphMtx, EigVecs, 2*EigVecs, ssotFull, EigValV, EigVecVV, false); }
catch(...) {
printf("\n ***EXCEPTION: TRIED %d GOT %d values** \n", CalcVals, EigValV.Len()); }
if (EigValV.Len() < EigVecs) {
printf(" ***TRIED %d GOT %d values** \n", CalcVals, EigValV.Len()); }
// CalcVals += EigVecs;
//}
TFltIntPrV EigValIdV;
for (int i = 0; i < EigValV.Len(); i++) {
EigValIdV.Add(TFltIntPr(EigValV[i], i));
}
EigValIdV.Sort(false);
EigValV.Sort(false);
for (int v = 0; v < EigValIdV.Len(); v++) { // vector components are not sorted!!!
EigVecV.Add();
EigVecVV.GetCol(EigValIdV[v].Val2, EigVecV.Last());
}
IsAllValVNeg(EigVecV[0], true);
}
示例9: 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;
}
}
示例10: TestConvertSubGraphs
// Test node subgraph conversion
void TestConvertSubGraphs() {
PNGraph NGraph;
PUNGraph UNGraph;
int N1, N2, N3;
int E1, E2, E3;
TIntV NIdV;
int i;
NGraph = GetTestTNGraph();
N1 = NGraph->GetNodes();
E1 = NGraph->GetEdges();
for (i = 0; i < 20; i += 2) {
NIdV.Add(i);
}
// TODO: fix TSnap::ConvertSubGraph<PUNGraph>(NGraph, NIdV, true), it fails
// UNGraph = TSnap::ConvertSubGraph<PUNGraph>(NGraph, NIdV, true);
UNGraph = TSnap::ConvertSubGraph<PUNGraph>(NGraph, NIdV);
N2 = UNGraph->GetNodes();
E2 = UNGraph->GetEdges();
NGraph = TSnap::ConvertSubGraph<PNGraph>(UNGraph, NIdV);
N3 = NGraph->GetNodes();
E3 = NGraph->GetEdges();
printf("---- TestConvertSubGraphs -----\n");
printf("nodes: %d,%d,%d, edges: %d,%d,%d\n", N1, N2, N3, E1, E2, E3);
printf("\n");
}
示例11: PlotEigValRank
/////////////////////////////////////////////////
// Spectral graph properties
void PlotEigValRank(const PUNGraph& Graph, const int& EigVals, const TStr& FNmPref, TStr DescStr) {
TFltV EigValV;
TSnap::GetEigVals(Graph, EigVals, EigValV);
EigValV.Sort(false);
if (DescStr.Empty()) { DescStr = FNmPref; }
TGnuPlot::PlotValV(EigValV, "eigVal."+FNmPref, TStr::Fmt("%s. G(%d, %d). Largest eig val = %f",
DescStr.CStr(), Graph->GetNodes(), Graph->GetEdges(), EigValV[0].Val), "Rank", "Eigen value", gpsLog10XY, false, gpwLinesPoints);
}
示例12: 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; }
}
示例13: 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);
}
示例14: PlotInvParticipRat
// Inverse participation ratio: normalize EigVec to have L2=1 and then I=sum_k EigVec[i]^4
// see Spectra of "real-world" graphs: Beyond the semicircle law by Farkas, Derenyi, Barabasi and Vicsek
void PlotInvParticipRat(const PUNGraph& Graph, const int& MaxEigVecs, const int& TimeLimit, const TStr& FNmPref, TStr DescStr) {
TFltPrV EigIprV;
GetInvParticipRat(Graph, MaxEigVecs, TimeLimit, EigIprV);
if (DescStr.Empty()) { DescStr = FNmPref; }
if (EigIprV.Empty()) { DescStr+=". FAIL"; EigIprV.Add(TFltPr(-1,-1)); return; }
TGnuPlot::PlotValV(EigIprV, "eigIPR."+FNmPref, TStr::Fmt("%s. G(%d, %d). Largest eig val = %f (%d values)",
DescStr.CStr(), Graph->GetNodes(), Graph->GetEdges(), EigIprV.Last().Val1(), EigIprV.Len()),
"Eigenvalue", "Inverse Participation Ratio of corresponding Eigenvector", gpsLog10Y, false, gpwPoints);
}
示例15: GetFarnessCentr
double GetFarnessCentr(const PUNGraph& Graph, const int& NId) {
TIntH NDistH(Graph->GetNodes());
TSnap::GetShortPath<PUNGraph>(Graph, NId, NDistH, true, TInt::Mx);
double sum = 0;
for (TIntH::TIter I = NDistH.BegI(); I < NDistH.EndI(); I++) {
sum += I->Dat();
}
if (NDistH.Len() > 1) { return sum / double(NDistH.Len() - 1); }
else { return 0.0; }
}