ADA m1
ADA m1
Design
• To plan and make decisions about (something that is being built
or created) to create the plans, drawings, etc., that show how
(something) will be made
Analysis
• A process of studying or examining something in detail in order
to understand it or explain
What are 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.
• An algorithm is thus a sequence of computational steps that transform the input
into the output.
• We can also view an algorithm as a tool for solving a well-specified
computational problem.
(A computational problem is a problem that can be solved step-by-step
with a computer. These problems usually have a well-defined input,
constraints, and conditions that the output must satisfied.)
• This course will teach you techniques of algorithm design and analysis
so that you can develop algorithms on your own, show that they give
the correct answer, and understand their efficiency.
Why study algorithms?
• Theoretical importance
• Practical importance
1-5
Data structures
• A data structure is a way to store and organize data in order to
facilitate access and modifications.
• No single data structure works well for all purposes, and so it is
important to know the strengths and limitations of several of them.
• 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.
• Of course, computers may be fast, but they are not infinitely fast.
• Memory may be inexpensive, but it is not free.
• Computing time is therefore a bounded resource, and so is space in
memory.
• You should use these resources wisely, and algorithms that are efficient
in terms of time or space will help you do so.
• Furthermore, with the ever-increasing capacities of computers, we
use them to solve larger problems than ever before.
• Analyzing an algorithm has come to mean predicting the resources that the
algorithm requires.
• Occasionally, 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.
• Generally, by analyzing several candidate algorithms for a problem, we can
identify a most efficient one.
• Algorithm analysis provides a means to distinguish between what is
practically possible and what is practically impossible.
• Efficient algorithms lead to efficient programs that make better use of
computer resources.
• Efficient programs sell better.
13
What is an algorithm?
An algorithm is a sequence of unambiguous instructions for solving a
problem, i.e., for obtaining a required output for any legitimate input in a
finite amount of time.
1. Input
2. Output
3. Definiteness
4. Finiteness
5. Effectiveness
1-14
Notion of algorithm and problem
Probleml
em
algorithm
algorithmic solution
(different from a conventional solution)
1-16
Important points
• Non-ambiguity
• Range of inputs
• The same algorithm can be represented in
different ways
• Several algorithms for solving the same
problem
17
gcd(m,n)
4. t ← t - 1
5. goto 2
18
Fundamentals of Algorithmic Problem Solving
Design an algorithm
Prove correctness
19
What does it mean to understand the problem?
20
Deciding on computational means
• The next principal decision is to choose between solving the problem exactly or
solving it approximately.
• In the former case, an algorithm is called an exact algorithm; in the latter case, an
algorithm is called an approximation algorithm.
• Why would one opt for an approximation algorithm?
• First, there are important problems that simply cannot be solved exactly for most
of their instances;
• examples include extracting square roots, solving nonlinear equations, and
evaluating definite integrals.
• Second, available algorithms for solving a problem exactly can be unacceptably slow
because of the problem’s intrinsic complexity.
Data structures
• One should pay close attention to choosing data structures appropriate
for the operations performed by the algorithm.
• Some algorithms would run longer if we used a linked list instead of an
array in its implementation .
• Also note that some of the algorithm design techniques depend
intimately on structuring or restructuring data specifying a problem’s
instance.
• Many years ago, an influential textbook proclaimed the fundamental
importance of both algorithms and data structures for computer
programming by its very title: Algorithms+ Data Structures = Programs .
• In the new world of object-oriented programming, data structures remain
crucially important for both design and analysis of algorithms.
Design an algorithm
•This is the main question this course seeks to answer by teaching you several
general design techniques.
Coding
How the objects and operations in the algorithm are
represented in the chosen Programming language?
27
Study of Algorithms
1. How to devise algorithms?
Algorithm design techniques
2. How to validate algorithms?
To show that algorithm produces correct answer for all possible legal
inputs.
3. How to analyze algorithms?
Quantitative judgements .
4. How to test a program?
Debugging – Executing programs on sample data sets to determine
whether faulty results occur, & if so correct them.
Profiling – Process of executing a correct program on datasets and
measuring the time and space it takes to compute the results
Algorithm Specification
Space Complexity
• Space complexity of an algorithm is the amount of space it
needs to run to completion
Time Complexity
• Time complexity is the amount of computer time an algorithm
needs to run to completion
Space complexity
Time complexity
2n+3 steps
Analysis framework
2 kinds of efficiency.
1. Time efficiency – indicates how fast an algorithm in question runs.
2. Space efficiency – deals with the extra space the algorithm requires.
Time is of more concern.
Measuring an Input’s size
- Algorithm’s efficiency is measured as a function of some parameter n
indicating the algorithm’s i/p size.
Eg. Sorting – no. of elements
check no. is prime or not – no. of bits b in the n’s binary representation
(b=log(n + 1))
• Units for measuring Run time
- Standard unit – sec, ms
- Count the no. of times each of the algorithms' operations is
executed.
- Identify the most important operation of the algorithm called basic
operation – the operation contributing the most to the total running
time and the no. of times the basic operation is executed.
Run time T(n) ≈ Cop C(n)
Where Cop – execution time of an algorithm’s basic operation on a
particular computer.
C(n) – No. of times this operation needs to be executed for this
algorithm
Theoretical analysis of time efficiency
Time efficiency is analyzed by determining the
number of repetitions of the basic operation
as a function of input size
• Basic operation: the operation that
contributes most towards the running time of
the algorithm
input size
T(n) ≈ copC(n)
Number of times
running time execution time basic operation is
for basic operation executed
Input size and basic operation examples
Visiting a vertex or
Typical graph problem #vertices and/or edges
traversing an edge
Best-case, average-case, worst-case
• Worst case efficiency of an algorithm is its efficiency for the worst-case input of
size n for which the algorithm runs the longest among all possible inputs of that
size.
• Bounds the run time from above ie., guarantees that for any instance of size n, the
run time will not exceed Cworst(n)
• Best case efficiency – of an algorithms is its efficiency for the best case i/p of size
n, which is an i/p of size n for which the algorithm runs the fastest among all
possible i/p of that size.
• Average case efficiency – yields the necessary information about algorithm’s
behaviour on a typical or random i/p.
Best-case, average-case, worst-case
For some algorithms efficiency depends on form of input:
• The framework’s primary interest lies in the order of growth of the algorithm’s
running time (extra memory/time units consumed) as its input size goes to
infinity.
Asymptotic Notations and Basic Efficiency Classes
• Asymptotic notations are mathematical tools used in computer
science and mathematics to describe the time and space complexity of
algorithms and functions.
• They provide a concise way to analyze the behavior of algorithms as
the input size grows towards infinity.
Big O Notation (O)
• The second notation,Ω(g(n)), stands for the set of all functions with
a higher or same order of growth as g(n) (to within a constant
multiple, as n goes to infinity).
• Represents the lower bound or best-case time complexity of an
algorithm.
• Denoted as Ω(g(n)), where g(n) is a function describing the growth
rate of the algorithm.
• It defines a lower limit on the growth rate of the algorithm, ignoring
constant factors and higher-order terms.
• Example: Ω(n) represents a linear time complexity, indicating that
the algorithm's running time grows at least as fast as the input size.
• For example, n3 ∈ (n2),
• 1/2n(n − 1) ∈ (n2), but 100n + 5 ∈ (n2).
Theta Notation (θ)
• Finally, θ(g(n)) is the set of all functions that have the same order of
growth as g(n) (to within a constant multiple, as n goes to infinity).
• Represents both the upper and lower bounds or average-case time
complexity of an algorithm.
• Denoted as θ(g(n)), where g(n) is a function describing the growth
rate of the algorithm.
• It defines a tight bound on the growth rate of the algorithm,
capturing both the best-case and worst-case scenarios.
• Example: θ(n log n) represents a linear time complexity, indicating
that the algorithm's running time grows at the same rate as the
product of the input size and its logarithm.
O-notation
• With the obvious initial condition M(1) = 1, we have the following recurrence
relation for the number of moves M(n):
1-109
Brute Force
A straightforward approach, usually based directly on the
problem’s statement and definitions of the concepts
involved
Examples:
1. Computing an (a > 0, n a nonnegative integer)
2. Computing n!
• Problem : given a string of n characters called the text and a string of m characters (m ≤ n)
called the pattern, find a substring of the text that matches the pattern.
• Brute-force Solution: align the pattern against the first m characters of the text and start
matching the corresponding pairs of characters from left to right until either all the m pairs
of the characters match (then the algorithm can stop) or a mismatching pair is encountered.
In the latter case, shift the pattern one position to the right and resume the character
comparisons, starting again with the first character of the pattern and its counterpart in the
text.
• Note that the last position in the text that can still be a beginning of a matching substring is
n − m (provided the text positions are indexed from 0 to n − 1). Beyond that position, there
are not enough characters to match the entire pattern; hence, the algorithm need not make
any comparisons there.
Brute-Force String Matching
• pattern: a string of m characters to search for
• text: a (longer) string of n characters to search in
• problem: find a substring in the text that matches the pattern
Brute-force algorithm
Step 1: Align pattern at beginning of text
Step 2:Moving from left to right, compare each character of
pattern to the corresponding character in text until
• all characters are found to match (successful search); or
• a mismatch is detected
Step 3 While pattern is not found and the text is not yet
exhausted, realign pattern one position to the right and
repeat Step 2
Examples of Brute-Force String Matching
1. Pattern: 001011
Text: 10010101101001100101111010
2. Pattern: happy
Text: It is never too late to have a happy
life.
Pseudocode and Efficiency