Professional Documents
Culture Documents
1St Unit Aop
1St Unit Aop
From the data structure point of view, following are some important categories of algorithms:
Search − Algorithm to search an item in a data structure.
Sort − Algorithm to sort items in a certain order.
Insert − Algorithm to insert item in a data structure.
Update − Algorithm to update an existing item in a data structure.
Delete − Algorithm to delete an existing item from a data structure.
Algorithm as Technology:
An algorithm is defined as a finite set of instructions that if followed, accomplishes a
particular task.
Suppose computers were infinitely fast and computer memory was free. Would you have any reason
to study algorithms? The answer is yes, if for no other reason than that you would still like to
demonstrate that your solution method terminates and does so with the correct answer.
If computers were infinitely fast, any correct method for solving a problem would do. You would
probably want your implementation to be within the bounds of good software engineering practice
(i.e., well designed and documented), but you would most often use whichever method was the
easiest to implement.
Of course, computers may be fast, but they are not infinitely fast. And memory may be cheap, but it is
not free. Computing time is therefore a bounded resource, and so is space in memory. These
resources should be used wisely, and algorithms that are efficient in terms of time or space will help
you do so.
Efficiency
Algorithms devised to solve the same problem often differ dramatically in their efficiency. These
differences can be much more significant than differences due to hardware and software.
The first, known as insertion sort, takes time roughly equal to c1n2 to sort n items, where c1 is
a constant that does not depend on n. That is, it takes time roughly proportional to n2. The
second, merge sort, takes time roughly equal to c2n lg n, where lg n stands for log2 n and c2 is
another constant that also does not depend on n. Insertion sort usually has a smaller constant
factor than merge sort, so that c1 < c2. We shall see that the constant factors can be far less
significant in the running time than the dependence on the input size n. Where merge sort has
2
a factor of lg n in its running time, insertion sort has a factor of n, which is much larger.
Although insertion sort is usually faster than merge sort for small input sizes, once the input
size n becomes large enough, merge sort's advantage of lg n vs. n will more than compensate
for the difference in constant factors. No matter how much smaller c1 is than c2, there will
always be a crossover point beyond which merge sort is faster.
For a concrete example, let us pit a faster computer (computer A) running insertion sort
against a slower computer (computer B) running merge sort. They each must sort an array of
one million numbers. Suppose that computer A executes one billion instructions per second
and computer B executes only ten million instructions per second, so that computer A is 100
times faster than computer B in raw computing power. To make the difference even more
dramatic, suppose that the world's craftiest programmer codes insertion sort in machine
language for computer A, and the resulting code requires 2n2 instructions to sort n numbers.
(Here, c1 = 2.) Merge sort, on the other hand, is programmed for computer B by an average
programmer using a high-level language with an inefficient compiler, with the resulting code
taking 50n lg n instructions (so that c2 = 50). To sort one million numbers, computer A takes
By using an algorithm whose running time grows more slowly, even with a poor compiler,
computer B runs 20 times faster than computer A! The advantage of merge sort is even more
pronounced when we sort ten million numbers: where insertion sort takes approximately 2.3
days, merge sort takes under 20 minutes. In general, as the problem size increases, so does
the relative advantage of merge sort.
Algorithms and other technologies
The example above shows that algorithms, like computer hardware, are a technology. Total
system performance depends on choosing efficient algorithms as much as on choosing fast
hardware. Just as rapid advances are being made in other computer technologies, they are
being made in algorithms as well.
You might wonder whether algorithms are truly that important on contemporary computers in
light of other advanced technologies, such as
hardware with high clock rates, pipelining, and superscalar architectures,
easy-to-use, intuitive graphical user interfaces (GUIs),
object-oriented systems, and
local-area and wide-area networking.
of validation is to ensure that the algorithm will work correctly independent of the
issues concerning the programming language to be used.
Analysis of Algorithm: It refers to the task of determining the computing time (Time
Complexity) and storage space required (Space Complexity) to execute an algorithm
on a computer. It allows you to compare various algorithm used to solve a particular
problem. Performance of the algorithm in best, average and worst case is measured.
Testing Programs: Debugging the program is done in this phase. It is the process of
executing a program on sample data sets to determine whether errors our and
correcting them.
Different Criteria that satisfy an algorithm: An algorithm must satisfy the following
criteria.
Input: Zero or more quantities are externally supplying.
Output: At least one quantity is produced.
Definiteness: Each instruction is clear and unambiguous.
Finiteness: If we trace out the instruction of an algorithm, then for all cases, the
algorithm terminates after a finite no. of steps.
Effectiveness: Every instruction must also be feasible.
Analysis of Algorithm:
It is process of analyzing the problem- solving capability of the algorithm in terms of the time
and size required (the size of the memory for storage while implementation). The analysis is
a process of estimating the efficiency of an algorithm. There are two fundamental parameters
based on which we can analysis the algorithm:
o Space Complexity: The space complexity can be understood as the amount of space
required by an algorithm to run to completion.
o Time Complexity: Time complexity is a function of input size n that refers to the
amount of time needed by an algorithm to run to completion.
Before you implement any algorithm as a program, it is better to find out which among these
algorithms are good in terms of time and memory. It would be best to analyze every
algorithm in terms of Time that relates to which one could execute faster
and Memory corresponding to which one will take less memory.
we will be focusing more on time rather than space because time is instead a more limiting
parameter in terms of the hardware. It is not easy to take a computer and change its speed. So,
if we are running an algorithm on a particular platform, we are more or less stuck with the
performance that platform can give us in terms of speed.
However, on the other hand, memory is relatively more flexible. We can increase the
memory as when required by simply adding a memory card. So, we will focus on time than
that of the space.
The running time is measured in terms of a particular piece of hardware, not a robust
measure. When we run the same algorithm on a different computer or use different
programming languages, we will encounter that the same algorithm takes a different time.
Generally, we make three types of analysis, which is as follows:
o Worst-case time complexity: For 'n' input size, the worst-case time complexity can
be defined as the maximum amount of time needed by an algorithm to complete its
4
execution. Thus, it is nothing but a function defined by the maximum number of steps
performed on an instance having an input size of n.
o Average case time complexity: For 'n' input size, the average-case time complexity
can be defined as the average amount of time needed by an algorithm to complete its
execution. Thus, it is nothing but a function defined by the average number of steps
performed on an instance having an input size of n.
o Best case time complexity: For 'n' input size, the best-case time complexity can be
defined as the minimum amount of time needed by an algorithm to complete its
execution. Thus, it is nothing but a function defined by the minimum number of steps
performed on an instance having an input size of n.
o Amortized: The sequence of operations applied to the input of size a averaged over
time.
Usually, the efficiency or running time of an algorithm is stated as function relating the input
length to the number of steps known as time complexity, or volume of memory known as
space complexity.
An algorithm should have the following characteristics −
Unambiguous − Algorithm should be clear and unambiguous. Each of its steps (or
phases), and their inputs/outputs should be clear and must lead to only one meaning.
Input − An algorithm should have 0 or more well-defined inputs.
Output − An algorithm should have 1 or more well-defined outputs, and should
match the desired output.
Finiteness − Algorithms must terminate after a finite number of steps.
Feasibility − Should be feasible with the available resources.
Independent − An algorithm should have step-by-step directions, which should be
independent of any programming code.
Designing Algorithm:
Classification by Design Method:
Greedy Method: In the greedy method, at each step, a decision is made to choose the
local optimum, without thinking about the future consequences.
Example: Fractional knapsack, Activity Selection.
Divide and Conquer: This method involves dividing the problem into sub problem,
recursively solving them, and then recombining them for the final answer.
Other Classifications: Apart from classifying the algorithms into the above broad categories,
the algorithm can be classified into other broad categories like:
Randomized Algorithms: Algorithms that make random choices for faster solutions
are known as randomized algorithms.
Example: Randomized Quicksort Algorithm.
Classification by Research Area: In CS each field has its own problems and needs
efficient algorithms.
Example: Sorting Algorithm, Searching Algorithm, Machine Learning
etc.
Branch and Bound Enumeration and Backtracking: These are mostly used in
Artificial Intelligence.
Growth of Functions:
Algorithm’s rate of growth enables us to figure out an algorithm’s efficiency along with the
ability to compare the performance of another algorithm. Input size matters as constants and
lower order terms are influenced by the large sized of inputs.
Asymptotic Notation:
Types:
Big-O Notation (O).
Omega Notation (Ω).
Theta Notation (θ).
7
Big-O Notation (O): It defines an upper bound of an algorithm, it bounds a function only
from above.
O(g(n)) = {f(n): there exist positive constants c and n 0 such that 0 ≤ f(n) ≤ c g(n) for all n
≥ n0}.
Theta Notation (θ): The theta notation bounds a function from above and below, so it
defines exact asymptotic behaviour.
Θ(g(n)) = {f(n):
there exist positive constants c1, c2 and n0 such that
0 ≤ c1 g(n) ≤ f(n) ≤ c2 g(n) for all n ≥ n0}.
Logarithms
9
Factorials
n! = 1 ⋅ 2 ⋅ 3 ⋅ … ⋅ n
Special case 0! = 1.
◦ A weak upper bound on the factorial is n! ≤n n since the Stirling’s
approximation says that
Factorial Computation: In mathematical concept, the end product of given positive (+)
integers (n) or number. Factorial is also called as the end product of an integer and all the
integers falling under it.
Product of all consecutive Integer numbers up to n is called Factorial of a Number and is
denoted by n! For Example, the value of 5! is 120.
Step 1: Start
Step 2: Declare Variable n, fact, i
Step 3: Read number from User
Step 4: Initialize Variable fact=1 and i=1
Step 5: Repeat Until i<=number
5.1 fact=fact*i
5.2 i=i+1
Step 6: Print fact
Step 7: Stop
In the Fibonacci sequence expect for the first two terms of the sequence, every other term is
the sum of the previous two terms. The first two numbers of the Fibonacci series are 0 and 1.
From the 3rd number onwards, the series will be the sum of the previous 2 numbers.
Step 1: Start.
Step 2: Declare variables I, a, b, show
Step 3: Initialize the variables, a=, b=, and show= 0
Step 4: Enter the number of terms of Fibonacci series to be printed
Step 5: Print First two terms of series
Step 6: Use loop for the following steps
Show= a+b
A=b
B=show
Increase value of I each time by 1
Print the values of show
Step 7: End
0,1,1,2,3,5,8,13,21,34…
Reverse the digits of an integer: It will read an integer positive number and reverse that
number.
Step 1: Start.
Step 2: Read the number n.
Step 3: [Initialize]
Set rev = 0
Step 4: Repeat step 5, 6 & 7 until n! =0
Step 5: set r= n% 10
Step 6: set rev= rev*10+r
Step 7: set n=n/10
Step 8: print rev
Step 9: Stop
Base Conversion:
Decimal to binary, octal or hex base
Binary, octal or hex to decimal base
Between binary and octal or hex
12
This activity introduces the concept that abstractions built upon binary sequences
can be used to represent all digital data. It also introduces the concept of
an algorithm. It focuses on the following learning objectives:
5b. Explanation of how number bases, including binary and decimal, are
used for reasoning about digital data.
16a. Use of natural language, pseudo-code, or a visual or textual
programming language to express an algorithm.
Introduction
This lesson assumes you have completed the homework on binary and
hexadecimal number systems. That homework decribed how the binary, decimal,
and hexadecimal number systems work and showed how to convert from one
number system to another.
In this lesson we want generalize what we learned their by seeing those number
systems as specific examples of a more general concept, a positional number
system.
We will develop algorithms that will enable you to perform conversions from one
number system to another.
Pseudocdes are halfway between natural languages like English and formal
programming languages.
Let's review some of the key points that you learned in the Khan Academy videos.
Our decimal number system (and the binary and hexadecimal systems) are
particular instances of the more general concept of a positional number
system.
In a positional number system the same symbol can represent different
values depending on its position (or place) in the numeral. For example, in
91, the 9 represents 90 (the 10s place) but in 19 it represents 9 (the ones
place). Contrast this with how symbols work in a non-positional system, like
Roman numerals, where X always represents 10.
The base of a number system represents the number of symbols it has:
Conversion Algorithms
1. Let n be the number of digits in the number. For example, 104 has 3 digits,
so n=3.
2. Let b be the base of the number. For example, 104 is decimal so b = 10.
3. Let s be a running total, initially 0.
4. For each digit in the number, working left to right do:
Subtract 1 from n.
Multiply the digit times bn and add it to s.
5. When your done with all the digits in the number, its decimal value will be s
2 1 2 × 81 = 16 80
4 0 4 × 80 = 4 84
Let n = 45.
Let b = 16.
Repeat
45 divided by b is 45/16 = 2 remainder 13. So d=2 and r=13. So m= D and
the new n is 2.
2 divided by b is 2/16 = 0 remainder 2. So d=0 and r=2. So m=2D and
the new n is 0. So 4510 = 2D16.
25 ÷ 2 12 1(MSB)
12 ÷ 2` 6 0
6÷2 3 0
3÷2 1 1
1÷2 0 1(LSB)
Therefore, from the above table, we can write,
(25)10 = (11001)2
128÷8 16 0(MSB)
16÷8 2 0
2÷8 0 2(LSB)
Therefore, the equivalent octal number = 2008
Decimal to Hexadecimal:
Again in decimal to hex conversion, we have to divide the given decimal number by 16.
Example 3: Convert 12810 to hex.
Solution: As per the method, we can create a table;
128÷16 8 0(MSB)
8÷16 0 8(LSB)
Therefore, the equivalent hexadecimal number is 8016
Here MSB stands for a Most significant bit and LSB stands for a least significant bit.
0 0000
1 0001
2 0010
3 0011
4 0100
5 0101
6 0110
7 0111
8 1000
9 1001
A (10) 1010
B (11) 1011
C (12) 1100
D (13) 1101
19
E (14) 1110
F (15) 1111
You can easily solve the problems based on hexadecimal and binary conversions with the
help of this table. Let us take an example.
Example: Convert (89)16 into a binary number.
Solution: From the table, we can get the binary value of 8 and 9, hexadecimal base numbers.
8 = 1000 and 9 = 1001
Therefore, (89)16 = (10001001)2
0 000
1 001
2 010
3 011
4 100
5 101
6 110
7 111
Example: Convert (214)8 into a binary number.
Solution: From the table, we know,
2 → 010
1 → 001
4 → 100
Therefore,(214)8 = (010001100)2