Vous êtes sur la page 1sur 9

Assignment # 01

DESIGN AND ANALYSIS OF


ALGORITHMS
SUBMITTED TO :TANVEER AHMED SIDDIQUI
SUBMITTED BY:HAMNA
ASLAM
REGISTRATION #
SUBMISSION DATE

SP15-BCS-016

19TH-sep-2016
Q 1) What is a flow chart? What are
its advantages
limitations/disadvantages?
FLOW CHART:A flowchart is a type of illustration that is used to signify
an algorithm, workflow or certain process showing the steps
used as boxes of different kinds, and their order is shown by
linking them with arrows. This diagrammatic representation
illustrates a solution model to a problem which is given.
Flowcharts are used in analyzing, designing, documenting or
managing a process or program in various fields.
It is a diagram of the sequence of arrangements or
proceedings of people or things involved in a complexed
system or activity.
1. Communication: - Flowcharts are enhanced way of
communicating the logics of a structure to all troubled.
2. proficient Coding: - The flowcharts act as a guide or
outline during the systems analysis and program
development stage.
3. appropriate Debugging: - The flowchart helps in
debugging process.
4. well-organized Program upholding: - The
maintenance of operating program becomes easy with
the help of flowchart. It helps the programmer to put
efforts more efficiently on that part.
5. faster clasp of associations Before any function
can be solved, it must be understood, the connection
between a variety of fundamentals of the function must
be recognized. The programmer can plan a extensive
process simply with the assist of a flowchart than by
written explanation.

6. efficient Analysis The flowchart becomes a outline


of a system that can be broken down into
comprehensive parts for study. Problems may be
identified and new approach may be recommended by
flowcharts.
7. Documentation Flowcharts serve as a good
documentation which assist greatly in program
conversion. In the incident of staff change, they supply
as guidance purpose by serving new staff in
understanding the presented programs.

Disadvantages Of Using FLOWCHARTS: 1. difficult logic: - Sometimes, the program logic is quite
complicated. In that case, flowchart becomes multipart
and clumsy.
2. Alterations and Modifications: - If alterations are
required we may have to re-draw the flow chart again.
3. duplicate: - As the symbols of flowchart cannot be
typed, reproduction of flowchart becomes a problem.
4. Complex logic Flowchart becomes complex and
clumsy where the problem logic is complex.
5. connection between circumstances and events
Sometimes it becomes difficult to establish the
relation among various conditions and the actions to be
taken there upon for a exacting condition.
6. consistency Flowcharts are although easy to track,
are not such a usual way of expressing events as
writing in English, nor are they simply translate into
Programming language.

Q2
Question # 3 (For all students)
Consider the following algorithm

ALGORITHM Euclid(m, n)
// This algorithm computes the Greatest Common Divisor of two positive integers by Euclids algorithm
// Input: Two non-negative integers m and n, not both zero.
// Output: Greatest Common Divisor of m and n
while n 0 do
rm%n
mn
nr
return m
What does Euclids algorithm do for a pair of numbers in which the first number is smaller than the
second one?
What is the largest number of times this can happen during the algorithms execution on such an
input?
Modify Euclids algorithm in the light of your answer ( part (a) and (b) )

a) For any input pair m, n such that 0 m < n (first number is smaller)
, Euclids algorithm simply swaps both the numbers on the initial
iteration.
As, gcd(m, n) = gcd(n, m) because m % n = m if m < n.
b) Such a swap can occur only once since gcd(m, n) = gcd(n, m mod
n) because the first number of the new pair (n) will be greater than its
second number (m % n) after every iteration of the algorithm .
c) while n 0 do
if m < n swap(m, n)
m <- m - n
return y

Question # 4 (For all students)


We discussed the following procedure for computing GCD in class room
Step 1 Assign the value of min{m,n} to t
Step 2 Divide m by t. If the remainder is 0, go to Step 3; otherwise, go to Step 4
Step 3 Divide n by t. If the remainder is 0, return t and stop; otherwise, go to Step 4
Step 4 Decrease t by 1 and go to Step 2
How do we know that the process will eventually stop?
The above procedure, in the form presented, does not work correctly when one of its input numbers is zero.
Write the above method in pseudo code form that incorporates the improvement so that it works for all valid
inputs.

Sol:
a) Well get to know that the process will eventally stop. Second
number of pair gets smaller with each iteration and it cant get
negative. So, the new value of n on next iteration is m % n, which will
always be smaller than n. Hence, values of second number in the pair
finally become zero and algorithm stops.

b) Sieve of Eratosthenes: The sieve of Eratosthenes is an algorithm


that provides such an effective procedure
Algorithm for generating consecutive primes not exceeding any given
integer n>1.
Input: Integer n 2
Output: List of primes less than or equal to n
for p 2 to n do A[p] p
do n for p 2 to
if A[p] 0 //p hasnt been previously eliminated from the list
j p* p //all smaller multiples 2p,,(p-1)p are eliminated
while j n do
A[j] 0 //mark element as eliminated
j j + p // p*(p+1), p*(p+2), ... //copy the remaining elements
of A to array L of the primes
i 0
for p2 to n do
if A[p] 0
L[i]A[p] i i +1

return L

Question # 5 (For all students)


Find gcd(31415, 14142) by applying Euclids algorithm.

Estimate how many times faster it will be to find gcd(31415, 14142) by Euclids algorithm compared
with the algorithm based on checking consecutive integers from min{m, n} down to gcd(m, n).
(http://www.cs.siena.edu/~ebreimer/courses/csis-385-s04/homework/sol1.html)

Sol:a. To find the gcd by euclids algorithm we will follow the following

procedure:
gcd(31415,14142) = 3131remainder and 2 is quotient
gcd(14142, 3131) = 1618 remainder and 4 quotient
gcd(3131, 1618) =1513 remainder
gcd(1618, 1513) =105 remainder
gcd(1513, 105) =43 remainder
gcd(105, 43) = 19 remainder
gcd(43, 19) = 5 remainder
gcd(19, 5) = 4 remainder
gcd(5, 4) = 1 remainder
gcd(4, 1) = 0 remainder
gcd(1, 0) = 1.
b. To answer this, we need to match up to the number of divisions the
algorithms make on the given input. The number of divisions made by
Euclids algorithm is 11 as we can calculate from part a. The number of

divisions made by the consecutive integer checking algorithm on each


of its 14142 iterations is either 1 and 2.
so the total number of multiplications is between 114142 and
214142.
Therefore, Euclids algorithm will be between
114142/11 1300 and 214142/11 2600 times faster.
So, 1300 to 2600 times faster.

Question # 6 (For all students)


What is the smallest number of divisions made by Euclids algorithm among all inputs 1 m, n 10?

What is the largest number of divisions made by Euclids algorithm among all inputs 1 m, n 10?

Note: Student will not earned marks if they just write answer (i.e. smallest number/ largest number).
Explain your complete solution with the help of example.

a. For any input pair m n 1, in which m is a multiple of n, So,


Euclids algorithm makes exactly one division, and it is the minimum
number feasible for two positive numbers.
b. The answer is 5 divisions.
EXAMPLE:- According to the Euclids algorithm in computing gcd(5, 8).
It is not too time consuming to get this answer by examining the
number of divisions made by the algorithm on all input pairs 1 <m<
n, the number of divisions required by Euclids algorithm to compute
gcd(m, n) is at most log(3)n) where = (1 + 5)/2.
Question # 7 (For all students)
Euclids algorithm, as presented in Euclids paper, uses subtractions rather than integer divisions. Write a
pseudo-code for this version of Euclids algorithm.

// Computes gcd(m, n) by Euclid's algorithm based on


subtractions
// Input: Two nonnegative integers m and n not both
equivalent to 0
// Output: The greatest common divisor of a and b

while x 0 do
if m < n swap(m, n)
m <- m n
return y

Question # 8 (For all students)


You are a software engineer at Techlogix Lahore. A year ago, your manager announced that your next
software product would comprise of 10 modules. As a system analyst, you analyzed the dependency of
module and time required to complete specific module. On the above analysis you compiled the following
table:
Module
Prerequisites Duration
M1
None
14
M2
M1
28
M3
M2
42
M4
M3
21
M5
M4, M9
21
M6
M2
20
M7
M6
10
M8
M5, M9
35
M9
M3
50
M10
M7,M8
00
Your manager want to know the minimum possible time for completing project according to the activities

described in above table. He assigns this task to you. You are designing an algorithm that returns the

minimum possible time for completing the project. As a software engineer/programmer, which data
structure would you select in order to solve the given problem algorithmically?

Solution:-

Question # 9 (For all students)


Suppose you are given a set S of n real numbers. Let x and y be largest and smallest element of S
respectively. We define range of S as:
Range = x y
For each representation of S given below, design an algorithm to compute the range.
[Specify your algorithms in Pseudo code]
An unsorted array.
A sorted array.
An array which is a concatenation of two sorted portions.
A sorted singly linked list.
A binary search tree.
Solution:-