February 2012 Master of Science in Information Technology (MScIT) Revised Fall 2011 Semester 1 MIT102 – Data & File Structures–

4 Credits
(Book ID: B1476) Assignment Set – 1 (60 Marks) 1. Discuss on algorithm complexity and time space tradeoff? An algorithm is a step by step procedure for solving a particular problem. One major purpose of this text is to develop efficient algorithms for the processing of our data. The efficiency of the algorithm is depending on the time and space it uses. The complexity of an algorithm is the function which gives the running time and/ or space in terms of the input size. Each of our algorithms will involve a particular data structure. The data structure which we choose will depend on many things, including the data and the frequency with which various data operation are applied. Sometimes the choice of data structure involves a time- space tradeoff by increasing the amount of space for storing the data, one may be able to reduce the time needed for processing the data, or vice versa. So, we may not always be able to use the most efficient algorithm. We illustrate the idea with two examples. Searching Algorithms Consider employee details file in which each record contains, among other data, the name and telephone number of its employee. Suppose we are given with the name of the employee and we want to find his or her telephone number. One way to do this is to linearly search through the file. Linear search: It searches each record one at a time, until finding the given name and hence the corresponding telephone number. The time required to execute the algorithm is proportional to the number of comparison. So, the average number of comparison for a file with n records is equal to n/2 i.e. the complexity of linear search is C (n)= n/2. This algorithm is impossible if we are searching through a list consisting of thousands of names, as in telephone books. If we have the records as sorted file, then we can use an efficient algorithm called binary search. Binary Search: Compare the given Name with the name in the middle of the list; this tells which half of the list contains Name. Then compare Name with the name in the middle of the correct half to determine which quarter of the list contains Name. Continue the process until finding Name in the list. The complexity of binary search is C (n) = log2n. Although the binary search algorithm is a very efficient algorithm, it has some major drawbacks. Specially, the algorithm assumes that one has direct access to the middle name in the list or a sublist. This means that the list must be stored in some type of array. Unfortunately, inserting an element in an array requires elements to be moved down the list, and deleting an element from an array requires element to be moved up the list. An Example of Time- Space Tradeoff Suppose a file of records contains names, social security numbers and much additional information among its fields. Sorting the file alphabetically and rising a binary search is a very efficient way to find the record for a given name. on the other hand, suppose we are given only with the social security number of a person. Then we have to do a linear search for the record, which is extremely time- cost consuming for a very large number of records. One way to solve that problem is to have another file which is sorted numerically according to social security number. This however would double the space required for storing the data. Another way is to have the main file

sorted numerically by social security number and to have a auxiliary array with only two columns, the first column containing an alphabetized list of the names and the second column containing pointers which give the locations of the corresponding records in the main file. This is one way of solving the problem that is used frequently, since additional space, containing only two columns, is minimal for the amount of extra information it provides.

2. Write a note on algorithmic notation. An algorithm is a step by step procedure in normal english language for solving a particular problem. Algorithm notation is the format that is used to specify algorithms. Example: Algorithm: A nonempty array DATA with N numerical values is given. This algorithm finds the location LOC and the value MAX of the largest element of DATA. The variable K is used as a counter. Step 1: [Initialize] Set K: = 1, LOC: = 1, and MAX: = DATA [1]. Step 2: [Increment counter] Set K: = K+1. Step 3: [Test counter] If K>N, then: Write: LOC, MAX, and Exit. Step 4: [Compare and update] If MAX < DATA [K], then: Set LOC: = K and MAX: = DATA [K]. Step 5: [Repeat loop] Go to Step 2. The following are certain conventions that we will use in presenting our algorithms. Steps, Control, exit: The steps of the algorithm are executed one after the other. Control may be transferred to Step n of the algorithm by the statement “Go to Step n”. As we have seen in the previous example. If several statement appears in same line then the controls are moved from left to right while execution. The algorithm is completed when the statement Exit is encountered Comments: It indicates the main purpose of steps. Comments appear in brackets in each step, either in the beginning or end of the step. Variable Names: Variable names will be capital letters, as in MAX and DATA. Single letter names of variables used as counters or subscript will also be capitalized in the algorithms even though lowercase may be used for these same variables in the accompanying mathematical description and analysis. Assignment Statement: Our assignment statements will use the dots- equal notation :=. For example Max: = DATA [1]. Input and Output: Data may be input and assigned to variables by means of a Read statement. Read: Variable name Similarly, messages, placed in quotation mar ks, and data in variables may be output by means of a Write or Print statement. Write: Messages and / or variable names. Procedures: The term “procedure” will be used for an independent algorithmic module which solves a particular problem. The use of the word “procedure” or “module” rather than “algorithm” for a given problem is simply a matter of taste.

3. Explain traversing a linked list. Traversing a linked list means processing each node of list exactly once. The linked list in memory is stored as linear array DATA and LINK with START pointing to the first element and NULL indicating the end of the list. Let us see the algorithm for traversing a linked list. Algorithm: Traversing a linked list 1. Set P: = START. [ Initializes pointer PTR] 2. Repeat Step 3 and 4 while P ≠ NULL. 3. Apply PROCESS to DATA [P]. 4. Set P := LINK [P] .[P points to the next node] [End of Step 2 loop] 5. Exit In the algorithm P is the pointer variable which points to the node that is currently being processed and LINK [P] points to the next node to be processed. P := LINK [P] Thus the pointer moves to the next node in the list as shown in the figure below figure

In the algorithm initially the pointer is assigned to the start variable P := START then it process the data at the first node of the list DATA [P]. Then the P is incremented to next node P := LINK [P] and then it process the data of second node. Then the P will again get incremented to next node and so on. This will be continued until the P = NULL which indicates the end of the list.

4. Explain the Linked list implementation of stack. A stack is a data structures in which insertion and deletion of items are made at the one end, called the top of the stack. We have two basic operations in stack they are push and pop. Push Operation: Push is used to insert an item into a stack. Pop Operation: Pop is used to delete an item from a stack. The figure 4.1 shows the stack with five items and the top pointer were the insertion and deletion are performed. From the figure 4.1 you can see that 5 is the current top item. If any new items are added, they are placed on the top of 5 and if any item are to be deleted, then 5 is the first to be deleted. This means that the last item entered or inserted is the first one to be removed or deleted. So, stacks are also called as last- in first- out (LIFO).

Figure 4.1: Stack

The figure 4.2 shows how the stack expands and shrinks. 4.2 (a) is the actual stack with five elements. Now item 6 is inserted, according to the definition the one position where the item 6 can be inserted is top and now 6 will be the top item. Similarly the item 7 is also inserted and now it is the top item that you can see in figure 4.2 (b) and (c). In figure 4.2(d) you can

see when an item has to be removed, then according to the definition the top item i.e. 7 is removed first.

Figure 4.2: Stack- expands and shrinks

Let us see how stack is implemented using linked list. As we have seen in previous section the advantages of linked list over array is that, it is not necessary to declare the size of linked list a head and the size can be changed dynamically. So, at any point we can expand or shrink the stack size. One more advantage is that cost of insertion and deletion is less compared to array implementation. Linked list implementation of stack uses singly linked list or one- way list where the DATA field contains the ITEM to be stored in stack and the link field contains the pointer to the next element in the stack. Here TOP points to the first node of linked list which contains the last entered element and null pointer of the last node indicates the bottom of stack. The figure 4.4 shows the linked list representation of stack.

The following are the operations to add an item into stack and remove an item from a stack. Push operation Push operation is performed by inserting a node into the front or start of the list. Even though in this we don‟t want to declare the size of linked list a head we have to check for the OVERFLOW condition of the free- storage list. The following procedure performs the push operation. First check the available space in the free storage list. If free space is available then make the pointer variable NEW to point the first node of AVAIL list, NEW: = AVAIL. Get the data for the new node, DATA [NEW]:= ITEM and make the link filed of new node to point the actual top node or first node in the list, LINK [NEW]:= TOP. Now new node becomes the first node in the list and the TOP pointer point to the NEW node. The figure 4.5 shows the PUSH operation in linked list implementation of stack.

Pop operation Pop operation is performed by removing the first or the start node of a linked list. Before executing the pop operation one must check for the UNDERFLOW condition, i.e. check whether stack has an item to be removed. The following procedure performs the pop operation. First check the UNDERFLOW condition. If stack is not empty then the element in the top node is copied to the ITEM, ITEM := DATA [TOP]. A temporary variable TEMP is made to point the top node, TEMP := TOP and the top pointer now points to the next node in the list, Top = LINK [TOP]. Now the node pointed by TEMP is moved to the AVAIL list, LINK [TEMP] AVAIL and AVAIL = TEMP. The figure 4.6 shows the POP operation in linked list implementation of stack.

5. Explain the process of converting the general tree to a binary tree.

6. Explain the process of insertion and deletion in B-tree.

Master your semester with Scribd & The New York Times

Special offer for students: Only $4.99/month.

Master your semester with Scribd & The New York Times

Cancel anytime.