Vous êtes sur la page 1sur 9

K u v e m p u U n i v e r s i t y

Assignments for B.Sc.(IT) & M.Sc.(IT) Courses



Subject: Algorithms
Subject Code: BSIT 41

Assignment: TA (Compulsory)
1. Define an algorithm. What are the properties of an algorithm?
Ans: An algorithm is a set of instructions that provide step-by-step specifications to perform a
task.
Example:-
Step by step procedure for display 10 natural numbers:-
1. Set the value of counter to 1
2. Display counter
3. Increment counter by 1
4. If counter <= 10, go to step 2
The preceding step -by -step procedure is an algorithm because it produces the correct
result in
A finite number of steps.
The properties of an algorithm are:
Input: Specifies the data set that is applied to the algorithm to check its validity.
Output: Specifies the data set that is produced as a result of the algorithm execution.
Definiteness: Specifies that the instructions described in the algorithm should be well
defined and should not create any ambiguity.
Termination: Specifies that the instructions described in the algorithm must contain a
proper termination condition.
Effectiveness: Specifies that the algorithm take less time and less memory space during its
execution.
2. Write a note on I) devising ii) validating and iii) testing of algorithms.
Ans: (i) Devising: The process of devising an algorithm is both an art and a science. This is
one part that cannot be automated fully. Given a problem description, one have to think of
converting this into a series of steps, which, when executed in a given sequence solve the
problem. To do this, one has to be familiar with the problem domain and also the computer
domains. This aspect may never be taught fully and most often, given a problem description,
how a person precedes to convert it into an algorithm becomes a matter of his style no
firm rules become applicable here.
ii) Validating: Once an algorithm has been devised, it becomes necessary to show that it
works. i.e it computes the correct answer to all possible, legal inputs. One simple way is to
code it into a program. However, converting the algorithms into programs is a time
consuming process. Hence, it is essential to be reasonably sure about the effectiveness of
the algorithm before it is coded. This process, at the algorithm level, is called validation.
Several mathematical and other empirical methods of validation are available. Providing the
validation of an algorithm is a fairly complex process and most often a complete theoretical
validation, though desirable, may not be provided. Alternately, algorithm segments, which
have been proved elsewhere, may be used and the overall working algorithm may be
empirically validated for several test cases. Such methods, although suffice in most cases,
may often lead to the presence of unidentified bugs or side effects later on.
(iii) Testing the Algorithm: The ultimate test of an algorithm is that the programs based on
the algorithm should run satisfactorily. Testing a program really involves two phases a)
debugging and b) profiling. Debugging is the process of executing programs with sample
datasets to determine if the results obtained are satisfactory. When unsatisfactory results
are generated, suitable changes are made in the program to get the desired results. On the
other hand, profiling or performance measurement is the process of executing a correct
program on different data sets to measure the time and space that it takes to compute the
results.
However, it is pointed out that debugging can only indicate the presence of errors but not
the absence of it. i.e., a program that yields unsatisfactory results with a sample data set is
definitely faulty, but just because a program is producing the desirable results with one/more
data sets cannot prove that the program is ideal. Even after it produces satisfactory results
with say 10000 data sets, its results may be faulty with the 10001th set. In order to actually
prove that a program is perfect, a process called proving is taken up. Here, the program is
analytically proved to correct and in such cases, it is bound to yield perfect results for all
possible sets of data
3. What is a linear data structure? Give examples. Describe how an array is represented.
Ans: LINEAR DATA STRUTURES- A data structure in which every data element has got
exactly two neighbors or two adjacent elements except two elements having exactly one
data element is called a linear data structure. Otherwise it is called a nonlinear data
structure.

Array and its representation
Array is a finite ordered list of data elements of same type. In order to create an array it
is required to reserve adequate number of memory locations. The allocated memory
should be contiguous in nature. The size of the array is finite and fixed up as a constant.
Some of the important operations related to arrays are:-
Creation () g A[n], Array created (reservation of adequate number of memory locations)
memory reserved,
Write (A, i, e) g Updated array with e at ith
position; Compare (i, j, Relational operator) g
Boolean;
Read (A, i) g e element at ith
position; Search (A, e) g Boolean;

4. Write algorithms to implement the following operations on a stack - create, push, pop.
Ans: Following are the algorithms to implement the operation on a stack.
a) Algorithm:
Create Output:
S, Stack created
Method:
Declare S[SIZE] //Array of size=SIZE
Declare and Initialize T=0 //Top pointer to remember the number of elements
Algorithm ends
b) Algorithm: Push
Input: (1) S, stack; (2) e, element to be inserted; (3) SIZE, size of
the stack; (4) T, the top pointer
Output: (1) S, updated; (2) T, updated
Method:
If (Isfull(S)) then
Print (stack
overflow) Else

T=T+1;
S[T]
=e
If end
Algorithm
ends
c) Algorithm: Pop
Input: (1) S, stack;
Output: (1) S, updated; (2) T, updated (3)eelement popped
Method:
If (Isempty(S)) then
Print (stack is
empty)
Else
e = S[T]
T=T-1;
If end
Algorithm ends

5. What is a first-in-first-out data structure? Write algorithms to perform the following
operations on it create, insertion, deletion, for testing overflow and empty conditions.
Ans: Queue is a linear data structure in which insertion can take place at only one end called
rear end and deletion can take place at other end called top end. The front and rear are two
terms used to represent the two ends of the list when it is implemented as queue. Queue is
also called First In First Out (FIFO) system since the first element in queue will be the first
element out of the queue.
Like stacks, queues may be represented in various ways, usually by means of one way list
or linear arrays. Generally, they are maintained in linear array QUEUE. Two pointers FRONT
and REAR are used to represent front and last element respectively. N may be the size of
the linear array. The condition when FRONT is NULL indicate that the queue is empty. The
condition when REAR is N indicated overflow.
Algorithm for Create
Create( )
{
Declare size of queue Q
front=null;
end=null;
}

Algorithm for Insert:
Insert(Q, x) // the size of queue Q is n and queue is linear
{
if front= 1 and end= n
Print Overflow
Exit
if front=null
set front=1
set end =1
else
end= end+1
Q[end]= x
}
Algorithm for Delete:
Delete(Q)
{
if front =null
print underflow
if front= end
Set front= null
end = null
else
front= front+1
}
6. What is a graph? What are the two ways of representing a graph? Describe with the
help of illustrative examples.
Ans: A graph that has neither self-loop nor parallel edges are called a simple graph,
otherwise it is called general graph. It should also be noted that, in drawing a graph, it is
immaterial whether the lines are drawn straight or curved, long or short: what is important is
the incidence between the edges and vertices.
Graph can be represented through
a) Adjacency list
b) Adjacency matrix.


Adjacency List: The graph is represented through linked list. The list shows that node 1 is connected to node 2,
3, and 4.

Adjacency Matrix: Each entry of 1 shows that there is an edge between the vertices.eg. An entry of 1 in 1 row
and 2 column shows that there is an edge between 1 and 2.

7. What is a circular queue? Write algorithms to implement the insertion and deletion
operations.
Ans: A circular queue uses the same conventions as that of linear queue. Using Front will always point
one position counter clockwise from the first element in the queue. In order to add an element, it
will be necessary to move rear one position clockwise. Similarly, it will be necessary to move front one
position clockwise each time a deletion is made. Nevertheless, the algorithms for create (), Isfull (),
Isempty (), Front () and Rear () are same as that of linear queue.
Insert(Q, x) // the size of queue Q is n and queue is circular
{
if front= 1 and end= n
Print Overflow
Exit
if front=null
set front=1
set end =1
else if end=n
set end=1
else
end= end+1
Q[end]= x
}
Delete(Q)
{
if front =null
print underflow
If front= end
Set front= null
end = null
else if front= n
set front=1
else
front= front+1
}
z
8. Write an algorithm to find the roots of a quadratic equation.
Ans: TO FIND THE ROOTS OF A QUADRATIC EQUATION
The algorithm to find the solution to a quadratic equation is:
Algorithm: Quadratic solver
Input : a,b,c the co-efficient of the Quadratic Equation
Output : The two roots of the Equation
Method

disc = ((b*b) (4*a*c))
if (disc = 0)
display roots are real and
equal
r1 = -
b/2a
r2 = -
b/2a
display
r1
display
r2
else
if(disc
>0)
display roots are real and
distinct
r1 = (-
b+sqrt(disc))/2a r2
= (-b-sqrt(disc))/2a
else
display roots are
complex
display real part,-
b/2a
display imaginary part, sqrt(absolute_value_of(disc))
display the two root exists in conjugates
end_if
Algorithm ends
9. Design an algorithm to check whether a given string is a palindrome or not.
Ans: Algorithm: check whether the string is a palindrome or not
Input: string, flag
Output: string is a palindrome
Method:
count = 0
while (the next character ch in the string is not empty)
a(count) = ch
count = count+1
end while
half = count/2palin = true
for (i=1 to half in steps of 1 and j=count to half in steps of 1 do)
if (a (i)! =a (j))
palin = false
break
end if
if (palin = true)
Display 'String is a palindrome'
else
Display 'String is not a palindrome'
end if
end for
Algorithm ends
10. Develop an algorithm to generate all the prime numbers between the given 2 limits.
Ans: Algorithm: Primality Testing (Second approach)
Input : between 10 & 20 , number
flag, test condition
Output : flag updated
Method
flag = 0
for(i=2 to square_root(n) in steps of +1 and flag = 0)
if( n % i = 0) // n mod i
flag = 1
end_if
end-for
if(flag = 0)
display Number is prime
else
display Number is not prime
end_if
Algorithm ends

Vous aimerez peut-être aussi