EE2209 - DATA STRUCTURES AND ALGORITHMS LABORATORY

LAB MANUAL

Department of Information Technology, Rajalakshmi Engineering College, Thandalam.

2

INDEX
I II III LAB SYLLABUS LAB PLAN PSEUDO CODES FOR LAB EXPERIMENTS 1. (a) Implementation of Linked List (b) Implementation of Doubly Linked list 2. Represent a polynomial as a linked list and write functions For polynomial addition. 3. Implementation of tree traversal 4. Implementation of stack ( infix to postfix conversion) 5. Implementation of Binary search Tree 6. Implementation of insertion in AVL trees 7. Implementation of hashing techniques 8. Implementation of backtracking algorithm for knapsack problem 9. Implementation of prim’s and kruskal’s algorithm 10.Implementation of dijktra’s algorithm using priority queues

3

11.Implementation of array based circular queue 12. Implementation of priority queues using heaps 13. Implementation of branch and bound algorithm 14. Implementation of Randomized algorithm 15. Implementation of Topological sort on a Directed graph to decide is it is cyclic

Implement hashing techniques 10. in-order. Implement stack and use it to convert infix to postfix expression 4.4 EE2209 32 DATA STRUCTURES AND ALGORITHMS LABORATORY 00 (Common to EEE. Represent a polynomial as a linked list and write functions for polynomial addition. Perform topological sort on a directed graph to decide if it is acyclic. Implement a backtracking algorithm for Knapsack problem 14. 11. Implement array-based circular queue and use it to simulate a producer-consumer problem. TOTAL : 45 PERIODS . 2. 7. Implement an expression tree. Implement Dijkstra's algorithm using priority queues 12. EIE& ICE) Aim: To develop skills in design and implementation of data structures and their applications. Implement singly and doubly linked lists. Produce its pre-order. Implement binary search tree. Implement priority queue using heaps 9. Implement Prim's and Kruskal's algorithms 13. 8. Implement a branch and bound algorithm for traveling salesperson problem 15. Implement any randomized algorithm. and post-order traversals. 6. 5. 1. Implement insertion in AVL trees. 3.

Implementation of tree traversal Implementation of stack (converting infix to postfix expression) Implementation of Binary search tree Implementation of hashing techniques Implementation of insertion in AVL Tree Implementation of Backtracking algorithm for Knapsack problem Implementation of Prim’s and Kruskal’s algorithm Implementation of Dijktra’s algorithm using priority queues Implementation of array based circular queue Implementation of priority queues using heaps Implementation of Branch and bound algorithm Implementation of Randomized algorithm Implementation of Topological sort on a Date 2 3 4 5 6 7 8 9 10 11 12 13 14 15 .5 LAB PLAN SI.No 1 Name of the Experiment (a) Implementation of Linked List (b) Implementation of Doubly Linked List Represent a polynomial as a linked list and write functions for polynomial addition.

6 Directed graph to decide is it is cyclic .

ALGORITHM: Step 1: Start the process. INSERT / DELETE. c) Find the node after which the new node is to be inserted. b) Get a new node and set DATA[NEWNODE] = ITEM. Step 2: Initialize and declare variables. e) Print the linked list after deletion. Step 4: If choice is INSERT then a) Enter the element to be inserted. No : 1(a) AIM: To implement a linked list and do all operations on it. ie LINK [PAR] = LINK [LOC]. b) Find the node containing the element (LOC) and its preceding node (PAR).7 IMPLEMENTATION OF A LINKED LIST Ex. Step 5: If choice is DELETE then a) Enter the element to be deleted. Step 6: Stop the process. d) Adjust the link fields. d) Adjust the link fields so that PAR points to the next element. c) Set ITEM = DATA [LOC] and delete the node LOC. e) Print the linked list after insertion. Step 3: Enter the choice. .

if desired.next.lastNode := newNode else node.prev := node newNode.lastNode while node ≠ null <do something with node.prev := newNode node.firstNode := newNode . direction can change many times. No : 1(b) AIM: To implement a doubly linked list and do all operations on it. null for empty list } Step 2: Iterating over the nodes Iterating through a doubly linked list can be done in either direction. node := list.next if node.next := node if node. Node node. ALGORITHM: Step 1: Data type declarations record Node { data prev } record List { Node firstNode // points to first node of list.next := node. In fact.prev := node.8 IMPLEMENTATION OF DOUBLY LINKED LIST Ex.data> node := node.next == null list.firstNode while node ≠ null <do something with node. Node newNode) newNode.next node := list. Node newNode) newNode.prev is null list. Node node.next := newNode function insertBefore(List list. null for empty list Node lastNode // points to last node of list.prev newNode.data> node := node.prev Step 3:Inserting a node function insertAfter(List list.

prev destroy node Viva Questions: • • • • • • A Linked list can grow and shrink in size dynamically at _______. newNode) A symmetric function inserts at the end: function insertEnd(List list. Delete an element from a doubly linked list.next := node.prev := null newNode. Reverse a linked list.next else node. only requiring care with the firstNode and lastNode: function remove(List list. Write an algorithm to detect loop in a linked list. Node node) if node.firstNode := newNode list.prev := node.prev. Node newNode) if list.9 else node. list.next.next := newNode node. (with and without recursion) Implement an algorithm to reverse a doubly linked list . newNode) else insertAfter(list.next if node. Node newNode) if list.firstNode. list.prev.next := null else insertBefore(list.lastNode.prev else node.lastNode := newNode newNode. Implement an algorithm to reverse a singly linked list.prev := newNode Function to insert a node at the beginning of a possibly-empty list: function insertBeginning(List list.firstNode == null list.next == null list.lastNode := node. newNode) Step 5:Deleting a node Removing a node is easier.lastNode == null insertBeginning(list.prev == null list.firstNode := node.

However.*/ typedef struct node { int data. No: 2 AIM: To represent polynomial as linked list and perform polynomial addition. while(p!=NULL ||p->data < pNew->data ) { pCur = pCur -> next . pNew ->next = NULL. pCur -> next = pNew .Let the new data to be entered be “d”.Use malloc get a node with address “pNew”. REPRESENT A POLYNOMIAL AS LINKED LIST AND IMPLEMENT POLYNOMIAL ADDITION Ex. }. Suppose we want to write a code to enter data “d” into the node and insert it into its proper place in the list. Try it using a constant amount of memory. such as addition or multiplication of polynomials.10 • How would you find a cycle in a linked list? Try to do it in O(n) time. Let the data be sorted and put in a singly linked linear list which is being pointed by address “head “. Step 2: Addition of two polynomials . } /* now examine the remaining list */ p = pCur -> next . pNew -> data = d. /*Inserting an element in a sorted linked list. struct node *next. pCur = head . p = p -> next . A polynomial can be represented in an array or in a linked list by simply storing the coefficient and exponent of each term. } pNew -> next = pCur -> next. struct node* pNew = (struct node*) (malloc(sizeof(struct node))). ALGORITHM: Step 1: Creating Node. /* check if data is smaller than smallest item on the list*/ if (pNew -> data < pCur -> data ) { pNew ->next = pCur . head = pNew. for any polynomial operation.

Let us call malloc to create a new node p3 to build the third list p3 = phead3. /* now traverse the lists till one list gets exhausted */ while ((p1 != NULL) || (p2 != NULL)) { / * if the exponent of p1 is higher than that of p2 then the next term in final list is going to be the node of p1* / while (p1 ->exp > p2 -> exp ) { p3 -> exp = p1 -> exp. Also assume that we have got a function append to insert a new node at the end of the given list. phead2 and phead3 represent the pointers of the three lists under consideration. } / * if p2 exponent turns out to be higher then make p3 same as p2 and append to final list * / while (p1 ->exp < p2 -> exp ) { p3 -> exp = p2 -> exp. p1 = phead1. Step 6: Wherever the exponents were matching. p2 = phead2. Step 5: If there was no item having same exponent. Step 4: Started with the highest power in any polynomial. Let each node contain two integers exp and coff .11 Consider addition of the following polynomials 5 x12 + 2 x9 + 4x7 + 6x6 + x3 7 x8 + 2 x7 + 8x6 + 6x4 + 2x2 + 3 x + 40 The resulting polynomial is going to be 5 x12 + 2 x9 + 7 x8 + 6 x7 + 14x6 + 6x4 +x3 2x2 + 3 x + 40 Step 3: Result of addition is going to be stored in a third list. p2 = p2 -> next. then simply append the remaining terms to the new list. append (p3. append (p3. p3 -> coff = p1 -> coff . phead3). then we must add the coefficients to get the term for . p3 -> coff = p2 -> coff . Let us assume that the two linked lists already contain relevant data about the two polynomials. simply appended the term to the new list. and continued with the process. /* now move to the next term in list 1*/ p1 = p1 -> next. Step 6: If one list gets exhausted earlier and the other list still contains some lower order terms. phead3). //Pseudo code Let phead1 . simply added the coefficients and then stored the term in the new list. } /* now consider the possibility that both exponents are same .

you do not have to do it term by term. as p1 is already pointing to remaining terms. p2 = p2->next . append (p3. and there are terms remaining only in list1. phead3) . phead3). } } /* now consider the possibility that list2 gets exhausted.12 the final list */ while (p1 ->exp = p2 -> exp ) { p3-> exp = p1-> exp. So all those terms have to be appended to end of list3. phead3) . However. else append (p2. . p3->coff = p1->coff + p2-> coff . p1 = p1->next . so simply append the pointer p1 to phead3 */ if ( p1 != NULL) append (p1.

Step 4: If choice is Inorder then a) Traverse the left subtree in inorder. Step 5: If choice is Preorder then a) Process the root node.13 . ALGORITHM: Step 1: Start the process. . Step 8: Stop the process. b) Process the root node. b) Traverse the left subtree in preorder. Step7: Print the Inorder / Preorder / Postorder traversal. b) Traverse the right subtree in postorder. IMPLEMENTATION OF TREE TRAVERSALS Ex. Step 3: Enter the choice. c) Traverse the right subtree in inorder. c) Traverse the right subtree in preorder. c) Process the root node. Step 2: Initialize and declare variables. Inorder / Preorder / Postorder. : Viva Questions: • Write a function and the node data structure to visit all of the nodes in a binary tree. :3 Date AIM: To implement tree traversals using linked list. No. Step 6: If choice is postorder then a) Traverse the left subtree in postorder.

starting at the top? Write a function to find the depth of a binary tree. level by level.14 • • • • • • What is a balanced tree Do a breadth first traversal of a tree. How do you represent an n-ary tree? Write a program to print the nodes of such a tree in breadth first order. What is a spanning Tree? . How would you print out the data in a binary tree.

This will be for holding our operators. Is the operator of greater. Read the next object in the string. 6. Start by initializing an empty stack. 5. \ Viva questions: . If not continue. pop them all and add them to the end of the postfix string.15 STACK IMPLEMENTATION FOR CONVERTING INFIX TO POSTFIX EXPRESSION Ex. 4. 2. Stop the process. and then push the current operator to the stack. or lesser precedence than the top most operator on the stack? If the operator is greater than the top most operator. Read the string. If the operator is lesser to the top most operator. getting the first operand and add it to the postfix string. equal. Repeat these steps until all of the operands and operators are taken care of. Afterwards look at the stack one last time to see if any operators remain. 3. pop the stack adding it to the postfix string. push the current operator and continue. If the operator is equal to the top most operator pop the top of the stack adding it to the postfix string and then push the current operator to the stack. normally a operator. No. :4 AIM: To implement stack for converting infix to postfix expression ALGORITHM: 1. and push the operator to the stack. Test the next operator to see if it is also lesser and repeat step 2.

16 • • • • • Given an expression tree with no parentheses in it. How would you implement a queue from a stack? What is stack? List the difference between queue and stack. write the program to give equivalent infix expression with parentheses inserted where necessary. How would you implement a queue from a stack? .

findMin(node)f if(node = NULL) //empty tree return NULL .node:leftChild) else // X > node:data return find(X. NULL otherwise. if it exists. :5 AIM: To implement binary search tree and to do BST operations ALGORITHM: find() Operation //Purpose: find Item X in the Tree //Inputs: data object X (object to be found). NULL otherwise.node:rightChild) { findMinimum() Operation //Purpose: return least data object X in the Tree //Inputs: binary-search-tree node node // Output: bst-node n containing least data object X.17 IMPLEMENTATION OF BINARY SEARCH TREE Ex. binary-search-tree node node // Output: bst-node n containing X. No. find(X. if it exists. node)f if(node = NULL) return NULL if(X = node:data) return node else if(X < node:data) return find(X.

node:leftChild) else if(X > node:data) delete(X. binary-search-tree node node //Effect: do nothing if tree does not contain X. // otherwise.18 if(node:leftChild = NULL) return node return findMin(node:leftChild) } insert() Operation //Purpose: insert data object X into the Tree //Inputs: data object X (to be inserted).NULL) return {if(X = node:data) return else if(X < node:data) insert(X. binary-search-tree node node //Effect: do nothing if tree already contains X. update binary search tree by adding a new node containing data object X insert(X. node)f if(node = NULL)f node = new binaryNode(X.NULL. update binary search tree by deleting the node containing data object X delete(X. node)f if(node = NULL) //nothing to do return if(X < node:data) delete(X. node:leftChild) else // X > node:data insert(X. // otherwise. node:rightChild) else f // found the node to be deleted! Take action based on number of node children if(node:leftChild = NULL and node:rightChild = NULL)f delete node . node:rightChild) } delete() Operation //Purpose: delete data object X from the Tree //Inputs: data object X (to be deleted).

rightChild) node:data = tempNode:data delete(node:data.19 node = NULL return {else if(node:leftChild = NULL)f tempNode = node node = node:rightChild delete tempNode {else if(node:rightChild = NULL)f (similar to the case when node:leftChild = NULL) {else f //replace node:data with minimum data from right subtree tempNode = findMin(node.node:rightChild) } } } Viva questions: • • • What is tree? What are the advantages of BST? Write a function and the node data structure to visit all of the nodes in a binary tree. .

ALGORITHM: int avl_insert(node *treep. if (!Balanced(tree)) path_top = treep. No. while (tree && target != tree->value) { direction next_step = (target > tree->value). :6 AIM: To implement Insertion in AVL tree. node *path_top = treep. returning 1 on success or 0 if it * already existed */ node tree = *treep.20 IMPLEMENTATION OF INSERTION IN AVL TREE Ex. } . tree = *treep. treep = &tree->next[next_step]. value_t target) { /* insert the target into the tree.

tree->value = target. C. tree->longer = NEITHER. . tree = malloc(sizeof(*tree)). } void avl_rebalance_path(node path. D. path->longer node avl_rotate_2(node *path_top. D = B->next[dir]. E. direction dir) { node B. *treep = tree. B = *path_top. avl_rebalance(path_top. * Until we find target. mark each node as longer * in the direction of target because we know we have * inserted target there */ while (path && target != path->value) { direction next_step = (target > path->value). return 1. tree->next[0] = tree->next[1] = NULL. target). value_t target) { /* Each node in path is currently balanced.21 if (tree) return 0.

direction third) { node B. in non-recursive style. using only constant space. C. F = B->next[dir]. } else { /* C holds the insertion so F is unbalanced */ F->longer = dir. if (third == dir) { /* E holds the insertion so B is unbalanced */ B->longer = 1-dir. D->next[dir] = F. B->next[dir] = C. D->next[1-dir] = B. D = F->next[1-dir]. } } and there you have an AVL insertion. E = D->next[dir]. D->longer = NEITHER.22 C = D->next[1-dir]. E = D->next[dir]. D->next[1-dir] = B. /* assume both trees are balanced */ B->longer = F->longer = NEITHER. D. F. return E. . E. B>next[dir] = C. } node avl_rotate_3(node *path_top. *path_top = D. B->longer = NEITHER. D->longer = NEITHER. if (third == NEITHER) return NULL. but sometimes performing more comparisons that absolutely needed. B = *path_top. return E. direction dir. *path_top = D. return C. /* node: C and E can be NULL */ C = D->next[1-dir]. F->next[1-dir] = E.

h(k. i) if T[j] = k then return j i = i +1 until T[j] = NIL or i = m Return NIL . k) i=0 Repeat j <-. No.h(k.23 IMPLEMENTATION OF HASHING TECHNIQUES Ex. k) i=0 Repeat j <-. i) if Y[j] = NIL then T[j] = k Return j use i = i +1 until i = m error "table overflow" For Searching: HASH-SEARCH (T. :7 AIM: To implement hashing techniques ALGORITHM: For insertion: HASH-INSERT (T.

23} r[48. 4. 17. 7. 21} 7.. 6. 12. 21. 12. 15. 11. 12. 16. 17. 15. 21. 20. 23. 5. 16. 22. //Use binary integer part of the sines of integers (Radians) as constants: for i from 0 to 63 k[i] := floor(abs(sin(i + 1)) × (2 pow 32)) //Initialize variables: var int h0 := 0x67452301 var int h1 := 0xEFCDAB89 var int h2 := 0x98BADCFE var int h3 := 0x10325476 //Pre-processing: append "1" bit to message append "0" bits until message length in bits ≡ 448 (mod 512) append bit /* bit. 22} r[16. 23. 6. 10. 0 ≤ i ≤ 15 //Initialize var int a := var int b := var int c := var int d := hash value for this chunk: h0 h1 h2 h3 //Main loop: for i from 0 to 63 if 0 ≤ i ≤ 15 then f := (b and c) or ((not b) and d) g := i else if 16 ≤ i ≤ 31 f := (d and b) or ((not d) and c) g := (5×i + 1) mod 16 . 20.15] := {7. 14. 17. 10. 4. 5. not byte */ length of unpadded message as 64-bit little-endian integer to message //Process the message in successive 512-bit chunks: for each 512-bit chunk of message break chunk into sixteen 32-bit little-endian words w[i]. 20. 17.. 5. 9. k //r specifies the per-round shift amounts r[ 0. 10. 22. 11. 16. 23. 12. 21. 15. 22. 11. 14. 16. 6. 10.24 MD5 – PSEUDO CODE: //Note: All variables are unsigned 32 bits and wrap modulo 2^32 when calculating var int[64] r. 9.. 14. 20} r[32. 15. 7.31] := {5. 9. 11.47] := {4. 9..63] := {6. 14. 4.

• List the hashing techniques. • How can a data be inserted in to the hash table? • What is hash table? • What are the applications of hashing techniques? .25 else if 32 ≤ i ≤ 47 f := b xor c xor d g := (3×i + 5) mod 16 else if 48 ≤ i ≤ 63 f := c xor (b or (not d)) g := (7×i) mod 16 temp d := c := b := a := //Add h0 := h1 := h2 := h3 := := d c b b + leftrotate((a + f + k[i] + w[g]) . r[i]) temp chunk's hash to result so far: a b c d this h0 + h1 + h2 + h3 + var int digest := h0 append h1 append h2 append h3 Viva questions: • Define hashing.

cost. if (left_child. while PQ not equal do current <. No.PQ. max_cost := root. :8 AIM:To implement backtracking algorithm for Knapsack problem.26 IMPLEMENTATION OF BACKTRACKING ALGORITHM FOR KNAPSACK PROBLEM Ex.bound > max_cost) then create left_child := next item. ALGORITHM:- function backtracking (current depth) if solution is valid return / print the solution else for each element from A[] source array let X[current depth] ß element if possible candidate (current depth + 1) backtracking (current depth + 1) end if end for end if end function (OR) Procedure knapsack: Initialize root. if (current.root. PQ <.cost > max_cost) .

bound > max_cost) PQ <. create right_child. end if. What is the significance of backtracking algorithm? . end procedure.left_child. Viva questions: • • Describe knapsack problem. if (left_child. end while.right_child. end if. // it skips packing the next item if (right_child. return best_solution and its cost.bound > max_cost) PQ <. update best_solution. end if. end if.27 max_cost := left_child.cost.

4 Initialize a priority queue Q to contain all edges in G. ALGORITHM:Prim’s Algorithm: E(1) is the set of the sides of the minimum genetic tree.E(2)=E  While E(1) contains less then n-1 sides and E(2)=0 do • From the sides of E(2) choose one with minimum cost-->e(ij) • E(2)=E(2)-{e(ij) } • If V(i).  E(1)=0. No. E(2) is the set of the remaining sides.V(j) do not belong in the same tree then o unite the trees of V(i) and V(j) to one tree. Kruskal’s Algorithm: 1 function Kruskal(G) 2 for each vertex v in G do 3 Define an elementary cluster C(v) ← {v}. 5 Define a tree T ← Ø //T will ultimately contain the edges of the MST 6 // n is total number of vertices .28 IMPLEMENTATION OF PRIM'S AND KRUSKAL'S ALGORITHMS Ex. • end (If) • end (While)  End Of Algorithm. :9 AIM:To implement prim's and kruskal's algorithms. using the weights as keys.

17 return tree T Viva questions: • • What is prim’s algorithm? Where is it used? What is the Significance of prim’s algorithm? . 11 // Note that the cluster contains more than one vertex only if an edge containing a pair of 12 // the vertices has been added to the tree.u) to T.29 7 while T has fewer than n-1 edges do 8 // edge u.removeMin() 10 // prevent cycles in T. that is. 14 if C(v) ≠ C(u) then 15 Add edge (v. and let C(u) be the cluster containing u.v) ← Q. add u. union C(v) and C(u).v only if T does not already contain a path between u and v. 13 Let C(v) be the cluster containing v.v is the minimum weighted route from/to v 9 (u. 16 Merge C(v) and C(u) into one cluster.

its distance recorded now is final and minimal. :10 AIM:To implement Dijkstra's algorithm using priority queues. A visited node will not be checked ever again. Set it to zero for our initial node and to infinity for all other nodes. and an edge connecting it with another node (B) is 2. Set initial node as current. 4. 1 function Dijkstra(Graph. 5. If this distance is less than the previously recorded distance (infinity in the beginning. 2. Mark all nodes as unvisited. For current node.30 IMPLEMENTATION OF DIJKSTRA'S ALGORITHM USING PRIORITY QUEUES Ex. overwrite the distance. source): 2 3 4 5 6 for each vertex v in Graph: dist[v] := infinity previous[v] := undefined dist[source] := 0 // Initializations // Unknown distance function from source to v // Previous node in optimal path from source // Distance from source to source Q := the set of all nodes in Graph . consider all its unvisited neighbours and calculate their distance (from the initial node). For example. Assign to every node a distance value. zero for the initial node). if current node (A) has distance of 6. No. ALGORITHM:1. Set the unvisited node with the smallest distance (from the initial node) as the next "current node" and continue from step 3 . the distance to B through A will be 6+2=8. mark it as visited. 3. When we are done considering all neighbours of the current node.

Try to use at least 90% of the memory space. // all remaining vertices are inaccessible alt := dist[u] + dist_between(u. What is the cost of enqueue and dequeue? Can you improve this? What if the queue is full (I was using an looping array)? What kind of mechanism would you use to increase its size? • Give a good data structure for having n queues ( n not fixed) in a finite memory segment. .v....31 // All nodes in the graph are unoptimized . Implement a queue class with Java.a) OUTPUT: Viva Questions: • • What is queue? You know what a queue is . v) if alt < dist[v]: dist[v] := alt previous[v] := u return previous[] // Relax (u.thus are in Q 7 8 9 10 11 12 13 14 15 16 17 while Q is not empty: // The main loop u := vertex in Q with smallest dist[] if dist[u] = infinity: break remove u from Q for each neighbor v of u: // where v has not yet been removed from Q. You can have some data-structure separate for each queue.

:11 Aim: To implement array based circular queue and to use the same for solving producer consumer problem Algorithm: . No.32 Implementation of array based circular queue Ex.

.33 Viva questions: • • • What is an array? Write the Significance of array based circular queue. Write any one application of array based circular queue.

34 IMPLEMENTATION OF PRIORITY QUEUE USING HEAPS Ex. No. :12 AIM:To implement priority queue using heaps. ALGORITHM:- .

0.1 while end > 0 do (swap the root(maximum value) of the heap with the last element of the heap) swap(a[end]. end) is input: end represents the limit of how far down the heap to sift. count-1) start := start .. a[0]) (decrease the size of the heap by one so that the previous max value will stay in its proper placement) end := end . count) is input: an unordered array a of length count (first place a in max-heap order) heapify(a..2) / 2 while start ≥ 0 do (sift down the node at index start to the proper place such that all nodes below the start index are in heap order) siftDown(a.. root := start while root * 2 + 1 ≤ end do (While the root has at least one child) child := root * 2 + 1 (root*2+1 points to the left child) (If the child has a sibling and the child's value is less than its sibling's.) if child + 1 ≤ end and a[child] < a[child + 1] then child := child + 1 (. a[child]) root := child (repeat to continue sifting down the child now) else return Viva questions: .35 function heapSort(a. then point to the right child instead) if a[root] < a[child] then (out of max-heap order) swap(a[root]. end) function heapify(a.count) is (start is assigned the index in a of the last parent node) start := (count .1 (after sifting down the root all nodes/elements are in heap order) function siftDown(a. start.1 (put the heap back in max-heap order) siftDown(a. start.. count) end := count .

No. :13 AIM:To implement the BRANCH AND BOUND ALGORITHM ALGORITHM:- . What is priority queue? Difference between calloc and malloc? IMPLEMENTATION OF BRANCH AND BOUND ALGORITHM Ex.36 • • • • What is malloc()? Define heap data structure.

No.37 Viva questions: • • What is traveling sales man problem? What is the significance of branch and bound algorithm? IMPLEMENTATION OF RANDOMIZED ALGORITHM (MINIMUM CUT) Ex. :14 AIM:- .

v) at random in G contract the edge. . ALGORITHM:find_min_cut(undirected graph G) { while there are more than 2 nodes in G do { pick an edge (u. while preserving multi-edges remove all loops } output the remaining edges } Viva questions: • • What is minimum cut method? What is the Significance of randomized algorithm? IMPLEMENTATION OF TOPOLOGICAL SORT ON A DIRECTED GRAPH TO DECIDE IF IT IS ACYCLIC Ex No :15 AIM:To perform topological sort on a directed graph to decide if it is acyclic.38 To implement the randomized algorithm of Minimum Cut.

39 ALGORITHM:L ← Empty list that will contain the sorted elements S ← Set of all nodes with no incoming edges while S is non-empty do remove a node n from S insert n into L for each node m with an edge e from n to m do remove edge e from the graph if m has no other incoming edges then insert m into S if graph has edges then output error message (graph has at least one cycle) else output message (proposed topologically sorted order: L) .

Sign up to vote on this title
UsefulNot useful