Пример #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;
}
Пример #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 */
	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;
}
Пример #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("\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;
}
Пример #4
0
/* De-allocate all links of the deque

	param: 	q		pointer to the deque
	pre:	none
	post:	All links (including Sentinel) are de-allocated
*/
void freeCirListDeque(struct cirListDeque *q){
	//Remove and free all links in list
    while(q->Sentinel->next != q->Sentinel)
        removeFrontCirListDeque(q);
    
    //Free sentinel
    free(q->Sentinel);
}
Пример #5
0
/* De-allocate all links of the deque

	param: 	q		pointer to the deque
	pre:	none
	post:	All links (including Sentinel) are de-allocated
*/
void freeCirListDeque(struct cirListDeque *q)
{
    while(q->size > 0)
	{
        removeFrontCirListDeque(q);
    }
    free(q->Sentinel);
}
Пример #6
0
/* De-allocate all links of the deque

	param: 	q		pointer to the deque
	pre:	none
	post:	All links (including Sentinel) are de-allocated
*/
void freeCirListDeque(struct cirListDeque *q)
{
	//free every link by just continually removing the top until size = 0
	while(!EQ(q->size, 0)) {
		printf("Deleting link that holds %g...\n", frontCirListDeque(q));
		removeFrontCirListDeque(q);
	}

	//free the sentinel
	free(q->Sentinel);
}
Пример #7
0
/* De-allocate all links of the deque

	param: 	q		pointer to the deque
	pre:	none
	post:	All links (including Sentinel) are de-allocated
*/
void freeCirListDeque(struct cirListDeque *q)
{
	/* FIXME: you must write this */
	while (q->size != 0){
		printf("Deleting: %g   \n", frontCirListDeque(q));
		removeFrontCirListDeque(q);
	}
     /* free memory up again */
	free(q->Sentinel);
	q->Sentinel = 0;


}
Пример #8
0
/* De-allocate all links of the deque

	param: 	q		pointer to the deque
	pre:	none
	post:	All links (including Sentinel) are de-allocated
*/
void freeCirListDeque(struct cirListDeque *q)
{
    /* FIXME: you must write this */
    /* first, make sure some things are true: */
    assert (q != 0);                   /* q is not null */
    assert(!isEmptyCirListDeque(q));   /* q isn't empty, yet */
    assert(q->size != 0);              /* q isn't empty, yet */

    while (q->size > 0)
    {
        removeFrontCirListDeque(q); /* could also removeBack... */
        /* q->size is decremented in _removeLink() */
    }
    assert(q->size == 0);              /* q should be empty now */
    free (q->Sentinel);                /* we are freeing Sent, not q (the deque) */
    q->Sentinel = 0;                   /* just to be safe */

}
Пример #9
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;
}
Пример #10
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;
}
Пример #11
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)
{
	/* DONE you will write this */
    /* use a stack */
    clearVisited(g);
    cirListDeque *stack = malloc(sizeof(cirListDeque));

    initCirListDeque(stack);
    
    Vertex *current = source;
    int i;
    for (i = 0; i < current->numNeighbors; i++) {
        addFrontCirListDeque(stack, current->neighbors[i]);
    }
    current->isVisited = 1;
    
    
    while (! isEmptyCirListDeque(stack)) {
        current = frontCirListDeque(stack);
        removeFrontCirListDeque(stack);
        if (current == destination) {
            return 1;
        }
        current->isVisited = 1;
        
        
        for (i = 0; i < current->numNeighbors; i++) {
            if (! current->neighbors[i]->isVisited) {
                addFrontCirListDeque(stack, current->neighbors[i]);
            }
            
        }
        
    }
    
	return 0;
}
Пример #12
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;
}
Пример #13
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;
}