Esempio n. 1
0
int Board::findPath(Crossroad* start, Crossroad* end, std::queue<Crossroad*>* pathfinder, PathType pathType) {

	if ((end->constructed == FREE || end->constructed == FLAG) && (start->constructed == FLAG || (start->constructed == ROAD && buildingRoad == true))) {

		clock_t start_time = clock();
		int return_value = breadthFirst(start, end, pathfinder, pathType);

		
		/*
		// for a-starish
		std::priority_queue<Crossroad*, std::vector<Crossroad*>,test> priority;
		int return_value = aStarish(start, end, &priority, pathType);
		*/
		fprintf(stderr, "ms %i \n", timeDiff_ms(start_time));
		if (end->previous != NULL) {
			Crossroad* current = end;
			while (current != start) {
				pathfinder->push(current);
				Crossroad* temp = current->previous;
				current->previous = NULL;
				current = temp;
			}
			pathfinder->push(start);
		}
		return return_value;

	}

	else
		return -1;

}
Esempio n. 2
0
int readGraph(){
    int startVertex = 0 ;
    int n = 0 ;
    scanf("%d",&startVertex);
    scanf("%d",&n);

    int ** matrix = readIn(n);
    printMatrix(matrix,n);

    struct queue* q = (struct queue*)malloc(sizeof(struct queue));
    init(q);
    struct stack stk;
    initialize(&stk);
    int * visited = (int *) malloc(sizeof(int)*n);
    int i = 0 ;
    for ( i = 0 ; i < n ; i++ ) visited[i] = 0 ;

    printf("\nBreadth First: ");
    breadthFirst(matrix,startVertex,n,q);


    printf("\nDepth First: ");
    depthFirst(matrix,startVertex,n,stk);
    printf("\nDepth First: ");
    depthCaller(matrix,n);
    printf("\n");

    free(q);
    free(visited);
    free(matrix);

    return 0 ;
}
Esempio n. 3
0
int Board::breadthFirst(Crossroad* start, Crossroad* end, std::queue<Crossroad*>* pathfinder, PathType pathType) {
	/*system("pause"); //DEBUG
	paintThySelf(30); //DEBUG
	al_flip_display(); //DEBUG*/

	fprintf(stderr, "distance %i \n", (int) (start->calculateDistance(end)));

	if (start == end) {
		while (!pathfinder->empty()) {
			Crossroad* temp = pathfinder->front();
			temp->visited = false;
			temp->previous = NULL;

			pathfinder->pop();
		}
		//start->previous = NULL;
		start->previous->pathing = true;
		return start->transportationCost();
	}

	else {
		const Directions directions[6] = {NORTH_WEST, NORTH_EAST, EAST, SOUTH_EAST, SOUTH_WEST, WEST };
		for (int i = 0; i < 6; ++i) {
			Crossroad *node = start->getNeighbour(directions[i]);
			if (pathCriteria(pathType, start->roads[i], end, node)) {
				//fprintf(stderr, "E\n"); //DEBUG
				//start->east->built = ROAD; //DEBUG
				node->previous = start;
				pathfinder->push(node);
				node->visited = true;
			}
		}

		if (pathfinder->empty()) {
			return -1;
		}

		//paintThySelf(30); //DEBUG
		//al_flip_display(); //DEGUB
		Crossroad* current = pathfinder->front();
		pathfinder->pop();

		int returnvalue = breadthFirst(current, end, pathfinder, pathType);

		if (start->pathing) {
			start->pathing = false;
			if(start->previous != NULL)
				start->previous->pathing = true;
		}
		else 
			start->previous = NULL;

		current->visited = false;
		return returnvalue;
	}
}
Esempio n. 4
0
int maxFlow(struct node * graph[], int from ,int to, int nV)
{
	struct node * resgraph[nV];
	struct node * temp;
	int i, j, temp_weight;
	int parent[nV];
	int max_flow = 0, path_flow;
	for(i = 0; i < nV; i++)
	{
		resgraph[i] = NULL;
	}
	
	for(i = 0; i < nV; i++)
	{
		temp = graph[i];
		while(temp != NULL)
		{
			addEdge(&resgraph[i], temp->toNode, temp->weight);
			temp = temp->next;
		}
	}
	
	while(breadthFirst(resgraph, from, to, nV, parent))
	{
		j = to;
		path_flow = MAX;
		while(j != from)
		{
			i = parent[j];
			temp_weight = weight(resgraph, i ,j, nV);
			j = i;
			if(path_flow < temp_weight)
				path_flow = path_flow;
			else
				path_flow = temp_weight;
		}
		j = to;
		while(j != from)
		{
			i = parent[j];
			updateWeight(resgraph, i, j, -path_flow);
			updateWeight(resgraph, j, i, path_flow);
			j = i;
		}
		max_flow += path_flow;
	}
	return max_flow;
}
Esempio n. 5
0
/**
 * Run multiple test.
 * @param transaction_list List of itemset and expression value.
 * @param trans4lcm File name for argument of LCM program. This file is made in this method.
 * @param threshold The statistical significance threshold.
 * @param set_method The procedure name for calibration p-value (fisher/u_test).
 * @param max_comb The maximum arity limit.
 * @param outlog File name for logging.
 * @param alternative hypothesis, 1 -> greater, 0 -> two sided, -1 -> less
 * @param max_lambda Return value of maximum lambda.
 * @return 
 */
int LampCore::runMultTest(const std::vector<Transaction*>& transaction_list, std::string& trans4lcm,
		double threshold, std::string& set_method, int max_comb,
		std::ostream& outlog, int alternative, double max_lambda) {
	int lam_star = 1;
	double k = -1;
	try {
		if (set_method.compare("fisher") == 0) {
			func_f = new Functions4fisher(transaction_list, std::abs(alternative));
		}
		else if (set_method.compare("u_test") == 0) {
			func_f = new Functions4u_test(transaction_list, alternative);
		}
		else if (set_method.compare("chi") == 0) {
			func_f = new Functions4chi(transaction_list, std::abs( alternative));
		}
		else {
			throw std::string("Error: choose \"fisher\", \"chi\" or \"u_test\" by using -p option.");
		}
		double lam = max_lambda;
		
		// check a MASL of max_lambda
		double n1 = 0.0;
		if (std::find(BINARY_METHODS.begin(), BINARY_METHODS.end(), set_method) !=  BINARY_METHODS.end()) {
			n1 = func_f->sumValue();
			if (n1 < max_lambda) {
				max_lambda = int( n1 );
				lam = int( n1 );
			}
		}
		
		fre_pattern->makeFile4Lem(transaction_list, trans4lcm); // make itemset file for lcm
		
		// If Fisher's exact test or chi-square test is used for computing P-value, 
		// LCM-LAMP is run to find optimal lambda.
		if (set_method == "fisher") {
			int neg_size = func_f->getAllSize() - func_f->getN1();
			n1 = std::min( n1, (double)neg_size );
			// # of positives == # of negatives, and two.sided hypothesis test.
			if (( func_f->getN1() == neg_size ) && (alternative == 0)) {
				lam_star = depthFirst( trans4lcm, max_comb, n1, 0.5*threshold, 1 );
			} else {
				lam_star = depthFirst( trans4lcm, max_comb, n1, threshold, 1 );
			}
		}
		else if (set_method == "chi") {
			int neg_size = func_f->getAllSize() - func_f->getN1();
			n1 = std::min( n1, (double)neg_size );
			// two-sided hypothesis test
			if (alternative == 0) {
				lam_star = depthFirst( trans4lcm, max_comb, n1, 0.5*threshold, 2 );
			}
			// one-sided
			else {
				lam_star = depthFirst( trans4lcm, max_comb, n1, threshold, 2 );
			}
		}
		// If Mann-Whitney U test of Chi-square test is used,
		// LAMP ver 1. is run for computing the optimal lambda. 
		else {
			// two-sided hypothesis test
			if (alternative == 0) {
				lam_star = breadthFirst( trans4lcm, max_comb, 0.5*threshold, lam, outlog );
			}
			// one-sided hypothesis test
			else {
				lam_star = breadthFirst( trans4lcm, max_comb, threshold, lam, outlog );
			}
		}

		fre_pattern->frequentPatterns( trans4lcm, lam_star, max_comb ); // P_lambda* at line 13
		k = fre_pattern->getTotal( lam_star );
	} catch (std::string &msg) {
		throw msg;
	} catch (...) {
		throw std::string("Error: An unexpected error occurred while trying multiple test.");
	}

	// multiple test by using k and lambda_star
	outlog << "finish calculation of K: " << k << std::endl;
	// If lam_star > max_lambda, m_lambda set to max_lambda.
	// This case cause when optimal solution is found at first step.
	outlog << lam_star << std::endl;
	if (max_lambda < lam_star)
		lam_star = max_lambda;

	return lam_star;
}