void pAOSOAutostepGradTree::get_is_leaf( VecInt& is_leaf )
{
  is_leaf.clear();
  is_leaf.resize(nodes_.size());

  std::list<pAOSOAutostepGradNode>::iterator it = nodes_.begin();
  for (int i = 0; it!=nodes_.end(); ++it, ++i) {
    if (it->left_==0 && it->right_==0)
      is_leaf[i] = 1;
    else
      is_leaf[i] = 0;
  } // for i
}
//--- MAIN PROGRAM
//-------------------------------------------------------------------------------------
int main(int argc, char * argv[]) {

    int seed;
    int a_type;
    int w_type;
    string title;
    string if_weighted;
    string if_help;
    const char *file_network;
    const char *file_names;

    if ( argc != 5 ) {
        printHelpMessage( argv[0] );
    }

    if_help = argv[1];
    if( if_help.compare("-h") == 0 || if_help.compare("-help") == 0 ) {
        printHelpMessage( argv[0] );
    }

    seed = atoi(argv[1]);
    cout << "> seed is " << seed << endl;

    //--- Initialize random seed:
    _rand.setSeed(seed);

    a_type = atoi(argv[2]);

    if( a_type < 1 || a_type > 3 ) {
        cout << "argument 2: the type of algorithm to run needs to be either (1,2,3): " << endl;
        cout << "          : 1 = Geodesic edge Betweenness" << endl;
        cout << "          : 2 = Random edge Betweenness"   << endl;
        cout << "          : 3 = Spectral Betweenness"      << endl;
        exit(1);
    }

    switch(a_type) {

    case 1:
        cout << "> Using Geodesic edge Betweenness." << endl;
        title = "Geodesic edge Betweenness.";
        break;

    case 2:
        cout << "> Using Random edge Betweenness." << endl;
        title = "RandomWalk edge Betweenness.";
        break;

    case 3:
        cout << "> Using Spectral Betweenness." << endl;
        title = "Spectral Betweenness.";
        break;

    default:
        break;

    }

    if_weighted = argv[3];

    if( if_weighted.compare("w") == 0 ) {
        w_type = 3;
        cout << "> Using a weighted network " << endl;
    } else {
        if( if_weighted.compare("nw") == 0 ) {
            w_type = 2;
            cout << "> Using a non-weighted network " << endl;
        } else {
            cout << "argument 3: specify if network file is weighted or not: " << endl;
            cout << "          : w  = Using a weighted network file " << endl;
            cout << "          : nw = Using a non-weighted network file " << endl;
            exit(1);
        }
    }

    file_network = argv[4];

    //--- Default values for parameters which may be modified from the commandline
    ihelper = Helper();
    reader.readFile(file_network, w_type);
    Gn      = reader.getNodeSet();
    Gelist  = reader.getEdgeSet();

    vector<int> key_listi;
    vector<int> key_listj;
    vector<int> key_listk;

    cout << "> The Global node list..." << endl;
    for(int i=1; i<Gn.size(); i++) {
        key_listi.push_back(Gn[i].ID);
        key_listj.push_back(Gn[i].ID);
        key_listk.push_back(-1);
        Gn[i].print();
        Gn[i].printEdges();
    }

    //--- To use getSubSample, comment following two lines, and
    //--- uncomment getSubSample(key_listj).
    n     = Gn;
    elist = Gelist;
    //getSubSample(key_listj);

    //cout << "The sub-node list ... " << endl;
    //for(int i=1; i<n.size(); i++){
    //n[i].print();
    //n[i].printEdges();
    //}

    cout << "> The Global edge list..." << endl;
    for(int i=0; i<elist.size(); i++) {
        elist[i].print();
    }

    forcytoscape = new fstream("OUT/communities_newman.txt",ios_base::out);
    (*forcytoscape) << "communities" << endl;

    removededges = new fstream("OUT/removededges.txt",ios_base::out);
    (*removededges) << "Removed Edges" << endl;
    (*removededges) << "so \t IDso \t si \t IDsi \t we \t Globalweight \t key" << endl;

    totallist = ihelper.cloneEdgeList(elist);

    com_max      = 0;
    specQ        = 0.0;
    double Q     = 0.0;
    double Q_SD     = 0.0;
    double Q_old = 0.0;
    double Q_SD_old = 0.0;

    int loop       = elist.size();
    int E          = loop;
    double Q_max   = 0.0;
    double Q_limit = 1.0;
    bool stopping  = false;

    int N = n.size()-1;

    R.resize(N,N);
    Ri.resize(N,N);
    A.resize(N,N);
    Ai.resize(N,N);
    Bi.resize(N,N);
    C.resize(N);

    S.resize(N,1);
    V.resize(N,1);
    T.resize(N,N);
    Ti.resize(N,N);
    Rc.resize((N-1),(N-1));
    Vi.resize(C.size(),1);

    B.resize(N,N);
    Bm.resize(N,N);
    Bgi.resize(N,N);

    keys_p.resize(N);
    keys_n.resize(N);

    u.resize(N,N);  //eigenvectors
    betai.resize(N);//eigenvalues

    SI.resize(N,2);
    si.resize(N);
    visited.resize(N);

    setupMatrices();

    cout << "> Running " << title.c_str() << endl;

    cstart = clock();

    if( a_type == 3 ) {
        //--- Calculate betweenness using the Spectral algorithm
        calculateSpectralModularity();
    } else {

        while( loop !=0 && !stopping ) {

            int old_max_com = com_max;

            //--- Calculate betweenness using Geodesic or RandomWalk algorithms
            if( a_type == 1 )
                calculateEdgeBetweennessGeodesic();
            else
                calculateEdgeBetweennessRandom();

            //--- Calculate the Modularity
            Q_old = Q;
            Q_SD_old = Q_SD;
            Q     = 0.0;
            Q_SD   = 0.0;

            Modularity(Q, Q_SD);

            //--- Store networks state if Modularity has increased during this iteraction
            if(com_max > old_max_com) {
                vec_mod.push_back(Q);
                vec_mod_err.push_back(Q_SD);
                vec_com_max.push_back(com_max);
                vec_nodes.push_back(storeNodes());
            }


            //--- Record the maximum Modularity value
            if( Q > Q_max ) {
                Q_max = Q;
            } else {
                if( Q_max > 0.0 && (Q_max - Q)/Q_max > Q_limit ) stopping = true;
            }


            //--- Find edge with maximum edge betweenness score and remove
            edge _max;
            _max = totallist[1].Clone();
            for(int i=1; i<totallist.size(); i++) {

                if( totallist[i].removed == false ) {

                    if(totallist[i].we >= _max.we) {

                        if(totallist[i].we > _max.we)
                            _max = totallist[i];
                        else {
                            int rdm = rand()%2;
                            if(rdm == 1) _max = totallist[i];
                        }
                    }
                }
                totallist[i].we = 0;
            }

            //--- Record the removed edges.
            _max.print( removededges );

            n[elist[_max.key-1].so].removeEdge(_max.key);
            n[elist[_max.key-1].si].removeEdge(_max.key);
            n[elist[_max.key-1].so].setDegree( (n[elist[_max.key-1].so].getDegree() - 1) );
            n[elist[_max.key-1].si].setDegree( (n[elist[_max.key-1].si].getDegree() - 1) );
            totallist[_max.key].removed = true;
            elist[_max.key-1].removed   = true;
            --loop;

            //--- Calculate the remaining processor time
            DrawProgressBar( 20, ((double)E - (double)loop)/(double)E );

        }
    }

    //--- Recored the CPU-time taken
    cend = clock();
    double cpu_time_used = ((double) (cend - cstart)) / CLOCKS_PER_SEC;
    cout << "" << endl;
    cout << "> cputime: " << cpu_time_used << " seconds " << endl;
    cout << "> Network (nodes): " << N << " (edges): " << E << endl;

    if( a_type != 3 ) {

        //--- Print all stored Modularity values
        modularityscore = new fstream("OUT/modularityscore.txt",ios_base::out);
        (*modularityscore) << title.c_str() << endl;
        for(int i=0; i<vec_mod.size(); i++) {
            (*modularityscore) << vec_mod[i] << " " << vec_mod_err[i] << " " << vec_com_max[i] << endl;
        }
        modularityscore->close();

        int ind   = findMax(vec_mod);
        int com   = 1;
        int _size = 0;
        int c_max = com_max;

        //--- Print node communities for maximum Modularity value, for Geodesic or RandomWalk runs
        communityout = new fstream("OUT/communityout.txt",ios_base::out);
        (*communityout) << "Max Q: " << vec_mod[ind] << " +- " << vec_mod_err[ind] << endl;
        (*communityout) << "cputime: " << cpu_time_used << " seconds " << endl;
        (*communityout) << "Network (nodes): " << N << " (edges): " << E << endl;

        while(com<(c_max+1)) {
            _size = 0;
            for(int i=0; i<vec_nodes[ind].size(); i++) {
                if(vec_nodes[ind][i].c == com ) {
                    (*communityout) << vec_nodes[ind][i].ID << "\t" << vec_nodes[ind][i].c << endl;
                    //vec_nodes[ind][i].print( communityout );
                    _size++;
                }
            }
            if(_size != 0)
                (*communityout) << "community: " << com << " size: " << _size << endl;
            com++;
        }

        for(int i=0; i<vec_nodes[ind].size(); i++) {
            for(int j=0; j<key_listi.size(); j++) {
                if(vec_nodes[ind][i].ID == key_listi[j]) {
                    key_listk[j] = vec_nodes[ind][i].c;
                    break;
                }
            }
        }

        //--- Print node communities for maximum Modularity for the consensus matrix
        consensusout = new fstream("OUT/consensusout.txt",ios_base::out);
        (*consensusout) << "key list" << endl;
        for(int i=0; i<key_listi.size(); i++) {
            if(key_listk[i] == -1 && key_listj[i] != -1)
                key_listj[i] = -1;
            (*consensusout) << key_listi[i] << " " << key_listj[i] << " " << key_listk[i] << endl;
            //(*consensusout) << key_listi[i] << " = " << key_listk[i] << endl;
            (*forcytoscape) << key_listi[i] << " = " << key_listk[i] << endl;
            cout << key_listi[i] << " " << key_listj[i] << " " << key_listk[i] << endl;
        }
    } else {

        int com   = 1;
        int _size = 0;
        int c_max = maxCommunity();

        //--- Store node communities for maximum Modularity for the Spectral Modularity run
        communityout = new fstream("OUT/communityout.txt",ios_base::out);
        (*communityout) << "communityout" << endl;
        (*communityout) << "Max Q: " << specQ << endl;
        (*communityout) << "cputime: " << cpu_time_used << " seconds " << endl;
        (*communityout) << "Network (nodes): " << N << " (edges): " << E << endl;
        while(com<(c_max+1)) {
            _size = 0;
            for(int i=0; i<n.size(); i++) {
                if(n[i].c == com ) {
                    n[i].print( communityout );
                    _size++;
                }
            }
            if(_size != 0)
                (*communityout) << "community: " << com << " size: " << _size << endl;
            com++;
        }

        for(int i=1; i<n.size(); i++) {
            for(int j=0; j<key_listi.size(); j++) {
                if(n[i].ID == key_listi[j]) {
                    key_listk[j] = n[i].c;
                    break;
                }
            }
        }

        //--- Print node communities for maximum Modularity the consensus matrix
        consensusout = new fstream("OUT/consensusout.txt",ios_base::out);
        (*consensusout) << "key list" << endl;
        for(int i=0; i<key_listi.size(); i++) {
            if(key_listk[i] == -1 && key_listj[i] != -1)
                key_listj[i] = -1;
            (*consensusout) << key_listi[i] << " " << key_listj[i] << " " << key_listk[i] << endl;
            //(*consensusout) << key_listi[i] << " = " << key_listk[i] << endl;
            (*forcytoscape) << key_listi[i] << " = " << key_listk[i] << endl;
            cout << key_listi[i] << " " << key_listj[i] << " " << key_listk[i] << endl;
        }


    }


    //--- Remove data structures
    communityout->close();
    forcytoscape->close();
    vec_mod.clear();
    vec_mod_err.clear();
    vec_nodes.clear();

    exit(1);

}