/* 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 */ 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){ addBackCirListDeque(que, cur->neighbors[i]); cur->neighbors[i]->isVisited = 1; } } cur->isVisited =1; } } return 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; }
/* 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; }
/* 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; }
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; }
/* 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; }
/* 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 */ clearVisited(g); cirListDeque queue; Vertex *currentVertex; int i; initCirListDeque(&queue); addBackCirListDeque(&queue, source); while(!isEmptyCirListDeque(&queue)) { currentVertex = frontCirListDeque(&queue); removeFrontCirListDeque(&queue); if(currentVertex->label == destination->label) return 1; else { if(currentVertex->isVisited == 0) currentVertex->isVisited = 1; for (i = 0; i < currentVertex->numNeighbors; i++) { if(currentVertex->label == destination->label) return 1; else { if(currentVertex->neighbors[i]->isVisited == 0) addBackCirListDeque(&queue, currentVertex->neighbors[i]); } } } } return 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 */ /* DFSRecursive (g, source, destination) */ assert( g != NULL ); assert( source != NULL ); assert( destination != NULL ); clearVisited(g); /* printf("Searching for %c from %c\n",destination->label,source->label); */ cirListDeque stack; /* a stack can be a special instance of deque */ /* can't use a pointer for 'stack' because there's nothing for it to point at */ Vertex* current; int i; /* C99 isn't included on this assignment for some reason */ initCirListDeque(&stack); addBackCirListDeque(&stack,source); /* push vertex source to top of stack */ /* printf("pushed %c\n",source->label); */ while( !isEmptyCirListDeque(&stack) ) /* loop until &stack is empty */ { /* printf("(SOL) current stack: "); */ /* printCirListDeque(&stack); */ /* printf("\n"); */ current = backCirListDeque(&stack); /* pop vertex from &stack */ removeBackCirListDeque(&stack); /* printf("removed %c\n", current->label); */ /* printf("current->label = %c\n",current->label); */ if ( current->label == destination->label ) { /* printf("found %c\n",destination->label); */ return 1; } else { if ( current->isVisited == 0 ) /* mark as visited if not already */ { current->isVisited = 1; /* printf("marked %c as visited\n",current->label); */ } else { /* printf("Skipping %c because it visited already.\n",current->label); */ } /* printf("checking %d neighbors\n",current->numNeighbors); */ for ( i = 0; i < current->numNeighbors; i++ ) /* push neighbors if necessary */ { if ( current->label == destination->label ) { /* printf("found %c\n",destination->label); */ return 1; } else { if ( current->neighbors[i]->isVisited == 0 ) { addBackCirListDeque( &stack,current->neighbors[i] ); /* printf("pushed %c\n",current->neighbors[i]->label); */ } /* else */ /* printf("Skipping %c because it visited already.\n",current->neighbors[i]->label); */ } } } } return 0; }