Chap 1 - Introduction To Algorithms
Chap 1 - Introduction To Algorithms
Introduction to Algorithms
Informally, an algorithm is any well-defined computational procedure that takes some value, or
set of values, as input and produces some value, or set of values, as output in a finite amount of
time. An algorithm is thus a sequence of computational steps that transform the input into the
output.
Let us consider the problem of preparing an omelet. To prepare, we follow these steps:
n algorithm for a computational problem is correct if every problem instance provided as input
A
halts or finishes its computing in finite time and outputs the correct solution to the problem
instance.
There are two main criteria for judging the merits of algorithms:
● C orrectness (does the algorithm give a solution to the problem in a finite number of
steps?)
● Efficiency (how much resources in terms of memory and time) does it take to execute
the).
efinition:A finite set of statements that guaranteesan optimal solution in a finite interval of
D
time
i) The internet allows people all over the world to easily and quickly access a lot of information.
Smart algorithms help websites manage this huge amount of data by finding the best paths for
the data to travel. They also use search engines to quickly find pages that contain specific
information.
ii) You need to compress a large file containing text to occupy less space. Algorithms such as
Huffman coding allow you to store with fewer bits.
1
CSE 221: Algorithms [ARN] Ariyan Hossain
Follow-up Question:
) Other than speed, what other measures of efficiency might you need to consider in a
Q
real-world setting?
Algorithm Specification:
Input - Every Algorithm must take zero or more number of input values from external.
●
● Output - Every Algorithm must produce an output as a result.
● Definiteness - Every statement/instruction in an algorithm must be unambiguous (only
one interpretation)
● Finiteness - For all different cases, the algorithm must produce results within a
● finite number of steps.
● Effectiveness - Every Instruction must be basic enough to be carried out and it
also must be feasible.
M
● emory: how much space is needed?
● Computational time: how fast the algorithm runs
unning Time:The running time of an algorithm refers to the amount of time it takes for the
R
algorithm to complete its execution, given an input.
unning time grows with the size of the input. The input size is the number of elements in the
R
input eg. size of an array, no. of elements in a matrix, vertices, and edges in a graph.
Running time = the number of primitive operations (steps) executed before termination such as
arithmetic operations (+,-), and decision-making (if, while).
If computers were infinitely fast and had infinite memory, 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 (for example, your implementation should be well-designed
and documented), but you would most often use whichever method was the easiest to
implement
ifferent algorithms that solve the same problem often differ dramatically in their efficiency (not
D
only due to differences between hardware and software). For example, two sorting algorithms:
2
insertion sort takes time roughly equal to c1n
to sort n items whereas merge sort takes c2nlgn.
When n is very large, the time difference between the algorithms is significant. There are more
than 100 million web searches every half hour, and more than 100 million emails sent every
minute, hence it is important to use an efficient algorithm.
2