Esempio n. 1
0
void test_sequential_random_Insert_DeleteMin(){
	
	FibHeap fib;
	priority_queue<int> ref_q;
	int NUM_NODES = 100000;
	
	for (int i = 0; i < NUM_NODES; i++){
		int curr = 	rand();
		fib.insertNode(curr);
		ref_q.push(-curr);			
		}

	for (int i = 0; i < NUM_NODES; i++){
		int val= fib.deleteMin();
		int ref = -ref_q.top();
		ref_q.pop();
	
		if (val != ref){
			cout << "\n Our value: " << val << " Ref Value: " << ref;
			return;
		}
	}
	cout << "\n Sequential Insert then Delete Succeeded";

	for (int i = 0; i < NUM_NODES; i++){
		int curr = 	rand();
		fib.insertNode(curr);
		ref_q.push(-curr);			
		if (i%50 == 0){
		int num_deletes = rand() % fib.getSize()+1;
			for (int j = 0; j < num_deletes; j++){
				int val= fib.deleteMin();
				int ref = -ref_q.top();
				ref_q.pop();
			
				if (val != ref){
					cout << "\n Random : Our value: " << val << " Ref Value: " << ref;
					return;
				}
			}
		}
	}
	cout << "\n Random Insert and Delete Succeeded";

}
Esempio n. 2
0
void test_decrease_key(){
	FibHeap fib;
	int NUM_NODES = 100;
	
	boost_heap pq;	
	map<int, boost_heap::handle_type> index;
	vector<int> keys;
	set<int> keyset;  // Used for getting logarithmic random generation and duplicate detection
	
	for (int i = 0; i < NUM_NODES; i++){
		unsigned int curr = 	rand();
		if (present(keyset, curr)) continue;
		
		fib.insertNode(curr);
		index[curr]=pq.push(curr);			
		keys.push_back(curr);
		keyset.insert(curr);
		
		if (i % 25 == 0){
			//Do some random decrease keys
			for (int j = 0; j < 20; j++){
				//Pick a key to change
				unsigned int to_change_index = rand() % keys.size(); 
				unsigned int to_change_val = keys[to_change_index];
				
				if (to_change_val == 0) continue;

				//Find a smaller value to change to which doesnt already exist
				// Or try for a bit
				unsigned int new_val = rand() % to_change_val;
				
				for (int k = 0; k < 100; k++){
				 if (present(keyset, new_val))
				  new_val = rand() % to_change_val;
				 else break;
				}
				if (present(keyset,new_val)) continue;
				
				//replace the old value with new one
				keys[to_change_index] = new_val;
				keyset.erase(to_change_val);
				keyset.insert(new_val);
				
				//perform operation on heaps
				pq.decrease(index[to_change_val],new_val);
				index[new_val] = index[to_change_val];
				index.erase(to_change_val);
				
				fib.decreaseKey(to_change_val, new_val);
				
			}
		}
		
		if (i%50 == 0){
			int num_deletes = rand() % fib.getSize()+1;
			for (int j = 0; j < num_deletes; j++){
				unsigned int val= fib.deleteMin();
				unsigned int ref = pq.top();
				pq.pop();
			
				if (val != ref){
					cout << "\n Random : Our value: " << val << " Ref Value: " << ref;
					return;
				}
				
				keyset.erase(val);
				
				std::vector<int>::iterator position = std::find(keys.begin(), keys.end(), val);
				if (position != keys.end()) // == vector.end() means the element was not found
    				keys.erase(position);
				else cout << "Should never get here, keys messed up";
				
				index.erase(val);
			}
		}
			
	}
	
	
cout <<"Test completed";
	
}