Chap 01
Chap 01
TO ALGORITHMS
By Dr. LOUNNAS Bilal
contents
1 Contents of this course 1
2 Basics of algorithms 2
2.1 What is algorithms? . . . . . . . . . . . . . . . . . . . . . . 2
2.2 Why should we care? . . . . . . . . . . . . . . . . . . . . . 3
2.3 History of algorithmic . . . . . . . . . . . . . . . . . . . . 4
2.4 Definition of algorithmic . . . . . . . . . . . . . . . . . . . 4
3 Moving forward with algorithmics 5
3.1 How to write an algorithm? . . . . . . . . . . . . . . . . . 5
3.2 Become a good algorithmic designer . . . . . . . . . . . . 6
3.3 Characteristics of algorithms . . . . . . . . . . . . . . . . 8
4 Classification 8
4.1 By implementation . . . . . . . . . . . . . . . . . . . . . . 8
4.2 By design paradigm . . . . . . . . . . . . . . . . . . . . . 10
4.3 Optimization problems . . . . . . . . . . . . . . . . . . . . 11
4.4 By field of study . . . . . . . . . . . . . . . . . . . . . . . . 12
4.5 By complexity . . . . . . . . . . . . . . . . . . . . . . . . . 12
list of figures
Figure 1 Rubik’s Cubes . . . . . . . . . . . . . . . . . . . . . 4
Figure 2 Flow chart of an algorithm (Euclid’s algorithm) . 5
Figure 3 Multiple solution for one problem . . . . . . . . . 7
Figure 4 A model set of the Tower of Hanoi (with 8 disks) 8
list of tables
1
basics of algorithms 2
2 basics of algorithms
2.1 What is algorithms?
When you are telling the computer what to do, you also get to
choose how it’s going to do it. That’s where computer algorithms
come in. The algorithm is the basic technique used to get the job done.
Let’s say that you have a friend arriving at the airport, and your
friend needs to get from the airport to your house. Here are four
different algorithms that you might give your friend for getting to your
home:
The taxi algorithm:
• Go to the taxi stand.
• Get in a taxi.
• Give the driver my address.
basics of algorithms 3
Imagine for a second you have two Rubik’s Cubes (Figure 1 on the fol-
lowing page) sitting out in front of you. One of them you are allowed
to use algorithms (like how many times or which direction to turn a
face), and the other you have to find your own way.
Almost certainly the the first way. There are already hundreds of
well-established algorithms to solve a Rubik’s cube, and trying to in-
vent your own method to solve it will take much longer than utilizing
the current ones. Worse than that, it is most likely that the method you
choose will be less efficient and slower than the methods that already
exist. But imagine that there are no well-established algorithms to
solve Rubik’s cube problem yet, however, your algorithm with its bad
performance will be counted as the first well-established algorithm for
solving cube problem no matter how good this algorithm is.
basics of algorithms 4
Another early use of the word is from 1240, in a manual titled Car-
men de Algorismo composed by Alexandre de Villedieu.
language.
Example
Let’s try to learn algorithm-writing by using an example. Problem -
Design an algorithm to add two numbers and display the result.
4 classification
There are various ways to classify algorithms, each with its own merits.
4.1 By implementation
4.1.1 Recursion
A recursive algorithm is one that invokes (makes reference to) itself
repeatedly until a certain condition (also known as termination con-
dition) matches, which is a method common to functional program-
ming. Iterative algorithms use repetitive constructs like loops and
sometimes additional data structures like stacks to solve the given
problems. Some problems are naturally suited for one implementa-
tion or the other. For example, towers of Hanoi (Figure ??) is well
understood using recursive implementation. Every recursive version
has an equivalent (but possibly more or less complex) iterative version,
and vice versa.
4.1.2 Logical
An algorithm may be viewed as controlled logical deduction. This
notion may be expressed as: Algorithm = logic + control. The logic
component expresses the axioms that may be used in the computation
and the control component determines the way in which deduction
is applied to the axioms. This is the basis for the logic programming
paradigm. In pure logic programming languages the control compo-
nent is fixed and algorithms are specified by supplying only the logic
component. The appeal of this approach is the elegant semantics: a
change in the axioms has a well-defined change in the algorithm.
Every field of science has its own problems and needs efficient algo-
rithms. Related problems in one field are often studied together. Some
example classes are search algorithms, sorting algorithms, merge al-
gorithms, numerical algorithms, graph algorithms, string algorithms,
computational geometric algorithms, combinatorial algorithms, medi-
cal algorithms, machine learning, cryptography, data compression al-
gorithms and parsing techniques.
Fields tend to overlap with each other, and algorithm advances in
one field may improve those of other, sometimes completely unrelated,
fields. For example, dynamic programming was invented for optimiza-
tion of resource consumption in industry, but is now used in solving a
broad range of problems in many fields.
4.5 By complexity