22C:30, 22C:115 Solution to Exam 2


  1. 	bits[i] = true;
    	genBitStrings(bits, i+1);
    	bits[i] = false;
    	genBitStrings(bits, i+1);
    

  2. Before getting rid of the neighbor of i using list_clear(..) insert the following code.
    	node* head = myVertices[i] -> link();
    	while(head != NULL){
    		// get the name of the current neighbor and then its index
    		string nbrName = head -> data();
    		int j = getIndex(nbrName);
    
    		// Search for the current vertex in neighbor's list
    		node* nbrHead = & myVertices[j];
    		while((nbrHead->link())->data() != name)
    			nbrHead = nbrHead -> link();
    
    		// Delete the current vertex from neighbor's list
    		list_remove(nbrHead);
    	
    		// Go to the next neighbor
    		head = head -> link();
    	}
    

  3. 	graph::~graph(){
    		for(int i = 0; i < myNumberVertices; i++)
    			list_clear(myVertices[i]->link());
    	}
    

  4. I show the BFS by identifying the parent of each vertex in the tree below. The root is vertex A.
    	VERTEX			PARENT
    	B			A
    	C			D
    	D			A
    	E			D
    	F			D
    	G			C
    	H			G
    

  5. The maximum amount of memory used by the local vectors list1 and list2 at any particular time in the execution of mergeSort is:
    4*256 + 4*128 + 4*64 + 4*32 + 4*16 + 4*8 + 4*4 + 4*2 = 4*510 = 2040