void sort1() { for (int i = 0; i < N; i++) { #pragma omp parallel for for (int j = i%2; j < N; j++) { sort_pair(j); } } }
void OutputDegree(string file) { sort_pair(sort_pool); ofstream fout; fout.open(file.c_str()); for(int i=0;i<sort_pool.size();i++) { fout<<sort_pool[i].first<<" "<<sort_pool[i].second<<endl; } fout.close(); fout.clear(); //call python to draw //system("python ./input/degree.py ./output/degree.txt 1 10 0 1.0"); }
void OutputTheta(string s) { vector<int> topic2docs[env_inst.NTOPICS]; ofstream fout(s.c_str()); int i,j; for(i=0; i<env_inst.NDOCS; i++) { vector<pair<int ,double>> vec; double sum=0; for(j=0;j<env_inst.NTOPICS; j++) { sum+=env_inst.theta[i][j]; } for(j=0;j<env_inst.NTOPICS;j++) { env_inst.theta[i][j]/=sum; fout<<env_inst.theta[i][j]<<" "; vec.push_back(make_pair(j,env_inst.theta[i][j])); } fout<<endl; sort_pair(vec); topic2docs[vec[0].first].push_back(i); } fout.close(); fout.clear(); fout.open(env_inst.OUTPUT_TD); for(int i=0;i<env_inst.NTOPICS;i++) { fout<<i<<"#"<<topic2docs[i].size()<<"#"; for(int j=0;j<topic2docs[i].size();j++) fout<<topic2docs[i][j]<<" "; fout<<endl; } fout.close(); }
void OutputPhi(string s) { ofstream fout(s.c_str()); int i,j; for(i=0; i<env_inst.NTOPICS; i++) { fout<<i<<" "; vector<pair<int ,double>> vec; for(j=0;j<env_inst.NWORDS; j++) { vec.push_back(make_pair(j,env_inst.phi[i][j])); } sort_pair(vec); for(j=0;j<env_inst.NWORDS && j<env_inst.NKW;j++) { fout<<env_inst.wordList[vec[j].first]<<"#"; } fout<<endl; } fout.close(); }
//========================================================================================================== // Mimimize the DFA using Myphill-Nerode based algorithm. The algorithm consider all state pairs, marking // them as non-equivalent if possible. When finished, those not marked are put in the same new state. // Steps: // 1. Mark as non-equivalent those pairs with different accepting values // 2. Iteratively mark pairs that for any input symbol go to a marked pair (or transition defined just for // one of them on that symbol). // 3. Combine unmarked pairs to form new states. // 4. Write the new transitions, mark accepting new states //========================================================================================================== void DFA::minimize() { //------------------------------------------------------------------------------------------------------ // Create the pairs and do initial mark (true means pair is non-equivalent) //------------------------------------------------------------------------------------------------------ vector<bool*> pairs; int num_states = get_num_states(); for(int i = 0; i < num_states - 1; ++i) { pairs.push_back(new bool[num_states - i - 1]); pairs[i] -= (i+1); // This ugly trick enables accessing pairs[i][j], but actually using just half the memory for(int j = i+1; j < num_states; ++j) { pairs[i][j] = (accepting[i] != accepting[j]); } } //------------------------------------------------------------------------------------------------------ // Mark until an iteration where no changes are made //------------------------------------------------------------------------------------------------------ bool changed = true; while(changed) { changed = false; for(int i = 0; i < num_states - 1; ++i) { for(int j = i+1; j < num_states; ++j) { if(pairs[i][j]) continue; // Pair already marked for(int sym = 0; sym < NUM_SYMBOLS; ++sym) { int x = get_next_state(i, sym), y = get_next_state(j, sym); if(x == y) continue; sort_pair(x,y); // Must have the smaller index first to access pairs table if(x == -1 or pairs[x][y]) { pairs[i][j] = true; changed = true; } } // for each symbol } } } //------------------------------------------------------------------------------------------------------ // Combine states: // 1. A new state is a set of old states which are equivalent // 2. If an old state is not equivalent to any other state, a new state is created that contains only it // 3. After adding a pair {i,j} (i < j}, there's no need to look at pairs {j,x} (j < x), because pair // {i,x} must have already been added. //------------------------------------------------------------------------------------------------------ vector<vector<int>> new_states; vector<int> old_to_new(num_states, -1); set<int> added_states; for(int i = 0; i < num_states - 1; ++i) { if(added_states.count(i) != 0) continue; new_states.push_back({i}); old_to_new[i] = new_states.size() - 1; for(int j = i+1; j < num_states; ++j) { if(not pairs[i][j]) { new_states.back().push_back(j); old_to_new[j] = new_states.size() - 1; added_states.insert(j); } } } if(added_states.empty()) // No minimization occurred return; // If the last state wasn't combined with any other state, add a new state that contains only it; // This is needed because the last state has no entry in the pairs table as a first of any pair if(added_states.count(num_states-1) == 0) new_states.push_back({num_states-1}); //------------------------------------------------------------------------------------------------------ // Write new transitions and mark accepting new states. Then replace the old DFA with the new one. //------------------------------------------------------------------------------------------------------ vector<int> new_accepting(new_states.size(), -1); vector<vector<int>> new_table(new_states.size(), vector<int>(NUM_SYMBOLS, -1)); for(int i = 0; i < new_states.size(); ++i) { for(auto s: new_states[i]) if(accepting[s] != accepting[new_states[i][0]]) throw string("DFA states found to be equivalent yet have different accepting values"); new_accepting[i] = accepting[new_states[i][0]]; // If the first is accepting they all are, and vice versa for(int sym = 0; sym < NUM_SYMBOLS; ++sym) { // Since all old states in this new states are equivalent, need to check only one int old_next_state = get_next_state(new_states[i][0], sym); if(old_next_state != -1) new_table[i][sym] = old_to_new[old_next_state]; } } accepting = new_accepting; table = new_table; //------------------------------------------------------------------------------------------------------ // Free memory //------------------------------------------------------------------------------------------------------ for(int i = 0; i < num_states - 1; ++i) { delete (pairs[i] + i + 1); } } // minimize()