Vous êtes sur la page 1sur 11

Master of Computer Application (MCA) – Semester – 4

MC0080 – Analysis and Design of Algorithms

Assignment Set – 1

Kalpojit Chakraborty
Reg.No.:510312331
L.C.Code:0137
MC0080 – Analysis and Design of Algorithms

1. Explain briefly the different building blocks of Algorithms.

Ans: the basic actions and corresponding instructions used in a computer system based on
Von Neumann architecture. We may recall that an instruction is a notation for an action and a
sequence of instructions defines a program whereas a sequence of actions constitutes a
process. An instruction is also called a statement. The following three basic actions and
corresponding instructions form the basis of any imperative language. For the purpose of
explanations, the notation similar to that of a high – level programming language is used.

A)Basic Actions & Instructions


i) Assignment of a value to a variable is denoted by Variable expression;
Where the expression is composed from variable and constant operands using familiar
operators like +, –, * etc.

Assignment action includes evaluation of the expression on the R.H.S. An example of


assignment instruction/ statement is
j 2 * i + j – r;

It is assumed that each of the variables occurring on R.H.S of the above statement, has a
value associated with it before the execution of the above statement. The association of a
value to a variable, whether occurring on L.H.S or on R.H.S, is made according to the
following rule. For each variable name, say i, there is a unique location, say loc 1 (i),
in the main memory. Each location loc (i), at any point of time contains a unique value say v
(i). Thus the value v (i) is associated to variable i. Using these values, the expression on
R.H.S is evaluated. The value so obtained is the new value of the variable on L.H.S. This
value is then stored as a new value of the variable (in this case, j) on L.H.S. It may be noted
that the variable on L.H.S (in this case, j) may also occur on R.H.S of the assignment
symbol. In such cases, the value corresponding to the occurrence on R.H.S
(of j, in this case) is finally replaced by a new value obtained by evaluating the expression on
R.H .S (in this case, 2 * i + j – r) The values of the other variables, viz., i and r remain
unchanged due to assignment statement.

ii) The next basic action is read values of variables i, j, etc. from some secondary storage
device, the identity of which is (implicitly) assumed here, by a statement of the form
read (i j, ….); The values corresponding to variables i, j,… in the read statement, are,
due to read statement, stored in the corresponding locations loc (i), loc(j) …., in the main
memory. The values are supplied either, by default, through the keyboard by the user or from
some secondary or external storage. In the latter case, the identity of the secondary
external storage is also specified in the read statement.
MC0080 – Analysis and Design of Algorithms

iii) The last of the three basic actions, is to deliver/ write values of some variables say i, j etc.
to the monitor or to an external secondary storage by a statement of the form Write ( i, j,…);
The values in the locations loc (i), loc(j),…., corresponding to the variables i, j…. , in the
write statement are copied to the monitory or a secondary storage. Generally, values are
written to the monitor by default. In case, the values are to be written to a secondary storage,
then identity of the secondary storage is also specified in the write statement. Further, if the
argument in a write statement is some sequence of characters enclosed
within quotes then the sequence of characters as such, but without quotes,
is given as output.

B)Control Mechanism and Control structures


In order to understand and to express an algorithm for solving a problem, it is not enough to
know just the basic actions viz., assignment, reads and writes. In addition we must know and
understand the control mechanisms.

(i) Direct sequencing: When the sequence of execution of instructions is to be the same as
the sequence in which instruction are written in program text, the control mechanism is
called direct sequencing.

(ii) Selection: In many situations, we intend to carry out some action A if condition Q is
satisfied and some other action B if condition Q is not satisfied. This intention can be denoted
by

If Q then do A else do B,

Where A and B are instructions, which may be even composite instructions obtained by
applying these structuring rules recursively to the other instructions.

(iii) Repetition: Iterative repetitive execution of a sequence of actions is the basis of


expressing long processes by comparatively small number of instructions. As we deal with
only faint processes, therefore, the repeated execution of the sequence of actions has to be
terminated. The termination may be achieved either through some condition Q or by stating
in advance the number of times the sequence is intended to be executed.

-----------------------------------------
MC0080 – Analysis and Design of Algorithms

2. Explain the concept of “Notation Ө” in algorithms.

Ans.

The Notation Ө:
Provides simultaneously both asymptotic lower bound and asymptotic upper bound for a
given function.

Let f(x) and g(x) be two functions, each from the set of natural numbers or positive real
numbers to positive real numbers. Then f(x) is said to be Ө(g(x)) (pronounced as big – theta
of g of x) if, there exists positive constants C1, C2 and k such that C2 g(x) ≤ f(x) ≤ C1g(x) for
all x ≥ k. (Note the last inequalities represent two conditions to be satisfied simultaneously
viz., C2 g(x) ≤ f(x) and f(x) ≤ C1 g(x)) We state, the following theorem without proof, which
relates the three functions O Ω Ө.

Theorem: For any two functions f(x) and g(x), f(x) = Ө (g(x)) if and only if f (x) = O (g(x))
and f(x) = Ω (g(x)) where f(x) and g(x) are nonnegative….

Example: Let f(n) = 1+2 + …. +n, n ≥ 1. Show that f(n) = Ө (n2).

Solution: First, we find an upper bound for the sum. For n ≥ 1, consider

1+2 + … + n ≤ n + n + … + n = n. n = n2.

This implies that f(n) = O(n2). Next, we obtain a lower bound for the sum.

We have

This proves that f(n) = Ω (n2). Thus by the above. Theorem f(n) = Ө (n2).
MC0080 – Analysis and Design of Algorithms

---------------------------
3. Describe the concept of quicksort with suitable examples
Ans.
Quick Sort :

Quick sort is also a ‘divide and conquer’ method of sorting. It was designed by C. A. R
Hoare, one of the pioneers of Computer Science and also Turing Award Winner for the year
1980. This method does more work in the first step of partitioning the list into two sublists.
Then combining the two lists becomes trivial. To partition the list, we first choose some
value from the list for which, we hope, about half the values will be less than the chosen
value and the remaining values will be more than the chosen value. Division into sublist is
done through the choice and use of a pivot value, which is a value in the given list so that all
values in the list less than the pivot are put in one list and rest of the values in the other list.
The process is applied recursively to the sublists till we get sublists of lengths one.
Example of Quick Sort
We use two indices one moving from left to right and other moving from right to left, using
the first element as pivot In each iteration, the index i while moving from left to the right,
notes the position of the value first from the left that is greater than pivot and stops
movement for the iteration. Similarly, in each iteration, the index j while moving from right
to left notes the position of the value first from the right that is less than the pivot and stops
movement for the iteration. The values at positions i and j are exchanged. Then, the next
iteration starts with current values of i and j onwards.
5 3 1 9 8 2 4 7 ( given list)
i j
5 3 1 9 8 2 4 7
i j
{the value 9 is the first value from left that is greater than the pivot viz 5 and
the value 4 is the first value from right that is less than the pivot. These
values are exchanged to get the following list}

5 3 1 4 8 2 9 7
i j
{Moving i towards right and j towards left and i stops. If it reaches a value
greater than the pivot and j stops if j meets a value less than the pivot. Also
both stop if j ≤ i. We get the list}

5 3 1 4 8 2 9 7
{After exchanging the values 2 and 8 we get the list}

5 3 1 4 2 8 9 7
I j
{the next moves of i to the right and j to the left make j < i and this indicates
the completion of one iteration of movements of i and j to get the list with
positions of i and j as follows}

5 3 1 4 2 8 9 7
MC0080 – Analysis and Design of Algorithms

j i
{At this stage, we exchange the pivot with value at position j , i .e., 2 and 5 are
exchanged so that pivot occupies almost the middle position as shown below}

2 3 1 4 5 8 9 7
{it may be noted that all values to left of 5 are leas than 5 and all values to
the right of 5 are greater than 5. Then the two sublists viz 2, 3, 1, 4 and 8, 9,
7 are sorted independently}

2 3 1 4 and 8 7 9
i j i j
2 3 1 4 and 8 7 9
i j i j
2 1 3 4 and 8 7 9
i j j i
{as j < i, exchanging the pivot values with 7, value at j th position, we get}
{moving i and j further we get}

2 1 3 4 and 7 8 9
j i i j
{as j < i, , exchanging the pivot 2 with value 1 at j, we get neglecting the
pivot the two sub lists to be sorted as 1 and 3, 4}

1 2 3 4
and i j
3 4
j i
{pivot position = 1 = j. And pivot position is neglected therefore, we need to
sort the remaining lists which are respectively empty and {4 } , which are
already sorted. This completes the sorting. Merging does not require any
extra time as already the entries are in sorted order}
On basis of the discussion above, we now formalize the quick sort
algorithm / procedure
Procedure Quick – Sort (A [ min … max])
{min is the lower index and max the upper index of the array to be sorted
using Quick Sort}
begin
if min < max then
p partition (A [ min .. max])
{p is the position such that for min ≤ i ≤ p – 1. A [ i ] ≤ A [ p] and for all j ≥ p + 1.
A[j]≥A[p]}
Quick – Sort (A [ min .. p – 1]);
Quick – Sort (A [ p + 1 … max ] );
end;
In the above procedure, the procedure partition is called. Next, we defined
the procedure partition.
MC0080 – Analysis and Design of Algorithms

Procedure partition (A [ min … max ] )


i, j : integer ;
s: real;
{in the procedure the first element is taken as pivot; the index i, used below
moves from left to right to find first value i = I i from left such that
A [ v1] > A [ I] . Similarly j moves from right to left and finds first value j = v2
such that A [ v2] < A [ I ] . If j > i , then A [ i ] and A [ j ] are exchanged.
If j ≤ I then
A [I] and A [ j] are exchanged}
s A [ min ] ; i min + 1; j max
While (i < j ) do
begin
while (A[i] < p) do
i i+1
while (A [ j] > p) do
j j–1
exchange (A [ i ], A [ j ])
{the exchange operation involves three assignments, viz, temp A [i ] ;
A[i] A [ j ] and A [ j ] temp, where temp is a new variable}
end,; {of while loop}
exchange (A [ I ] , A [ j ] );
return j
{the index j is such that in the next iteration sorting is to be done for the two
subarray}
A [ min, …, j – 1 ] and A [ j + 1, …, max]

-------------------------------------
MC0080 – Analysis and Design of Algorithms

4. What do you mean by linked lists? Explain with example?

Ans.
Linked lists:
A linked list is a data structure in which the objects are arranged in a linear order. Unlike an
array, though, in which the linear order is determined by the array indices, the order in a
linked list is determined by a pointer in each object. Linked lists provide a simple, flexible
representation for dynamic sets.
As shown in Figure, each element of a doubly linked list L is an object with key field and
two other pointer fields: next and prev. The object may also contain other satellite data.
Given an element x in the list, next [x] points to its successor in the linked list, and prev [x]
points to its
predecessor. If prev [x]= NIL, the element x has no predecessor and is therefore the first
element, or head, of the list. If next [x] = NIL, the element x has no successor and is
therefore the last element, or tail, of the list. An attribute head [L] points to the first element
of the list. IF head [L] = NIL, the list is empty.
A list may have one of several forms. It may be either singly linked or doubly linked, it may
be sorted or not, and it may be circular or not. If a list is singly linked, we omit the prev
pointer in each element. If a list is sorted, the linear order of the list corresponds to the linear
order of keys stored in elements of the list; the minimum element is the head of the list, and
the
maximum element is the tail. If the list is unsorted, the elements can appear in any order. In
a circular list, the prev pointer of the head of the list points to the tail, and the next pointer of
the tail of the list points to the head. The list may thus be viewed as a ring of elements.

A doubly linked list L representing the dynamic set {1, 4, 9, 16}. Each element in the list
is an object with fields for the key and pointers (shown by arrows) to the next and
previous objects. The next field of the tail and the prev field of the head are NIL,
indicated by a diagonal slash. The attribute head [L] points to the head. (b) Following
the execution of LISTINSERT (L, x), where key [x] = 25, the linked list has a new object
with key 25 as the new head. This new object points to the old head with key 9. (c) The
result of the subsequent call LIST-DELETE (L, x), where x points to the object
with key 4.
MC0080 – Analysis and Design of Algorithms

-----------------------------------------
5. For a binomial tree Bk Prove that
(i) there are 2k nodes

(ii) the height of the trees is k

(iii) there are exactly nodes at depth i for i = 0, 1,…..,k


MC0080 – Analysis and Design of Algorithms

6. What do you mean by Fibonacci heaps? Explain their structure?

Ans.

Fibonacci heaps
a Fibonacci heap is a collection of min-heap-ordered trees. trees within Fibonacci heaps are
rooted but unordered.

Structure of Fibonacci heaps


Like a binomial heap, a Fibonacci heap is a collection of min-heap-ordered trees. The trees
in a Fibonacci heap are not constrained to be binomial trees, however. Figure shows an
example of a Fibonacci heap. Unlike trees within binomial heaps, which are ordered, trees
within Fibonacci heaps are rooted but unordered. As Figure shows, each node x contains a
pointer p [x] to its parent and a pointer child [x] to any one of its children. The children of x
are linked together in a circular, doubly linked list, which we call the child list of x. Each
child y in a child list as pointers left [y] and right [y] that point to y’s left and right siblings,
respectively. If node y is an only child, then left [y] = right [y] = y. The order in which
siblings appear in a child list is arbitrary.

(a)A Fibonacci heap consisting of five min-heap-ordered trees and 14 nodes. The
dashed line indicates the root list. The minimum node of the heap is the node containing
the key 3. The three marked nodes are blackened. The potential of this particular
Fibonacci heap is 5+2.3=11. (b) A more complete representation showing pointers p (up
arrows), child (down arrows), and left and right (sideways arrows).
MC0080 – Analysis and Design of Algorithms

Two other fields in each node will be of use. The number of children in the child list of node
x is stored in degree[x]. The Boolean-valued field mark[x] indicates whether node x has lost
a child since the last time x was made the child of another node. Newly created nodes are
unmarked, and a node x becomes unmarked whenever it is made the child of another node.
A given Fibonacci heap H is accessed by a pointer min [H] to the root of a tree containing a
minimum key; this node is called the minimum node of the Fibonacci heap. If a Fibonacci
heap H is empty, then min [H] = NIL. The roots of all the trees in a Fibonacci heap are linked
together using their left and right pointers into a circular, doubly linked list called the root
list of the Fibonacci heap. The pointer min [H] thus points to the node in the root list whose
key is minimum. The order of the trees within a root list is arbitrary. We rely on one other
attribute for a Fibonacci heap H : the number of nodes currently in H is kept in n[H].

END

Vous aimerez peut-être aussi