function expandSeqTree()

in archive/src/app/GraphBuilder.js [78:149]


function expandSeqTree(rootSeq, graphs, expandCnt, minSupport, maxSupport, terms, itemset) {
  if( rootSeq.words ) {
    for( var i = 0; i < rootSeq.words.length; i++ ) {
      rootSeq.graph.nodes.push(rootSeq.words[i]);
      expandCnt--;
    }
  }

  var seqs = [];
  var leafSeqs = [];
  seqs.push(rootSeq);
  while(seqs.length > 0 && expandCnt > 0) {
    /* find the candidate sequence with largest support DB */
    seqs.sort(function(a, b) {return a.size - b.size;}); // TODO: rewrite
    var s = seqs.pop();

    var graph = s.graph;

    var s0 = s.r, s1 = s.l;

    if( !s0 && !s1 ) {
      /* find the next frequent sequence */
      var result = growSeq(s, terms, minSupport, maxSupport, itemset);
      s0 = result.s0;
      s1 = result.s1;
      var word = result.word;
      var pos = result.pos;
      var cnt = result.cnt;

      if( cnt < minSupport ) {
        leafSeqs.push(s);
      }
      else{
        /* create new sequences and add new word */
        if(!graph) {
          graph = new RawGraph(minSupport, maxSupport);
          graphs.push(graph);
        }
        var newWord = {entity:itemset[word], freq:cnt, id:graph.totalNodeCnt++};
        newWord.topTweet = s1.DBs[0].tweetId;
        var newWords = s.words.slice();
        newWords.splice(pos, 0, newWord);
        newWord.seq = s1;
        s0.words = s.words;
        s1.words = newWords;
        s1.newWord = newWord;
        s0.graph = s.graph;
        s1.graph = graph;
      }
    }

    if(s1) {
      s1.graph.nodes.push(s1.newWord);
      //console.log(s1.newWord.entity);
      expandCnt--;
    }

    /* add new sequences to seqTree */
    s.l = s1;
    s.r = s0;

    /* add new sequences to candidates */
    if(s1)
      seqs.push(s1);
    if(s0 && s0.size >= minSupport )
      seqs.push(s0);

  }

  leafSeqs = leafSeqs.concat(seqs);
  return leafSeqs;
}