void VortexTransition::ConstructSequence()
{
  for (int i=_ts; i<_ts+_tl-1; i++) {
    // fprintf(stderr, "=====t=%d\n", i);

    VortexTransitionMatrix tm = Matrix(i);
    assert(tm.Valid());

    if (i == _ts) { // initial
      for (int k=0; k<tm.n0(); k++) {
        int gid = NewVortexSequence(i);
        _seqs[gid].tl ++;
        _seqs[gid].lids.push_back(k);
        _seqmap[std::make_pair(i, k)] = gid;
        _invseqmap[std::make_pair(i, gid)] = k;
      }
    }

    for (int k=0; k<tm.NModules(); k++) {
      int event;
      std::set<int> lhs, rhs;
      tm.GetModule(k, lhs, rhs, event);

      if (lhs.size() == 1 && rhs.size() == 1) { // ordinary case
        int l = *lhs.begin(), r = *rhs.begin();
        int gid = _seqmap[std::make_pair(i, l)];
        _seqs[gid].tl ++;
        _seqs[gid].lids.push_back(r);
        _seqmap[std::make_pair(i+1, r)] = gid;
        _invseqmap[std::make_pair(i+1, gid)] = r;
      } else { // some events, need re-ID
        for (std::set<int>::iterator it=rhs.begin(); it!=rhs.end(); it++) {
          int r = *it; 
          int gid = NewVortexSequence(i+1);
          _seqs[gid].tl ++;
          _seqs[gid].lids.push_back(r);
          _seqmap[std::make_pair(i+1, r)] = gid;
          _invseqmap[std::make_pair(i+1, gid)] = r;
        }
      }

      // build events
      // if (event >= VORTEX_EVENT_MERGE) {
      if (event > VORTEX_EVENT_DUMMY) {
        VortexEvent e;
        e.frame = i;
        e.type = event;
        e.lhs = lhs;
        e.rhs = rhs;
        _events.push_back(e);
      }
    }
  }

  // RandomColorSchemes();
  SequenceGraphColoring(); 
}
void VortexTransition::AddMatrix(const VortexTransitionMatrix& m)
{
  if (!m.Valid()) return;
  int t = m.t0();
  _matrices[t] = m;
}