You are on page 1of 17

MCS-21 ASSIGNMENT (2010-11)

This assignment has three questions, which carry 80 marks. Answer all the questions. Viva-voca carries 20 marks. You may use illustrations and diagrams to enhance the explanations. Please go through the guidelines regarding assignments given in the programme Guide. Ensure that you dont copy the program from course material or any other source. All the implementations should be in C language. Question 1: An alternative linked representation for sparse matrices uses nodes that have the field down, right, row, col and value. Each nonzero entry of the sparse matrix is represented by a node. The zero terms are not explicitly stored. The nodes are linked together to form two circular lists. The first list, the row list, is made up by linking nodes by rows and within rows by columns using the right field. The second list, the column list, is made up by linking nodes via the down field. In this list, nodes are linked by columns and within columns by rows. These two lists share a common header node. In addition, a node is added to contain the dimensions of the matrix. (a )Write any 5 x 8 Matrix that has exactly nine nonzero terms such that at least one nonzero term appears in each row and each columns. For this sparse matrix, draw the linked representation. Ans. Matrixes with good number of zero entries are called sparse matrixes. A triangular matrix is a square matrix in which all the elements either above or below the main diagonal are zero. The triangular matrix is sparse matrix in which all the elements except for the main diagonal on the immediate upper and lower side are zeroes. Tridiagonal matrixes are also sparse matrixes. Now we will consider the sparse matrix from storage point of view. Suppose that the entire sparse matrix is stored. Then, a considerable amount of memory which stores the matrix consists of zeroes. This is nothing but wastage may count to mega bytes. So, an efficient method of storing sparse matrixes has to be looked into. 0 1 2 0 0 0 0 1 0 4 0 2 0 0 0 3 0 0 0
1

4 0 0 0

5 5 0 0

6 0 0 9

7 0 0 0

3 4

0 0

0 0

3 0

0 8

0 0

0 0

0 0

0 0

A common way of representing non zero elements of a sparse matrix always specifies the number of rows, number o0f columns and number of non zero elements in matrix. The number 5 represent the total number of rows sparse matrix. Similarly the number 8 represents the total number of columns in the matrix. The number 8 presents total number of non zero elements in the matrix. Each non zero element is stored from the second row, with the 1st and 2nd elements of the row, indicating the row number and column number respectively in which the element is present in the original matrix. The 3 rd element I this row stores the actual value of the non zero element. For example, the 3tuple representation of the matrix is shown below:7 0 1 2 3 4 5 7 3 1 4 1 0 2 9 5 4 9 3 1 8

Program concept: A matrix as input and prints 3 tuple representation which is in sparse matrix is as follows:# include <stdio.h> #include <conio.h> Void main() { Int [5][5], rows, columns, i, j; Printf(enter the order of the matrix. The Orer should be less than 5 x 5:\n); Scanf(%d %d, & rows, & columns); Printf(Enter elements of the matrix : \n); For (i = 0; i < rows i i++) { For (j=0; j < columns; j++)

{ Snaf(%d, & a[i][j]); } } Printf(The 3-tuple representation of matrix is :/n); For (i=0; i<row; i++) { For (j=0; j<columns; j++) { If(a[i] [j] != 0) { Printf(%d %d %d, (i+1), (j+1), a[i] [j]); } } } Getch( ); } The Output :- Enter the order of the matrix. The order should be less than 5 x 5 33 Enter the elements of the matrix:1 0 0 2 1 0 3 0 4

The 3-tuple representation of the matrix is: 1 1 1 1 2 3 1 2 3


3

2 3

2 3

1 4

(a) Suppose that an m x n matrix with t non-zero terms is represented as above. How small must t be so that the above linked scheme uses less space than an m x n array uses? Ans.

(b) Design a suitable external (i.e one that can be used for input and output) representation for a sparse matrix. Your representation should not require explicit input of the zero terms. Ans. In the array implementation of lists, we will use array to hold the entries and a separate counter to keep track of the number of positions are occupies. A structure will be declared which consist of array and counter. For simplicity, we have taken list entry as integer of course, we can also taken list entry as structures of employees record of the student record etc. Count 1 11 2 22 3 33 4 44 5 55 6 66 7 77 8

INSERTION In An array implementation of list, elements are stored in continuous locations. To add element to the list at the end, we can add it without any problem. But, suppose if we want to insert the element at the beginning or middle of the list, then we have to rewrite all the elements after the position where the element has to be inserted. We have to shift (n)th element to (n+1)th position, where n is number of elements in the list. The (n-1)th element to (n)th Position and this will continue until the (r)th element to (r+1)th position, where r is the position of insertion.

For doing this, the count will be incremented Before Insertion Count 1 11 Step 1 Count 11 Step 2 Count 11 22 33 44 55 66 66 77 22 33 44 55 66 77 77 2 22 3 33 4 44 5 55 6 66 7 77 8

Step 3

Count 11 22 33 44 55 55 66 77

Step 4 Count 11 22 33 44 55 66 66 77

Step 5

Count 11 22 33 35 44 55 66 66 77

It will demonstrate the insertion of an elements at desired position. Inserting an element into contiguous list (Linear Array) at specified position. #include <stdio.h> #include <conio.h>

Void main { Type of struct { Int data [10]; Int count; } list; / * prototypes of functions * / Void insert (list * , int, int); Void create (list *); Void traverse (list *); / * Defination of the insert function * / Void insert (list * start, int position, int element) { Int temp = start = start_ > count ; If(element !=0) { While (temp > position) { Start _> data[temp + |]= start_> data[temp]; Temp - -; } Start _> data[position]= element; Start _>count ++; }

} /*Definition of create function to READ data values into the list */ Void create (list * start) { Int i=0, test = 1; While (test) { fflush (stdin); printf(input value for :%d, i); scanf(%d, & start _> data [i]); if(start _> data [i] ==0) { Test = 0; Else i++; } Start _> count = i; } Getch ( ); } /* OUTPUT FUNCTION TO PRINT ON THE CONSOLE */ Void traverse (list * start) { Int i; For (i=0; i< start_>count; i++)

{ Printf(\n value at the position : %d: %d, i, Start _>data[i]); } } /*main function*/ Void main( ) { Int position, element; List |; Create (& |); Printf(\n entered list as follows); Fflush(stdin); Traverse(&|); Fflush(stdin); Printf(%d & position); Fflush(stdin); Printf(/n input the value for the position); Scanf(%d, & element); Insert(& |, position, element); Traverse (&|);

Question 2: Draw two binary trees whose preorder listing is abcdefgh and whose postorder listing is dcbgfhea. Also, list the nodes of binary trees inorder and level order.
8

Ans. There are three types of tree traversals, namely, preorder, post order and inorder. Preorder Traversal:Each node is visited before its children are visited, the root is visited first. Algorithm For Pre Order Traversal:1. Visit root node

2. Traverse Left sub-tree in preorder

3. Traverse right sub-tree in preorder Exam of pre order traversal:Reading a book, as we do not read next chapter unless we complete all the sections of previous chapter and all its sections refer to the following on the figure:

BOOK

PREFACE CHAPTER 1

CHAPTER 8

SUMMARY

SECTION 1

SECTION 1

SECTION 4

SECTION 1.1

SECTION 4.1

SECTION 4.2

SECTION 4.1.1

As each node is traversed only once, the time complexity of pre order is T(n) = O(n), where the number n is of nodes in the tree.So, the given preorder listing is abcdefgh and the binary tree is as following:

Algorithm For Pestorder Traversal:

10

1. Traversal left sub-tree in post order 2. Traversal sub-tree in post order 3. Visit root node Finding the space occupied by files and directories in a file system requires calculation of space required by all files in the directory (children in tree structure) refer to following figure: / ROOT / dir 1 FILE 1 / dir 2 FILE n / dir 3 FILE 1

Calculation of space occupied by file system: A Post Order Traversal The given post order listing is dcbgfhea. So, the binary tree for post order is on the next page:a

11

Algorithm For Inorder Traversal 1. Traversal left sub-tree 2. Visit node 3. Traverse right sub-tree For eg.

+ * / 4 7 3 2 1 -

Inorder-Traversal

In order Traversal can be best described by an expression tree, where the operators are at parent node and operates at leaf nodes. Now let us consider the above expression tree. The preorder, postorder and inorder traversal are given below: Preorder : +*/427 31 Postorder : 42 / 7 *31 - + Inorder traversal : - ((((4/2) * 7) + (3 - 1)) So, given preorder and postorder are silved by given the inorder to both as follow:-

12

The given preorders inorder is :Inorder of preorder :- (d(cbe) a(gfh)) The inorder of postorder is :(((dbc) hg) ae) And level order is the order where all the nodes of the same level are travelled first travelled first starting from the root.

Question 3: Suppose that an undirected graph is represented with the array adjacency list representation. Write an algorithm to remove the an edge from G. What is the time complexity of the algorithm.

Ans. Adjacency List Representation: An adjacency list representation of a graph G= {V, E} consists of an array of adjacency list denoted by adj of V list. For each vertex U e V, adj[U] consists of all vertices adjacent to U in the graph G. Consider the graph for undirected graph:-

1 3 5

2 4

The following is the adjacency list representation of graph is

13

Adj [1] = {2, 3, 5} Adj [2] = {2, 4} Adj [3] = {1, 4, 5} Adj [4] = {2, 3, 5} Adj [5] = {1, 3, 4} An adjacency matrix representation of a graph G= (V, E) is a matrix A(aij) such that aij = { 1 if edge (i,j) belongs to E O otherwise The Adjacency matrix for the graph of above is given below:1 2 3 4 5 1 0 1 1 0 1 2 1 0 0 1 0 3 1 0 0 1 1 4 0 1 1 0 1 5 1 1 1 1 0

Adjacency matrix of an undirected graph is symmetric on main diagonal. When directions are not marked on the edges, then the graph is called an undirected graph. Consider a graph G= (V,E). We wish to find out the shortest path from a single source vertex vEV, to every vertex vEV. The single source shortest path algorithm is based on assumption that no edges have negative weights. Now, we shall discuss about an algorithm in which we will remove an edge from the graph G. And we can solve the path (shortest) algorithm which starts with a single source and finds shortest path to all vertices in the graph. This problem is helpful in finding distance between all pairs problem is mother of all shortest paths problems. In this algorithm, we will represent the graph by adjacency matrix.

14

The weight of an edge (ij in an adjacency array representation as follow o if i=j Weight of the undirected edges from i to j i.e. (i,j) If i-j and (i,j) belongs to If i=j and (i,j) belongs to E All the pairs shortest paths problem can be considered as a generalization of single-source-shortest-path problem by using Dijkstras algorithm by varying the source node among all nodes in the graph. We can use negative edges is allowed then we can not use Dijkastras algorithm. In this section we shall use a recursive solution to the pairs (edges) problem known as Floyd-warshall algorithm, which runs in O(n) time. This algorithm is based on the following principle for the graph G let V={1, 2, 3, ------n}. Let us consider a sub set of the vertices {1, 2, 3, k). For any pair opf vertices are from {1, 2, 3, -------k}. This algorithm will exploit the relationship between path P and shortest path from i to j whose intermediate vertices are from {1, 2, 3, ------k-1} with the following two possibilities. If k is not an intermediate vertex in the path p, then all intermediate vertices of the path p are in {1, 2, 3,-----k-1}. Thus shortest path from i to j with intermediate vertices in (1, 2, 3, ----k) is also the shortest path from i to j with vertices {1, 2, 3, -----k}. We also define a path matrix P where P[i][j] holds intermediate vertex k on the least cost path from i to j that leads to the shortest path from i to j. Algorithm for lessing edges from G N= Number of rows of the graph D [i] [j] = C [i] [j] For k from 1 to n Do for i=1 to n Do for j= 1 to n D [i] [j] = minimum (dij(k - 1), dik(k - 1)+dki(k 1)) Enddo

E= i, j

15

Enddo Enddo Where dij(k - 1) = minimum path from i to j using intermediate vertices Where dik(k - 1) = minimum path from j to k using intermediate vertices Where dkj(k - 1) = minimum path from k to j using intermediate vertices Programme:#include <stdio.h> #include <conio.h> Void main ( ) { All pairs shortest paths (int N, matrix C, matrix P, matrix D) { Int i, j, k; If (i=j then C[i] [j]=0) { For (i=0; i<N; i++) { For (j=0; j<N; J++) { D[i] [j]= C[i] [j]; P[i] [j] = -1; } D[i] [j]=0; } For (k=0; k<N; k++) { For (i=0; i<N; i++) { For (j=0; j<N; j++) { If (D [i] [k] + D [k] [j] < D [i] [j]) { D [i] [j] = D[i] [k] +D[k] [j] P [i] [j] = k; } }

16

} } } } Getch( ); }

17

You might also like