Beispiel #1
0
// parse:
//   10:16, 16 Sep 2004
//   10:20, 2004 Sep 16
//   2005-07-07 20:30:35
//   23:24:07, 2005-07-10
//   9 July 2005 14:38
//   21:16, July 9, 2005
//   06:02, 10 July 2005
bool TStrUtil::GetTmFromStr(const char* TmStr, TSecTm& Tm) {
  static TStrV MonthV1, MonthV2;
  if (MonthV1.Empty()) {
    TStr("january|february|march|april|may|june|july|august|september|october|november|december").SplitOnAllCh('|', MonthV1);
    TStr("jan|feb|mar|apr|may|jun|jul|aug|sep|oct|nov|dec").SplitOnAllCh('|', MonthV2);
  }
  TChA Tmp(TmStr);
  Tmp.ToLc();
  TVec<char *> WrdV;
  const char* End = Tmp.CStr()+Tmp.Len();
  int Col = -1, Cols=0;
  for (char *b = Tmp.CStr(); b <End; ) {
    WrdV.Add(b);
    while (*b && ! (*b==' ' || *b=='-' || *b==':' || *b==',')) { b++; }
    if (*b==':') { if(Col==-1) { Col=WrdV.Len(); } Cols++;  }
    *b=0; b++;
    while (*b && (*b==' ' || *b=='-' || *b==':' || *b==',')) { b++; }
  }
  if (Cols == 2) {
    if (Col+1 >= WrdV.Len()) { return false; }
    WrdV.Del(Col+1);
  }
  if (Col<1) { return false; }
  const int Hr = atoi(WrdV[Col-1]);
  const int Min = atoi(WrdV[Col]);
  WrdV.Del(Col);  WrdV.Del(Col-1);
  if (WrdV.Len() != 3) { return false; }
  int y=0,m=1,d=2, Mon=-1;
  if (TCh::IsAlpha(WrdV[0][0])) {
    y=2; m=0; d=1;
  } else if (TCh::IsAlpha(WrdV[1][0])) {
    y=2; m=1; d=0;
  } else if (TCh::IsAlpha(WrdV[2][0])) {
    y=0; m=2; d=1;
  } else {
    y=0; m=1; d=2;
    Mon = atoi(WrdV[m]);
  }
  int Day = atoi(WrdV[d]);
  if (Mon <= 0) { Mon = MonthV1.SearchForw(WrdV[m])+1; }
  if (Mon <= 0) { Mon = MonthV2.SearchForw(WrdV[m])+1; }
  if (Mon == 0) { return false; }
  int Year = atoi(WrdV[y]);
  if (Day > Year) { ::Swap(Day, Year); }
  //printf("%d-%02d-%02d  %02d:%02d\n", Year, Mon, Day, Hr, Min);
  Tm = TSecTm(Year, Mon, Day, Hr, Min, 0);
  return true;
}
double ave_path_length (PUNGraph p) {
  TVec<TInt> v;
  double tot_lengths = 0.0;
  for (TUNGraph::TNodeI n = p->BegNI(); n != p->EndNI(); n++) {
    v = v + n.GetId();
  }
  //  cerr << "vlen: " << v.Len() << endl;
  TBreathFS<PUNGraph> b(p);
  double tot_pairs = 0.0;
  while (v.Len () > 0) {
    TInt last = v[v.Len()-1];
    b.DoBfs (last, true, true);
    for (TVec<TInt>::TIter i = v.BegI(); (*i) != last; i++) {
      int length;
      length = b.GetHops (last, (*i));
      if (length == length) {
	tot_lengths += length;
	tot_pairs += 1;
      }
    }
    //    cerr << "tps: " << tot_pairs << ", last: " << last << ", beg: " << v[*(v.BegI())] << endl;
    v.Del(v.Len()-1);
  } 
  // cerr << "paths: " << tot_lengths << " " << tot_pairs << " " << (tot_lengths/tot_pairs) << endl;
  return tot_lengths / tot_pairs;
}
void TGreedyAlg::runGreedyAlgorithm() {
    outputGraph = TKColourNet::New();
    
    for (THash<TInt, TNodeInfo>::TIter NI = nodeNmH.BegI(); NI < nodeNmH.EndI(); NI++) {
        outputGraph->AddNode(NI.GetKey(), TKColourNode());
        printf("Added node %d to output graph\n", (int) NI.GetKey());
    }
    
    // for each node i
    for (THash<TInt, TNodeInfo>::TIter NI = nodeNmH.BegI(); NI < nodeNmH.EndI(); NI++) {
        int nodeI = NI.GetKey();
        printf("*****     Considering node i: %d     *****\n", nodeI);
        
        // initialise unaccounted cascades U
        TVec<TCascade> cascades = cascadeV;
        
        // initialise parental neighbourhood
        TIntV parentalNeighbourhood;
        
        bool uselessUnaccountedCasacdesLeft = false;
        while ((cascades.Len() != 0) && (!uselessUnaccountedCasacdesLeft)) {
//            printf("%d Casacdes left...\n", cascades.Len());
            
            // find node j that could possible have infected node i that has been observed for largest number of cascades
            int argmax = -1;
            int maxNoCascades = 0;
            
            bool areUnaccountedCascadesUseless = true;
            for (THash<TInt, TNodeInfo>::TIter LNI = nodeNmH.BegI(); LNI < nodeNmH.EndI(); LNI++) {
                int nodeJ = LNI.GetKey();
//                printf("nodeJ: %d\n", nodeJ);
                int countPotentialNoCascades = 0;
                if (nodeI != nodeJ) {
                    for (int c = 0; c < cascades.Len(); c++) {
                        TCascade cascade = cascades[c];
                        if (cascade.IsNode(nodeJ) && cascade.IsNode(nodeI)) {
//                            if (cascade.GetTm(nodeI) > cascade.GetTm(nodeJ)) {
                            if (cascade.GetTm(nodeJ) == (cascade.GetTm(nodeI) - 1)) {
                                countPotentialNoCascades++;
                            }
                        }
                    }
                    if (countPotentialNoCascades > maxNoCascades) {
                        maxNoCascades = countPotentialNoCascades;
                        argmax = nodeJ;
                        areUnaccountedCascadesUseless = false;
                    }
                    else {
                        areUnaccountedCascadesUseless = areUnaccountedCascadesUseless && true;
                    }
                }
            }
            
            if (areUnaccountedCascadesUseless) {
                uselessUnaccountedCasacdesLeft = true;
            }
            
            if (argmax != -1) {
//                printf("argmax (k) = %d, noCasacdesAppearedIn = %d\n", argmax, maxNoCascades);
                
                // add arg max (k) to the set of parental neighbours
                parentalNeighbourhood.Add(argmax);
            }
            
            // remove the cascades which k belongs to
            TIntV cascadesToRemove;
            for (int c = 0; c < cascades.Len(); c++) {
                TCascade cascade = cascades[c];
                if (cascade.IsNode(argmax) && cascade.IsNode(nodeI)) {
                    if (cascade.GetTm(nodeI) > cascade.GetTm(argmax)) {
                        cascadesToRemove.Add(c);
                    }
                }
            }
            cascadesToRemove.Sort();
            
//            printf("cascadesToRemove: ");
            for (int i = 0; i < cascadesToRemove.Len(); i++) {
//                printf("%d, ", (int) cascadesToRemove[i]);
                cascades.Del(cascadesToRemove[i]-i);
            }
//            printf("\n");
        }
        
        // add edges from node i to each of parent
        for (int i = 0; i < parentalNeighbourhood.Len(); i++) {
            int srcNodeId = parentalNeighbourhood[i];
            int dstNodeId = nodeI;
            outputGraph->AddEdge(srcNodeId, dstNodeId);
//            printf("#####     Added Edge: %d -> %d     #####\n", srcNodeId, dstNodeId);
        }
    }
}