Академический Документы
Профессиональный Документы
Культура Документы
UNIT -I
Algorithms: Problem solving – Top-Down and Bottom- up approaches to algorithm design – Use of algorithms
in problem solving – Design, Implementation, Verification of algorithm – Efficiency analysis of algorithms:
Space, Time complexity, and Frequency count – Sample algorithms: Exchange the value of two variables –
Summation of set of numbers – Decimal to Binary conversion – Sorting – Factorial – Fibonacci – Finding a
largest Number in an array - Reverse the order of elements in array.
ALGORITHMS (ANALYSIS AND DESIGN)
DEFINITION
Algorithm is a step by step procedure or method for solving a problem by a computer in a finite number of
steps.
Every algorithm must satisfy the following criteria as,
Input (Zero or more) Finiteness (Finite number of steps)
Output (at least one) Effectiveness.
Definiteness (instruction must be clear and
unambiguous)
PROBLEM SOLVING
It is a logical process of breaking down the problem into smaller parts each of which can be solved step by
step to obtain the final solution.
The basic steps are used to solve the problem as,
Formulating the problem and deciding the data types to be entered.
Identify the steps of computation that are necessary for getting the solution.
Identify a decision point that is under what circumstances a particular operation is to be
performed.
Finding the result and verifying the values.
Procedure for Problem Solving
Step 1: Understanding the Problem
Think manually and test some data as, what is expected and How to get it?
The problem should formulate correctly.
Step 2: Construction of the list of variables
It is needed to find the solution of the problem.
Place the value in a given variable. Using that variable and solve the problem.
The name chosen for the variables should be an aid to memory.
Step 3: Output Design
This step is used to decide the layout for the output.
73
SUBJECT: DATA STRUCTURES AND ALGORITHMS UNIT- I CLASS: II-BCA
74
SUBJECT: DATA STRUCTURES AND ALGORITHMS UNIT- I CLASS: II-BCA
Advantages
1. It proceeds from the highest level to the lowest level.
2. Individual operation will be split up in to number of elementary steps.
3. Easy to share the problem development.
4. Increased comprehensive of the problem
5. Unnecessary lower-level details are required.
6. Reduced debugging time
Bottom-Up Approach of Problem Solving
When faced with a large and complex problem, it may be difficult to see how the whole thing can be
solved.
To solve parts of the problem individually and finally join each of the solutions together to form the
complete solution. This is called a Bottom-Up Approach.
82
SUBJECT: DATA STRUCTURES AND ALGORITHMS UNIT- I CLASS: II-BCA
Disadvantage
Parts of the solution may not fit together easily
There may be lack of consistency among modules, and thus, more re-programming may have to be
carried out.
83
SUBJECT: DATA STRUCTURES AND ALGORITHMS UNIT- I CLASS: II-BCA
Algorithm
BEGIN Set i to 1
Read k Set sum to 0
if k <= 0 repeat k times
then Add i to sum
Write “illegal value for k” Increment i by 1
END end of the repeat loop
Else Write “the sum of the first”, k, “integer is”, sum
END
DESIGN OF ALGORITHMS
It is decide which method will prove the most effective for the solution of that problem.
Each of the algorithms will involve a particular data structure.
The choice of data structure may involve time-space tradeoff. (ie) by increasing the amount of space
for storing the data
Once the data structure is chosen for a particular application, program is written by the set of logical
instructions that manipulate the related data items.
A study of data structure is also a study of the algorithm that controls them.
The algorithm should have the following traits as,
Completely free of ambiguity
Should be efficient
Should be concise and compact to facilitate verification of their correction.
How To Design An Algorithm
Algorithm design is a creative activity.
The first step in designing an algorithm is to produce a clear specification of the problem.
Approaches Explanation
Greedy Algorithm : Selects the best available option until all options finish. Ex: Shortest Path
Algorithm
Divide and Conquer : The big problems are divided into same type of smaller problems and
Algorithm design the algorithm to combine the implementation of these smaller
problems for implementing bigger problem. Ex: Quick Sort.
Non-Recursive : A set of instructions that perform a logical operation can be grouped
Algorithm together as a function. If a function calls itself, it is called as Direct
84
SUBJECT: DATA STRUCTURES AND ALGORITHMS UNIT- I CLASS: II-BCA
Recursion.
If a function calls another function, which invokes the calling function, it is
called as Indirect Recursion.
Randomized : This algorithm uses the feature of random number instead of a fixed
Algorithm number. It gives different results with different input data.
Backtrack Algorithm : Used to write a function (procedure) which traverses the solution space.
Ex: Game Tree.
Modular : Using this approach, each programmer can be given a specific set of
Programming modules to work on. Module are small, it is simpler to understand it as a
Approach unit of code
It is easier to test and debug a shorter module.
VERIFICATION OF ALGORITHM
Algorithm verification is a proof that the algorithm will accomplish its task.
This kind of proof is formulated by looking at the specifications for the subprograms.
These specifications combine properly to accomplish the task of the whole algorithm.
Verification of algorithm would consist of determining the quality of the output received.
The feedback sets the basis for making necessary changes in the already designed algorithm
specifications.
2
SUBJECT: DATA STRUCTURES AND ALGORITHMS UNIT- I CLASS: II-BCA
3
SUBJECT: DATA STRUCTURES AND ALGORITHMS UNIT- I CLASS: II-BCA
3. (log n) is when we divide linear list to half each time and traverse the middle element.
4. (n log n) is when we divide list half each time and traverse that half portion.
SAMPLE ALGORITHMS
Exchange the values of two variables.
Algorithm Steps
1. Begin
2. Get the values of variables x and y.
3. Assign the value of x to t.
4. Assign the value of y to x.
5. Assign the value of t to y.
6. Show the values of x and y.
7. Stop.
Example: Exchanging the Value of Two Variables
Exchanging the value of two variables means interchanging their values. We can clearly understand
this by an example. Suppose we have two variables x and y. Our aim is to swap or interchange the value
of x and y.
The original values of x and y are:
To do so, we need one temporary variable, i.e. t. First, copy the value of x into t, i.e.
4
SUBJECT: DATA STRUCTURES AND ALGORITHMS UNIT- I CLASS: II-BCA
We find that the value of x and y have now been interchanged as desired.
5
SUBJECT: DATA STRUCTURES AND ALGORITHMS UNIT- I CLASS: II-BCA
Algorithm
1. Begin
2. Read n numbers to be summed.
3. Initialize sum as 0.
4. Initialize 'count' as 1.
5. While 'count' is less than or equal to n, i.e. numbers to be added, repeatedly do:
a. Read the number at position 'count', i.e. when count is 1 read 1st number, when count is 2,
read second number and so on.
b. Update the current sum by adding to it the number read.
c. Add 1 to 'count'.
6. Write the sum of n numbers. (After the number at nth count has been added, the control will shift to step
7.)
7. Stop.
6
SUBJECT: DATA STRUCTURES AND ALGORITHMS UNIT- I CLASS: II-BCA
A computer can directly understand only binary number system. But we use data in the form of
decimal digits. All such numbers are converted to binary codes within the computer because computers
can operate using binary numbers only.
7
SUBJECT: DATA STRUCTURES AND ALGORITHMS UNIT- I CLASS: II-BCA
Algorithm
1. Begin
2. Get the decimal number whose binary equivalent we want.
3 Divide the decimal number by 2.
4. Write the remainder.
5. Quotient of previous stage become the dividend and again divide it by 2 at next stage.
6. Write the remainder to right hand side (R.H.S) of previous remainder.
7. Repeat the process successively till quotient is zero.
8. The set of digits formed by the remainders at different stages, when placed in the reverse order, will
form the binary equivalent of the decimal number.
9. Stop.
Find the smallest value and place it as first item in the array. At the same time, move the value of the
first location of the array to the location where smallest value was stored. It means interchange the value
of a [5] with a[l] using algorithm given in Section. Current values will be as show in Figure.
Figure Now a [1] element has the smallest value. See Figure
8
SUBJECT: DATA STRUCTURES AND ALGORITHMS UNIT- I CLASS: II-BCA
Figure now compare element of a [2] with other element in the unsorted array. We get the array as seen in
Figure
Swap the value of a[2] and a[6] with the help of temporary variable, temp. See Figure below
Repeat the above steps to get the 3rd element, then 4th, and so on.
Note that we need to process the unsorted array only (n - 1) times. The last element of the array would
now be in the ascending order.
9
SUBJECT: DATA STRUCTURES AND ALGORITHMS UNIT- I CLASS: II-BCA
Sorted Array
Algorithm
1. Begin
2. Read numbers and store them in an array of n elements i.e. size of array a is n with elements as a[1],
a[21, a[31, a[n].
3. Find the smallest element within unsorted array.
4. Exchange i.e. swap smallest value of the array element and store it in 11' element of the unsorted part of
array.
5. Repeat steps 3 and 4 until all the elements in the array are arranged in the ascending order.
6. Stop
10
SUBJECT: DATA STRUCTURES AND ALGORITHMS UNIT- I CLASS: II-BCA
FACTORIAL COMPUTATION
Problem
Given a number n, compute n factorial(n) where n 0.
Algorithm development
To start the development of this algorithm by examining the definition of n!. to given that
n! = 1 × 2 × 3 × … × (n-1) × n for n ≥ 1
and by definition
0! = 1
In formulating our design for this problem we need to kept in mind that the computer’s arithmetic unit can
only multiply two numbers at a time.
11
SUBJECT: DATA STRUCTURES AND ALGORITHMS UNIT- I CLASS: II-BCA
Algorithm description
Algorithm :
Step 1 : Start
Start 2 : Read n
Start 3 : Initialize counter variable i to 1 and fact to 1
Start 4 : if i <= n go to step 5 otherwise goto step 7
Start 5 : calculate fact = fact * i
Start 6 : increment counter variable i and goto step 4
Start 7 : Write fact.
Start 8 : Stop
Pascal implementation
Function nfactorial (n:integer)
begin
fact:=1;
for i:=1 to n do
fact:= i * fact;
nfactorial:=fact
end
12
SUBJECT: DATA STRUCTURES AND ALGORITHMS UNIT- I CLASS: II-BCA
That is,
a:=0 [1] term before preceding term
b:= 1 [2] preceding term
c:= a + b [3] new term
a := b [4] term before preceding term becomes preceding term
b := c [5] preceding term becomes new term
After making step [5] we are in a position where we can use the definition to generate the next Fibonacci
number. A way to do this is to loop back to step [3]. Further investigation of steps [3] – [5] indicates they can
be placed in a loop to iteratively generate Fibonacci numbers. The essential mechanism we could use is:
a:=0;
b:=1;
i := 2;
while i < n do
begin
i := i + 1;
c := a + b;
a := b;
b := c
end
13
SUBJECT: DATA STRUCTURES AND ALGORITHMS UNIT- I CLASS: II-BCA
Algorithm description
Start
Declare variables i, a,b , show
Initialize the variables, a=0, b=1, and show =0
Enter the number of terms of Fibonacci series to be printed
Print First two terms of series
Use loop for the following steps
-> show=a+b
-> a=b
-> b=show
-> increase value of i each time by 1
-> print the value of show
End
14
SUBJECT: DATA STRUCTURES AND ALGORITHMS UNIT- I CLASS: II-BCA
Here
a[1]=a[5]
a[2]=a[4]
a[3]=a[3]
Here, we can see the suffixes/subscripts on left hand side are in increasing order and the
suffixes/subscripts on right hand side are in decreasing order.
For determining the reverse-array subscript values, we can use the formula [n - i + 1] where 'n' is total
number of elements an 'i' is the subscript of the element of original array.
n-i+1
5-1+1=5
5-2+1=4
5-3+1=3
Swapping the values of those elements in the array would thus be reversed.
To exchange the values of the elements of the array, we need one temporary variable. Then we do the
swapping as given below:
temp = a[i]
a[i] = a[n -i + 1]
a[n - i + 1] = temp
15
SUBJECT: DATA STRUCTURES AND ALGORITHMS UNIT- I CLASS: II-BCA
Algorithm
1. Begin
2. Get the values and store them in an array with elements a [1], a [2], a [3], a[4]…a[n] where n is the
number of elements.
3. Compute r, the number of exchange needed to reverse the array. r = integer value of 1/2.
4. Exchange the ith element with [n - i + 1] using
temp = a [i]
a [i] = a [n - i + 1]
a [n - i + 1] = temp
5. Carry step 4, r times increasing i by 1.
16
SUBJECT: DATA STRUCTURES AND ALGORITHMS UNIT- I CLASS: II-BCA
Algorithm
1. Begin
2. Get the value of element in an array and store them the array elements a[1], a[2]…..a[n] where n is
the number of elements.
3. Set temporary variable and assign the value of lst array element a[l] to temp. .
4. Compare temp with the next element a [2] of the array.
5. If the element is having greater value than temp, assign that value to temp.
6. Repeat step 4 and 5 till last element a[n] of the array is encountered.
7. Final value of temp is the largest value stored in the array.
8. Stop
IMPORTANT QUESTIONS:
2 MARKS:
1. Define an Algorithm.
2. What is abstract data type?
3. Define 'top down programming' approach briefly.
4. Define recursion briefly.
5. Define — datatype.
6. Write the conventions of storing any matrix in memory.
7. Define Data Structures.
8. Define algorithm and program.
9. List the two criteria used to measure the performance of an algorithm.
10. What is recursion?
11. What are the three types of linked list?
12. What do you mean by Time and space complexity?
5 MARKS:
10 MARKS:
1. Mention any four rules for writing an efficiency algorithm and discuss.
2. Explain any four issues in writing an algorithm Discuss in detail.
3. Explain the topdown and bottomup approaches handled for algorithm design.
4. Explain various steps to analysis an algorithm.
5. Discuss the procedure for problem solving.
6. Explain the logic and algorithm to find the and smallest number in an array.
7. List the basic steps involved in the development of an algorithm and explain in detail.
18