本文整理汇总了C++中PUNGraph::AddEdge方法的典型用法代码示例。如果您正苦于以下问题:C++ PUNGraph::AddEdge方法的具体用法?C++ PUNGraph::AddEdge怎么用?C++ PUNGraph::AddEdge使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类PUNGraph
的用法示例。
在下文中一共展示了PUNGraph::AddEdge方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: GetSmallGraph
//Graph: 3--0--4
// /|
// 1-2
PUNGraph TUNGraph::GetSmallGraph() {
PUNGraph Graph = TUNGraph::New();
for (int i = 0; i < 5; i++) { Graph->AddNode(i); }
Graph->AddEdge(0,1); Graph->AddEdge(0,2);
Graph->AddEdge(0,3); Graph->AddEdge(0,4);
Graph->AddEdge(1,2);
return Graph;
}
示例2: main
int main(int argc, char* argv[]) {
Env = TEnv(argc, argv, TNotify::StdNotify);
Env.PrepArgs(TStr::Fmt("Clique Percolation Method. build: %s, %s. Time: %s", __TIME__, __DATE__, TExeTm::GetCurTm()));
TExeTm ExeTm;
Try
const TStr InFNm = Env.GetIfArgPrefixStr("-i:", "../as20graph.txt", "Input undirected graph file (single directed edge per line)");
const int OverlapSz = Env.GetIfArgPrefixInt("-k:", 2, "Min clique overlap");
TStr OutFNm = Env.GetIfArgPrefixStr("-o:", "", "Output file prefix");
if (OutFNm.Empty()) { OutFNm = InFNm.GetFMid(); }
PUNGraph G;
if (InFNm == "DEMO") { // small demo graph
G = TUNGraph::New();
for (int i = 1; i < 8; i++) { G->AddNode(i); }
G->AddEdge(1,2);
G->AddEdge(2,3); G->AddEdge(2,4);
G->AddEdge(3,4);
G->AddEdge(4,5); G->AddEdge(4,7);
G->AddEdge(5,6); G->AddEdge(5,7);
G->AddEdge(6,7);
// draw the small graph using GraphViz
TSnap::DrawGViz(G, gvlNeato, "small_graph.png", "", true);
}
// load graph
else if (InFNm.GetFExt().GetLc()==".ungraph") {
TFIn FIn(InFNm); G=TUNGraph::Load(FIn); }
else if (InFNm.GetFExt().GetLc()==".ngraph") {
TFIn FIn(InFNm); G=TSnap::ConvertGraph<PUNGraph>(TNGraph::Load(FIn), false); }
else {
G = TSnap::LoadEdgeList<PUNGraph>(InFNm, 0, 1); }
// find communities
TVec<TIntV> CmtyV;
TCliqueOverlap::GetCPMCommunities(G, OverlapSz+1, CmtyV);
// save result
FILE *F = fopen(TStr::Fmt("cpm-%s.txt", OutFNm.CStr()).CStr(), "wt");
fprintf(F, "# %d Overlapping Clique Percolation Communities (min clique overlap %d)\n", CmtyV.Len(), OverlapSz);
fprintf(F, "# Each line contains nodes belonging to the same community community\n");
for (int i = 0; i < CmtyV.Len(); i++) {
fprintf(F, "%d", CmtyV[i][0].Val);
for (int j = 1; j < CmtyV[i].Len(); j++) {
fprintf(F, "\t%d", CmtyV[i][j].Val);
}
fprintf(F, "\n");
}
Catch
printf("\nrun time: %s (%s)\n", ExeTm.GetTmStr(), TSecTm::GetCurTm().GetTmStr().CStr());
return 0;
}
示例3: GetTestTUNGraph
// Generate TUNGraph
PUNGraph GetTestTUNGraph() {
PUNGraph Graph = TUNGraph::New();
for (int i = 0; i < 20; i++) {
Graph->AddNode(i);
}
for (int i = 0; i < 20; i++) {
Graph->AddEdge(i,(i+1) % 20);
Graph->AddEdge(i,(i+2) % 20);
Graph->AddEdge(i,(i+3) % 20);
}
return Graph;
}
示例4: GetActorGraph
// actors collaboration graph
PUNGraph TImdbNet::GetActorGraph() const {
TIntPrSet EdgeSet;
for (TNodeI NI = BegNI(); NI < EndNI(); NI++) {
if (NI().GetTy() == mtyActor) {
const int NId1 = NI.GetId();
for (int e = 0; e < NI.GetOutDeg(); e++) {
if (NI.GetOutNDat(e).GetTy() != mtyActor) {
TNodeI NI2 = GetNI(NI.GetOutNId(e));
for (int e2 = 0; e2 < NI2.GetInDeg(); e2++) {
if (NI2.GetInNDat(e2).GetTy() == mtyActor) {
const int NId2 = NI2.GetInNId(e2);
EdgeSet.AddKey(TIntPr(TMath::Mn(NId1, NId2), TMath::Mx(NId1, NId2)));
}
}
}
}
}
}
PUNGraph G = TUNGraph::New();
for (int i = 0; i < EdgeSet.Len(); i++) {
const int NId1 = EdgeSet[i].Val1;
const int NId2 = EdgeSet[i].Val2;
if (! G->IsNode(NId1)) { G->AddNode(NId1); }
if (! G->IsNode(NId2)) { G->AddNode(NId2); }
G->AddEdge(NId1, NId2);
}
return G;
}
示例5: Get1CnCom
// get the node ids in 1-connected components
void Get1CnCom(const PUNGraph& Graph, TCnComV& Cn1ComV) {
//TCnCom::GetWccCnt(Graph, SzCntV); IAssertR(SzCntV.Len() == 1, "Graph is not connected.");
TIntPrV EdgeV;
GetEdgeBridges(Graph, EdgeV);
if (EdgeV.Empty()) { Cn1ComV.Clr(false); return; }
PUNGraph TmpG = TUNGraph::New();
*TmpG = *Graph;
for (int e = 0; e < EdgeV.Len(); e++) {
TmpG->DelEdge(EdgeV[e].Val1, EdgeV[e].Val2); }
TCnComV CnComV; GetWccs(TmpG, CnComV);
IAssert(CnComV.Len() >= 2);
const TIntV& MxWcc = CnComV[0].NIdV;
TIntSet MxCcSet(MxWcc.Len());
for (int i = 0; i < MxWcc.Len(); i++) {
MxCcSet.AddKey(MxWcc[i]); }
// create new graph: bridges not touching MxCc of G with no bridges
for (int e = 0; e < EdgeV.Len(); e++) {
if (! MxCcSet.IsKey(EdgeV[e].Val1) && ! MxCcSet.IsKey(EdgeV[e].Val2)) {
TmpG->AddEdge(EdgeV[e].Val1, EdgeV[e].Val2); }
}
GetWccs(TmpG, Cn1ComV);
// remove the largest component of G
for (int c = 0; c < Cn1ComV.Len(); c++) {
if (MxCcSet.IsKey(Cn1ComV[c].NIdV[0])) {
Cn1ComV.Del(c); break; }
}
}
示例6: GenPy
int GenPy(PUNGraph &res, ofstream& TFile, const TStr& parameters)
{
Env = TEnv(parameters, TNotify::StdNotify);
TStr mN = Env.GetIfArgPrefixStr("-module:", "random_graphs", "Module name");
TStr fN = Env.GetIfArgPrefixStr("-func:", "fast_gnp_random_graph", "Function name");
PyObject **G = new PyObject*[1];
char *moduleName = mN.CStr();
char *funcName = fN.CStr();
AddFuncInfo();
TStrV args, argTypes;
if (!ParseArgs(funcName, parameters, args, argTypes))
{
printf("Fail to parse arguments for NetworkX generation...\n");
return 0;
};
TExeTm execTime;
if (!CallPyFunction(moduleName, funcName, args, argTypes, G))
{
cout << "CallPyFunction() raised error. Execution terminated.\n";
system("pause");
exit(1);
};
TFile << "Time of generation of graph by NetworkX: " << execTime.GetTmStr() << endl;
execTime.Tick();
PyObject*** nodes = new PyObject**[1];
GetNodes(G, nodes);
int nodesCount = PyList_Size(*(nodes[0]));
//printf("nodesCount = %d, ", nodesCount);
res = PUNGraph::TObj::New();
res->Reserve(nodesCount, nodesCount*nodesCount);
for (size_t i = 0; i < nodesCount; i++)
res->AddNode(i);
Py_DECREF(nodes);
PyObject*** edges = new PyObject**[1];
GetEdges(G, edges);
int edgesCount = PyList_Size(*(edges[0]));
//printf("edgesCount = %d\n", edgesCount);
for (size_t i = 0; i < edgesCount; i++)
{
PyObject* item = PySequence_Fast_GET_ITEM(*(edges[0]), i);
int v1, v2;
PyObject* node = PySequence_Fast_GET_ITEM(item,0);
v1 = PyLong_AsLong(node);
node = PySequence_Fast_GET_ITEM(item,1);
v2 = PyLong_AsLong(node);
res->AddEdge(v1,v2);
}
TFile << "Time of copying of graph from NetworkX representation: " << execTime.GetTmStr() << endl;
Py_DECREF(G);
Py_DECREF(edges);
//Py_Finalize(); // очищение памяти, отданной интерпретатору
return 0;
}
示例7: TriadGetTestTUNGraph
// Generate TUNGraph
PUNGraph TriadGetTestTUNGraph() {
PUNGraph Graph = TUNGraph::New();
for (int i = 0; i < 6; i++) {
Graph->AddNode(i);
}
for (int i = 1; i < 6; i++) {
Graph->AddEdge(0, i);
}
Graph->AddEdge(2, 3);
Graph->AddEdge(1, 5);
Graph->AddEdge(2, 5);
return Graph;
}
示例8: get_PUNGraph
PUNGraph get_PUNGraph (const int *m, const int nval, const int nodes) {
PUNGraph g = PUNGraph::New ();
for (int i = 1; i<= nodes; i++) {
g->AddNode(i);
}
for (int i =0; i < nval; i++) {
g->AddEdge(m[i], m[nval + i]);
}
return g;
}
示例9: RndConnectInsideCommunity
void TAGM::RndConnectInsideCommunity(PUNGraph& Graph, const TIntV& CmtyV, const double& Prob, TRnd& Rnd){
int CNodes = CmtyV.Len();
int CEdges = Rnd.GetBinomialDev(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++;
}
}
}
示例10: 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;
}
示例11: UndirCopy
void UndirCopy(PNGraph& dir_graph, PUNGraph& undir_graph) {
// Add all of the nodes into the new graph
for (TNGraph::TNodeI node = dir_graph->BegNI(); node < dir_graph->EndNI();
node++) {
int curr_node = node.GetId();
undir_graph->AddNode(curr_node);
}
for (TNGraph::TNodeI node = dir_graph->BegNI(); node < dir_graph->EndNI();
node++) {
int curr_node = node.GetId();
for (int e = 0; e < node.GetOutDeg(); ++e) {
int nbr_node = node.GetOutNId(e);
if (!undir_graph->IsEdge(curr_node, nbr_node)) {
undir_graph->AddEdge(curr_node, nbr_node);
}
}
}
}
示例12: CalculateOverlapMtx
PUNGraph TCliqueOverlap::CalculateOverlapMtx(const TVec<TIntV>& MaxCliques, int MinNodeOverlap) {
const int n = MaxCliques.Len();
//Convert max cliques to HashSets
TVec<THashSet<TInt> > cliques;
for (int i=0; i<n; i++) {
const int len = MaxCliques[i].Len();
cliques.Add();
if (len < MinNodeOverlap) { continue; }
THashSet<TInt>& set = cliques.Last(); set.Gen(len);
for (int j=0; j<len; j++) { set.AddKey(MaxCliques[i][j]); }
}
//Init clique clique overlap matrix
PUNGraph OverlapMtx = TUNGraph::New();
for (int i=0; i < n; i++) {
OverlapMtx->AddNode(i); }
//Calculate clique clique overlap matrix
for (int i=0; i<n; i++) {
for (int j=i+1; j<n; j++) {
if (Intersection(cliques[i], cliques[j]) >= MinNodeOverlap) {
OverlapMtx->AddEdge(i,j); }
}
}
return OverlapMtx;
}
示例13: ManipulateNodesEdges
// Test node, edge creation
void ManipulateNodesEdges() {
int NNodes = 10000;
int NEdges = 100000;
const char *FName = "test.graph";
PUNGraph Graph;
PUNGraph Graph1;
PUNGraph Graph2;
int i;
int n;
int NCount;
int ECount1;
int ECount2;
int x,y;
bool t;
Graph = TUNGraph::New();
t = Graph->Empty();
// create the nodes
for (i = 0; i < NNodes; i++) {
Graph->AddNode(i);
}
t = Graph->Empty();
n = Graph->GetNodes();
// create random edges
NCount = NEdges;
while (NCount > 0) {
x = (long) (drand48() * NNodes);
y = (long) (drand48() * NNodes);
// Graph->GetEdges() is not correct for the loops (x == y),
// skip the loops in this test
if (x != y && !Graph->IsEdge(x,y)) {
n = Graph->AddEdge(x, y);
NCount--;
}
}
PrintGStats("ManipulateNodesEdges:Graph",Graph);
// get all the nodes
NCount = 0;
for (TUNGraph::TNodeI NI = Graph->BegNI(); NI < Graph->EndNI(); NI++) {
NCount++;
}
// get all the edges for all the nodes
ECount1 = 0;
for (TUNGraph::TNodeI NI = Graph->BegNI(); NI < Graph->EndNI(); NI++) {
for (int e = 0; e < NI.GetOutDeg(); e++) {
ECount1++;
}
}
ECount1 /= 2;
// get all the edges directly
ECount2 = 0;
for (TUNGraph::TEdgeI EI = Graph->BegEI(); EI < Graph->EndEI(); EI++) {
ECount2++;
}
printf("graph ManipulateNodesEdges:Graph, nodes %d, edges1 %d, edges2 %d\n",
NCount, ECount1, ECount2);
// assignment
Graph1 = TUNGraph::New();
*Graph1 = *Graph;
PrintGStats("ManipulateNodesEdges:Graph1",Graph1);
// save the graph
{
TFOut FOut(FName);
Graph->Save(FOut);
FOut.Flush();
}
// load the graph
{
TFIn FIn(FName);
Graph2 = TUNGraph::Load(FIn);
}
PrintGStats("ManipulateNodesEdges:Graph2",Graph2);
// remove all the nodes and edges
for (i = 0; i < NNodes; i++) {
n = Graph->GetRndNId();
Graph->DelNode(n);
}
PrintGStats("ManipulateNodesEdges:Graph",Graph);
Graph1->Clr();
PrintGStats("ManipulateNodesEdges:Graph1",Graph1);
}
示例14: 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;
}
示例15: FOut
// Test node, edge creation
TEST(TUNGraph, ManipulateNodesEdges) {
int NNodes = 10000;
int NEdges = 100000;
const char *FName = "test.graph";
PUNGraph Graph;
PUNGraph Graph1;
PUNGraph Graph2;
int i;
int n;
int NCount;
int x,y;
int Deg, InDeg, OutDeg;
Graph = TUNGraph::New();
EXPECT_EQ(1,Graph->Empty());
// create the nodes
for (i = 0; i < NNodes; i++) {
Graph->AddNode(i);
}
EXPECT_EQ(0,Graph->Empty());
EXPECT_EQ(NNodes,Graph->GetNodes());
// create random edges
NCount = NEdges;
while (NCount > 0) {
x = (long) (drand48() * NNodes);
y = (long) (drand48() * NNodes);
// Graph->GetEdges() is not correct for the loops (x == y),
// skip the loops in this test
if (x != y && !Graph->IsEdge(x,y)) {
n = Graph->AddEdge(x, y);
NCount--;
}
}
EXPECT_EQ(NEdges,Graph->GetEdges());
EXPECT_EQ(0,Graph->Empty());
EXPECT_EQ(1,Graph->IsOk());
for (i = 0; i < NNodes; i++) {
EXPECT_EQ(1,Graph->IsNode(i));
}
EXPECT_EQ(0,Graph->IsNode(NNodes));
EXPECT_EQ(0,Graph->IsNode(NNodes+1));
EXPECT_EQ(0,Graph->IsNode(2*NNodes));
// nodes iterator
NCount = 0;
for (TUNGraph::TNodeI NI = Graph->BegNI(); NI < Graph->EndNI(); NI++) {
NCount++;
}
EXPECT_EQ(NNodes,NCount);
// edges per node iterator
NCount = 0;
for (TUNGraph::TNodeI NI = Graph->BegNI(); NI < Graph->EndNI(); NI++) {
for (int e = 0; e < NI.GetOutDeg(); e++) {
NCount++;
}
}
EXPECT_EQ(NEdges*2,NCount);
// edges iterator
NCount = 0;
for (TUNGraph::TEdgeI EI = Graph->BegEI(); EI < Graph->EndEI(); EI++) {
NCount++;
}
EXPECT_EQ(NEdges,NCount);
// node degree
for (TUNGraph::TNodeI NI = Graph->BegNI(); NI < Graph->EndNI(); NI++) {
Deg = NI.GetDeg();
InDeg = NI.GetInDeg();
OutDeg = NI.GetOutDeg();
EXPECT_EQ(Deg,InDeg);
EXPECT_EQ(Deg,OutDeg);
}
// assignment
Graph1 = TUNGraph::New();
*Graph1 = *Graph;
EXPECT_EQ(NNodes,Graph1->GetNodes());
EXPECT_EQ(NEdges,Graph1->GetEdges());
EXPECT_EQ(0,Graph1->Empty());
EXPECT_EQ(1,Graph1->IsOk());
// saving and loading
{
TFOut FOut(FName);
Graph->Save(FOut);
FOut.Flush();
}
//.........这里部分代码省略.........