· page 3 of 26 running time of worst case is approximately equal to h k c 6 j which is...

26
www.ashekalaziz.wordpress.com Page 1 of 26 Data Structure Test Paper 1 1(a) What is Data Structure? Briefly explain the operations of data structures. Answer: The logical and mathematical model of a particular organization of data is called Data Structure. Data structure should be simple enough so that it can easily be processed when necessary and data should bear the relationship with the real world scenario. The operations on the data structures are – (i) Insertion: Inserting a new item or data or information into the structure. (ii) Deletion: Removing an item or data or information from the structure. (iii) Traversing: If there is collection of data items, then accessing each data item exactly once sometimes called visiting each node or element sequentially. (iv) Searching: Finding some particular data item or information among the collection of information satisfying specific precondition. (v) Sorting: arrangement of data items in specific order like ascending order or descending order of integer contents or names in alphabetical order etc. (vi) Merging: If there two different list of elements, then both can be combined together to form a new list of elements. Here both the list can in sorted order and newly formed combined list should also be in sorted manner. We should remember that Sorting and Merging are the operations on data structures in special case. [Source: Seymour Lipschutz] (b) Define algorithm and complexity of an algorithm. Answer: The term algorithm comes from ‘or’, ‘go’ and ‘rythm’. Algorithm is a set of tasks which proceeds sequentially until desired result is obtained or a well defined problem is solved. The first algorithm was devised by Abu Musa Al-Zaber Al Kwarizmi to find the result of Greatest Common Division of few integers. [Source: Knuth] If we think to find a particular data item in an array using sequential searching i.e. visiting each element from the beginning and accessing the immediate next element successively until we get out desired element then the time required to find the data item depends on the number of elements in the list sometimes called the size of the data or input size to the searching algorithm. If the size is smaller, much time may not be required to catch the desired data object but if the size is large then time will be much more consumed obviously. This is called complexity of algorithm. We should remember that running time of algorithm and size of the data for operation are two constraints of

Upload: others

Post on 17-Mar-2020

0 views

Category:

Documents


0 download

TRANSCRIPT

Page 1:  · Page 3 of 26 Running time of worst case is approximately equal to H K C 6 J which is approximately equal to running time of average case. For example, if input size is 1000000

www.ashekalaziz.wordpress.com

Page 1 of 26

Data Structure

Test Paper 1

1(a) What is Data Structure? Briefly explain the operations of data structures.

Answer:

The logical and mathematical model of a particular organization of data is called Data Structure. Data structure should be simple enough so that it can easily be processed when necessary and data should bear the relationship with the real world scenario. The operations on the data structures are –

(i) Insertion: Inserting a new item or data or information into the structure. (ii) Deletion: Removing an item or data or information from the structure. (iii) Traversing: If there is collection of data items, then accessing each data item exactly once

sometimes called visiting each node or element sequentially. (iv) Searching: Finding some particular data item or information among the collection of

information satisfying specific precondition. (v) Sorting: arrangement of data items in specific order like ascending order or descending

order of integer contents or names in alphabetical order etc. (vi) Merging: If there two different list of elements, then both can be combined together to

form a new list of elements. Here both the list can in sorted order and newly formed combined list should also be in sorted manner.

We should remember that Sorting and Merging are the operations on data structures in special case. [Source: Seymour Lipschutz]

(b) Define algorithm and complexity of an algorithm.

Answer:

The term algorithm comes from ‘or’, ‘go’ and ‘rythm’. Algorithm is a set of tasks which proceeds sequentially until desired result is obtained or a well defined problem is solved. The first algorithm was devised by Abu Musa Al-Zaber Al Kwarizmi to find the result of Greatest Common Division of few integers. [Source: Knuth]

If we think to find a particular data item in an array using sequential searching i.e. visiting each element from the beginning and accessing the immediate next element successively until we get out desired element then the time required to find the data item depends on the number of elements in the list sometimes called the size of the data or input size to the searching algorithm. If the size is smaller, much time may not be required to catch the desired data object but if the size is large then time will be much more consumed obviously. This is called complexity of algorithm. We should remember that running time of algorithm and size of the data for operation are two constraints of

Page 2:  · Page 3 of 26 Running time of worst case is approximately equal to H K C 6 J which is approximately equal to running time of average case. For example, if input size is 1000000

www.ashekalaziz.wordpress.com

Page 2 of 26

algorithm complexity and complexity can be expressed as a function of either time or space. [Ref: Seymour Lipschutz]

Another important fact is that if the desired data item is resided in the very beginning of the list then whatever may be the the size of the list, time requirement is the minimum but if would be in the last element even unavailable then time requirement will be the maximum to finish the traversal. These are the best case and worst case respectively. But we are generally concerned with the average cases that desired element might be in between the start and end position of the data list and probability to find the item. Experts concern about the algorithm modification to optimize the time requirements. [Ref: Seymour Lipschutz , Shahani]

(c) Explain the complexity of Bubble sort, linear search and binary search.

Answer:

Linear Search:

Let us consider that n number of elements in an array. We want to find a desired element which even may not be in the list. If complexity of input size n can be written as C(n) then it should be equal to n in this case all n elements should be visited but no findings. C(n)= n. This is the worst case consideration. Now we consider the average case consideration the desired data item can be found within the list and the probability to find in each element accessing are equally likely. Then the probability p=1/n. If we multiply this probability for each element visit as 1st element, 2nd element, … … , nth element and sum up then we can get the complexity.

퐶(푛) = 1.1푛

+ 2.1푛

+ 3.1푛

+ … + 푛.1푛

= (1 + 2 + 3 +⋯+ 푛).1푛

= 푛(푛 + 1)

2.1푛

= 푛 + 1

2

Using big O notation, we can write the complexity as O(n).

Binary Search:

The principle of binary search reduces the size of the searching domain of data collection half in each pass. That is why we require at most comparisons f(n) to find the desired item as

2 ( ) > 푛 or equivalently 푓(푛) =< ⌊푙표푔 푛⌋ + 1

Page 3:  · Page 3 of 26 Running time of worst case is approximately equal to H K C 6 J which is approximately equal to running time of average case. For example, if input size is 1000000

www.ashekalaziz.wordpress.com

Page 3 of 26

Running time of worst case is approximately equal to 푙표푔 푛 which is approximately equal to running time of average case.

For example, if input size is 1000000 then only 20 comparisons are required to find the required data item.

2 > 1000 = 1000000

Bubble Sort:

According to Bubble sort algorithm, if we want to sort list of data items in ascending order then (n-1) number of comparisons are done in first pass. Then the highest element is placed in far right position. (n-2) number of comparisons is done in second pass and next highest element is placed before the highest element at the far right, similarly the third, fourth and so on. Mathematically we can wirte,

푓(푛) = (푛 − 1) + (푛 − 2) + … … + 2 + 1 = 푛(푛 − 1)

2=푛 − 푛

2

Using big O notation we can write the complexity as O(푛 ). [Source: Seymour Lipschutz]

(d) What are the applications of data structure?

Answer:

If we have several students named ‘Ehsan Topu’, ‘Raian Islam’, ‘Iffat Ara’, ‘Fahima Akhter’ and ‘Shahadat Hossain’ then these names can be stored in a computer memory as follows. This is an application of one dimensional array of STUDENT names.

Let us consider a table of data consists of above students with their roll numbers and departments where they study. An array of records is devised below for such representation.

Ehsan Topu

Raian Islam

Iffat Ara

Fahima Akhter

Shahadat Hossain

STUDENT

Page 4:  · Page 3 of 26 Running time of worst case is approximately equal to H K C 6 J which is approximately equal to running time of average case. For example, if input size is 1000000

www.ashekalaziz.wordpress.com

Page 4 of 26

A linked representation of above tabular information is shown below.

Now let us look a hierarchical representation of above record.

2(a) What is an array? Describe the representation of one dimensional array in memory.

Answer:

An array is a list of data elements of single type either integer or real or character. No multiple types of data can be stored in an array. An array has fixed size i.e. the number of elements it can hold is fixed. For example, an array is declared with name DATA that consist of N number of integer values.

STUDENT

Ehsan Topu

Raian Islam

Iffat Ara

Fahima Akhter

Shahadat Hossain

1101

2106

2107

3208

4309

Business Admin

Computer Science

Computer Science

Electronics

Architecture

Roll No Names Department

STUDENT

Department

Business Admin

Computer Science

Electronics

Architecture

1

2

3

4

Roll No Names

Ehsan Topu

Raian Islam

Iffat Ara

Fahima Akhter

Shahadat Hossain

1101

2106

2107

3208

4309

Pointer

2

2

3

4

1

Student

Roll No Name Department

First name Last name

Address

House No Street City

Page 5:  · Page 3 of 26 Running time of worst case is approximately equal to H K C 6 J which is approximately equal to running time of average case. For example, if input size is 1000000

www.ashekalaziz.wordpress.com

Page 5 of 26

First element can be accessed as DATA[1], second as DATA[2] and so on. This is a one dimensional array. In computer memory, it is represented as follows.

(b) Write the down the algorithm of Bubble Sort. Suppose that following numbers are stored in an array.

DATA: 7, 18, 25, 2, 6, 12, 9. Sort the array of DATA in descending order using Bubble Sort.

Answer:

Bubble sort algorithm:

BUBBLE(DATA, N)

Here DATA is an array with N elements. This algorithm sorts the elements in ascending order in DATA.

1. Repeat Step 2 and 3 for K=1 to N-1 2. Set PTR=1 [Initialize pass pointer PTR] 3. Repeat while PTR ≤ N-K [Execute pass]

(a) If DATA[PTR]>DATA[PTR+1] then

Interchange DATA[PTR] and DATA[PTR+1] [End of if structure] (b) Set PTR=PTR+1 [End of inner loop]

[End of Step 1 outer loop] 4. Exit [Source: Seymour Lipschutz]

.

.

.

DATA[1]

DATA[2]

DATA[3]

DATA[3] . . . . .

DATA[N]

Page 6:  · Page 3 of 26 Running time of worst case is approximately equal to H K C 6 J which is approximately equal to running time of average case. For example, if input size is 1000000

www.ashekalaziz.wordpress.com

Page 6 of 26

Dry Run:

Here the data set is sorted in descending order within first three passes.

(c) Write down the algorithm to delete an item from an array.

Answer:

DELETE(DATA, N, K, ITEM)

Here DATA is a linear array with N elements and K is a positive integer such that K ≤ N. This algorithm deletes the Kth element from DATA.

1. Set IITEM = DATA[K] 2. Repeat for J = K to N-1

DATA: 7, 18, 25, 2, 6, 12, 9 DATA: 18, 7, 25, 2, 6, 12, 9 DATA: 18, 25, 7, 2, 6, 12, 9 DATA: 18, 25, 7, 2, 6, 12, 9 DATA: 18, 25, 7, 6, 2, 12, 9 DATA: 18, 25, 7, 6, 12, 2, 9 DATA: 18, 25, 7, 6, 12, 9, 2

DATA: 18, 25, 7, 6, 12, 9, 2 DATA: 25, 18, 7, 6, 12, 9, 2 DATA: 25, 18, 7, 6, 12, 9, 2 DATA: 25, 18, 7, 6, 12, 9, 2 DATA: 25, 18, 7, 12, 6, 9, 2 DATA: 25, 18, 7, 12, 9, 6, 2

DATA: 25, 18, 7, 12, 9, 6, 2 DATA: 25, 18, 7, 12, 9, 6, 2 DATA: 25, 18, 7, 12, 9, 6, 2 DATA: 25, 18, 12, 7, 9, 6, 2 DATA: 25, 18, 12, 9, 7, 6, 2

DATA: 25, 18, 12, 9, 7, 6, 2

DATA: 25, 18, 12, 9, 7, 6, 2

DATA: 25, 18, 12, 9, 7, 6, 2

DATA: 25, 18, 12, 9, 7, 6, 2

Page 7:  · Page 3 of 26 Running time of worst case is approximately equal to H K C 6 J which is approximately equal to running time of average case. For example, if input size is 1000000

www.ashekalaziz.wordpress.com

Page 7 of 26

Set DATA[J] = DATA[J+1] [Move (J+1)st element upward] [End of loop]

3. Set N = N-1[Reset the number N of elements in DATA] 4. Exit [Source: Seymour Lipschutz]

(d) Consider a 22×5 matrix array called SCORE. Suppose Base (SCORE)=300 and there are w = 4 words per memory cell. If the programming language stores two dimensional arrays using row major order, then what is the address of SCORE[15, 4]?

Answer:

For two dimensional array, we know that to find the address of the location in the array A at [J, K] cell for row major order,

LOC(A[J, K]) = Base(A) + w[n(J-1) + (K-1)]

Here, Base(A)=300 J=15, K=4 w=4 n=5 since m×n array is 22×5 is given Therefor, LOC(A[15, 4]) = Base(A) + w[n(J-1) + (K-1)] = 300 + 4[5(15-1)+(4-1)] = 300 + 4×73 = 592

3(a) Write down the algorithm to insert a new ITEM in any node of a linked list.

Answer:

Algorithm: [Source: Seymour Lipschutz]

INSLOC(INFO, LINK, START, AVAIL, LOC, ITEM)

This algorithm inserts ITEM so that ITEM follows the node with location LOC or inserts ITEM as the first node when LOC=NULL

1. If AVAIL=NULL, then Write: Overflow and Exit. [Overflow?] 2. Set NEW=AVAIL and AVAIL=LINK[AVAIL] [Remove first node from avail list] 3. Set INFO[NEW]=ITEM [copies new data into new node] 4. If LOC=NULL then Set LINK[NEW]=START and START=NEW [insert as first node]

Else Set LINK[NEW]=LINK[LOC] and LINK[LOC]=NEW [insert after node with location] [End of if structure]

5. Exit

Above algorithm’s operation is graphically illustrated below.

Page 8:  · Page 3 of 26 Running time of worst case is approximately equal to H K C 6 J which is approximately equal to running time of average case. For example, if input size is 1000000

www.ashekalaziz.wordpress.com

Page 8 of 26

Initial:

Step 3:

Step 4:

(b) The following list of names is assigned (in order) to a linear array INFO: Maisha, Jannatul, Brishty, Papia, Dina, Aditi, Kaniza, Nusaiba, Rinki, Eron, Shahnima, Halima. That is INFO[1]=Maisha, INFO[2]=Jannatul, … … … … … , INFO[12]=Halima.

Assign the value to an array LINK and variable START so that INFO, LINK and START from an alphabetical order of listing of the names.

Answer:

START

LOC Node B

AVAIL

NEW

ITEM

Node B

LOC START

NEW

Page 9:  · Page 3 of 26 Running time of worst case is approximately equal to H K C 6 J which is approximately equal to running time of average case. For example, if input size is 1000000

www.ashekalaziz.wordpress.com

Page 9 of 26

(c) What do you mean two-way linked list? Explain with example.

Answer:

A two-way linked list is a linear linked list consists of nodes with three fields – (i) An information field that contains data; (ii) A pointer field that contains the location of next node in the list and (iii)

START INFO LINK

Aditi

Brishty

Dina

Eron

Halima

Jannatul

Kaniza

Maisha

Nusaiba

Papia

Rinki

Shahnima NULL

Page 10:  · Page 3 of 26 Running time of worst case is approximately equal to H K C 6 J which is approximately equal to running time of average case. For example, if input size is 1000000

www.ashekalaziz.wordpress.com

Page 10 of 26

Another pointer field that contains the location of the previous node in the list. A two-way linked list is depicted below.

Example:

[Source: Seymour Lipschutz]

(d) Define with necessary figures for ‘grounded header list’ and ‘circular header list’.

Answer:

FIRST

× ×

LAST

INFO field of node N

BACK pointer field of node N

FORW pointer field of node N

Kirk

Dean

Maxwell

Adams

Lane

Green

Samuels

Fields

Nelson

NAME

7

6

11

12

3

0

4

1

0

2

8

9

FORW

8

5

7

0

1

11

12

3

4

BACK

1

2

3

4

5

6

7

8

9

10

11

12

5

9

10

FIRST

LAST

AVAIL

Page 11:  · Page 3 of 26 Running time of worst case is approximately equal to H K C 6 J which is approximately equal to running time of average case. For example, if input size is 1000000

www.ashekalaziz.wordpress.com

Page 11 of 26

A ‘grounded header linked list’ is nothing but the simple linear linked list where the last node’s LINK field contains NULL value i.e. does not point any new node. And a ‘circular header list’ is linked list which last node’s LINK field contains the address of the header node i.e. last node points the header node. Both type of lists are depicted below.

Figure: A grounded header linked list

Figure: A circular linked list

4(a) Define OVERFLOW and UNDERFLOW condition of a stack.

Answer:

We know that stack is a data structure in which data should be inserted and deleted only at one end. Obviously a stack has specific size i.e. number of elements it can hold. When one element is pushed into stack then it is placed at the available cell towards bottom and successive elements on the above free cell of the previous elements placed. In this sequence, if the top position is occupied by some element then the stack is full and if a programmer wants to push a new element into the data stack, the OVERFLOW condition occurs as no free cell is available in the stack.

On the other hand, if a programmer wants to delete or pop an element from the stack, the element at the top should come first and the successive elements toward the bottom. In a case occurs when no elements are in the stack i.e. stack is empty but the programmer wants to delete or pop an element then the condition of UNDERFLOW occurs. [Ref. Seymour Lipschutz]

START

Header Node

×

START

Header Node

×

Page 12:  · Page 3 of 26 Running time of worst case is approximately equal to H K C 6 J which is approximately equal to running time of average case. For example, if input size is 1000000

www.ashekalaziz.wordpress.com

Page 12 of 26

(b) What do you mean by ‘infix notation’ and ‘reverse polish notation’? Explain with example.

Answer:

If we have two operands A and B and the operator + then we write the arithmetic expression as A+B which is known as infix notation. If we like to multiply an operand C with summation result of above expression then we write (A+B)*C. We should remember that this expression is not equivalent to A+(B*C), because order of precedence is different in two expressions.

Since using infix notation, the order of operations and operands are not uniquely defined for the order of operations to be performed, Polish notation is used to avoid this ambiguity. The operator is written before the operands, for example, +AB is written in Polish notation instead of A+B as written in infix notation. For reverse Polish notation, the operator is written after the operands i.e. AB+. For infix notation (A+B)*C we can write the reverse Polish notation as AB+C*. [Ref. Seymour Lipschutz]

(c) Convert the following arithmetic infix expression Q to equivalent postfix expression P.

Q: A+(B*C-(D/E↑F)*G)*H.

Answer:

A + ( B * C - ( D / E ↑ F ) * G ) * H ) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20

Symbol Scanned Stack Expression P (1) A ( A (2) + ( + A (3) ( ( + ( A (4) B ( + ( A B (5) * ( + ( * A B (6) C ( + ( * A B C (7) - ( + ( - A B C * (8) ( ( + ( - ( A B C * (9) D ( + ( - ( A B C * D (10) / ( + ( - ( / A B C * D (11) E ( + ( - ( / A B C * D E (12) ↑ ( + ( - ( / ↑ A B C * D E (13) F ( + ( - ( / ↑ A B C * D E F (14) ) ( + ( - A B C * D E F ↑ / (15) * ( + ( - * A B C * D E F ↑ / (16) G ( + ( - * A B C * D E F ↑ / G (17) ) ( + A B C * D E F ↑ / G * - (18) * ( + * A B C * D E F ↑ / G * - (19) H ( + * A B C * D E F ↑ / G * - H (20) ) A B C * D E F ↑ / G * - H * +

[Source: Seymour Lipschutz]

Page 13:  · Page 3 of 26 Running time of worst case is approximately equal to H K C 6 J which is approximately equal to running time of average case. For example, if input size is 1000000

www.ashekalaziz.wordpress.com

Page 13 of 26

(d) Find the value of A(1,3) using the definition of the Ackermann function.

Answer:

We know that the Ackermann function is

If m=0 and n≠0 then A(m, n)=n+1 … … … … … … … (i)

If m≠0 and n=0 then A(m, n)=A(m-1, 1) … … … … … (ii)

If m≠0 and n≠0 then A(m, n)=A(m-1, A(m, n-1)) … … (iii)

For A(1, 3) = A(1-1, A(1, 3-1)) [eq (iii)]

= A(0, A(1, 2))

= A(0, A(1-1, A(1, 2-1))) [eq (iii)]

= A(0, A(0, A(1,1)))

= A(0, A(0, A(1-1, A(1, 1-1)))) [eq (iii)]

= A(0, A(0, A(0, A(1,0))))

= A(0, A(0, A(0, A(1-1,1)))) [eq (ii)]

= A(0, A(0, A(0, A(0,1))))

= A(0, A(0, A(0, 1+1))) [eq (i)]

= A(0, A(0, A(0, 2)))

= A(0, A(0, 2+1)) [eq (i)]

= A(0, A(0, 3))

= A(0, 3+1) [eq (i)]

= A(0, 4)

= 4+1

= 5 [Source: Seymour Lipschutz]

5(a) Define Complete Binary trees and Extended Binary trees with example. Consider the following algebraic expression E=(2x+y)(5a-b)3 . Draw the tree which corresponds to the expression E.

Answer:

In a binary tree, a node can have at most two children. A binary tree can be a complete binary tree if all its levels have maximum number of possible nodes except possibly the last level. In the last level, the nodes appear as far left as possible. Following T22 is a complete binary tree.

Page 14:  · Page 3 of 26 Running time of worst case is approximately equal to H K C 6 J which is approximately equal to running time of average case. For example, if input size is 1000000

www.ashekalaziz.wordpress.com

Page 14 of 26

Figure: Complete binary tree T22

A binary tree becomes an extended binary tree or 2-tree when its’ each node either has 2 children or no child. The nodes with 2 children are called ‘internal nodes’ and nodes with no child are called ‘external nodes’. An extended binary tree is shown below.

Figure: Extended binary tree or 2-tree

1

2 3

4 5 6 7

8 9 10 11 12 13 14 15

16 17 18 19 20 21 22

Page 15:  · Page 3 of 26 Running time of worst case is approximately equal to H K C 6 J which is approximately equal to running time of average case. For example, if input size is 1000000

www.ashekalaziz.wordpress.com

Page 15 of 26

A tree is constructed below following the expression E=(2x+y)(5a-b)3

[Source: Seymour Lipschutz]

(b) What is Recursion? Write an algorithm which calculates Fibonacci series.

Answer:

Recursion or recursive procedure is procedure or function that calls itself from within the function. The question is how such function is terminated. In order to avoid infinite looping in recursive function, there should be some condition for which the function will not be called by itself. So two most common properties of Recursion or recursive procedure are –

(i) There must be some criteria called base criteria for which the function or procedure does not call itself. In function definition, there should be some specific base value for which the function does not refer to itself. This is done for a recursive function to be well defined.

(ii) Each time the procedure does call itself (directly or indirectly), it must be closer to base criteria. In the argument list of the recursive function, new value is substituted where new values are determined using mathematical expressions to be closer to the base value.

[Source: Seymour Lipschutz, Byron S Gottfried]

Algorithm to calculate Fibonacci series with recursive procedure:

FIBONACCI(FIB, N)

*

+

*

2 x

y

3 -

b *

5 a

Page 16:  · Page 3 of 26 Running time of worst case is approximately equal to H K C 6 J which is approximately equal to running time of average case. For example, if input size is 1000000

www.ashekalaziz.wordpress.com

Page 16 of 26

This procedure calculates FN and returns the value in the first parameter FIB.

1. If N=0 or N=1 then Set FIB = N and Return 2. Call FIBONACCI(FIBA, N-2) 3. Call FIBONACCI(FIBB, N-1) 4. Set FIB = FIBA + FIBB 5. Return

(c) Build a heap tree with different stages from the list of numbers 40, 30, 50, 22, 60, 55, 77 and 56.

Answer:

(a) ITEM = 40 (b) ITEM = 30 (c) ITEM = 50

(d) ITEM = 22 (e) ITEM = 60

(e) ITEM = 55 (e) ITEM = 77

40 40

30

50

30 40

22

50

30 40

60

50 40

22 30

60

50 55

22 30 40

77

50 60

22 30 40 55

Page 17:  · Page 3 of 26 Running time of worst case is approximately equal to H K C 6 J which is approximately equal to running time of average case. For example, if input size is 1000000

www.ashekalaziz.wordpress.com

Page 17 of 26

(f) ITEM = 56

[Ref: Seymour Lipschutz]

(d) Briefly describe towers of Hanoi problem with example.

Answer:

Let us suppose there are 3 (three) disks and there are 3 (three) polls or pegs where the disks are placed in one peg. We should remember that all disks are different in size and no bigger disk can be placed upon smaller disk i.e. only smaller disk is placed upon bigger disks and all three disks are placed in same fashion. The game is to transfer all three disks from source peg to destination peg using middle peg as intermediate or auxiliary poll for halt. Whole game steps are shown below. Here number of disk n=3 and three pegs are A, B and C.

77

56 60

50 30 40 55

22

A B C

Initial

A B C

A→C

A B C

A→B

A B C

C→B

Page 18:  · Page 3 of 26 Running time of worst case is approximately equal to H K C 6 J which is approximately equal to running time of average case. For example, if input size is 1000000

www.ashekalaziz.wordpress.com

Page 18 of 26

[Ref: Seymour Lipschutz]

6(a) Suppose the following list of letters is inserted in order into an empty binary search tree.

J, R, D, G, T, E, M, H, P, A, F, Q

(i) Find the final tree T (ii) Find the in order traversal of T

Answer:

(i)

A B C

A→C

A B C

B→A

A B C

B→C

A B C

A→C

J

R

J J

R D

J

R D

G

J

R D

G T

Item = J Item = R Item = D

Item = G Item = T

Page 19:  · Page 3 of 26 Running time of worst case is approximately equal to H K C 6 J which is approximately equal to running time of average case. For example, if input size is 1000000

www.ashekalaziz.wordpress.com

Page 19 of 26

J

R D

G T

E

J

R D

G T

E

M

J

R D

G T

E

M

H H

M

E

T G

D

J

R

P

H

M

E

T G

D

J

R

P

A

Item = E Item = M

Item = H Item = P

Item = P

Page 20:  · Page 3 of 26 Running time of worst case is approximately equal to H K C 6 J which is approximately equal to running time of average case. For example, if input size is 1000000

www.ashekalaziz.wordpress.com

Page 20 of 26

(ii) The result of the in order traversal of final tree is – A, D, E, F, G, H, J, M, P, Q, R, T

b) Write down the Warshall’s algorithm for a directed graph G with M nodes that finds the path matrix P.

Answer:

H

M

E

T G

D

J

R

P

A

F Q

H

M

E

T G

D

J

R

P

A

F Item = F

Item = Q

Page 21:  · Page 3 of 26 Running time of worst case is approximately equal to H K C 6 J which is approximately equal to running time of average case. For example, if input size is 1000000

www.ashekalaziz.wordpress.com

Page 21 of 26

Warshall’s algorithm: A directed graph G with M nodes is maintained in memory by its adjacency matrix A. this algorithm finds the Boolean path matrix P of the graph G.

1. Repeat for I, J = 1,2, … …, M [Initializes P] If A[I, J] = 0 then Set P[I, J] = 0; Else Set P[I, J] = 1

[End of loop]

2. Repeat Steps 3 and 4 for K = 1,2, … … , M [Updates P] 3. Repeat Step 4 for I = 1, 2, … … , M 4. Repeat for J = 1, 2, … … , M

Set P[I, J] = P[I, J] ˅ (P[I, K] ˄ P[K, J]) [End of loop] [End of Step 3loop] [End of Step 2 loop]

5. Exit

[Source: Seymour Lipschutz]

(c) Consider the graph G in the following figure. Suppose nodes are stored in memory in an array Data as follows.

DATA: X, Y, Z, W

(i) Find the adjacency matrix A of graph G (ii) Find the path matrix P of G using powers of the adjacency matrix A

Answer:

(i) 퐴 =

0 0 0 11 0 1 11 0 0 10 0 1 0

X Y

Z W

X Y Z W XYZW

Page 22:  · Page 3 of 26 Running time of worst case is approximately equal to H K C 6 J which is approximately equal to running time of average case. For example, if input size is 1000000

www.ashekalaziz.wordpress.com

Page 22 of 26

(ii) 퐴 =

0 0 1 01 0 1 20 0 1 11 0 0 1

퐴 =

1 0 0 11 0 2 21 0 1 10 0 1 1

퐴 =

0 0 1 12 0 2 31 0 1 21 0 1 1

We know that 퐵 = 퐴 + 퐴 + 퐴 + … … + 퐴

Since we have four nodes in the graph G, we can consider m = 4

퐵 =

1 0 2 35 0 6 83 0 3 52 0 3 5

If we replace the nonzero entries in B matrix by 1, then we get the path matrix as follows.

푃 =

1 0 1 11 0 1 11 0 1 11 0 1 1

[Source: Seymour Lipschutz]

(d) Find the number of spanning trees of the graph G as given below:

Answer:

A

E

B C

D F

A

E

B C

D F

Spamming Tree 1

Page 23:  · Page 3 of 26 Running time of worst case is approximately equal to H K C 6 J which is approximately equal to running time of average case. For example, if input size is 1000000

www.ashekalaziz.wordpress.com

Page 23 of 26

A

E

B C

D F

A

E

B C

D F

A

E

B C

D F

A

E

B C

D F

Spamming Tree 2

Spamming Tree 3

Spamming Tree 4

Spamming Tree 5

Page 24:  · Page 3 of 26 Running time of worst case is approximately equal to H K C 6 J which is approximately equal to running time of average case. For example, if input size is 1000000

www.ashekalaziz.wordpress.com

Page 24 of 26

A

E

B C

D F

A

E

B C

D F

A

E

B C

D F

A

E

B C

D F

Spamming Tree 6

Spamming Tree 7

Spamming Tree 8

Spamming Tree 9

Page 25:  · Page 3 of 26 Running time of worst case is approximately equal to H K C 6 J which is approximately equal to running time of average case. For example, if input size is 1000000

www.ashekalaziz.wordpress.com

Page 25 of 26

A

E

B C

D F

A

E

B C

D F

A

E

B C

D F

A

E

B C

D F

Spamming Tree 10

Spamming Tree 11

Spamming Tree 12

Spamming Tree 13

Page 26:  · Page 3 of 26 Running time of worst case is approximately equal to H K C 6 J which is approximately equal to running time of average case. For example, if input size is 1000000

www.ashekalaziz.wordpress.com

Page 26 of 26

A

E

B C

D F

A

E

B C

D F

Spamming Tree 14

Spamming Tree 15