示例#1
0
文件: demo-gio.cpp 项目: Accio/snap
// Loads a directed network in the DyNetML format
void IODyNet() {
  
  const int NNodes = 500;
  const int NEdges = 2000;
  
  const char *FName = "demo.xml.dat";
  
  PNGraph GOut, GIn;
  GOut = GenRndGnm<PNGraph>(NNodes, NEdges);
  
  // Create XML graph file
  FILE *F = fopen(FName, "w");
  
  fprintf(F, "<network>\n");
  for (TNGraph::TEdgeI EI = GOut->BegEI(); EI < GOut->EndEI(); EI++) {
    TInt Src = EI.GetSrcNId();
    TInt Dst = EI.GetDstNId();
    fprintf(F, "\t<link source=\"%d\" target=\"%d\"/>\n", Src.Val, Dst.Val);
  }
  fprintf(F, "</network>\n");
  fclose(F);
  
  GIn = LoadDyNet(FName);
  
  PrintGStats("DyNet - Out", GOut);
  PrintGStats("DyNet - In", GIn);
}
示例#2
0
///////////////////////////////////////////////////////////////////////////////
// Two-node (static edge) counting methods
void TempMotifCounter::Count3TEdge2Node(double delta, Counter2D& counts) {
  // Get a vector of undirected edges (so we can use openmp parallel for over it)
  TVec<TIntPair> undir_edges;
  for (TNGraph::TEdgeI it = static_graph_->BegEI(); it < static_graph_->EndEI(); it++) {
    int src = it.GetSrcNId();
    int dst = it.GetDstNId();
    // Only consider undirected edges
    if (src < dst || (dst < src && !static_graph_->IsEdge(dst, src))) {
      undir_edges.Add(TIntPair(src, dst));
    }
  }
  counts = Counter2D(2, 2);
  #pragma omp parallel for schedule(dynamic)
  for (int i = 0; i < undir_edges.Len(); i++) {
    TIntPair edge = undir_edges[i];
    Counter3D local;
    Count3TEdge2Node(edge.Key, edge.Dat, delta, local);
    #pragma omp critical
    {
      counts(0, 0) += local(0, 1, 0) + local(1, 0, 1);  // M_{5,1}
      counts(0, 1) += local(1, 0, 0) + local(0, 1, 1);  // M_{5,2}
      counts(1, 0) += local(0, 0, 0) + local(1, 1, 1);  // M_{6,1}
      counts(1, 1) += local(0, 0, 1) + local(1, 1, 0);  // M_{6,2}
    }
  }
}
示例#3
0
void QuoteGraph::LogEdges(TStr FileName) {
  FILE *Q = fopen(FileName.CStr(), "w"); // Skyfall!!!!
  TNGraph::TEdgeI End = QGraph->EndEI();
  for (TNGraph::TEdgeI iter = QGraph->BegEI(); iter != End; iter++) {
    fprintf(Q, "%d\t%d\n", iter.GetSrcNId(), iter.GetDstNId());
  }
  fclose(Q);
}
示例#4
0
void gdf(PNGraph Graph){
        std::ofstream graph;
        graph.open("graph.gdf");
        graph << "nodedef>name VARCHAR\n";

        for (TNGraph::TNodeI NI = Graph->BegNI(); NI < Graph->EndNI(); NI++)
                graph << NI.GetId() << ",\n";
        graph << "edgedef>node1 VARCHAR,node2 VARCHAR\n";
        for (TNGraph::TEdgeI EI = Graph->BegEI(); EI < Graph->EndEI(); EI++)
                graph << EI.GetSrcNId() << "," << EI.GetDstNId() << "\n";
        graph.close();
}
示例#5
0
void TNetInfBs::SetModels(const double& minalpha, const double& maxalpha, const double& minbeta, const double& maxbeta) {
	if (GroundTruth->GetNodes() == 0) {
		printf("Ground truth must be generated before running SetModels!\n");
		return;
	}

	// assign a different alpha to every node
	for (TNGraph::TEdgeI EI = GroundTruth->BegEI(); EI < GroundTruth->EndEI(); EI++) {
		Alphas.AddDat(TIntPr(EI.GetSrcNId(), EI.GetDstNId())) = minalpha + (double)TFlt::Rnd.GetUniDev() * (maxalpha-minalpha);
		Betas.AddDat(TIntPr(EI.GetSrcNId(), EI.GetDstNId())) = minbeta + (double)TFlt::Rnd.GetUniDev() * (maxbeta-minbeta);

	// printf("Edge:(%d,%d) alpha:%f beta:%f\n", EI.GetSrcNId(), EI.GetDstNId(), Alphas.GetDat(TIntPr(EI.GetSrcNId(), EI.GetDstNId())).Val, Betas.GetDat(TIntPr(EI.GetSrcNId(), EI.GetDstNId())).Val);
	}
}
示例#6
0
void TNetInfBs::SavePlaneTextNet(const TStr& OutFNm) {
  TIntSet NIdSet;
  FILE *F = fopen(OutFNm.CStr(), "wt");
  for (THash<TInt, TNodeInfo>::TIter NI = NodeNmH.BegI(); NI < NodeNmH.EndI(); NI++) {
    fprintf(F, "%d,%d\r\n", NI.GetKey().Val, NI.GetKey().Val);
  }

  fprintf(F, "\r\n");

  for (TNGraph::TEdgeI EI = Graph->BegEI(); EI < Graph->EndEI(); EI++) {
    fprintf(F, "%d,%d\r\n", EI.GetSrcNId(), EI.GetDstNId());
  }
  fclose(F);
}
示例#7
0
void TNetInfBs::SavePajek(const TStr& OutFNm) {
    TIntSet NIdSet;
    FILE *F = fopen(OutFNm.CStr(), "wt");
    fprintf(F, "*Vertices %d\r\n", NIdSet.Len());
    for (THash<TInt, TNodeInfo>::TIter NI = NodeNmH.BegI(); NI < NodeNmH.EndI(); NI++) {
      const TNodeInfo& I = NI.GetDat();
      fprintf(F, "%d \"%s\" ic Blue x_fact %f y_fact %f\r\n", NI.GetKey().Val,
        I.Name.CStr(), TMath::Mx<double>(log((double)I.Vol)-5,1), TMath::Mx<double>(log((double)I.Vol)-5,1));
    }
    fprintf(F, "*Arcs\r\n");
    for (TNGraph::TEdgeI EI = Graph->BegEI(); EI < Graph->EndEI(); EI++) {
      fprintf(F, "%d %d 1\r\n", EI.GetSrcNId(), EI.GetDstNId());
    }
    fclose(F);
}
示例#8
0
void graphson(PNGraph Graph){
        std::ofstream graph;
        graph.open("graph.json");
        graph << "{\n";
        graph << "    \"graph\": {\n";
        graph << "        " << "\"mode\": \"NORMAL\", \n";
        graph << "        " << "\"vertices\": [\n";
        int i = 0;
        for (TNGraph::TNodeI NI = Graph->BegNI(); NI < Graph->EndNI(); NI++) {
                i++;
                graph << "            " << "{\n";
                graph << "                " << "\"_id\": \"" << NI.GetId() << "\",\n";
                graph << "                " << "\"_type\": \"vertex\"\n";
                if (i == Graph->GetNodes()) {
                        graph << "            " << "}\n";
                } else {
                        graph << "            " << "},\n";
                }
        }


        graph << "        " << "],\n";
        graph << "        " << "\"edges\": [\n";

        i = 0;
        printf("Edges: %d", Graph->GetEdges());
        for (TNGraph::TEdgeI EI = Graph->BegEI(); EI < Graph->EndEI(); EI++) {
                graph << "            " << "{\n"
                      << "                " << "\"_id\": \"" << i++ << "\",\n"
                      << "                " << "\"_type\": \"edge\"\n"
                      << "                " << "\"_outV\": \"" << EI.GetSrcNId() << "\"\n"
                      << "                " << "\"_inV\": \""<< EI.GetDstNId() << "\"\n";

                if (i == Graph->GetEdges())
                        graph << "            " << "}\n";
                else
                        graph << "            " << "},\n";

        }
        graph << "        " << "]\n";
        graph << "    " << "}\n";
        graph << "}\n";

        graph.close();
}
示例#9
0
文件: n2v.cpp 项目: jsw883/snap
void node2vec(PNGraph& InNet, double& ParamP, double& ParamQ, int& Dimensions,
 int& WalkLen, int& NumWalks, int& WinSize, int& Iter, bool& Verbose,
 TIntFltVH& EmbeddingsHV) {
  PWNet NewNet = PWNet::New();
  for (TNGraph::TEdgeI EI = InNet->BegEI(); EI < InNet->EndEI(); EI++) {
    if (!NewNet->IsNode(EI.GetSrcNId())) { NewNet->AddNode(EI.GetSrcNId()); }
    if (!NewNet->IsNode(EI.GetDstNId())) { NewNet->AddNode(EI.GetDstNId()); }
    NewNet->AddEdge(EI.GetSrcNId(), EI.GetDstNId(), 1.0);
  }
  node2vec(NewNet, ParamP, ParamQ, Dimensions, WalkLen, NumWalks, WinSize, Iter, 
   Verbose, EmbeddingsHV);
}
示例#10
0
void graphMl(PNGraph Graph){
        std::ofstream graphml;
        graphml.open("graph.graphml");

        graphml << "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n";
        graphml << "<graphml xmlns=\"http://graphml.graphdrawing.org/xmlns\"\n";
        graphml << "    xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"\n";
        graphml << "    xsi:schemaLocation=\"http://graphml.graphdrawing.org/xmlns\n";
        graphml << "     http://graphml.graphdrawing.org/xmlns/1.1/graphml.xsd\">\n";
        graphml << "  <graph id=\"G\" edgedefault=\"directed\">\n";

        for (TNGraph::TNodeI NI = Graph->BegNI(); NI < Graph->EndNI(); NI++)
                graphml << "    <node id=\"" << NI.GetId() << "\"/>\n";
        int i = 1;
        for (TNGraph::TEdgeI EI = Graph->BegEI(); EI < Graph->EndEI(); EI++) {
                graphml << "    <edge id=\"e" << i++ << "\" directed=\"true"
                        << "\" source=\""     << EI.GetSrcNId()
                        << "\" target=\""     << EI.GetDstNId() << "\"/>\n";
        }
        graphml << "  </graph>\n";
        graphml << "</graphml>\n";
        graphml.close();
}
示例#11
0
void gexf(PNGraph Graph){
        std::ofstream graph;
        graph.open("graph.gexf");
        graph << "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
              << "<gexf xmlns=\"http://www.gexf.net/1.2draft\" version=\"1.2\">\n"
              << "  <graph mode=\"static\" defaultedgetype=\"directed\">\n"
              << "    <nodes>\n";
        for (TNGraph::TNodeI NI = Graph->BegNI(); NI < Graph->EndNI(); NI++)
                graph << "      <node id=\"" << NI.GetId() << "\"/>\n";

        graph << "    </nodes>\n";
        graph << "    <edges>\n";
        int i = 1;
        for (TNGraph::TEdgeI EI = Graph->BegEI(); EI < Graph->EndEI(); EI++)
                graph << "      <edge id=\"e" << i++
                      << "\" directed=\"true" << "\" source=\""
                      << EI.GetSrcNId()       << "\" target=\""
                      << EI.GetDstNId()      << "\"/>\n";

        graph << "    </edges>\n"
              << "  </graph>\n"
              << "</gexf>\n";
        graph.close();
}
示例#12
0
void TNetInfBs::SaveGroundTruth(const TStr& OutFNm) {
	TFOut FOut(OutFNm);

	// write nodes to file
	for (TNGraph::TNodeI NI = GroundTruth->BegNI(); NI < GroundTruth->EndNI(); NI++) {
		FOut.PutStr(TStr::Fmt("%d,%d\r\n", NI.GetId(), NI.GetId())); // nodes
	}

	FOut.PutStr("\r\n");

	// write edges to file (not allowing self loops in the network)
	for (TNGraph::TEdgeI EI = GroundTruth->BegEI(); EI < GroundTruth->EndEI(); EI++) {
		// not allowing self loops in the Kronecker network
		if (EI.GetSrcNId() != EI.GetDstNId()) {
			if (Alphas.IsKey(TIntPr(EI.GetSrcNId(), EI.GetDstNId())))
				FOut.PutStr(TStr::Fmt("%d,%d,%f\r\n", EI.GetSrcNId(), EI.GetDstNId(), Alphas.GetDat(TIntPr(EI.GetSrcNId(), EI.GetDstNId())).Val));
			else
				FOut.PutStr(TStr::Fmt("%d,%d,1\r\n", EI.GetSrcNId(), EI.GetDstNId()));
		}
	}
}
示例#13
0
void TempMotifCounter::Count3TEdgeTriads(double delta, Counter3D& counts) {
  counts = Counter3D(2, 2, 2);

  // Get the counts on each undirected edge
  TVec< THash<TInt, TInt> > edge_counts(static_graph_->GetMxNId());
  TVec< THash<TInt, TIntV> > assignments(static_graph_->GetMxNId());
  for (TNGraph::TEdgeI it = static_graph_->BegEI();
       it < static_graph_->EndEI(); it++) {
    int src = it.GetSrcNId();
    int dst = it.GetDstNId();
    int min_node = MIN(src, dst);
    int max_node = MAX(src, dst);
    edge_counts[min_node](max_node) += temporal_data_[src](dst).Len();
    assignments[min_node](max_node) = TIntV();
  }
  
  // Assign triangles to the edge with the most events
  TIntV Us, Vs, Ws;
  GetAllStaticTriangles(Us, Vs, Ws);
  #pragma omp parallel for schedule(dynamic)
  for (int i = 0; i < Us.Len(); i++) {
    int u = Us[i];
    int v = Vs[i];
    int w = Ws[i];
    int counts_uv = edge_counts[MIN(u, v)].GetDat(MAX(u, v));
    int counts_uw = edge_counts[MIN(u, w)].GetDat(MAX(u, w));
    int counts_vw = edge_counts[MIN(v, w)].GetDat(MAX(v, w));
    if        (counts_uv >= MAX(counts_uw, counts_vw)) {
      #pragma omp critical
      {
        TIntV& assignment = assignments[MIN(u, v)].GetDat(MAX(u, v));
        assignment.Add(w);
      }
    } else if (counts_uw >= MAX(counts_uv, counts_vw)) {
      #pragma omp critical
      {
        TIntV& assignment = assignments[MIN(u, w)].GetDat(MAX(u, w));
        assignment.Add(v);      
      }
    } else if (counts_vw >= MAX(counts_uv, counts_uw)) {
      #pragma omp critical
      {
        TIntV& assignment = assignments[MIN(v, w)].GetDat(MAX(v, w));
        assignment.Add(u);              
      }
    }
  }

  TVec<TIntPair> all_edges;
  TIntV all_nodes;
  GetAllNodes(all_nodes);  
  for (int node_id = 0; node_id < all_nodes.Len(); node_id++) {
    int u = all_nodes[node_id];
    TIntV nbrs;
    GetAllNeighbors(u, nbrs);
    for (int nbr_id = 0; nbr_id < nbrs.Len(); nbr_id++) {
      int v = nbrs[nbr_id];
      if (assignments[u].IsKey(v) && assignments[u].GetDat(v).Len() > 0) {
        all_edges.Add(TIntPair(u, v));
      }
    }
  }

  // Count triangles on edges with the assigned neighbors
  #pragma omp parallel for schedule(dynamic)
  for (int edge_id = 0; edge_id < all_edges.Len(); edge_id++) {
    TIntPair edge = all_edges[edge_id];
    int u = edge.Key;
    int v = edge.Dat;
    // Continue if no assignment
    if (!assignments[u].IsKey(v)) { continue; }
    TIntV& uv_assignment = assignments[u].GetDat(v);
    // Continue if no data
    if (uv_assignment.Len() == 0) { continue; }
    // Get all events on (u, v)
    TVec<TriadEdgeData> events;
    TVec<TIntPair> ts_indices;
    int index = 0;
    int nbr_index = 0;
    // Assign indices from 0, 1, ..., num_nbrs + 2
    AddTriadEdgeData(events, ts_indices, index, u, v, nbr_index, 0, 1);
    nbr_index++;
    AddTriadEdgeData(events, ts_indices, index, v, u, nbr_index, 0, 0);
    nbr_index++;
    // Get all events on triangles assigned to (u, v)
    for (int w_id = 0; w_id < uv_assignment.Len(); w_id++) {
      int w = uv_assignment[w_id];
      AddTriadEdgeData(events, ts_indices, index, w, u, nbr_index, 0, 0);
      AddTriadEdgeData(events, ts_indices, index, w, v, nbr_index, 0, 1);
      AddTriadEdgeData(events, ts_indices, index, u, w, nbr_index, 1, 0);
      AddTriadEdgeData(events, ts_indices, index, v, w, nbr_index, 1, 1);
      nbr_index++;      
    }
    // Put events in sorted order
    ts_indices.Sort();
    TIntV timestamps(ts_indices.Len());
    TVec<TriadEdgeData> sorted_events(ts_indices.Len());
    for (int i = 0; i < ts_indices.Len(); i++) {
      timestamps[i] = ts_indices[i].Key;
      sorted_events[i] = events[ts_indices[i].Dat];
    }
    
    // Get the counts and update the counter
    ThreeTEdgeTriadCounter tetc(nbr_index, 0, 1);
    tetc.Count(sorted_events, timestamps, delta);
    #pragma omp critical
    {
      for (int dir1 = 0; dir1 < 2; dir1++) {
        for (int dir2 = 0; dir2 < 2; dir2++) {
          for (int dir3 = 0; dir3 < 2; dir3++) {        
            counts(dir1, dir2, dir3) += tetc.Counts(dir1, dir2, dir3);
          }
        }
      }
    }
  }
}