Beispiel #1
0
/* This function should return a boolean (encoded as an int) indicating
 * whether or not a path exists between the argument vertices.
 * param: g			Graph to perform the search in
 * param: source	Vertex to originate the search from
 * param: destination Vertex to stop the search from (if it is found)
 * ret: boolean indicating whether or not a path exists
 */
int BFS(Graph* g, Vertex* source, Vertex* destination)
{
	/* FIXME you will write this */
	//use queue
	int contains = 0;
	struct cirListDeque *queue = malloc(sizeof(struct cirListDeque));
	initCirListDeque(queue);

	clearVisited(g);
	struct Vertex *current;
	current = source;
	
		while(current != destination)
		{ 
				if(current->isVisited == 0)
				{
					current->isVisited = 1;

					for(int x = 0; x < current->numNeighbors; x++)
					{
						if(current->neighbors[x]->isVisited == 0)
						{
							addBackCirListDeque(queue, current->neighbors[x]);
						}
					}
					if(!isEmptyCirListDeque(queue))
					{
					current = frontCirListDeque(queue);
					removeFrontCirListDeque(queue);
					}
					else
						return contains;
				}
				else
				{
					if(!isEmptyCirListDeque(queue))
					{
					current = frontCirListDeque(queue);
					removeFrontCirListDeque(queue);
					}
					else
						return contains;
				}
			
		}
	if(current == destination)
		{contains = 1;}
	removeAllCirListDeque(queue);

	return contains;
}
Beispiel #2
0
/* This function should return a boolean (encoded as an int) indicating
 * whether or not a path exists between the argument vertices.
 * param: g			Graph to perform the search in
 * param: source	Vertex to originate the search from
 * param: destination Vertex to stop the search from (if it is found)
 * ret: boolean indicating whether or not a path exists
 */
int BFS(Graph* g, Vertex* source, Vertex* destination){
	/* FIXME you will write this */

	/* create and initialize circular list deque pointer */
    struct cirListDeque *stack = malloc(sizeof(struct cirListDeque));
    initCirListDeque(stack);

    /*/ create vertex pointer and clear visited nodes */
    struct Vertex *temp = source;
    clearVisited(g);

    /* add to front */
    addFrontCirListDeque(stack, temp);

    /* if not empty, use BFS */
    while(!isEmptyCirListDeque(stack)){

        /*remove back(top) value */
        temp = backCirListDeque(stack);
        removeBackCirListDeque(stack);

        /* check if visited, mark, and return true  */
        if(!temp -> isVisited){
            temp -> isVisited = 1;
        }
        if(temp == destination){

            removeAllCirListDeque(stack);
            return 1;
        }

        /* check is the neighboring nodes have been visited. add to stack if not */
        for(int i = 0; i < temp->numNeighbors; i++){
            if(!temp->neighbors[i]->isVisited){
                addFrontCirListDeque(stack, temp->neighbors[i]);
            }
        }
    }

    return 0;

}
Beispiel #3
0
/* This function should return a boolean (encoded as an int) indicating
 * whether or not a path exists between the argument vertices.
 * param: g			Graph to perform the search in
 * param: source	Vertex to originate the search from
 * param: destination Vertex to stop the search from (if it is found)
 * ret: boolean indicating whether or not a path exists
 */
int DFS(Graph* g, Vertex* source, Vertex* destination){
	/* FIXME you will write this */

	struct cirListDeque *stack = malloc(sizeof(struct cirListDeque));
	initCirListDeque(stack);

	struct Vertex *temp = source;
    clearVisited(g);

	/* add to the front of the circular list deque */
	addFrontCirListDeque(stack, temp);

	/*if it isn't empty, use DFS */
	while(!isEmptyCirListDeque(stack)){

        temp = backCirListDeque(stack);
        removeBackCirListDeque(stack);

        /* check if visited, mark if it hasn't been visited */
        if(!temp -> isVisited){
            temp ->isVisited = 1;
        }

        if(temp == destination){

            /* if you reach the destination, remove from the list to free memory, return 1 */
            removeAllCirListDeque(stack);
            return 1;
        }

        /* check is the neighboring nodes have been visited. add to stack if not */
        for(int i = 0; i < temp->numNeighbors; i++){
            if(!temp->neighbors[i] -> isVisited){
                addFrontCirListDeque(stack, temp->neighbors[i]);
            }
        }
	}

    return 0;
}