Exemplo n.º 1
0
// simulate SI model cascade using infection probability Beta until the cascade reaches size CascSz
PNGraph RunSICascade(PUNGraph G, const double& Beta, const int& CascSz, TIntH& NIdInfTmH) {
  PNGraph Casc = TNGraph::New();
  const int StartId = G->GetRndNId();
  Casc->AddNode(StartId);
  NIdInfTmH.AddDat(StartId, NIdInfTmH.Len());
  for (int X = 0; X < 10*CascSz; X++) {
    TIntV CascNIdV;  Casc->GetNIdV(CascNIdV);
    for (int n = 0; n < CascNIdV.Len(); n++) {
      const TUNGraph::TNodeI NI = G->GetNI(CascNIdV[n]);
      for (int i = 0; i < NI.GetOutDeg(); i++) {
        if (Casc->IsNode(NI.GetOutNId(i))) { continue; }
        if (TInt::Rnd.GetUniDev() < Beta) {
          Casc->AddNode(NI.GetOutNId(i));
          NIdInfTmH.AddDat(NI.GetOutNId(i), NIdInfTmH.Len());
          Casc->AddEdge(NI.GetId(), NI.GetOutNId(i));
          if (Casc->GetNodes() == CascSz) { return Casc; }
        }
      }
    }
  }
  return Casc;
}
Exemplo n.º 2
0
PNEANet KNNJaccardParallel(PNGraph Graph,int K) {
  PNEANet KNN = TNEANet::New();
  TIntV NIdV;
  Graph->GetNIdV (NIdV);
  int size = NIdV.Len();
  for (int ind = 0; ind < size; ind++) {
    KNN->AddNode(NIdV[ind]);
  }
  KNN->AddFltAttrE("sim");
  TVec<TVec<TPair<TFlt, TInt>, int >, int > TopKList;
  TVec<TVec<TPair<TFlt, TInt>, int >, int > ThTopK; // for each thread
  TIntV NodeList;
  TIntV ThNodeList;// for each thread
  int NumThreads = omp_get_max_threads();
  omp_set_num_threads(NumThreads);
  #pragma omp parallel private(ThNodeList, ThTopK)
  {
    TIntV* Neighbors_old = new TIntV();
    TIntV* Neighbors = new TIntV();
    TIntV* temp;

    #pragma omp for schedule(dynamic,1000)
    for (int ind = 0; ind < size; ind++) {
      TNGraph::TNodeI NI = Graph->GetNI(NIdV[ind]);
      if (NI.GetInDeg() > 0) {
        continue;
      }
      if (NI.GetOutDeg() == 0) {
        continue;
      }

      TVec<TPair<TFlt, TInt>, int > TopK;
      for (int i = 0; i < K; i++) {
          TopK.Add(TPair<TFlt,TInt>(0.0, -1));
      }

      Neighbors->Clr(false);
      Neighbors_old->Clr(false);

      for (int i = 0; i < NI.GetOutDeg(); i++) {
        TNGraph::TNodeI Inst_NI = Graph->GetNI(NI.GetOutNId(i));
        MergeNbrs(Neighbors, Neighbors_old, Inst_NI);

        temp = Neighbors_old;
        temp->Clr(false);
        Neighbors_old = Neighbors;
        Neighbors = temp;
      }

      // Swap neighbors and Neighbors_old

      temp = Neighbors_old;
      Neighbors_old = Neighbors;
      Neighbors = temp;
      for(int j = 0; j< Neighbors->Len(); j++) {

        TNGraph::TNodeI Auth_NI = Graph->GetNI((*Neighbors)[j]);

        float similarity = JaccardSim(NI, Auth_NI);
        if (TopK[K-1].GetVal1() < similarity) {
          int index = 0;
          for (int i = K-2; i >= 0; i--)
            if (TopK[i].GetVal1() < similarity) {
              TopK.SetVal(i+1, TopK[i]);
            } else {
              index = i+1;
              break;
            }
          TopK.SetVal(index, TPair<TFlt, TInt>(similarity, (*Neighbors)[j]));
        }
      }

      ThTopK.Add(TopK);
      ThNodeList.Add(NIdV[ind]);

//    if (ct%10000 == 0)
//    	cout<<ct<<" avg neighbor degree = "<<sum_neighbors*1.0/ct<<" "<<currentDateTime()<<endl;

    }
    #pragma omp critical
    {
      for (int j = 0; j < ThTopK.Len(); j++) {
        TopKList.Add(ThTopK[j]);
        NodeList.Add(ThNodeList[j]);
      }
    }
	}

  int size2 = NodeList.Len();
  for (int i= 0; i < size2 ; i++) {

    for (int j = 0; j < K; j++) {
      if (TopKList[i][j].GetVal2() <= -1) {
        break;
      }
      int EId = KNN->AddEdge(NodeList[i], TopKList[i][j].GetVal2());
      KNN->AddFltAttrDatE(EId, TopKList[i][j].GetVal1(), "sim");
    }
  }
  return KNN;
}
Exemplo n.º 3
0
PNEANet KNNJaccard(PNGraph Graph, int K) {
  PNEANet KNN = TNEANet::New();

  int sum_neighbors = 0;
  int ct;
  int end;
  end = Graph->GetNodes();
  TIntV* Neighbors_old = new TIntV();
  TIntV* Neighbors = new TIntV();
  TIntV* temp;
  TIntV NIdV;
  Graph->GetNIdV (NIdV);
  int size = NIdV.Len();
  for (int ind = 0; ind < size; ind++) {
    KNN->AddNode(NIdV[ind]);
  }
  KNN->AddFltAttrE("sim");

  for (int ind = 0; ind < size; ind++) {
    TNGraph::TNodeI NI = Graph->GetNI(NIdV[ind]);
    if (NI.GetInDeg() > 0) {
      continue;
    }
    if (NI.GetOutDeg() == 0) {
      continue;
    }
    ct ++;

    TVec<TPair<TFlt, TInt> > TopK;
    for (int i = 0; i < K; i++) {
      TopK.Add(TPair<TFlt,TInt>(0.0, -1));
    }

    Neighbors->Clr(false);
    Neighbors_old->Clr(false);

    for (int i = 0; i < NI.GetOutDeg(); i++) {
      TNGraph::TNodeI Inst_NI = Graph->GetNI(NI.GetOutNId(i));
      MergeNbrs(Neighbors, Neighbors_old, Inst_NI);

      temp = Neighbors_old;
      temp->Clr(false);
      Neighbors_old = Neighbors;
      Neighbors = temp;
    }
    int num = Neighbors_old->Len();
    sum_neighbors += num;

    //Swap neighbors and Neighbors_old

    temp = Neighbors_old;
    Neighbors_old = Neighbors;
    Neighbors = temp;
    for (int j = 0; j< Neighbors->Len(); j++) {

      TNGraph::TNodeI Auth_NI = Graph->GetNI((*Neighbors)[j]);

      float similarity = JaccardSim(NI, Auth_NI);
      if (TopK[K-1].GetVal1() < similarity) {
        int index = 0;
        for (int i = K-2; i >= 0; i--)
          if (TopK[i].GetVal1() < similarity) {
            TopK.SetVal(i+1, TopK[i]);
          } else {
            index = i+1;
            break;
          }
        TopK.SetVal(index, TPair<TFlt, TInt>(similarity, (*Neighbors)[j]));
      }
    }

    for (int i = 0; i < K; i++) {
      int EId = KNN->AddEdge(NI.GetId(), TopK[i].GetVal2());
      KNN->AddFltAttrDatE(EId, TopK[i].GetVal1(), "sim");
    }

//    if (ct%10000 == 0)
//    	cout<<ct<<" avg neighbor degree = "<<sum_neighbors*1.0/ct<<" "<<currentDateTime()<<endl;

  }

  return KNN;
}