2. Discussion Outline
1.Overview of Algorithm Problem Solving
2.Fundamentals of Algorithm Problem
Solving Understanding the Problem
Decide on: computational means, exact vs.
approximate solving, algorithm design
technique
Designing an Algorithm
Prove Correctness
Analyze the Algorithm
Code the Algorithm
TODAY'S
DISCUSSIO
N
4. Any Air travel connects several cities
Some Cities are connected by direct flights
Want to compute all pairs of cities connected by
sequence of flights
Example 1
: Air Travel
5.
6. Observations
Paths
Representation of graph for manipulating it as a program with
suitable data structure
Design efficient algorithm
Efficiency
Number of cities (N) ; Number of direct flights (F)
Computing paths between N and F
Dependency
How much N and F values can be handled - Online booking
requries responses in seconds
7. EXAMPLE2 XERO
XSHO
P
1.When xerox shop to give some
job on schedule time (Project
report)
2.Any discounts
3.Brute force approach
4.Decomposition - Large problem
first and then smaller one next
(Includes recursion)
5.Based on some criteria - One with
number of pages, most related with
the deadline - Optimal solution
9. EXAMPLE3 DO
CUMENTSIMILARITY
1.Plagiarism detection
2.Changes in version of code
3.Web search queries more effectively
Measure of Similarity
Repeat of distance
Edit distance - How many changes we can make in
the document?
Add/ remove a letter - One change
Compute the minimum distance
11. EXAMPLE4
Write a program that meets the following assumption:
There is a point in the center of the screen.
In the right upper part of the screen, the program randomly draws
a geometrical figure (triangle, quadrilateral).
The player's task is to draw a figure in the lower left part of the
screen so that after turning 180 degrees it will cover as accurately
as possible with the figure drawn by the computer.
After decomposing the problem, we
get a set of sub-problems related to
the graphic part. These sub-problems
will not be more difficult
13. 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.
The notion of the algorithm
14. for w hich an
to be specified
1. The nonambiguity requirement for
each step of an algorithm cannot be
compromised.
2.The range of inputs
algorithm works has
carefully.
3.The same algorithm can be
represented in several different ways.
4.There may exist several algorithms for
solving the same problem.
5.Algorithms for the same problem
can be based on very different ideas
and can solve the problem with
dramatically different speeds.
Important Points
15. Clear and Unambiguous
clear and
clear in all
Well-Defined Inputs
If an algorithm says to take inputs, it
should be well-defined inputs.
Finiteness
The algorithm must be finite, i.e. it
should not end up in an infinite loops
or similar.
Language Independent
The Algorithm designed must be
language-independent hence it can
be implemented in any language, and
yet the output will be same, as
expected.
Algorithm should be
unambiguous.
Each steps should be
aspects and m ust give only one
meaning.
Well-Defined Outputs
The algorithm must clearly define what
output will be yielded and it should be
well-defined as w ell.
Feasible
The algorithm must be simple,
generic and practical, such that it can
be executed
It must not contain some future
technology
16. gcd(m, n) = gcd(n, m mod n)
where m mod n is the remainder of the division of m by n, until m mod n
is equal to 0.
For example,
gcd(60, 24) can be computed as follows:
gcd(60, 24)
= gcd(24, 12)
= gcd(12, 0)
= 12.
Euclid’s algorithm - Concept
17. Step 1
If n = 0, return the value of m as the answer and stop; otherwise,
proceed to Step 2.
Step 2
Divide m by n and assign the value of the remainder to r.
Step 3
Assign the value of n to m and the value of r to n. Go to Step 1.
Euclid’s algorithm for computing gcd(m, n)
18. ALGORITHM Euclid(m, n)
//Computes gcd(m, n) by Euclid’s algorithm
//Input: Two nonnegative, not-both-zero integers m and n
//Output: Greatest common divisor of m and n
while n ≠ 0 do
r ← m mod n
m← n
n← r
return m
Euclid’s algorithm - Pseudocode
19. In first method is based on the definition of GCD of m and n as the largest
integer that divides both numbers evenly.
Now Consider,
t = min{m, n}
1.If t divides both m and n then t is the answer
2.Else simply decrease the t by 1and try again
For example,
For numbers 60 and 24, the algorithm will try first 24, then 23, and so on,
until it reaches 1
2, where it stops.
Euclid’s algorithm - Method 2
20. Consecutive integer checking algorithm for computing gcd(m, n)
Step 1
Assign the value of min{m, n} to t.
Step 2
Divide m by t. If the remainder of this division is 0, go to Step 3;
otherwise, go to Step 4.
Step 3
Divide n by t. If the remainder of this division is 0, return the value of t as
the answer and stop; otherwise, proceed to Step 4.
Step 4
Decrease the value of t by 1
. Go to Step 2.
Euclid’s algorithm - Method 2
21. Middle-school procedure for computing gcd(m, n)
Step 1
Find the prime factors of m.
Step 2
Find the prime factors of n.
Step 3
Identify all the common factors in the two prime expansions found in Step
1and Step 2. (If p is a common factor occurring pm and pn times in m and
n, respectively, it should be repeated min{pm, pn } times.)
Step 4
Compute the product of all the common factors and return it as the
greatest common divisor of the numbers given.
Euclid’s algorithm - Method 3
22. Thus, for the numbers 60 and 24, we get
60 = 2 . 2 . 3 . 5
24 = 2 . 2 . 2 . 3
gcd(60, 24) = 2 . 2 . 3 = 1
2.
Euclid’s algorithm - Method 3
23. ADVANTAGES
O
F
AN
ALGO
RITHM
Algorithm is a step-wise
representation to a given problem -
easy to understand
An algorithm uses a definite
procedure
In Algorithm the problem is broken
down into smaller steps - easy to
convert it into an actual program.
It is not dependent on any
programming language - anyone
can understand
a logical sequence -
Every step is
easy to debug
25. E X A M P LE 1:
Going to market to purchase a Pen
26. E X A M P LE 1:
Going to market to purchase a Pen
Step 1
: Prepare yourself to go to market
Step 2: Ckeck your wallet for money
Step 3: If there is no money in the
wallet, replenish it
Step 4: Go to the shop
Step 5: Ask for your favorite brand of
pen
Step 6: If pen is not available, go to
Step 7 else go to Step 1
0
Step 7: Give money to the shop keeper
Step 8: Keep the purchased pen safely
Step 9: Go back home
Step 10: Ask for any other brand of pen
Step 1
1
: Go to Step 7
28. Before designing an algorithm is to understand completely the
problem given.
Read the problem’s description, do a few small examples by hand,
think about special cases, and ask questions again if needed.
For Computing applications we can use a known algorithm for solving
it.
To know its strengths and weaknesses, especially if you have to choose
among several available algorithms.
Will not find a readily available algorithm and will have to design your
own.
29. An input to an algorithm specifies an instance of the problem the
algorithm solves.
Very important to specify exactly the set of instances the algorithm
needs to handle.
If fails the algorithm may work correctly for a majority of inputs but
crash on some “boundary” value.
Correct algorithm is not one that works most of the time, but one that
works correctly for all legitimate inputs.
30. Algorithms in use today are still destined to be programmed for a
computer closely resembling the von Neumann machine
The essence of this architecture is captured by the so-called random-
access machine (RAM).
Its central assumption is that instructions are executed one after
another, one operation at a time.
Accordingly, algorithms designed to be executed on such machines are
called sequential algorithms.
31. Exact algorithm
Always find the optimal solution to a given optimization problem.
Approximation algorithm
We can prove a bound on the ratio between the optimal solution and the
solution produced by the algorithm.
32. Important Points
1)There are important problems that cannot be solved exactly
Example:
Extracting square roots
Solving non linear equations
Evaluating definite integrals
2) Solving a problem exactly can be unacceptably slow because of problem's
a part of a more sophisticated
intrinsic complexity
3) An approximation algorithm can be
algorithm that solves a problem exactly
33. Some algorithms demand predicated on ingenious data structures
Data structures remain crucially important for both design and
analysis of algorithms
34. An design technique is a general approach to solving problems
algorithmically that is applicable to a variety of problems from different
areas of computing.
1.They provide guidance for designing algorithms for new problems,
i.e., problems for which there is no known satisfactory algorithm.
2.Algorithms are the cornerstone of computer science.
3.Make it possible to classify algorithms according to an underlying
design idea; therefore, they can serve as a natural way to both
categorize and study algorithms.
35. Natural Language:
Using a natural language has an obvious appeal;
However, the inherent ambiguity of any natural language makes a
succinct and clear description of algorithms surprisingly difficult.
Pseudocode:
Is a mixture of a natural language and programming language-like
constructs.
Pseudocode is usually more precise than natural language, and its
usage often yields more succinct algorithm descriptions.
36. Flowchart:
A method of expressing an algorithm by a collection of connected
geometric shapes containing descriptions of the algorithm’s steps.
Has proved to be inconvenient for all but very simple algorithms
Note:
Can be fed into an electronic computer directly
37. Have to prove that the algorithm yields a required result for every
legitimate input in a finite amount of time.
For Some algorithms, a proof of correctness is quite easy;
For others, it can be quite complex.
A common technique for proving correctness is to use mathematical
induction because an algorithm’s iterations provide a natural
sequence of steps needed for such proofs.
Even if algorithm fails for one input the algorithm has to be redesign
38. Approximation algorithm is less straight forward than exact
algorithms
For approximation algorithm the correctness can be proved if the
error occurred does not exceed the predefined limit
Note:
39. Two important types of efficiency:
1.Time efficiency - How fast the algorithm runs
2.Space efficiency - How much extra memory the algorithm used
3.Generality - Desirable characteristics of an algorithm
Example:
The problem of determining whether two integers are relatively
prime, i.e., whether their only common divisor is equal to 1.
40. Algorithms are destined to be ultimately implemented as computer
programs.
The important aspect is the possibility of making the transition from
an algorithm to a program either incorrectly or very inefficiently.