KEMBAR78
Algorithm description in data structures | PPTX
WHAT IS AN ALGORITHM?
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.
An algorithm is thus a sequence of computational steps that transform the
input into the output.
ALGORITHM
An algorithm: is an outline of the steps that a program or any computational
procedure has to take.
A program: on the other hand is an implementation of an algorithm and it
could be in any programming language.
Data structure: is the way we need to organize the data, so that it can be used
effectively by the program.
WHAT IS AN ALGORITHM?
We can also view an algorithm as a tool for solving a well-specified
computational problem.
The statement of the problem specifies in general terms the desired
input/output relationship.
The algorithm describes a specific computational procedure for achieving that
input/output relationship.
ALGORITHM
What is an algorithmic problem?
specifications
of an input ?
specification
of output as
function of
input
Input: 67, 34, 12, 5, 7, 67, 1
Output: 1, 5, 7, 12, 34, 67, 67
ALGORITHM
What is an algorithmic problem?
specifications
of an input Algorithm
specification
of output as
function of
input
An algorithm is essentially, describing the actions that one should take on the
input instance to get the specified output.
WHAT IS AN ALGORITHM?
For example, we might need to sort a sequence of numbers into non-
decreasing order.
Here is how we formally define the sorting problem.
Input: A sequence of n numbers <a1, a2, . . ., an >.
Output: A permutation (reordering) <a’1, a’2, . . ., a’n > of the input sequence such
that a’1≤ a’2 ≤. . . ≤a’n .
WHAT IS AN ALGORITHM?
For example, we might need to sort a sequence of numbers into non-
decreasing order.
Here is how we formally define the sorting problem.
Input: A sequence of n numbers <a1, a2, . . ., an >.
Output: A permutation (reordering) <a’1, a’2, . . ., a’n > of the input sequence such
that a’1≤ a’2 ≤. . . ≤a’n .
WHAT IS AN ALGORITHM?
For example, given the input sequence <31; 41; 59; 26; 41; 58>, a sorting
algorithm returns as output the sequence <26; 31; 41; 41; 58; 59>.
Such an input sequence is called an instance of the sorting problem.
In general, an instance of a problem consists of the input (satisfying whatever
constraints are imposed in the problem statement) needed to compute a
solution to the problem.
ALGORITHM
Characteristics of an Algorithm
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
WHAT IS AN ALGORITHM?
An algorithm is said to be correct if, for every input instance, it halts with the
correct output.
We say that a correct algorithm solves the given computational problem.
An incorrect algorithm might not halt at all on some input instances, or it might
halt with an incorrect answer.
Contrary to what you might expect, incorrect algorithms can sometimes be
useful, if we can control their error rate.
WHAT IS AN ALGORITHM?
There are two main criteria for judging the merits of algorithms:
1. correctness (does the algorithm give solution to the problem in a finite
number of steps?) and
2. efficiency (how much resources (in terms of memory and time) does it take
to execute the).
WHY THE ANALYSIS OF ALGORITHMS?
In computer science, multiple algorithms are available for solving the same
problem (for example, a sorting problem has many algorithms, like insertion
sort, selection sort, quick sort and many more).
Algorithm analysis helps us to determine which algorithm is most efficient in
terms of time and space consumed.
GOAL OF THE ANALYSIS OF ALGORITHMS
The goal of the analysis of algorithms is to compare algorithms (or solutions)
mainly in terms of running time but also in terms of other factors (e.g.,
memory, developer effort, etc.)
HOW DOES ONE MEASURE THE RUNNING TIME OF AN ALGORITHM?
Let us look at the empirical (experimental) study.
1. Write a program that implements the algorithm.
2. Run the program with varying data sets in which some are smaller, some are
of larger data sets, some would be of some kinds and some would be of
different kinds of varying composition.
3. Use a method (like System.currentTimeMillis()) to get an accurate measure
of the actual running time.
HOW DOES ONE MEASURE THE RUNNING TIME OF AN ALGORITHM?
This has certain limitations.
1. First you have to implement the algorithm in which we will be able to determine how
good your algorithm is. Implementing it is a huge overhead, where you have to spend
considerable amount of time.
2. Experiments can be done only on a limited set of inputs.
3. You can run your experiment on a small set of instances and that might not really
indicate the time that your algorithm is taking for other inputs, which you have not
considered in your experiment.
4. It is machine dependent.
HOW DOES ONE MEASURE THE RUNNING TIME OF AN ALGORITHM?
Theoretical or apriori analysis.
• Let us assume that we express the running time of a given algorithm as a
function of the input size n (i.e., f ( n )) and compare these different functions
corresponding to running times.
• That is, this methodology aims at associating with each algorithm a function
f(n) that characterizes the running time of the algorithm in terms of the input
size n.
• This kind of comparison is independent of machine time, programming style,
etc.
WHAT IS RUNNING TIME ANALYSIS?
It is the process of determining how processing time increases as the size of the
problem (input size) increases.
Input size is the number of elements in the input, and depending on the
problem type, the input may be of different types.
WHAT IS RUNNING TIME ANALYSIS?
Typical functions that will be encountered include n and n2
.
For example, we will write statements of the type:
"Algorithm A runs in time proportional to n," meaning that if we were to
perform experiments, we would find that the actual running time of algorithm
A on any input of size n never exceeds cn, where c is a constant that depends
on the hardware and software environment used in the experiment.
WHAT IS RUNNING TIME ANALYSIS?
Given two algorithms A and B, where
A runs in time proportional to n and
B runs intime proportional to n2
,
Then, we will prefer A to B, since the function n grows at a smaller rate than the
function n2
.
WHAT IS RATE OF GROWTH?
The rate at which the running time increases as a function of input is called rate
of growth.
TYPES OF ANALYSIS
To analyze the given algorithm, we need to know with which inputs the
algorithm takes less time (performing wel1) and with which inputs the
algorithm takes a long time.
We have already seen that an algorithm can be represented in the form of an
expression.
That means we represent the algorithm with multiple expressions:
one for the case where it takes less time and another for the case where it
takes more time.
TYPES OF ANALYSIS
In general, the first case is called the best case and the second case is called the
worst case for the algorithm.
• Worst case
○ Defines the input for which the algorithm takes a long time (slowest time to
complete).
○ Input is the one for which the algorithm runs the slowest.
TYPES OF ANALYSIS
In general, the first case is called the best case and the second case is called the
worst case for the algorithm.
• Best case
o Defines the input for which the algorithm takes the least time (fastest time
to complete).
o Input is the one for which the algorithm runs the fastest.
TYPES OF ANALYSIS
• Average case
○ Provides a prediction about the running time of the algorithm.
○ Run the algorithm many times, using many different inputs that come from
some distribution that generates these inputs, compute the total running time
(by adding the individual times), and divide by the number of trials.
○ Assumes that the input is random.
Lower Bound <= Average Time <= Upper Bound
WHAT IS AN ALGORITHM?
Let us consider the problem of preparing an omelette.
To prepare an omelette, we follow the steps given below:
1) Get the frying pan.
2) Get the oil.
A. Do we have oil?
a. If yes, put it in the pan.
b. If no, do we want to buy oil?
i. If yes, then go out and buy.
ii. If no, we can terminate.
3) Turn on the stove, etc...

Algorithm description in data structures

  • 1.
    WHAT IS ANALGORITHM? 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. An algorithm is thus a sequence of computational steps that transform the input into the output.
  • 2.
    ALGORITHM An algorithm: isan outline of the steps that a program or any computational procedure has to take. A program: on the other hand is an implementation of an algorithm and it could be in any programming language. Data structure: is the way we need to organize the data, so that it can be used effectively by the program.
  • 3.
    WHAT IS ANALGORITHM? We can also view an algorithm as a tool for solving a well-specified computational problem. The statement of the problem specifies in general terms the desired input/output relationship. The algorithm describes a specific computational procedure for achieving that input/output relationship.
  • 4.
    ALGORITHM What is analgorithmic problem? specifications of an input ? specification of output as function of input Input: 67, 34, 12, 5, 7, 67, 1 Output: 1, 5, 7, 12, 34, 67, 67
  • 5.
    ALGORITHM What is analgorithmic problem? specifications of an input Algorithm specification of output as function of input An algorithm is essentially, describing the actions that one should take on the input instance to get the specified output.
  • 6.
    WHAT IS ANALGORITHM? For example, we might need to sort a sequence of numbers into non- decreasing order. Here is how we formally define the sorting problem. Input: A sequence of n numbers <a1, a2, . . ., an >. Output: A permutation (reordering) <a’1, a’2, . . ., a’n > of the input sequence such that a’1≤ a’2 ≤. . . ≤a’n .
  • 7.
    WHAT IS ANALGORITHM? For example, we might need to sort a sequence of numbers into non- decreasing order. Here is how we formally define the sorting problem. Input: A sequence of n numbers <a1, a2, . . ., an >. Output: A permutation (reordering) <a’1, a’2, . . ., a’n > of the input sequence such that a’1≤ a’2 ≤. . . ≤a’n .
  • 8.
    WHAT IS ANALGORITHM? For example, given the input sequence <31; 41; 59; 26; 41; 58>, a sorting algorithm returns as output the sequence <26; 31; 41; 41; 58; 59>. Such an input sequence is called an instance of the sorting problem. In general, an instance of a problem consists of the input (satisfying whatever constraints are imposed in the problem statement) needed to compute a solution to the problem.
  • 9.
    ALGORITHM Characteristics of anAlgorithm 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
  • 10.
    WHAT IS ANALGORITHM? An algorithm is said to be correct if, for every input instance, it halts with the correct output. We say that a correct algorithm solves the given computational problem. An incorrect algorithm might not halt at all on some input instances, or it might halt with an incorrect answer. Contrary to what you might expect, incorrect algorithms can sometimes be useful, if we can control their error rate.
  • 11.
    WHAT IS ANALGORITHM? There are two main criteria for judging the merits of algorithms: 1. correctness (does the algorithm give solution to the problem in a finite number of steps?) and 2. efficiency (how much resources (in terms of memory and time) does it take to execute the).
  • 12.
    WHY THE ANALYSISOF ALGORITHMS? In computer science, multiple algorithms are available for solving the same problem (for example, a sorting problem has many algorithms, like insertion sort, selection sort, quick sort and many more). Algorithm analysis helps us to determine which algorithm is most efficient in terms of time and space consumed.
  • 13.
    GOAL OF THEANALYSIS OF ALGORITHMS The goal of the analysis of algorithms is to compare algorithms (or solutions) mainly in terms of running time but also in terms of other factors (e.g., memory, developer effort, etc.)
  • 14.
    HOW DOES ONEMEASURE THE RUNNING TIME OF AN ALGORITHM? Let us look at the empirical (experimental) study. 1. Write a program that implements the algorithm. 2. Run the program with varying data sets in which some are smaller, some are of larger data sets, some would be of some kinds and some would be of different kinds of varying composition. 3. Use a method (like System.currentTimeMillis()) to get an accurate measure of the actual running time.
  • 15.
    HOW DOES ONEMEASURE THE RUNNING TIME OF AN ALGORITHM? This has certain limitations. 1. First you have to implement the algorithm in which we will be able to determine how good your algorithm is. Implementing it is a huge overhead, where you have to spend considerable amount of time. 2. Experiments can be done only on a limited set of inputs. 3. You can run your experiment on a small set of instances and that might not really indicate the time that your algorithm is taking for other inputs, which you have not considered in your experiment. 4. It is machine dependent.
  • 16.
    HOW DOES ONEMEASURE THE RUNNING TIME OF AN ALGORITHM? Theoretical or apriori analysis. • Let us assume that we express the running time of a given algorithm as a function of the input size n (i.e., f ( n )) and compare these different functions corresponding to running times. • That is, this methodology aims at associating with each algorithm a function f(n) that characterizes the running time of the algorithm in terms of the input size n. • This kind of comparison is independent of machine time, programming style, etc.
  • 17.
    WHAT IS RUNNINGTIME ANALYSIS? It is the process of determining how processing time increases as the size of the problem (input size) increases. Input size is the number of elements in the input, and depending on the problem type, the input may be of different types.
  • 18.
    WHAT IS RUNNINGTIME ANALYSIS? Typical functions that will be encountered include n and n2 . For example, we will write statements of the type: "Algorithm A runs in time proportional to n," meaning that if we were to perform experiments, we would find that the actual running time of algorithm A on any input of size n never exceeds cn, where c is a constant that depends on the hardware and software environment used in the experiment.
  • 19.
    WHAT IS RUNNINGTIME ANALYSIS? Given two algorithms A and B, where A runs in time proportional to n and B runs intime proportional to n2 , Then, we will prefer A to B, since the function n grows at a smaller rate than the function n2 .
  • 20.
    WHAT IS RATEOF GROWTH? The rate at which the running time increases as a function of input is called rate of growth.
  • 21.
    TYPES OF ANALYSIS Toanalyze the given algorithm, we need to know with which inputs the algorithm takes less time (performing wel1) and with which inputs the algorithm takes a long time. We have already seen that an algorithm can be represented in the form of an expression. That means we represent the algorithm with multiple expressions: one for the case where it takes less time and another for the case where it takes more time.
  • 22.
    TYPES OF ANALYSIS Ingeneral, the first case is called the best case and the second case is called the worst case for the algorithm. • Worst case ○ Defines the input for which the algorithm takes a long time (slowest time to complete). ○ Input is the one for which the algorithm runs the slowest.
  • 23.
    TYPES OF ANALYSIS Ingeneral, the first case is called the best case and the second case is called the worst case for the algorithm. • Best case o Defines the input for which the algorithm takes the least time (fastest time to complete). o Input is the one for which the algorithm runs the fastest.
  • 24.
    TYPES OF ANALYSIS •Average case ○ Provides a prediction about the running time of the algorithm. ○ Run the algorithm many times, using many different inputs that come from some distribution that generates these inputs, compute the total running time (by adding the individual times), and divide by the number of trials. ○ Assumes that the input is random. Lower Bound <= Average Time <= Upper Bound
  • 25.
    WHAT IS ANALGORITHM? Let us consider the problem of preparing an omelette. To prepare an omelette, we follow the steps given below: 1) Get the frying pan. 2) Get the oil. A. Do we have oil? a. If yes, put it in the pan. b. If no, do we want to buy oil? i. If yes, then go out and buy. ii. If no, we can terminate. 3) Turn on the stove, etc...