Algorithm Teach Chapter 1 73448
Algorithm Teach Chapter 1 73448
Algorithm Teach Chapter 1 73448
• Once we have give a problem, the first step to solve the problem is
obtaining ones own abstract view, or model , of the problem. This process
of modeling is called abstraction.
• The model defines an abstract view to the problem. This implies that the
model focuses only on problem related things and a programmer tries to
define the properties of the problem.
– These properties include
– The data which are affected and
– The operations that are involved in the problem.
• With abstraction you create a well-defined entity that can be properly
handled. These entities define the data structure of the program.
• An entity with the properties just described is called an abstract data type
(ADT).
Abstract Data Types
• An ADT consists of an abstract data structure and operations. Put in other
terms, an ADT is an abstraction of a data structure.
– The ADT specifies:
1. What can be stored in the Abstract Data Type
2. What operations can be done on/by the Abstract Data Type.
• For example, if we are going to model employees of an organization:
– This ADT stores employees with their relevant attributes and
discarding irrelevant attributes.
– This ADT supports hiring, firing, retiring, … operations.
• A data structure is a language construct that the programmer has defined
in order to implement an abstract data type.
• There are lots of formalized and standard Abstract data types such as
Stacks, Queues, Trees, lists, graph etc.
Abstraction
• Abstraction is a process of classifying characteristics as relevant and
irrelevant for the particular purpose at hand and ignoring the irrelevant
ones.
• Applying abstraction correctly is the essence of successful programming
• How do data structures model the world or some part of the world?
– The value held by a data structure represents some specific
characteristic of the world
– The characteristic being modeled restricts the possible values held by a
data structure
– The characteristic being modeled restricts the possible operations to be
performed on the data structure.
Algorithms
• An algorithm is a well-defined computational procedure that takes some
value or a set of values as input and produces some value or a set of values
as output.
• Data structures model the static part of the world. They are unchanging
while the world is changing. In order to model the dynamic part of the
world we need to work with algorithms.
• Algorithms are the dynamic part of a program’s world model. An algorithm
transforms data structures from one state to another state in two ways:
– An algorithm may change the value held by a data structure
– An algorithm may change the data structure itself
• The quality of a data structure is related to its ability to successfully model
the characteristics of the world. Similarly, the quality of an algorithm is
related to its ability to successfully simulate the changes in the world.
Cont..
• However, the quality of data structure and algorithms is determined by their
ability to work together well.
• Because correct data structures lead to simple and efficient algorithms and
correct algorithms lead to accurate and efficient data structures.
• Algorithm can be good or bad depending on different properties. So let
discus some of them:
Properties of an algorithm
– Finiteness: Algorithm must complete after a finite number of steps.
– Definiteness: Each step must be clearly defined, having one and only
one interpretation. At each point in computation, one should be able to
tell exactly what happens next.
– Sequence: Each step must have a unique defined preceding and
succeeding step. The first step (start step) and last step (halt step) must
be clearly noted.
Cont..
– Correctness: It must compute correct answer for all possible legal
inputs.
– Language Independence: It must not depend on any programming
language.
– Completeness: It must solve the problem completely.
– Effectiveness: It must be possible to perform each step exactly and in
a finite amount of time.
– Efficiency: It must solve with the least amount of computational
resources such as time and space.
– Generality: Algorithm should be valid on all possible inputs.
– Input/Output: There must be a specified number of input values, and
one or more result values.
– Feasibility: It must be possible to perform each instruction.
Algorithm Analysis Concepts
• Algorithm analysis refers to the process of determining the amount of
computing time and storage space required by different algorithms. In other
words, it’s a process of predicting the resource requirement of algorithms in
a given environment.
• In order to solve a problem, there are many possible algorithms. So we
have to be able to choose the best algorithm for the problem at hand using
some scientific method.
• To classify some data structures and algorithms as good, we need precise
ways of analyzing them in terms of resource requirement. The main
resources are:
– Running Time: Running time is the dominant standard.
– Memory Usage Example
Cont..
• Running time is usually treated as the most important since computational time is the
most precious resource in most problem domains.
• There are two approaches to measure the efficiency of algorithms:
– Empirical/expermental: Programming competing algorithms and trying them on
different instances.
– Theoretical: Determining the quantity of resources required mathematically
(Execution time, memory space, etc.) needed by each algorithm
• Algorithm efficiency is used to describe properties of an algorithm relating
to how much of various types of resources (eg. Run/cpu time, memory) it
consumes.
• However, it is difficult to use actual clock-time as a consistent measure of an algorithm’s
efficiency, because clock-time can vary based on many things.
For example,
– Specific processor speed
– Current processor load
– Specific data for a particular program
• Input Size
• Input Properties
– Operating Environment
Complexity Analysis
• Complexity Analysis is the systematic study of the cost of computation,
measured either in time units or in operations performed, or in the amount of
storage space required.
• The goal is to have a meaningful measure that permits comparison of
algorithms independent of operating platform.
• There are two things to consider:
– Time Complexity: Determine the approximate number of operations
required to solve a problem of size n.
– Space Complexity: Determine the approximate memory required to solve
a problem of size n.
• Complexity analysis involves two distinct phases:
– Algorithm Analysis: Analysis of the algorithm or data structure to produce
a function T (n), that describes the algorithm in terms of the operations
performed in order to measure the complexity of the algorithm.
– Order of Magnitude Analysis: Analysis of the function T (n) to determine
the general complexity category to which it belongs.
– It provides a useful approximation to the actual number of steps in the
computation.
Cont..
• There is no generally accepted set of rules for algorithm analysis. However,
an exact count of operations is commonly used.
Analysis Rules:
1. We assume an arbitrary time unit.
2. Execution of one of the following operations takes time 1:
– Assignment Operation
– Single Input/Output Operation
– Single Boolean Operations
– Single Arithmetic Operations
– Function Return
3. Running time of a selection statement (if, switch) is the time for the
condition evaluation + the maximum of the running times for the individual
clauses in the selection.
Cont..
4. Loops: Running time for a loop is equal to the running time for the
statements inside the loop * number of iterations.
The total running time of a statement inside a group of nested loops is the
running time of the statements multiplied by the product of the sizes of all
the loops.
5. Running time of a function call is 1 for setup + the time for any parameter
calculations + the time required for the execution of the function body.
Note: Running time=summation of running time of all fragment
Examples: Count of Basic Operations (Time Units) to Compute
• Worst Case (Tworst): The amount of time the algorithm takes on the worst
possible set of inputs.
• Best Case (Tbest): The amount of time the algorithm takes on the smallest
possible set of inputs.
Asymptotic Analysis
• There are five notations used to describe a running time function. These
are:
– Big-Oh Notation (O)
– Big-Omega Notation (Ω)
– Theta Notation (Θ)
– Little-o Notation (o)
– Little-Omega Notation (ω)
The Big-Oh Notation
• Big-Oh notation is a way of comparing algorithms and is used for
computing the complexity of algorithms; i.e., the amount of time that it
takes for computer program to run .
• It’s only concerned with what happens for very a large value of n.
Therefore only the largest term in the expression (function) is needed.
• For example, if the number of operations in an algorithm is n2 – n, n is
insignificant compared to n2 for large values of n. Hence the n term is
ignored. Of course, for small values of n, it may be important.
• However, Big-Oh is mainly concerned with large values of n.
• Formal Definition: f (n)= O (g (n)) if there exist c, k ε ℛ+ such that for all
n≥ k, f (n) ≤ c.g (n).
• Demonstrating that a function f(n) is big-O of a function g(n) requires that
we find specific constants c and k for which the inequality holds.
Cont..
• Examples: The following points are facts that you can use for Big-Oh problems:
– 1<=n for all n>=1
– n<=n2 for all n>=1
– 2n <=n! for all n>=4
– log2n<=n for all n>=2
– n<=nlog2n for all n>=2
1. f(n)=10n+5 and g(n)=n. Show that f(n) is O(g(n)).
To show that f(n) is O(g(n)) we must show that constants c and k such
that
• f(n) <=c.g(n) for all n>=k
• Or 10n+5<=c.n for all n>=k
• Try c=15. Then we need to show that 10n+5<=15n
• Solving for n we get: 5<5n or 1<=n.
• So f(n) =10n+5 <=15.g(n) for all n>=1.
• (c=15,k=1).
Example 2: f(n)=5n2+2n+1 g(n)=n2. Show that f(n) is O(g(n)).
Big-Omega Notation(Ω)
• Just as O-notation provides an asymptotic upper bound on a function, Ω
notation provides an asymptotic lower bound.
• Formal Definition: A function f(n) is Ω( g (n)) if there exist constants c
and k ε ℛ+ such that f(n) >=c. g(n) for all n>=k.
• f(n)= Ω( g (n)) means that f(n) is greater than or equal to some constant
multiple of g(n) for all values of n greater than or equal to some k.
• Example 1: If f(n) =n2, then f(n)= Ω ( n)
In simple terms, f(n)= Ω( g (n)) means that the growth rate of f(n) is
greater that or equal to g(n).
Example 2: f(n)=5n2+2n+1 g(n)=n2. Show that f(n) is Ω(g(n)).
Theta Notation(Θ)
• A function f (n) belongs to the set of Θ(g(n)) if there exist positive
constants c1 and c2 such that it can be sandwiched between c1.g(n) and
c2.g(n), for sufficiently large values of n.
Formal Definition: A function f (n) is Θ (g(n)) if it is both O( g(n) )
and Ω ( g(n) ).
• In other words, there exist constants c1, c2, and k >0 such that c1.g
(n)<=f(n)<=c2. g(n) for all n >= k
• If f(n)= Θ(g(n)), then g(n) is an asymptotically tight bound for f(n).
• In simple terms, f(n)= Θ (g(n)) means that f(n) and g(n) have the same rate
of growth.
Eg: f(n)=5n2+2n+1 g(n)=n2. Show that f(n) is Ω(g(n)).
Little-o Notation(o)