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;
}
/* Reverse the deque

	param: 	q		pointer to the deque
	pre:	q is not null and q is not empty
	post: 	the deque is reversed
*/
void reverseCirListDeque(struct cirListDeque *q)
{
	//make sure q is not null
	assert(!EQ(q, 0));

	//make sure q is not empty
	assert(!isEmptyCirListDeque(q));

	//declare an iterator
	struct DLink *i = q->Sentinel->next;

	//declare a temp for reversing links in chain
	struct DLink *temp = NULL;

	//reverse the next and prev pointers
	while(!EQ(i, q->Sentinel)) {
		temp = i->prev;
		i->prev = i->next;
		i->next = temp;
		i = i->prev;
	}

	//update sentinal aka change its first and next
	temp = q->Sentinel->next;
	q->Sentinel->next = q->Sentinel->prev;
	q->Sentinel->prev = temp;

}
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 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;
}
/* Remove the back of the deque

	param: 	q		pointer to the deque
	pre:	q is not null and q is not empty
	post:	the back is removed from the deque
*/
void removeBackCirListDeque(struct cirListDeque *q)
{
  	/* DONE: you must write this */	 
	assert(q != 0 && !isEmptyCirListDeque(q));

	_removeLink(q, q->Sentinel->prev);
}
Beispiel #5
0
/* Remove the front of the deque

	param: 	q		pointer to the deque
	pre:	q is not null and q is not empty
	post:	the front is removed from the deque
*/
void removeFrontCirListDeque (struct cirListDeque *q) {
	/* FIXME: you must write this */
        assert(q != 0);
        assert(!isEmptyCirListDeque(q));
	 
        _removeLink(q, q->Sentinel->next);
}
Beispiel #6
0
/* Get the value of the back of the deque

	param: 	q		pointer to the deque
	pre:	q is not null and q is not empty
	post:	none
	ret: 	value of the back of the deque
*/
TYPE backCirListDeque(struct cirListDeque *q)
{
	/* FIXME: you must write this */	 
	/*temporary return value..you may need to change it*/
    assert(!isEmptyCirListDeque(q));
	return(q->Sentinel->prev->value);
}
/* Get the value of the back of the deque

	param: 	q		pointer to the deque
	pre:	q is not null and q is not empty
	post:	none
	ret: 	value of the back of the deque
*/
TYPE backCirListDeque(struct cirListDeque *q)
{
	/* DONE: you must write this */	 
	assert(q != 0 && !isEmptyCirListDeque(q));

	/*temporary return value..you may need to change it*/
	return q->Sentinel->prev->value;
}
/* Remove the back of the deque

	param: 	q		pointer to the deque
	pre:	q is not null and q is not empty
	post:	the back is removed from the deque
*/
void removeBackCirListDeque(struct cirListDeque *q)
{
  	//make sure q is not null
	assert(!EQ(q, 0));

	//make sure q is not empty
	assert(!isEmptyCirListDeque(q));	

	//remove the last link
	_removeLink(q, q->Sentinel->prev); 
}
/* Get the value of the back of the deque

	param: 	q		pointer to the deque
	pre:	q is not null and q is not empty
	post:	none
	ret: 	value of the back of the deque
*/
TYPE backCirListDeque(struct cirListDeque *q)
{
	//make sure q is not null
	assert(!EQ(q, 0));

	//make sure q is not empty
	assert(!isEmptyCirListDeque(q));

	//return the value of the last link
	return (q->Sentinel->prev->value);
}
/* Get the value of the front of the deque

	param: 	q		pointer to the deque
	pre:	q is not null and q is not empty
	post:	none
	ret: 	value of the front of the deque
*/
TYPE frontCirListDeque(struct cirListDeque *q) 
{
	//make sure q is not null
	assert(!EQ(q, 0));

	//make sure q is not empty
	assert(!isEmptyCirListDeque(q));

	//return the value of the first link
	return (q->Sentinel->next->value);
}
/* Remove a link from the deque

	param: 	q		pointer to the deque
	param: 	lnk		pointer to the link to be removed
	pre:	q is not null and q is not empty
	post:	the link is removed from the deque
*/
void _removeLink(struct cirListDeque *q, struct DLink *lnk)
{
	/* DONE: you must write this */	 
	assert(q != 0 && !isEmptyCirListDeque(q));

	lnk->prev->next = lnk->next;
	lnk->next->prev = lnk->prev;

	free(lnk);
	q->size--;
}
/* Get the value of the back of the deque

	param: 	q		pointer to the deque
	pre:	q is not null and q is not empty
	post:	none
	ret: 	value of the back of the deque
*/
TYPE backCirListDeque(struct cirListDeque *q)
{
    /* FIXME: you must write this */
    /* temporary return value..you may need to change it*/
    /* return(1);*/
    assert (q != 0);                    /* q is not null */
    assert(!isEmptyCirListDeque(q));    /* q is not empty */

    return q->Sentinel->prev->value;

}
/* Remove the front of the deque

	param: 	q		pointer to the deque
	pre:	q is not null and q is not empty
	post:	the front is removed from the deque
*/
void removeFrontCirListDeque (struct cirListDeque *q)
{
	//make sure q is not null
	assert(!EQ(q, 0));

	//make sure q is not empty
	assert(!isEmptyCirListDeque(q));

	//remove the first link
	_removeLink(q, q->Sentinel->next);

}
Beispiel #14
0
/* Adds a link after another link

	param: 	q		pointer to the deque
	param: 	lnk		pointer to the existing link in the deque
	param: 	newLnk	pointer to the new link to add after the existing link
	pre:	q is not null and q is not empty
	pre: 	lnk and newLnk are not null
	pre:	lnk is in the deque
	post:	the new link is added into the deque after the existing link
*/
void _addLinkAfter(struct cirListDeque *q, struct DLink *lnk, struct DLink *newLnk)
{
	assert(!isEmptyCirListDeque(q));

	/* re-allocate pointers */
  	newLnk->next = lnk->next;
	newLnk->prev = lnk;
	lnk->next->prev = newLnk;
	lnk->next = newLnk;

	/* increase the size of the deque */
	q->size++;
}
/* Print the links in the deque from front to back

	param: 	q		pointer to the deque
	pre:	q is not null and q is not empty
	post: 	the links in the deque are printed from front to back
*/
void printCirListDeque(struct cirListDeque *q)
{
	/*  DONE: you must write this */	 
	assert(q != 0 && !isEmptyCirListDeque(q));

	struct DLink *cursor = q->Sentinel->next;

	while (cursor != q->Sentinel){
		printf("%g ", cursor->value);
		cursor = cursor->next;
	}
	printf("\n");
}
Beispiel #16
0
/* Print the links in the deque from front to back

	param: 	q		pointer to the deque
	pre:	q is not null and q is not empty
	post: 	the links in the deque are printed from front to back
*/
void printCirListDeque(struct cirListDeque *q)
{
	/* FIXME: you must write this */
    assert(q != 0);
    assert(!isEmptyCirListDeque(q));
    struct DLink* printLink = q->Sentinel->next;
    
    while (printLink != q->Sentinel) {
        printf("%g\n", printLink->value);
        printLink = printLink->next;
    }

}
Beispiel #17
0
/* Print the links in the deque from front to back

	param: 	q		pointer to the deque
	pre:	q is not null and q is not empty
	post: 	the links in the deque are printed from front to back
*/
void printCirListDeque(struct cirListDeque *q)
{
	struct DLink *lnk;
	assert(!isEmptyCirListDeque(q));
	lnk = q->last->next;
	do
	{
		printf("%f\n", lnk->value);
		lnk = lnk->next;
	}
	while(!EQ(lnk, q->last->next));

	printf("+++++++++++\n");
}
Beispiel #18
0
/* Adds a link to the front of the deque

	param: 	q		pointer to the deque
	param: 	val		value for the link to be added
	pre:	q is not null
	post:	a link storing val is added to the front of the deque
*/
void addFrontCirListDeque(struct cirListDeque *q, TYPE val)
{
	struct DLink * lnk = _createLink(val);
	if (isEmptyCirListDeque(q))
	{
		/* if q is currently empty, the added link becomes the only link in the deque */
	  	lnk->next = lnk;
		lnk->prev = lnk;
		q->size++;
		/* change last to the newly added link */
		q->last = lnk;
	}
	else
		_addLinkAfter(q, q->last, lnk);
}
Beispiel #19
0
/* Print the links in the deque from front to back

	param: 	q		pointer to the deque
	pre:	q is not null and q is not empty
	post: 	the links in the deque are printed from front to back
*/
void printCirListDeque(struct cirListDeque *q)
{
	/* FIXME: you must write this */
        assert(q != 0);
        assert(!isEmptyCirListDeque(q));
	 
        struct DLink * temp = q->Sentinel->next;

        printf("List Contains: ");
        while (temp != q->Sentinel)
        {
           printf("%g ", temp->value);
           temp = temp->next;
        }
        printf("\n");
}
Beispiel #20
0
/* Remove a link from the deque

	param: 	q		pointer to the deque
	param: 	lnk		pointer to the link to be removed
	pre:	q is not null and q is not empty
	post:	the link is removed from the deque
*/
void _removeLink(struct cirListDeque *q, struct DLink *lnk)
{
	/* FIXME: you must write this */
        assert(q != 0);
        assert(!isEmptyCirListDeque(q));

        lnk->next->prev = lnk->prev;
        lnk->prev->next = lnk->next;

        // Fee Allocated Memory
        free(lnk);

        // Decrese Size of Dequeue 
        q->size--;	 

}
/* Print the links in the deque from front to back

	param: 	q		pointer to the deque
	pre:	q is not null and q is not empty
	post: 	the links in the deque are printed from front to back
*/
void printCirListDeque(struct cirListDeque *q)
{
    /* FIXME: you must write this */
    assert (q != 0);                   /* q is not null */
    assert(!isEmptyCirListDeque(q));   /* q isn't empty */

    struct DLink *tmp = q->Sentinel->next; /* iterator for moving thru links */

    int i;
    for (i = 0; i < (q->size); i++)
    {
        printf(" %f\n", tmp->value);
        tmp = tmp->next;
    }
    printf("\n");
}
/* Print the links in the deque from front to back

	param: 	q		pointer to the deque
	pre:	q is not null and q is not empty
	post: 	the links in the deque are printed from front to back
*/
void printCirListDeque(struct cirListDeque *q)
{
	//make sure q is not null
	assert(!EQ(q, 0));

	//make sure q is not empty
	assert(!isEmptyCirListDeque(q));	

	//new iterator
	struct DLink *i = q->Sentinel->next;

	//while iterator has not reached the end of the linked list
	while(!EQ(i, q->Sentinel)) {
		printf("%g ", i->value);	//print the value
		i = i->next;	//update
	}
	printf("\n"); 
}
/* Remove a link from the deque

	param: 	q		pointer to the deque
	param: 	lnk		pointer to the link to be removed
	pre:	q is not null and q is not empty
	post:	the link is removed from the deque
*/
void _removeLink(struct cirListDeque *q, struct DLink *lnk)
{
	//make sure q is not null
	assert(!EQ(q, 0));

	//make sure q is not empty
	assert(!isEmptyCirListDeque(q));

	//make sure chain does not break
	lnk->prev->next = lnk->next;
	lnk->next->prev = lnk->prev;

	//free link
	free(lnk);

	//decrement size
	q->size--;
}
/* 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 */

}
Beispiel #25
0
/* Reverse the deque

	param: 	q		pointer to the deque
	pre:	q is not null and q is not empty
	post: 	the deque is reversed
*/
void reverseCirListDeque(struct cirListDeque *q)
{
	/* FIXME: you must write this */
        assert(q != 0);
        assert(!isEmptyCirListDeque(q));
       
        struct DLink * first = q->Sentinel;
        struct DLink * temp = q->Sentinel->next;
	 
        int i;
        for (i = 0; i <= q->size; i++)
        {
           first->next = first->prev;
           first->prev = temp;
           first = temp;
           temp = temp->next;
        }
}
/* Remove a link from the deque

	param: 	q		pointer to the deque
	param: 	lnk		pointer to the link to be removed
	pre:	q is not null and q is not empty
	post:	the link is removed from the deque
*/
void _removeLink(struct cirListDeque *q, struct DLink *lnk)
{
    /* FIXME: you must write this */
    assert (q != 0);                    /* q is not null */
    assert(!isEmptyCirListDeque(q));    /* q is not empty */

    /* make sure lnk is a thing that exists (not null and size !=0) */
    assert (lnk != 0);
    assert (q->size != 0);

    /* connect lnk's neighbors before freeing lnk */
    lnk->prev->next = lnk->next;
    lnk->next->prev = lnk->prev;

    free(lnk);                          /* free at last! */

    q->size--;

}
Beispiel #27
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 #28
0
/* Reverse the deque

	param: 	q		pointer to the deque
	pre:	q is not null and q is not empty
	post: 	the deque is reversed
*/
void reverseCirListDeque(struct cirListDeque *q)
{
	/* FIXME: you must write this */
    assert(q != 0);
    assert(!isEmptyCirListDeque(q));
    struct DLink* test = q->Sentinel->next;
    struct DLink* testNext;
    
    while (test != q->Sentinel) {
        testNext = test->next;
        test->next = test->prev;
        test->prev = testNext;
        test = testNext;
    }
    testNext = q->Sentinel->next;
    q->Sentinel->next = q->Sentinel->prev;
    q->Sentinel->prev = testNext;
    

}
Beispiel #29
0
/* Remove a link from the deque

	param: 	q		pointer to the deque
	param: 	lnk		pointer to the link to be removed
	pre:	q is not null and q is not empty
	pre:	lnk is in the deque
	post:	the link is removed from the deque
*/
void _removeLink(struct cirListDeque *q, struct DLink *lnk)
{
	assert(!isEmptyCirListDeque(q));
  	if (EQ(lnk, lnk->next))
	{
		/* if lnk is the only link in the deque, set last to null */
	  	q->last = 0;
	}
	else
	{
		lnk->next->prev = lnk->prev;
		lnk->prev->next = lnk->next;
		/* if lnk is the last link, change last to the previous of lnk */
		if (EQ(lnk, q->last))
			q->last = lnk->prev;
	}

	free(lnk);
	q->size--;
}
/* Reverse the deque

	param: 	q		pointer to the deque
	pre:	q is not null and q is not empty
	post: 	the deque is reversed
*/
void reverseCirListDeque(struct cirListDeque *q)
{
	/* DONE: you must write this */
	assert(q != 0 && !isEmptyCirListDeque(q));

	struct DLink *cursor = q->Sentinel;
	struct DLink *nextLink;
	struct DLink *swapTemp;

	do {
		//retrieve the next link before making any changes
		nextLink = cursor->next;

		//swap next and previous
		swapTemp = cursor->next;
		cursor->next = cursor->prev;
		cursor->prev = swapTemp;

		cursor = nextLink;
	} while (cursor != q->Sentinel);
}