Conception D - Algorithmes English
Conception D - Algorithmes English
1
Intitulé du Master : Science de Données et Intelligence Artificielle Semestre : S1
Intitulé de la matière: Advanced Operation Research Prof. LAYEB A.
These algorithm design techniques are powerful tools for solving different
types of problems, but it is important to choose the most suitable technique
based on the specific characteristics and constraints of the problem you are
trying to solve.
II. How to choose the best algorithm design technique for a given
problem?
Sometimes, it is very difficult to choose the right solving algorithm for various
reasons. Here are some guidelines for choosing the best algorithm design
technique for a given problem:
It is about identifying the structure of the problem and choosing the most
suitable algorithm design technique. There can be several possible good
answers.
1. Greedy algorithms
2
Intitulé du Master : Science de Données et Intelligence Artificielle Semestre : S1
Intitulé de la matière: Advanced Operation Research Prof. LAYEB A.
solution, they are often quick and simple to implement, making them useful for many
problems. The generic algorithm for constructing a greedy algorithm is as follows:
S←∅
function Greedy( C : Set) : set
while ¬Solution(S) ∧ C ≠ ∅ do
x ← item in C which max( Select(X))
S ← S ∪ {x}
if Solution(S) return S
else Ruturn «No solution !»
Local Choice Principle: Greedy algorithms make decisions based only on the
information available locally at each step. They do not consider the long-term
consequences of their choices.
Locally Optimal Solutions: At each step, a greedy algorithm selects the best
local option, meaning the one that appears to be the best among the
immediately available choices. This leads to locally optimal solutions.
Incremental Construction Strategy: Solutions are built progressively, step
by step, by adding or selecting elements incrementally. Each step is guided by
the pursuit of the best local choice.
No Backtracking: Greedy algorithms generally do not backtrack to reevaluate
or change a previously made decision. Once a choice is made, it is final.
3
Intitulé du Master : Science de Données et Intelligence Artificielle Semestre : S1
Intitulé de la matière: Advanced Operation Research Prof. LAYEB A.
Local Decisions Are Independent: The local decisions do not affect each
other.
In summary, the main disadvantages are the lack of guaranteed optimality and the
inability to reconsider local decisions. However, for many real-world problems, the
simplicity-to-efficiency ratio of greedy algorithms is very attractive compared to more
complex techniques.
4
Intitulé du Master : Science de Données et Intelligence Artificielle Semestre : S1
Intitulé de la matière: Advanced Operation Research Prof. LAYEB A.
tries to anticipate future outcomes of current choices, which can lead to better-
quality solutions.
Randomized Greedy Algorithm: This variant introduces an element of
randomness into the greedy algorithm. At each step, multiple choices are
generated randomly, and the best among them is selected. This can help avoid
being trapped in a local optimum.
Two-Phase Greedy Algorithms: These algorithms are a method for solving
combinatorial optimization problems, combining two distinct steps: a
construction phase and an improvement phase. This approach aims to obtain
an initial solution through a greedy strategy, then further improve it using a
local optimization method or a metaheuristic.
Greedy Algorithms with Machine Learning: Use machine learning
techniques. This can learn from past problem instances to make better
decisions.
Hybrid Greedy Algorithms: Combine greedy approaches with other
algorithmic paradigms. The advantage is to leverage strengths of multiple
approaches. Example: Greedy randomized adaptive search procedure
(GRASP)
We assume that customers only give you amounts in whole euros (no cents for
simplicity);
The available coin and bill values are: 1, 2, 5, 10, 20, 50, 100, 200, and 500. We
assume you have as many of each coin or bill as needed. For simplicity, we will refer
to both coins and bills as "coins."
A greedy algorithm consists of always choosing the highest value coin or bill that
does not exceed the remaining amount.
Example:
A customer buys an item that costs 53 euros and pays with a 200-euro bill. You need
to give them 147 euros in change. One way to do this is by giving them a 100-euro
bill, two 20-euro bills, one 5-euro bill, and a 2-euro coin.
5
Intitulé du Master : Science de Données et Intelligence Artificielle Semestre : S1
Intitulé de la matière: Advanced Operation Research Prof. LAYEB A.
2. knapsack problem :
In this problem, you have a knapsack with capacity C and a list of objects with
weights and values. The goal is to fill the knapsack to maximize the total value of the
objects while respecting the knapsack's capacity. This problem is very important and
has many variations and applications in different fields.
0/1 Knapsack Problem: Each item can either be included (1) or not included
(0). This version is NP-hard and cannot be solved optimally with a simple
greedy approach.
Fractional Knapsack Problem: Items can be broken into smaller pieces, so
the thief can take fractions of items. This version can be solved optimally
using a greedy algorithm.
Here are the main greedy algorithms used to solve the knapsack problem:
This algorithm provides a near-optimal solution for the knapsack in terms of value.
This algorithm seeks to maximize total value, even if the number of items is not
maximal.
The greedy solution is {Item 4, Item 1, Item 2} for a total value of 4 + 1 + 3 = 8. This
is the optimal solution for this knapsack problem.
7
Intitulé du Master : Science de Données et Intelligence Artificielle Semestre : S1
Intitulé de la matière: Advanced Operation Research Prof. LAYEB A.
Prim's algorithm solves the problem of finding a minimum spanning tree (MST) in an
undirected, weighted graph. It starts from an arbitrary node and iteratively adds the
closest node (or the one with the smallest weight) to the partially constructed subtree.
Initialization:
Loop Step: Repeat the following steps until all vertices are included in the
spanning tree:
a) Add the vertex connected by the selected edge to the set of vertices
included in the spanning tree.
b) Add the selected edge to the spanning tree as well.
Correctness:
Prim's algorithm always produces the correct MST because of the cut property:
For any cut in a graph, the minimum weight edge crossing the cut is in the
MST.
8
Intitulé du Master : Science de Données et Intelligence Artificielle Semestre : S1
Intitulé de la matière: Advanced Operation Research Prof. LAYEB A.
The Travelling Salesman Problem (TSP) involves finding the shortest path that
visits each city exactly once and returns to the starting city. This problem is important
due to its numerous applications in transportation, networks, and other fields.
The Nearest Neighbor Algorithm is a greedy technique that solves the TSP by
choosing the nearest unvisited city at each step. Its principle is simple: at each step,
the algorithm selects the nearest unvisited city from the current city and adds it to the
ongoing tour. This process continues until all cities have been visited, with the final
step being to return to the starting city to complete the cycle. Here’s a summary of the
Nearest Neighbor Algorithm in key steps:
9
Intitulé du Master : Science de Données et Intelligence Artificielle Semestre : S1
Intitulé de la matière: Advanced Operation Research Prof. LAYEB A.
Example: Consider a set of cities with their distances between two cities:
ABCDE
A 0 8 6 5 9
B 8 0 4 7 3
C 6 4 0 5 2
D 5 7 5 0 6
E 9 3 2 6 0
Suppose we start at city A (0). Here’s how the Nearest Neighbor Algorithm would
be applied:
1. Start at city A.
2. The nearest city to A is city D (distance = 5).
3. From city D, the nearest city is city C (distance = 5).
4. From city C, the nearest city is city E (distance =2).
5. From city E, the nearest city is city B (distance = 3).
6. Finally, return to city A from city E (distance = 8).
Start with a cycle containing only one city. (we can pick two cities and create
a tour between them)
At each step, choose the unvisited city that minimizes the total distance if
inserted into the existing cycle.
Insert the selected city at the appropriate position to form a longer cycle.
Repeat these steps until all cities are visited.
This algorithm is slightly more complex than the Nearest Neighbor Algorithm, but it
can produce better solutions in some cases.
10
Intitulé du Master : Science de Données et Intelligence Artificielle Semestre : S1
Intitulé de la matière: Advanced Operation Research Prof. LAYEB A.
Go through the items one by one in the order they are given.
For each item, find the first available container that can accommodate it
without exceeding its capacity.
Place the item in that container and mark it as used. If no container can fit the
item, create a new container.
Go through the items one by one in the order they are given.
For each item, find the available container with the smallest available space
that can accommodate the item.
Place the item in that container. If no container fits, create a new one.
Go through the items one by one in the order they are given.
For each item, find the container with the largest available space and place the
item there. If no container fits, create a new one.
Conclusion
11
Intitulé du Master : Science de Données et Intelligence Artificielle Semestre : S1
Intitulé de la matière: Advanced Operation Research Prof. LAYEB A.
They don’t always guarantee a globally optimal solution but often give good
approximations.
Problems like shortest paths, assignment problems, and knapsack problems are
well-suited to greedy approaches.
Classic algorithms like Prim, Kruskal, and Huffman demonstrate the
efficiency of the greedy technique for these problems.
Although greedy design is simple, proving the optimality for a given problem
can be challenging.
Dynamic programming is more reliable for ensuring optimal solutions.
Greedy construction algorithms are intuitive, efficient, and widely used for
References
Cormen, Thomas H., Charles E. Leiserson, Ronald L. Rivest, and Clifford Stein. "Introduction
to algorithms." Massachusetts Institute of Technology (2009).
Jeff Erickson. "Algorithms." University of Illinois at Urbana-Champaign (2022).
https://jeffe.cs.illinois.edu/teaching/algorithms/
12