void pageRank(myset<WebPage*>& pages, int steps, char* configFile) {
	
	for (set<WebPage*>::iterator it = pages.begin(); it != pages.end(); ++it) {
		(*it)->setPR(1.0/pages.size());
	}
	//pageRankHelper(pages, steps);
}
void pageRankHelper(myset<WebPage*>& pages, int steps) {
	// for each step
	for (int i=0; i<steps; i++) {
		cout << "STEP " << i+1 << endl;
		// create temp map to store PR for current step
		map<string,double> temp;
		for (set<WebPage*>::iterator it = pages.begin(); it != pages.end(); ++it) {
			double sum = 0;
			set<WebPage*> in = (*it)->incoming_links();
			for (set<WebPage*>::iterator it2 = in.begin(); it2 != in.end(); ++it2) {
				sum += (*it2)->PR()/((*it2)->outgoing_links().size() + 1); // all nodes have a self loop
			}
			// add itself as an incoming AND outgoing link 
			sum += (*it)->PR()/((*it)->outgoing_links().size() + 1);
			temp.insert(pair<string,double>((*it)->filename(), sum));
			//cout << "  insert:" << (*it)->filename() << " " << sum << endl;
		}

		// update PR for each page
		double total = 0;
		for (set<WebPage*>::iterator it = pages.begin(); it != pages.end(); ++it) {
			if (temp.find((*it)->filename()) == temp.end()) cout << "CANNOT FIND" << endl;
			
			// update its PR value ----------------------------------make e a variable in config -------
			double e = 0.15;
			double pr = (1-e)*temp.find((*it)->filename())->second + e/pages.size();
			(*it)->setPR(pr);
			cout << "  " << (*it)->filename() << " " << pr << endl;
			total += pr;
		}
		cout << "  " << total << endl;
	}
}
//the case if encounter a angle bracket "["
void MDparser::angleBracket(std::string& tobeStored, bool& readyToStore, std::ifstream& myinput, myset<std::string>& allWords, myset<std::string>& allLinks){
	string buffer("");

    char letter=(char)myinput.get();
    while(letter!=']'&& !myinput.fail()){
      buffer=buffer+letter;
      letter=(char)myinput.get();
   }
   buffer+="]";
   //parsing,change bool varibale to false
   for (int i=0;i<(int)buffer.size();i++){
   		letter=buffer[i];
   		if(isLetter(letter)){
   			readyToStore=true;
   			tobeStored+=letter;
   		}
   		else if(!isLetter(letter)&&readyToStore){
   			allWords.insert(tobeStored);
   			tobeStored="";
   			readyToStore=false;
   		}
   		else if(!isLetter(letter)&&!readyToStore){
   			//donothing
   		}
   }

   if(!myinput.fail()){
   	   letter=(char)myinput.get(); //get the next letter after']'
		if(letter=='('){
			buffer="";
			letter=(char)myinput.get();
			while(letter!=')'){
				buffer+=letter;
				letter=(char)myinput.get();
			}
			ifstream my_input(buffer.c_str());
			if(!my_input.fail()){
				allLinks.insert(buffer);				
			}
			my_input.close();
			buffer="";
			readyToStore=false;
		}
		else{
			if(isLetter(letter)){
				readyToStore=true;
				tobeStored+=letter;
			}
			else{
				readyToStore=false;
			}
		}
   }
}
Beispiel #4
0
void search() {
    if(!vis[v[0]][v[1]][v[2]][v[3]]) {
        vis[v[0]][v[1]][v[2]][v[3]] = true;
        maxans = max(maxans, ans);
        if(s.size() == 5)
            return;
        for(int i = 0; i < 4; ++i) {
            if(v[i] < n) {
                if(!s.find(bd[v[i]][i])) {
                    s.insert(bd[v[i]][i]);
                }
                else {
                    s.erase(bd[v[i]][i]);
                    ++ans;
                }
                ++v[i];
                search();
                --v[i];
                if(!s.find(bd[v[i]][i])) {
                    s.insert(bd[v[i]][i]);
                    --ans;
                }
                else {
                    s.erase(bd[v[i]][i]);
                }
            }
        }
    }
}
void MDparser::parse(std::string filename, myset<std::string>& allWords, myset<std::string>& allLinks){
	ifstream myinput(filename.c_str());
	string tobeStored("");
	char nextCharacter;
	bool readyToStore=false;
	while(true){
		nextCharacter=(char)myinput.get();
		if(myinput.fail()){
			if(readyToStore){
				allWords.insert(tobeStored);
			}
			myinput.close();
			return;
		}
		//case when the next character is open parathesis
		if(nextCharacter=='['){
			if(readyToStore){
				allWords.insert(tobeStored);
				readyToStore=false;
				tobeStored="";
			}
			angleBracket(tobeStored,readyToStore, myinput, allWords, allLinks);

		}
		else if(isLetter(nextCharacter)){
			readyToStore=true;
			tobeStored+=nextCharacter;

		}
		else if(readyToStore&&!isLetter(nextCharacter)){
			allWords.insert(tobeStored);
			readyToStore=false;
			tobeStored="";
		}

	}
}
Beispiel #6
0
int main() {
    while(scanf("%d", &n) != EOF) {
        if(n == 0)
            break;
        for(int i = 0; i < n; ++i)
            for(int j = 0; j < 4; ++j)
                scanf("%d", &bd[i][j]);
        for(int i = 0; i <= n; ++i)
            for(int j = 0; j <= n; ++j)
                for(int k = 0; k <= n; ++k)
                    fill(vis[i][j][k], vis[i][j][k] + n + 1, false);
        //memset(vis, false, sizeof(vis));
        maxans = ans = 0;
        s.clear();
        for(int i = 0; i < 4; ++i)
            v[i] = 0;
        search();
        printf("%d\n", maxans);
    }
    return 0;
}
Beispiel #7
0
/**
 * Updates the set containing all unique words in the text
 */
void WebPage::all_words(const myset<std::string> & words){
    for(myset<std::string> ::iterator it = words.begin(); it != words.end(); ++it){
      allWords.insert(*it);
    }
}