Survey
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
PESIT BANGALORE SOUTH CAMPUS, BENGALURU DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING Second Internal Assessment Data Structures (3rd CSE A) Solution Key 1.a) The basis of the algorithm is the following fact: Why is this true? We can rewrite m as follows: Now any divisor d common to m and n must divide the first term with no remainder, since it is the product of n and an integer. Therefore, d must also divide the second term since d divides m and m is the sum of the two terms. Since any divisor common to m and n must divide the remainder of m/n, we know that, in particular, the gcd does, since it is a common divisor. It just happens to be the greatest such divisor. So by taking the GCD(n, remainder of m/n), we can "close in quickly" on the GCD of m and n. (This is extremely clever -- you wouldn't be expected to come up with something like this in an algorithm question on a CS101 exam.) Now we can write: int gcd(int m, int n) { if ((m % n) == 0) return n; else return gcd(n, m % n); } gcd(468, 24) gcd(24, 12) => 12 gcd(135, 19) gcd(19, 2) gcd(2, 1) => 1 1.b) int convert(int dec) { if (dec == 0) { return 0; } else { return (dec % 2 + 10 * convert(dec / 2)); } } 2.a) function queuepush (obj) stack1.push(obj) function queuepop () if stack2 is empty if stack1 is empty return null while stack1 is not empty stack2.push(stack1.pop()) return stack2.pop() Complexity The push function is just a simple push on to the first stack which is O(1). The pop function’s worst case will still be O(n), but only when the second stack is empty. If the second stack contains items however then the algorithm is O(1). 2.b) void towers(int num, char frompeg, char topeg, char auxpeg) { if (num == 1) { printf("\n Move disk 1 from peg %c to peg %c", frompeg, topeg); return; } towers(num - 1, frompeg, auxpeg, topeg); printf("\n Move disk %d from peg %c to peg %c", num, frompeg, topeg); towers(num - 1, auxpeg, topeg, frompeg); } 3.a) #define SIZE 5 /* Size of Circular Queue */ int CQ[SIZE],f=-1,r=-1; /* Global declarations */ CQinsert(int elem) { /* Function for Insert operation */ if( CQfull()) printf("\n\n Overflow!!!!\n\n"); else { if(f==-1)f=0; r=(r+1) % SIZE; CQ[r]=elem; } } int CQdelete() { /* Function for Delete operation */ int elem; if(CQempty()){ printf("\n\nUnderflow!!!!\n\n"); return(-1); } else { elem=CQ[f]; if(f==r){ f=-1; r=-1;} /* Q has only one element ? */ else f=(f+1) % SIZE; return(elem); } } int CQfull() { /* Function to Check Circular Queue Full */ if( (f==r+1) || (f == 0 && r== SIZE-1)) return 1; return 0; } int CQempty() { /* Function to Check Circular Queue Empty */ if(f== -1) return 1; return 0; } display() { /* Function to display status of Circular Queue */ int i; if(CQempty()) printf(" \n Empty Queue\n"); else { printf("Front[%d]->",f); for(i=f;i!=r;i=(i+1)%SIZE) printf("%d ",CQ[i]); printf("%d ",CQ[i]); printf("<-[%d]Rear",r); } } 3.b) Priority Queue is more specialized data structure than Queue. Like ordinary queue, priority queue has same method but with a major difference. In Priority queue items are ordered by key value so that item with the lowest value of key is at front and item with the highest value of key is at rear or vice versa. So we're assigned priority to item based on its key value. Lower the value, higher the priority. Following are the principal methods of a Priority Queue. Basic Operations insert / enqueue − add an item to the rear of the queue. remove / dequeue − remove an item from the front of the queue. void insert(int data){ int i = 0; if(!isFull()){ // if queue is empty, insert the data if(itemCount == 0){ intArray[itemCount++] = data; }else{ // start from the right end of the queue for(i = itemCount - 1; i >= 0; i-- ){ // if data is larger, shift existing item to right end if(data > intArray[i]){ intArray[i+1] = intArray[i]; }else{ break; } } // insert the data intArray[i+1] = data; itemCount++; } } } int removeData(){ return intArray[--itemCount]; } 4.a) FIND-PATH(x, y) 1. if (x,y outside maze) return false 2. if (x,y is goal) return true 3. if (x,y not open) return false 4. mark x,y as part of solution path 5. if (FIND-PATH(North of x,y) == true) return true 6. if (FIND-PATH(East of x,y) == true) return true 7. if (FIND-PATH(South of x,y) == true) return true 8. if (FIND-PATH(West of x,y) == true) return true 9. unmark x,y as part of solution path 10. return false 5.a) Fuction For Insert at begining void insertAtBeg(node **start,node **end,int item ) { node *ptr; ptr=(node*)malloc(sizeof(node)); ptr->info=item; if(*start==NULL) { *start=ptr; *end=ptr; } else { ptr->prev = NULL; ptr->next=*start; (*start)->prev=ptr; *start=ptr; } } Fuction For Insert at Last void insertAtlast(node **start,node **end,int item ) { node *ptr; ptr=(node*)malloc(sizeof(node)); ptr->info=item; if(*start==NULL) { ptr->prev = ptr->next = NULL ; *start = *end = ptr ; } else { ptr->prev = *end; (*end)->next = ptr ; ptr->next= NULL; (*end)=ptr; } } 5.b) void reverse(struct node **head) { struct node *p, *q, *r; p = q = r = *head; p = p->next->next; q = q->next; r->next = NULL; q->next = r; while (p != NULL) { r = q; q = p; p = p->next; q->next = r; } *head = q; }