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 DFS(Graph* g, Vertex* source, Vertex* destination)
{
	/* FIXME you will write this */
	struct cirListDeque *que = malloc(sizeof(struct cirListDeque));
	struct Vertex *cur = malloc(sizeof(struct Vertex));

		clearVisited(g);
		initCirListDeque(que);
		addFrontCirListDeque(que, source);

		while(!isEmptyCirListDeque(que)){
			cur = frontCirListDeque(que);
			removeFrontCirListDeque(que);
			if(cur == destination){
				return 1;
			}

			else if(!cur->isVisited){
				int i;
				for(i=0; i<cur->numNeighbors; ++i){
					if(!cur->neighbors[i]->isVisited){
						addFrontCirListDeque(que, cur->neighbors[i]);
						cur->neighbors[i]->isVisited = 1;
					}
				}
				cur->isVisited =1;
			}

		}
		return 0;
}
Beispiel #2
0
int main(){
	struct cirListDeque* q = createCirListDeque(); 
	addBackCirListDeque(q, (TYPE)1);
	addBackCirListDeque(q, (TYPE)2);
	addBackCirListDeque(q, (TYPE)3);
	addFrontCirListDeque(q, (TYPE)4);
	addFrontCirListDeque(q, (TYPE)5);
	addFrontCirListDeque(q, (TYPE)6);
	printCirListDeque(q);
	printf("\n");
	//6, 5, 4, 1, 2, 3
	printf("%g\n", frontCirListDeque(q));
	printf("\n");
	//6
	printf("%g\n", backCirListDeque(q));
	printf("\n");
	//3
	removeFrontCirListDeque(q);
	//5, 4, 1, 2, 3
	removeBackCirListDeque(q);
	//5, 4, 1, 2
	printCirListDeque(q);
	printf("\n");
	//5, 4, 1, 2
	reverseCirListDeque(q);
	//2, 1, 4, 5
	printCirListDeque(q);
	printf("\n");
	//2, 1, 4, 5
	deleteCirListDeque(q);
	return 0;
}
Beispiel #3
0
int main(){
    struct cirListDeque* q = createCirListDeque();
    addBackCirListDeque(q, (TYPE)1);
    addBackCirListDeque(q, (TYPE)2);
    addBackCirListDeque(q, (TYPE)3);
    addFrontCirListDeque(q, (TYPE)4);
    addFrontCirListDeque(q, (TYPE)5);
    addFrontCirListDeque(q, (TYPE)6);
    printCirListDeque(q);
    printf("%g\n", frontCirListDeque(q));
    printf("%g\n", backCirListDeque(q));
    removeFrontCirListDeque(q);
    removeBackCirListDeque(q);
    printCirListDeque(q);
    reverseCirListDeque(q);
    printCirListDeque(q);
    return 0;
}
Beispiel #4
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 #5
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;
}
Beispiel #6
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 */
	//use stack, add front, remove front
	int contains = 0;
	struct cirListDeque *stack = malloc(sizeof(struct cirListDeque));
	initCirListDeque(stack);

	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)
						{
							addFrontCirListDeque(stack, current->neighbors[x]);
						}
					}
					if(!isEmptyCirListDeque(stack))
					{
					current = frontCirListDeque(stack);
					removeFrontCirListDeque(stack);
					}
					else
						return contains;
				}
				else
				{
					if(!isEmptyCirListDeque(stack))
					{
					current = frontCirListDeque(stack);
					removeFrontCirListDeque(stack);
					}
					else
						return contains;
				}
			
		}
	if(current == destination)
		{contains = 1;}
	removeAllCirListDeque(stack);

	return contains;
}
int main () {
	struct cirListDeque testDeque;

	printf("Testing Deque ADT based on Circularly-Doubly-Linked List \n");
	/*Initialize the deque */
	initCirListDeque(&testDeque);
	/*Add 3 to the back */
	addBackCirListDeque(&testDeque, 3);
	/*Add 4 to the back */
	addBackCirListDeque(&testDeque, 4);
	/*Add 5 to the back */
	addBackCirListDeque(&testDeque, 5);

	/*Should print 3.0  4.0  5.0 */
	printCirListDeque(&testDeque);
	
	/*Add 2 to the front */
	addFrontCirListDeque(&testDeque, 2);
	/*Add 1 to the front */
	addFrontCirListDeque(&testDeque, 1);

	/*Should print 1.0, 2.0, 3.0,  4.0,  5.0 */
	printCirListDeque(&testDeque);

	/*Should print front: 5.0; back: 1.0 */
	printf("front: %g; back: %g\n\n", frontCirListDeque(&testDeque), backCirListDeque(&testDeque));
	/*Remove the back of the deque */
	removeBackCirListDeque(&testDeque);
	/*Should print front: 5.0; back: 2.0 */
	printf("front: %g; back: %g\n\n", frontCirListDeque(&testDeque), backCirListDeque(&testDeque));
	/*Remove the front of the deque */
	removeFrontCirListDeque(&testDeque);
	/*Should print front: 4.0; back: 2.0 */
	printf("front: %g; back: %g\n\n", frontCirListDeque(&testDeque), backCirListDeque(&testDeque));
	freeCirListDeque(&testDeque);

	return EXIT_SUCCESS;
}
Beispiel #8
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)
{
	/* DONE you will write this */
    /* use a queue */
    clearVisited(g);
    cirListDeque *queue = malloc(sizeof(cirListDeque));
    
    initCirListDeque(queue);
    
    Vertex *current = source;
    int i;
    for (i = 0; i < current->numNeighbors; i++) {
        addFrontCirListDeque(queue, current->neighbors[i]);
    }
    current->isVisited = 1;
    
    
    while (! isEmptyCirListDeque(queue)) {
        current = backCirListDeque(queue);
        removeBackCirListDeque(queue);
        if (current == destination) {
            return 1;
        }
        current->isVisited = 1;
        
        
        for (i = 0; i < current->numNeighbors; i++) {
            if (! current->neighbors[i]->isVisited) {
                addFrontCirListDeque(queue, current->neighbors[i]);
            }
            
        }
        
    }
    
	return 0;
}
int main()
{
	struct cirListDeque* q = createCirListDeque(); 
	addBackCirListDeque(q, (TYPE)1);				//adds to back of deque 3 times
	addBackCirListDeque(q, (TYPE)2);
	addBackCirListDeque(q, (TYPE)3);
	addFrontCirListDeque(q, (TYPE)4);				//adds to front of deque 3 times
	addFrontCirListDeque(q, (TYPE)5);
	addFrontCirListDeque(q, (TYPE)6);				// should end up being 6 5 4 1 2 3
	printf("Printing list.\n");
	printCirListDeque(q);
	printf("\nDisplaying front and back of list.");
	printf("\nFront: %g", frontCirListDeque(q));
	printf("\nBack: %g\n", backCirListDeque(q));
	removeFrontCirListDeque(q);						// removes 6
	removeBackCirListDeque(q);						// removes 3
	printf("\nList with front and back removed.\n");
	printCirListDeque(q);
	printf("\nList will get reversed.\n");
	reverseCirListDeque(q);							// reverses deque should be 2 1 4 5
	printCirListDeque(q);
	printf("\n");
	return 0;
}