本文整理汇总了C++中PUNGraph类的典型用法代码示例。如果您正苦于以下问题:C++ PUNGraph类的具体用法?C++ PUNGraph怎么用?C++ PUNGraph使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了PUNGraph类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: 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(UGraph, NId));
}
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;
}
示例2: GetMaximalCliques
void TCliqueOverlap::GetMaximalCliques(const PUNGraph& G, int MinMaxCliqueSize, TVec<TIntV>& MaxCliques) {
if (G->GetNodes() == 0) return;
//
m_G = G;
m_minMaxCliqueSize = MinMaxCliqueSize;
m_maxCliques =& MaxCliques;
m_Q.Clr();
//
THashSet<TInt> SUBG;
THashSet<TInt> CAND;
for (TUNGraph::TNodeI NI=m_G->BegNI(); NI<m_G->EndNI(); NI++) {
TInt nId = NI.GetId();
SUBG.AddKey(nId);
CAND.AddKey(nId);
}
//
Expand(SUBG, CAND);
}
示例3: GetGroupDegreeCentr
double GetGroupDegreeCentr(const PUNGraph& Graph, const TIntH& GroupNodes) {
int deg;
TIntH NN;
TIntH GroupNodes1;
for (THashKeyDatI<TInt, TInt> NI = GroupNodes.BegI(); NI < GroupNodes.EndI(); NI++)
GroupNodes1.AddDat(NI.GetDat(), NI.GetDat());
for (THashKeyDatI<TInt, TInt> NI = GroupNodes1.BegI(); NI < GroupNodes1.EndI(); NI++){
TUNGraph::TNodeI node = Graph->GetNI(NI.GetKey());
deg = node.GetDeg();
for (int j = 0; j < deg; j++){
if (GroupNodes1.IsKey(node.GetNbrNId(j)) == 0 && NN.IsKey(node.GetNbrNId(j)) == 0)
NN.AddDat(node.GetNbrNId(j), NI.GetKey());
}
}
return (double)NN.Len();
}
示例4: CmtyGirvanNewmanStep
// GIRVAN-NEWMAN algorithm
// 1. The betweenness of all existing edges in the network is calculated first.
// 2. The edge with the highest betweenness is removed.
// 3. The betweenness of all edges affected by the removal is recalculated.
// 4. Steps 2 and 3 are repeated until no edges remain.
// Girvan M. and Newman M. E. J., Community structure in social and biological networks, Proc. Natl. Acad. Sci. USA 99, 7821–7826 (2002)
// Keep removing edges from Graph until one of the connected components of Graph splits into two.
void CmtyGirvanNewmanStep(PUNGraph& Graph, TIntV& Cmty1, TIntV& Cmty2) {
TIntPrFltH BtwEH;
TBreathFS<PUNGraph> BFS(Graph);
Cmty1.Clr(false); Cmty2.Clr(false);
while (true) {
TSnap::GetBetweennessCentr(Graph, BtwEH);
BtwEH.SortByDat(false);
if (BtwEH.Empty()) { return; }
const int NId1 = BtwEH.GetKey(0).Val1;
const int NId2 = BtwEH.GetKey(0).Val2;
Graph->DelEdge(NId1, NId2);
BFS.DoBfs(NId1, true, false, NId2, TInt::Mx);
if (BFS.GetHops(NId1, NId2) == -1) { // two components
TSnap::GetNodeWcc(Graph, NId1, Cmty1);
TSnap::GetNodeWcc(Graph, NId2, Cmty2);
return;
}
}
}
示例5: 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);
}
示例6: GetSubGraph
// renumber node ids 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++) {
NewGraph.AddNode(n);
NIdSet.AddKey(NIdV[n]);
}
for (int n = 0; n < NIdV.Len(); n++) {
const TUNGraph::TNodeI NI = Graph->GetNI(NIdV[n]);
const int SrcNId = NIdSet.GetKeyId(NI.GetId());
for (int edge = 0; edge < NI.GetDeg(); edge++) {
const int OutNId = NIdSet.GetKeyId(NI.GetNbhNId(edge));
if (NewGraph.IsNode(OutNId)) {
NewGraph.AddEdge(SrcNId, OutNId); }
}
}
return NewGraphPt;
}
示例7: RndConnectInsideCommunity
///Connect members of a given community by Erdos-Renyi
void TAGM::RndConnectInsideCommunity(PUNGraph& Graph, const TIntV& CmtyV, const double& Prob, TRnd& Rnd) {
int CNodes = CmtyV.Len(), CEdges;
if (CNodes < 20) {
CEdges = (int) Rnd.GetBinomialDev(Prob, CNodes * (CNodes-1) / 2);
} else {
CEdges = (int) (Prob * CNodes * (CNodes - 1) / 2);
}
THashSet<TIntPr> NewEdgeSet(CEdges);
for (int edge = 0; edge < CEdges; ) {
int SrcNId = CmtyV[Rnd.GetUniDevInt(CNodes)];
int DstNId = CmtyV[Rnd.GetUniDevInt(CNodes)];
if (SrcNId > DstNId) {
Swap(SrcNId,DstNId);
}
if (SrcNId != DstNId && ! NewEdgeSet.IsKey(TIntPr(SrcNId, DstNId))) { // is new edge
NewEdgeSet.AddKey(TIntPr(SrcNId, DstNId));
Graph->AddEdge(SrcNId, DstNId);
edge++;
}
}
}
示例8: GetInvParticipRat
void GetInvParticipRat(const PUNGraph& Graph, int MaxEigVecs, int TimeLimit, TFltPrV& EigValIprV) {
TUNGraphMtx GraphMtx(Graph);
TFltVV EigVecVV;
TFltV EigValV;
TExeTm ExeTm;
if (MaxEigVecs<=1) { MaxEigVecs=1000; }
int EigVecs = TMath::Mn(Graph->GetNodes(), MaxEigVecs);
printf("start %d vecs...", EigVecs);
try {
TSparseSVD::Lanczos2(GraphMtx, EigVecs, TimeLimit, ssotFull, EigValV, EigVecVV, false);
} catch(...) {
printf("\n ***EXCEPTION: TRIED %d GOT %d values** \n", EigVecs, EigValV.Len()); }
printf(" ***TRIED %d GOT %d values in %s\n", EigVecs, EigValV.Len(), ExeTm.GetStr());
TFltV EigVec;
EigValIprV.Clr();
if (EigValV.Empty()) { return; }
for (int v = 0; v < EigVecVV.GetCols(); v++) {
EigVecVV.GetCol(v, EigVec);
EigValIprV.Add(TFltPr(EigValV[v], GetInvParticipRat(EigVec)));
}
EigValIprV.Sort();
}
示例9: 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; }
}
示例10: GetMotifCount
void GetMotifCount(const PUNGraph& G, const int MotifSize, TVec <int64> & MotifV, const int num) {
if (MotifSize == 3) {
MotifV = TVec <int64> (2);
MotifV.PutAll(0);
TSnap::GetTriads(G,MotifV[mtThreeClosed],MotifV[mtThreeOpen],num);
}
else {
MotifV = TVec <int64> (6);
MotifV.PutAll(0);
TIntPrV V(G->GetEdges(), 0);
for (TUNGraph::TEdgeI EI = G->BegEI(); EI < G->EndEI(); EI++) {
V.Add(TIntPr(EI.GetSrcNId(), EI.GetDstNId()));
}
TRnd blargh;
V.Shuffle(blargh);
for (int z = 0; z < num; z++) {
int SrcNId = V[z].Val1.Val, DstNId = V[z].Val2.Val;
TUNGraph::TNodeI SrcNI = G->GetNI(SrcNId), DstNI = G->GetNI(DstNId);
TIntV SrcV(SrcNI.GetOutDeg(),0), DstV(DstNI.GetOutDeg(),0), BothV(min(SrcNI.GetOutDeg(), DstNI.GetOutDeg()),0);
SrcV.Clr(0,-1);
DstV.Clr(0,-1);
BothV.Clr(0,-1);
//Grouping the vertices into sets
for (int e = 0; e < SrcNI.GetOutDeg(); e++) {
if (SrcNI.GetOutNId(e) == DstNId) continue;
if (G->IsEdge(DstNId, SrcNI.GetOutNId(e)) ) { BothV.Add(SrcNI.GetOutNId(e)); }
else { SrcV.Add(SrcNI.GetOutNId(e)); }
}
for (int e = 0; e < DstNI.GetOutDeg(); e++) {
if (DstNI.GetOutNId(e) == SrcNId) continue;
if (G->IsEdge(SrcNId, DstNI.GetOutNId(e)) == 0) { DstV.Add(DstNI.GetOutNId(e)); }
}
//Compute Motif 0 and 1
for (int i = 0; i < SrcV.Len(); i++) {
for (int j = 0; j < DstV.Len(); j++) {
if (G->IsEdge(SrcV[i], DstV[j]) ) { MotifV[mfFourSquare]++; }
else MotifV[mfFourLine]++;
}
}
//Compute Motif 2 and 3
for (int i = 0; i < SrcV.Len(); i++) {
for (int j = i + 1; j < SrcV.Len(); j++) {
if (G->IsEdge(SrcV[i], SrcV[j]) ) { MotifV[mfFourTriangleEdge]++; }
else MotifV[mfFourStar]++;
}
}
for (int i = 0; i < DstV.Len(); i++) {
for (int j = i + 1; j < DstV.Len(); j++) {
if (G->IsEdge(DstV[i], DstV[j]) ) { MotifV[mfFourTriangleEdge]++; }
else MotifV[mfFourStar]++;
}
}
//Compute Motif 4 and 5
for (int i = 0; i < BothV.Len(); i++) {
for (int j = i + 1; j < BothV.Len(); j++) {
if (G->IsEdge(BothV[i], BothV[j]) ) { MotifV[mfFourComplete]++; }
else MotifV[mfFourSquareDiag]++;
}
}
}
MotifV[mfFourSquare] /= 4ll;
MotifV[mfFourStar] /= 3ll;
MotifV[mfFourComplete] /= 6ll;
}
}
示例11: sample
void sample (const int *m, const int *n, const int *h, const int *ns, const int *in, const int *infection_state, const int *mde, const int *bi, const int *br, double * result) {
const int nodes = *h;
const int nval = (*n)/2;
int num_seeds = *ns;
int infect_type = *in;
int mode = *mde;
int burnin = *bi;
int branch = *br;
PUNGraph g = get_PUNGraph (m, nval, nodes);
THash<TInt, TInt> * visited = choose_seeds (g, num_seeds, infection_state, infect_type);
TVec <VisitedNode *> queue;
TIntV qids;
for (THash<TInt, TInt>::TIter n = visited->BegI(); n != visited->EndI(); n++) {
queue = queue + new VisitedNode (n->Key);
qids = qids + n->Key;
//cerr << "enqueued " << n->Key << endl;
}
TInt counted = 0;
TInt first_unprocessed = 0;
TFlt infected_mass = 0.0;
TFlt total_mass = 0.0;
TFlt revisits = 0.0;
TFlt trehits = 0.0;
//cerr << "nodeId\tneigh\tnbh_size\tinfected?\tinfected_mass\ttotal_mass" << endl;
while (counted < 500 && first_unprocessed < queue.Len()) {
VisitedNode * current_node = queue [first_unprocessed];
first_unprocessed++;
TUNGraph::TNodeI NI = g->GetNI (current_node->id);
TInt neighborhood_size = NI.GetDeg();
// cerr << counted << " " << current_node->id << endl;
if (counted >= burnin) {
if (infection_state[(current_node->id) - 1] == 1)
infected_mass += 1.0/TFlt(neighborhood_size);
total_mass += 1.0/TFlt(neighborhood_size);
}
//cerr << current_node->id << "\t" << neighborhood_size << "\t" << (1.0/TFlt(neighborhood_size))
// << "\t" << infection_state[(current_node->id) - 1] << "\t" << infected_mass << "\t" << total_mass << endl;
// build list of unvisited neighbors
TVec<TInt> neighbors;
for (int i = 0; i < neighborhood_size; i++) {
TInt neighbor = NI.GetNbrNId(i);
if (mode == 0 && visited->IsKey(neighbor)) continue;
else if (mode == 2 && isChild (current_node, neighbor)) continue;
else if (mode == 3 && current_node-> previous != NULL && current_node->previous->id == neighbor) continue;
else neighbors = neighbors + neighbor;
}
TInt num_legal_neighbors = neighbors.Len();
TInt sample_size = TMath::Mn<TInt> (branch, num_legal_neighbors);
THash <TInt, TInt> * choices = choose (num_legal_neighbors, sample_size);
for (THash<TInt, TInt>::TIter n = choices->BegI(); n != choices->EndI(); n++) {
if (queue.Len() >= 500) break;
queue = queue + new VisitedNode (neighbors[n->Key], current_node);
if (visited->IsKey(neighbors[n->Key])) revisits++;
if (isChild(current_node, neighbors[n->Key])) trehits++;
if (!visited->IsKey(neighbors[n->Key])) qids = qids + neighbors[n->Key];
visited->AddDat(neighbors[n->Key], 1);
}
counted++;
}
// cout << (infected_mass / total_mass) << endl;
delete (visited);
result[0] = (infected_mass / total_mass);
result[1] = revisits;
result[2] = trehits;
result[3] = counted;
//PUNGraph p (&g);
PUNGraph p = TSnap:: GetSubGraph (g, qids, false);
TCnComV convec;
result[4] = TSnap::GetClustCf(p, -1);
TSnap::GetWccs(p, convec);
result[5] = convec.Len();
result[6] = ave_path_length (p);
}
示例12: main
//.........这里部分代码省略.........
TStr FNInDD = prefix;
// os << InFNm << "_HopPlot";
// TStr FNHops = os.str();
// os.str("");
// os.clear();
TStr FNHops = prefix;
// os << InFNm << "_WeaklyConnectedComponents";
// TStr FNWeaklyConnectedComps = os.str();
// os.str("");
// os.clear();
TStr FNWeaklyConnectedComps = prefix;
// os << InFNm << "_StronglyConnectedComponents";
// TStr FNStronglyConnectedComps = os.str();
// os.str("");
// os.clear();
TStr FNStronglyConnectedComps = prefix;
// os << InFNm << "ClusteringCoefficient";
// TStr FNClusteringCoeff = os.str();
// os.str("");
// os.clear();
TStr FNClusteringCoeff = prefix;
TStr FNCentrality = prefix + "_centrality.dat";
Env = TEnv(argc, argv, TNotify::StdNotify);
Env.PrepArgs(TStr::Fmt("GraphInfo. build: %s, %s. Time: %s", __TIME__, __DATE__, TExeTm::GetCurTm()));
TExeTm ExeTm;
Try
PNGraph Graph = TSnap::LoadEdgeList<PNGraph>(InFNm);
// if (! IsDir) { TSnap::MakeUnDir(Graph); }
printf("nodes:%d edges:%d\nCreating plots...\n", Graph->GetNodes(), Graph->GetEdges());
// const int Vals = Graph->GetNodes()/2 > 200 ? 200 : Graph->GetNodes()/2;
// if (PlotDD) {
TSnap::PlotOutDegDistr(Graph, FNOutDD, Desc, false, false);
TSnap::PlotInDegDistr(Graph, FNInDD, Desc, false, false);
printf("In and Out done\n");
// }
// if (PlotCDD) {
// TSnap::PlotOutDegDistr(Graph, FNOutCDD, Desc, true, false); // <======== waere interessant
// TSnap::PlotInDegDistr(Graph, FNInCDD, Desc, true, false);
// }
// if (PlotHop) {
TSnap::PlotHops(Graph, FNHops, Desc, false, 8);
printf("hops done\n");
// }
// if (PlotWcc) {
TSnap::PlotWccDistr(Graph, FNWeaklyConnectedComps, Desc);
printf("wcc done\n");
// }
// if (PlotScc) {
TSnap::PlotSccDistr(Graph, FNStronglyConnectedComps, Desc);
printf("scc done\n");
// }
// if (PlotClustCf) {
TSnap::PlotClustCf(Graph, FNClusteringCoeff, Desc);
printf("CC done\n");
// }
// if (PlotSVal) {
// TSnap::PlotSngValRank(Graph, Vals, OutFNm, Desc);
// }
// if(PlotSVec) {
// TSnap::PlotSngVec(Graph, OutFNm, Desc);
// }
PUNGraph UGraph = TSnap::ConvertGraph<PUNGraph>(Graph);
TIntFltH BtwH, PRankH, CcfH, CloseH;
printf(" PageRank... "); TSnap::GetPageRank(Graph, PRankH, 0.85);
printf(" Betweenness (SLOW!)..."); TSnap::GetBetweennessCentr(UGraph, BtwH, 0.1);
printf(" Clustering..."); TSnap::GetNodeClustCf(UGraph, CcfH);
printf(" Closeness (SLOW!)...");
for (TUNGraph::TNodeI NI = UGraph->BegNI(); NI < UGraph->EndNI(); NI++) {
const int NId = NI.GetId();
CloseH.AddDat(NId, TSnap::GetClosenessCentr(UGraph, NId));
}
printf("\nDONE! saving...");
FILE *F = fopen(FNCentrality.CStr(), "wt");
fprintf(F,"#Network: %s\n", prefix.CStr());
fprintf(F,"#Nodes: %d\tEdges: %d\n", Graph->GetNodes(), Graph->GetEdges());
fprintf(F,"#NodeId\tDegree\tCloseness\tBetweennes\tClusteringCoefficient\tPageRank\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 ClustCf = CcfH.GetDat(NId);
const double PgrCentr = PRankH.GetDat(NId);
fprintf(F, "%d\t%f\t%f\t%f\t%f\t%f\n", NId,
DegCentr, CloCentr, BtwCentr, ClustCf, PgrCentr);
}
fclose(F);
Catch
printf("\nrun time: %s (%s)\n", ExeTm.GetTmStr(), TSecTm::GetCurTm().GetTmStr().CStr());
return 0;
}
示例13: TCNMQMatrix
TCNMQMatrix(const PUNGraph& Graph) : CmtyQH(Graph->GetNodes()),
MxQHeap(Graph->GetNodes(), 0), CmtyIdUF(Graph->GetNodes()) { Init(Graph); }
示例14: main
int main(int argc, char* argv[]) {
Env = TEnv(argc, argv, TNotify::StdNotify);
Env.PrepArgs(TStr::Fmt("cesna. 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:", "./1912.edges", "Input edgelist file name");
const TStr LabelFNm = Env.GetIfArgPrefixStr("-l:", "", "Input file name for node names (Node ID, Node label) ");
const TStr AttrFNm = Env.GetIfArgPrefixStr("-a:", "./1912.nodefeat", "Input node attribute file name");
const TStr ANameFNm = Env.GetIfArgPrefixStr("-n:", "./1912.nodefeatnames", "Input file name for node attribute names");
int OptComs = Env.GetIfArgPrefixInt("-c:", 10, "The number of communities to detect (-1: detect automatically)");
const int MinComs = Env.GetIfArgPrefixInt("-mc:", 3, "Minimum number of communities to try");
const int MaxComs = Env.GetIfArgPrefixInt("-xc:", 20, "Maximum number of communities to try");
const int DivComs = Env.GetIfArgPrefixInt("-nc:", 5, "How many trials for the number of communities");
const int NumThreads = Env.GetIfArgPrefixInt("-nt:", 4, "Number of threads for parallelization");
const double AttrWeight = Env.GetIfArgPrefixFlt("-aw:", 0.5, "We maximize (1 - aw) P(Network) + aw * P(Attributes)");
const double LassoWeight = Env.GetIfArgPrefixFlt("-lw:", 1.0, "Weight for l-1 regularization on learning the logistic model parameters");
const double StepAlpha = Env.GetIfArgPrefixFlt("-sa:", 0.05, "Alpha for backtracking line search");
const double StepBeta = Env.GetIfArgPrefixFlt("-sb:", 0.3, "Beta for backtracking line search");
const double MinFeatFrac = Env.GetIfArgPrefixFlt("-mf:", 0.0, "If the fraction of nodes with positive values for an attribute is smaller than this, we ignore that attribute");
#ifdef USE_OPENMP
omp_set_num_threads(NumThreads);
#endif
PUNGraph G;
TIntStrH NIDNameH;
TStrHash<TInt> NodeNameH;
TVec<TFltV> Wck;
TVec<TIntV> EstCmtyVV;
if (InFNm.IsStrIn(".ungraph")) {
TFIn GFIn(InFNm);
G = TUNGraph::Load(GFIn);
} else {
G = TAGMUtil::LoadEdgeListStr<PUNGraph>(InFNm, NodeNameH);
NIDNameH.Gen(NodeNameH.Len());
for (int s = 0; s < NodeNameH.Len(); s++) { NIDNameH.AddDat(s, NodeNameH.GetKey(s)); }
}
if (LabelFNm.Len() > 0) {
TSsParser Ss(LabelFNm, ssfTabSep);
while (Ss.Next()) {
if (Ss.Len() > 0) { NIDNameH.AddDat(Ss.GetInt(0), Ss.GetFld(1)); }
}
}
printf("Graph: %d Nodes %d Edges\n", G->GetNodes(), G->GetEdges());
//load attribute
TIntV NIDV;
G->GetNIdV(NIDV);
THash<TInt, TIntV> RawNIDAttrH, NIDAttrH;
TIntStrH RawFeatNameH, FeatNameH;
if (ANameFNm.Len() > 0) {
TSsParser Ss(ANameFNm, ssfTabSep);
while (Ss.Next()) {
if (Ss.Len() > 0) { RawFeatNameH.AddDat(Ss.GetInt(0), Ss.GetFld(1)); }
}
}
TCesnaUtil::LoadNIDAttrHFromNIDKH(NIDV, AttrFNm, RawNIDAttrH, NodeNameH);
TCesnaUtil::FilterLowEntropy(RawNIDAttrH, NIDAttrH, RawFeatNameH, FeatNameH, MinFeatFrac);
TExeTm RunTm;
TCesna CS(G, NIDAttrH, 10, 10);
if (OptComs == -1) {
printf("finding number of communities\n");
OptComs = CS.FindComs(NumThreads, MaxComs, MinComs, DivComs, "", false, 0.1, StepAlpha, StepBeta);
}
CS.NeighborComInit(OptComs);
CS.SetWeightAttr(AttrWeight);
CS.SetLassoCoef(LassoWeight);
if (NumThreads == 1 || G->GetEdges() < 1000) {
CS.MLEGradAscent(0.0001, 1000 * G->GetNodes(), "", StepAlpha, StepBeta);
} else {
CS.MLEGradAscentParallel(0.0001, 1000, NumThreads, "", StepAlpha, StepBeta);
}
CS.GetCmtyVV(EstCmtyVV, Wck);
TAGMUtil::DumpCmtyVV(OutFPrx + "cmtyvv.txt", EstCmtyVV, NIDNameH);
FILE* F = fopen((OutFPrx + "weights.txt").CStr(), "wt");
if (FeatNameH.Len() == Wck[0].Len()) {
fprintf(F, "#");
for (int k = 0; k < FeatNameH.Len(); k++) {
fprintf(F, "%s", FeatNameH[k].CStr());
if (k < FeatNameH.Len() - 1) { fprintf(F, "\t"); }
}
fprintf(F, "\n");
}
for (int c = 0; c < Wck.Len(); c++) {
for (int k = 0; k < Wck[c].Len(); k++) {
fprintf(F, "%f", Wck[c][k].Val);
if (k < Wck[c].Len() - 1) { fprintf(F, "\t"); }
}
fprintf(F, "\n");
}
fclose(F);
Catch
printf("\nrun time: %s (%s)\n", ExeTm.GetTmStr(), TSecTm::GetCurTm().GetTmStr().CStr());
//.........这里部分代码省略.........
示例15: main
int main(int argc, char* argv[]) {
Env = TEnv(argc, argv, TNotify::StdNotify);
Env.PrepArgs(TStr::Fmt("Memetracker Converter. Build: %s, %s. Time: %s",
__TIME__, __DATE__, TExeTm::GetCurTm()), 1, true);
const TStr netinfInputFile = Env.GetIfArgPrefixStr(
"-netinfInputFile=",
"data/memetracker/InfoNet5000Q1000NEXP.txt",
"File containing the inferred network, used as underlying structure");
const TStr clusteredCascadesInputFile = Env.GetIfArgPrefixStr(
"-clusteredCascadesInputFile=",
"data/memetracker/clust-qt08080902w3mfq5.txt",
"File containing clustered instances of memes and their path on the web");
const TInt cascadeInputLine = Env.GetIfArgPrefixInt(
"-cascadeInputLine=", 75926,
"Specific cascade to read from clusteredCascadesInputFile");
const TStr snapNetworkOutputFile = Env.GetIfArgPrefixStr(
"-snapNetworkOutputFile=", "data/memetracker/memeNetwork.dat",
"File where to write the output network as a snap binary");
const TStr groundTruthOutputFile = Env.GetIfArgPrefixStr(
"-groundTruthOutputFile=", "data/memetracker/memeGroundTruth.txt",
"File where to write the ground truth");
// Build the network from the most popular websites.
ifstream inputfile(netinfInputFile.CStr());
string line;
getline(inputfile, line); // read header line from file
map<string, unsigned int> urlToNodeIdHash;
PUNGraph graph = PUNGraph::New();
unsigned int nodeId = 0;
while (getline(inputfile, line)) {
istringstream iss(line);
string idx, src, dst;
iss >> idx >> src >> dst;
if (urlToNodeIdHash.find(src) == urlToNodeIdHash.end()) {
urlToNodeIdHash[src] = nodeId;
graph->AddNode(nodeId);
nodeId++;
}
if (urlToNodeIdHash.find(dst) == urlToNodeIdHash.end()) {
urlToNodeIdHash[dst] = nodeId;
graph->AddNode(nodeId);
nodeId++;
}
graph->AddEdge(urlToNodeIdHash[src], urlToNodeIdHash[dst]);
}
// Save network.
{ TFOut FOut(snapNetworkOutputFile); graph->Save(FOut); }
// Read one memetracker entry.
ifstream memetracker(clusteredCascadesInputFile.CStr());
for (int i = 0; i < cascadeInputLine; ++i)
getline(memetracker, line);
getline(memetracker, line);
int entries, dummyInt;
istringstream iss(line);
iss >> dummyInt >> entries;
cout << "Building cascade for ";
while (!iss.eof()) {
string phrase;
iss >> phrase;
cout << phrase << " ";
}
cout << endl;
// Dump cascade to some file.
ofstream dumpStream;
dumpStream.open(groundTruthOutputFile.CStr());
string dummy, url;
map<string, unsigned int> infectionTimeHash;
unsigned int infectionTime = 0;
for (int i = 0; i < entries; ++i) {
// Read through each "infected" URL.
getline(memetracker, line);
istringstream iss(line);
// These fields of the cascade entry ar not important.
iss >> dummy >> dummy >> dummy >> dummy;
iss >> url;
// Parse the URL and identify the host website.
uri::uri instance(url);
assert(instance.is_valid());
// If node not in network or already infected, skip.
if (urlToNodeIdHash.find(instance.host()) == urlToNodeIdHash.end() ||
infectionTimeHash.find(instance.host()) != infectionTimeHash.end())
continue;
infectionTimeHash[instance.host()] = infectionTime++;
// Dump as pair of <nodeId, infectionTime>.
dumpStream << urlToNodeIdHash[instance.host()] << " " <<
infectionTimeHash[instance.host()] << endl;
}
return 0;
}