Vous êtes sur la page 1sur 7

Algorithms

An algorithm is a sequence of unambiguous instructions for


solving a computational problem,
Problem i.e., for obtaining a required
output for any legitimatemmeem
input in a finite amount of time.
mnmn
mmmm
m
algorithm
m

input “computer” output

As an example of illustrating the notion of algorithm, we consider three


methods
for solving the same problem: Computing th greatest common
divisor.

Greatest common divisor of two non negative, not- both-zero integers


m and n, denoted by gcd(m,n), is defined as the largest integer that
divides both m and n evenly.
First try -- Euclid’s algorithm: gcd(m, n) = gcd(n, m mod n)

Euclid’s algorithm is a famous method which applies repeated


equality.
gcd(m,n) = gcd(n,m mod n)

For example: gcd(60,24) = gdc(24,12)=gcd(12,0)=12.


 gcd(m, n): the largest integer that divides both m and n.
 First try -- Euclid’s algorithm: gcd(m, n) = gcd(n, m mod n)

Step1: If n = 0, return the value of m as the answer and stop;


otherwise, proceed to Step 2.
Step2: 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.

Pseudocode of Euclid’s Algorithm:

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

Second Try for gcd(m, n)

Consecutive Integer Algorithm

 Step1: Assign the value of min{m, n} to t.


 Step2: Divide m by t. If the remainder of this division is 0,
go to Step3;otherwise, go to Step 4.
 Step3: 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 Step4.
 Step4: Decrease the value of t by 1. Go to Step2.

Third try for gcd(m, n)

Middle-school procedure
 Step1: Find the prime factors of m.
 Step2: Find the prime factors of n.
 Step3: Identify all the common factors in the two prime
expansions found in Step1 and Step2. (If p is a common
factor occurring Pm and Pn times in m and n, respectively,
it should be repeated in min{Pm, Pn} times.)
 Step4: Compute the product of all the common factors and
return it as the gcd of the numbers given.

Thus for the numbers 60,24 we get:

60 = 2 . 2 . 3 . 5
24 = 2 . 2 . 2 . 3
gcd(60,24) = 2 . 2 . 3 = 12

What can we learn from the previous 3 examples?

 Each step of an algorithm must be unambiguous.


 The same algorithm can be represented in several different
ways. (different pseudocodes)
 There might exists more than one algorithm for a certain
problem.
 Algorithms for the same problem can be based on very different
ideas and can solve the problem with dramatically different
speeds.

Fundamentals of Algorithmic Problem Solving

 Understanding the problem


 Reading the problems description carefully, Asking
questions, do a few examples by hand, think about special
cases, etc.
 Deciding on
 Exact vs. approximate problem solving
 Appropriate data structure
 Design an algorithm
 Proving correctness
 Analyzing an algorithm
 Time efficiency : how fast the algorithm runs
 Space efficiency: how much extra memory the algorithm
needs.
 Simplicity and generality
 Coding an algorithm

Understanding the problem:


An input to an algorithm specifies an instance of the problem the
algorithms solves. It is very important to specify exactly te range of
instances the algorithms needs to handle. Failure to do this, the
algorithm may work correctly for a majority of inout but crash on some
“boundary” value. A correct algorithm is not one that works most of
the time but one that works correctly for all legitimate inputs.

Ascertaining the Capabilities of a Computational Device:


Once having understood a problem, one must ascertain the capabilities
of the computational device the algorithm is intended for. In the case
of random-access memory (RAM), its central assumption is that
instructions are executed one after another, one operation at a time.
Accordingly algorithms are designed to be executed on such machines
are called sequential algorithms.
Some new computers can execute operations concurrently; i.e. in
parallel. Algorithms that take advantage of this capability are called
parallel algorithms

Choosing between Exact and Appropriate Problem Solving:


The next principal decision is to choose between solving the problem
exactly or solving it appropriately. The former case algorithm is called
exact algorithm latter case, an algorithm is called appropriate
algorithm.

Deciding on Appropriate Data Structure:


In the new world of object oriented programming, data structures
remain crucially important for both design and analysis of algorithms.
Algorithms + Data structures = Programs

Algorithm Design Techniques:


What is an algorithm design technique?
An algorithm design technique is a general approach to solving
problems algorithmically that is applicable to a variety of problems for
different areas of computing.

Methods of Specifying an Algorithm:


Once having designed an algorithm, you need to specify it in some
fashion. Using natural language is obviously difficult.
A pseudocode is a mixture of a natural language and programming
language like constructs. A pseudocode is usually more precise than an
natural language.
Computer scientists never have agreed on a single form pf
pseudocode.

Proving an Algorithm’s Correctness:

Once an algorithm has been specified, you have to prove its


correctness. That is you have to prove that the algorithm yields a
required result for every legitimate input in a finite amount of time. For
algorithms, a proof of correctness is to use mathematical induction
because an algorithm’s iteration provide a natural sequence of steps
needed for such proofs.
The notion of correctness for approximation algorithms is less
straightforward than it is for exact algorithms. Errors for approximation
algorithms should not exceed a predefined limit.

Analyzing an Algorithm:

After correctness, the most important is efficiency. There are two


kinds of algorithm efficiency:
• Time efficiency: indicates how fast the algorithm runs.
• Space efficiency: indicates how much extra memory the
algorithm needs.

Another characteristic of an algorithm is simplicity. Simplicity is an


important algorithm characteristic. Because simple algorithms are
easier to understand and easier to program. Resulting programs
usually contain fewer bugs. Simper algorithms are also more efficient
than more complicated alternatives.

Another characteristic of an algorithm is generality. There are two


issues here:
• Generality of the problem the algorithm solves – it is easier to
design an algorithm for a more general problem. But there are
situations where designing a more general algorithm is
unnecessary or difficult or even impossible.
• The range of inputs it accepts – your main concern should be
designing an algorithm that can handle a range of inputs that is
natural for the problem at hand.

Coding an Algorithm:
Most algorithms are ultimately implemented as computer programs.
Transition of algorithm to a program must be done carefully. Validity of
the computer program is established by testing. Testing of computer
programs is an art rather than science.

Important Problem Types

 Sorting
 Searching
 String processing
 Graph problems
 Combination problems
 Geometric problems
 Numerical problems

Sorting:

 Rearrange the items of a given list in ascending order.


Input: A sequence of n numbers <a1, a2, …, an>
Output: A reordering <a´1, a´2, …, a´n> of the input
sequence such that a´1≤ a´2 ≤ … ≤ a´n.
Sorting can be done to sort list of numbers, characters from
an alphabet, character strings and records maintained by
schools about students and libraries about books and
companies about their employees.
 Why sorting?
Help searching
Algorithms often use sorting as a key subroutine.
 Sorting key
A specially chosen piece of information used to guide sorting.
I.e., sort student records by names.

 Examples of sorting algorithms


 Selection sort
 Bubble sort
 Insertion sort
 Merge sort
 Heap sort

Evaluate sorting algorithm complexity: the number of key


comparisons.
Two properties
 Stability: A sorting algorithm is called stable if it preserves
the relative order of any two equal elements in its input.
 In place : A sorting algorithm is in place if it does not
require extra memory, except, possibly for a few memory
units.

Searching:

The searching problem deals with finding a given value, called a


search key, in a given set.
 Examples of searching algorithms
 Sequential searching
 Binary searching

String

A string is a sequence of characters from an alphabet. Strings


comprises of Text strings: letters, numbers, and special characters. Bit
strings comprises of zeros and ones.
String matching: searching for a given word/pattern in a text.

Graph Problems
A graph is a collection of points called vertices, some of which are
connected by line segments called edges. Graphs can be used for:

• Modeling real-life problems


• Modeling WWW
• transportation
• communication networks
• Project scheduling …

Examples of widely used graph algorithms:

Graph traversal algorithms


Shortest-path algorithms
Topological sorting

The widely known graph problems are:

Traveling salesman problem – Which finds the shortest tour through n


cities that visits every city exactly once.
The graph coloring problem – assigns the smallest number of colors to
vertices of a graph so that no two adjacent vertices are the same color.

Combinational Problems:

The traveling saleman problems and the graph coloring problems are
examples of combination problems. These problems ask to find a
combinatorial object – such as permutation, a combination, or a subset
– that satisfies certain constraints and has some desired property.

Geometic Problems:

Geometric algorithms deal with geometric objects such as points,


lines and polygons. Ancient greeks have invented procedures for
solving problems of constructing simple geometric shapes – triangles,
circles etc.

Numerical Problems:

Numerical problems another large area of application, are problems


that involve mathematical objects of continuous nature: solving
equations and systems of equations, computing definite integrals,
evaluating functions.

Vous aimerez peut-être aussi