Algorithms Vca II Year
Algorithms Vca II Year
An algorithm is any well defined set of instructions that takes some value, or set of values as Input, and produces
some value, or set of values as output. An algorithm is thus a sequence of computational steps that transform the
input into the output.
We can also say that an algorithm is a mechanism for solving a given problem. The statement of the problem
specifies the desired input/output relationship.
Example
Write an algorithm to find area of a rectangle.
Step 1: Start
Step 2: Take length and breadth and store them as L and B?
Step 3: Multiply by L and B and store it in area
Step 4: Print area
Step 5: Stop
Write an algorithm to check whether he is eligible to vote? (More than or equal to 18 years old).
Step 1: Start
Step 2: Take age and store it in age
Step 3: Check age value, if age >= 18 then go to step 4 else step 5
Step 4: Print Eligible to vote and go to step 6
Step 5: Print Not eligible to vote
Step 6: Stop
The term algorithm is believed to derive from the name of a 9th century Arabian mathematician Al-Khowarizmi.
A computer program is simply an implementation of an algorithm on a computer.
Efficiency
The efficiency refers to the ability of an algorithm to solve a given problem keeping in mind the different
constraints. The efficiency depends on the resources that the algorithm requires.
Computational time (CPU consumption)
Memory space (RAM consumption)
Communication bandwidth consumption
The efficiency depends on:
o Memory
o Time
o Number of steps
o Number of particular operations
Number of disk operations
Number of network packets
Example:
Sequential search in a list of size n
Worst-case: n comparisons
Best-case: 1 comparison
Average-case: n/2 comparisons
The algorithm runs in linear time - Linear number of operations
Analysis
Analyzing an algorithm has come to mean predicting the resources that the algorithm requires. Resources such as
memory, communication bandwidth, or computer hardware are of primary concern, but most often it is
computational time that we want to measure. By analyzing several candidate algorithms, a most efficient one can
be easily identified.
The following are the criteria which are used for analyzing algorithms:1.
Correctness:
Before determining an algorithm is correct, we should know what CORRECT means.
Therefore, we need a specific condition, the input on which it is expected to work on (pre-conditions) and what
result it will produce (Post-conditions ). If the pre-conditions are satisfied , the post-conditions will be true.
2.
Fundamental Notations
Notation
This notation bounds a function with constant factor. We say:
F(n) = (g(n))
If there exists positive constants n0, c1and c2 such that the value of f(n) always lies between C1g(n) and
C2g(n).
1.
O Notations
2.
Notation
This notation gives a lower bound for a function within a constant factor, we write f(n)=(g(n)) if
there are positive constants such that f(n) always lie above or on g(n).
Note: n here refers to the no. of inputs.
FlowChart
Example
Draw a flowchart to find the simple interest. (Sequence)