本文整理汇总了C++中PNGraph类的典型用法代码示例。如果您正苦于以下问题:C++ PNGraph类的具体用法?C++ PNGraph怎么用?C++ PNGraph使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了PNGraph类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: GetSmallGraph
PNGraph TNGraph::GetSmallGraph() {
PNGraph G = TNGraph::New();
for (int i = 0; i < 5; i++) { G->AddNode(i); }
G->AddEdge(0,1); G->AddEdge(1,2); G->AddEdge(0,2);
G->AddEdge(1,3); G->AddEdge(3,4); G->AddEdge(2,3);
return G;
}
示例2: LoadDyNetGraphV
// DyNetML format, loads all the networks in the file
TVec<PNGraph> LoadDyNetGraphV(const TStr& FNm) {
TXmlLx XmlLx(TFIn::New(FNm), xspTruncate);
TVec<PNGraph> GraphV;
THashSet<TStr> NIdStr;
while (XmlLx.GetSym()!=xsyEof) {
if (XmlLx.Sym==xsySTag && XmlLx.TagNm=="network") {
PNGraph G = TNGraph::New();
GraphV.Add(G);
XmlLx.GetSym();
while (XmlLx.TagNm=="link") {
TStr Str1, Val1, Str2, Val2;
XmlLx.GetArg(0, Str1, Val1); XmlLx.GetArg(1, Str2, Val2);
IAssert(Str1=="source" && Str2=="target");
NIdStr.AddKey(Val1); NIdStr.AddKey(Val2);
const int src=NIdStr.GetKeyId(Val1);
const int dst=NIdStr.GetKeyId(Val2);
if (! G->IsNode(src)) { G->AddNode(src); }
if (! G->IsNode(dst)) { G->AddNode(dst); }
G->AddEdge(src, dst);
XmlLx.GetSym();
}
}
}
return GraphV;
}
示例3: Add
void TGStatVec::Add(const PNGraph& Graph, const TSecTm& Time, const TStr& GraphNm) {
if (Graph->GetNodes() < (int) TGStatVec::MinNodesEdges) {
printf(" ** TGStatVec::Add: graph too small (%d nodes).SKIP\n", Graph->GetNodes());
return;
}
Add(TGStat::New(Graph, Time, StatFSet, GraphNm));
}
示例4: TEST
// Test GetLen2Paths: Number of path lengths 2 between pair of nodes
TEST(triad, TestGetLen2Paths) {
// Test TUNGraph
PUNGraph GraphTUN = TriadGetTestTUNGraph();
for (int i = 0; i < GraphTUN->GetNodes(); i++) {
for (int j = i + 1; j < GraphTUN->GetNodes(); j++) {
VerifyLen2Paths(i, j, TSnap::GetLen2Paths(GraphTUN, i, j), 0);
}
}
// Test TNGraph which is different from undirected due to out neighbors.
PNGraph GraphTN = TriadGetTestTNGraph();
for (int i = 0; i < GraphTN->GetNodes(); i++) {
for (int j = i + 1; j < GraphTN->GetNodes(); j++) {
VerifyLen2Paths(i, j, TSnap::GetLen2Paths(GraphTN, i, j), 1);
}
}
// Test TNEGraph which is different from undirected due to out neighbors.
PNEGraph GraphTNE = TriadGetTestTNEGraph();
for (int i = 0; i < GraphTNE->GetNodes(); i++) {
for (int j = i + 1; j < GraphTNE->GetNodes(); j++) {
VerifyLen2Paths(i, j, TSnap::GetLen2Paths(GraphTNE, i, j), 2);
}
}
}
示例5: TEST
// Test edge subgraph conversion
TEST(subgraph, TestConvertESubGraphs) {
PNEGraph NEGraph;
PNGraph NGraph;
TIntV NIdV;
TIntV EIdV;
int i;
NGraph = GetTestTNGraph();
EXPECT_EQ(20,NGraph->GetNodes());
EXPECT_EQ(60,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);
NEGraph = TSnap::ConvertGraph<PNEGraph>(NGraph);
EXPECT_EQ(20,NEGraph->GetNodes());
EXPECT_EQ(60,NEGraph->GetEdges());
// select every second edge
i = 0;
for (TNEGraph::TEdgeI EI = NEGraph->BegEI(); EI < NEGraph->EndEI(); EI++) {
if (i == 0) {
EIdV.Add(EI.GetId());
}
i = (i + 1) % 2;
}
NGraph = TSnap::ConvertESubGraph<PNGraph>(NEGraph, EIdV);
EXPECT_EQ(20,NGraph->GetNodes());
EXPECT_EQ(30,NGraph->GetEdges());
}
示例6: printf
void TGStat::TakeStat(const PNGraph& Graph, const TSecTm& _Time, TFSet StatFSet, const TStr& GraphName) {
printf("\n===TakeStat: G(%u, %u)\n", Graph->GetNodes(), Graph->GetEdges());
TExeTm ExeTm, FullTm;
Time = _Time;
GraphNm = GraphName;
if (StatFSet.In(gsvNone)) { return; }
TakeBasicStat(Graph, false);
TakeDiam(Graph, StatFSet, false);
if (StatFSet.In(gsdWcc) || StatFSet.In(gsdWccHops) || StatFSet.In(gsvFullDiam) || StatFSet.In(gsvEffWccDiam)) {
PNGraph WccGraph = TSnap::GetMxWcc(Graph);
TakeBasicStat(WccGraph, true);
TakeDiam(WccGraph, StatFSet, true);
}
// degrees
TakeDegDistr(Graph, StatFSet);
// components
TakeConnComp(Graph, StatFSet);
// spectral
TakeSpectral(Graph, StatFSet, -1);
// clustering coeffient
if (StatFSet.In(gsdClustCf) || StatFSet.In(gsvClustCf)) {
TakeClustCf(Graph); }
if (StatFSet.In(gsdTriadPart)) {
TakeTriadPart(Graph); }
printf(" [%s]\n", FullTm.GetTmStr());
}
示例7: getNumOfPathsFromVect
void getNumOfPathsFromVect(const PNGraph& graph, std::vector<int> srcIds, int srcSampleSz, std::vector<int> dstIds, int dstSampleSz, char* fileName) {
std::random_shuffle(srcIds.begin(), srcIds.end());
std::random_shuffle(dstIds.begin(), dstIds.end());
std::ofstream outputFile;
outputFile.open(fileName);
for (int i = 0; i < srcIds.size() && i < srcSampleSz; ++i) {
int srcNodeId = srcIds[i];
if (!graph->IsNode(srcNodeId)) continue;
for (int j = 0; j < dstIds.size() && j < dstSampleSz; ++j) {
int dstNodeId = dstIds[j];
if (!graph->IsNode(dstNodeId)) continue;
int shortPath = TSnap::GetShortPath(graph, srcNodeId, dstNodeId, true);
if (shortPath > 4 || shortPath <= 2) continue;
int numOfPaths = getNumOfIndependentPaths(graph, srcNodeId, dstNodeId);
char buffer[100];
sprintf(buffer, "%d\t%d\t%d", srcNodeId, dstNodeId, numOfPaths);
std::cout << buffer << std::endl;
outputFile << buffer << std::endl;
}
}
outputFile.close();
}
示例8: GetEgonet
PNGraph GetEgonet(const PNGraph& Graph, const int CtrNId, int& InEdges, int& OutEdges) {
PNGraph NewGraphPt = TNGraph::New();
TNGraph& NewGraph = *NewGraphPt;
NewGraph.AddNode(CtrNId);
const TNGraph::TNodeI& CtrNode = Graph->GetNI(CtrNId);
for (int i = 0; i < CtrNode.GetDeg(); ++i) {
NewGraph.AddNode(CtrNode.GetNbrNId(i));
}
InEdges = 0;
OutEdges = 0;
for (int i = 0; i < CtrNode.GetDeg(); ++i) {
int NbrNId = CtrNode.GetNbrNId(i);
const TNGraph::TNodeI& NbrNode = Graph->GetNI(NbrNId);
for (int j = 0; j < NbrNode.GetInDeg(); ++j) {
int NbrNbrNId = NbrNode.GetInNId(j);
if (NewGraph.IsNode(NbrNbrNId)) {
NewGraph.AddEdge(NbrNbrNId, NbrNId);
} else {
InEdges++;
}
}
for (int j = 0; j < NbrNode.GetOutDeg(); ++j) {
int NbrNbrNId = NbrNode.GetOutNId(j);
if (!NewGraph.IsNode(NbrNbrNId)) {
OutEdges++;
}
}
}
return NewGraphPt;
}
示例9: getDistance
void getDistance(const PNGraph& graph, std::vector<int> srcIds, std::vector<int> dstIds, int sampleSize, TFltPrV& ret) {
std::random_shuffle(srcIds.begin(), srcIds.end());
std::random_shuffle(dstIds.begin(), dstIds.end());
int distance[20];
for (int i = 0; i < 20; distance[i++] = 0);
int sampleCount = 0;
for (int i = 0; i < srcIds.size(); ++i) {
int srcNodeId = srcIds[i];
if (!graph->IsNode(srcNodeId)) continue;
for (int j = 0; j < dstIds.size(); ++j) {
int dstNodeId = dstIds[j];
if (!graph->IsNode(dstNodeId)) continue;
int shortDist = TSnap::GetShortPath(graph, srcNodeId, dstNodeId, true);
distance[shortDist]++;
sampleCount++;
printIntArray(distance, 20);
}
if (sampleCount > sampleSize) break;
}
for (int i = 0; i < 20; ++i) {
ret.Add(TFltPr(i, distance[i]));
}
}
示例10: getNumOfIndependentPaths
int getNumOfIndependentPaths(const PNGraph& graph, int srcNodeID, int dstNodeID) {
int ret = 0;
while (true) {
PNGraph bfsGraph = TSnap::GetBfsTree(graph, srcNodeID, true, false);
if (!bfsGraph->IsNode(dstNodeID)) {
return ret;
}
printf("%d hops\n", TSnap::GetShortPath(bfsGraph, srcNodeID, dstNodeID, true));
// Go back from dstNode to src
int itrNodeId = dstNodeID;
while (itrNodeId != srcNodeID) {
TNGraph::TNodeI curNode = bfsGraph->GetNI(itrNodeId);
int parentNodeId = curNode.GetInNId(0);
// Delete Edges
// graph->DelEdge(parentNodeId, itrNodeId, true);
// Delete Node
if (itrNodeId != dstNodeID && itrNodeId != srcNodeID) {
graph->DelNode(itrNodeId);
}
itrNodeId = parentNodeId;
}
++ret;
}
}
示例11: 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");
}
示例12: CheckReciprocity
bool CheckReciprocity(const PNGraph& G){
for (int i = 0; i < G->GetNodes(); i++){
if (G->GetNI(i).GetInDeg() != G->GetNI(i).GetOutDeg())
return false;
}
return true;
}
示例13: MinSup
/////////////////////////////////////////////////
// Trawling the web for emerging communities
// graph, left points to right
TTrawling::TTrawling(const PNGraph& Graph, const int& MinSupport) : MinSup(MinSupport) {
TIntH ItemCntH;
for (TNGraph::TNodeI NI = Graph->BegNI(); NI < Graph->EndNI(); NI++) {
IAssert(NI.GetOutDeg()==0 || NI.GetInDeg()==0); // edges only point from left to right
if (NI.GetOutDeg()==0) { continue; }
for (int e = 0; e < NI.GetOutDeg(); e++) {
ItemCntH.AddDat(NI.GetOutNId(e)) += 1;
}
}
TIntV RightV;
for (TNGraph::TNodeI NI = Graph->BegNI(); NI < Graph->EndNI(); NI++) {
IAssert(NI.GetOutDeg()==0 || NI.GetInDeg()==0); // edges only point from left to right
if (NI.GetOutDeg()==0) { continue; }
RightV.Clr(false);
for (int e = 0; e < NI.GetOutDeg(); e++) {
const int itm = NI.GetOutNId(e);
// only include items that already are above minimum support
if (ItemCntH.GetDat(itm) >= MinSup) {
RightV.Add(itm); }
}
if (! RightV.Empty()) {
NIdSetH.AddDat(NI.GetId(), RightV);
}
}
//
for (int n = 0; n < NIdSetH.Len(); n++) {
const TIntV& Set = NIdSetH[n];
for (int s = 0; s < Set.Len(); s++) {
SetNIdH.AddDat(Set[s]).Add(n);
}
}
}
示例14: analyzeSimNetProps
void analyzeSimNetProps() {
const char *eFName = "epidemicRoutingNetEdges.txt";
const char *pFName = "prophetRoutingNetEdges.txt";
PNGraph eGraph = TSnap::LoadEdgeListStr<PNGraph>(eFName, 0, 1);
PNEGraph pGraph = TSnap::LoadEdgeListStr<PNEGraph>(pFName, 0, 1);
PNGraph randGraph = TSnap::GenRndGnm<PNGraph>(eGraph->GetNodes(), eGraph->GetEdges(), true, TInt::Rnd);
chdir("dot");
for (int i=0; i<10; i++) {
TIntV NIdV;
for (int j = 0; j < 10; j++) {
int randNode = eGraph->GetRndNId();
NIdV.AddUnique(randNode);
}
// Plot the mesage propagtion in Endroy-Renyi graphs
PNGraph randFlow = TSnap::GetSubGraph<PNGraph>(randGraph, NIdV);
char randf[50]; sprintf(randf,"%d-erdos.dot",i);
TSnap::SaveGViz(randFlow, randf, TStr("Edros-Renyi random graph"));
// Now plot epidemic routing
PNGraph epidemicFlow = TSnap::GetSubGraph<PNGraph>(eGraph, NIdV);
char epf[50]; sprintf(epf,"%d-epidemic.dot",i);
TSnap::SaveGViz(epidemicFlow, epf, TStr("Epidemic routing"));
}
}
示例15: 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);
}