Data Structures & Algorithm Techniques
Data Structures & Algorithm Techniques
and ALGORITHMS
Bachelor's Degree in Computer Science
DESIGN TECHNIQUES OF
ALGORITHMS
Data Structures and Algorithms
Greedy Algorithms
Dynamic Programming
Backtracking
smaller size.
for i = 1, 2, ..., k
si=resolver(pi)
small(p, q)
solucion =solucion_directa(p, q)
in another case
m = divide(p, q);
solucion =combinar
(divide_you_will_win (p, m),
divide_venceras (m+1, q);
Divide and Conquer
MergeSort
MergeSort (i, j: integer)
It is small if the size is less than a base case.
if small(i, j)
DirectOrder(i, j)
in another case
The original array is split into two pieces of equal size (or as close as possible)
possible), that is to say n/2 y n/2 */
s = (i + j) divided by 2
Greedy Algorithms
Greedy
Estos algoritmos funcionan por pasos:
Be part of an empty solution.
At each step, the next element is chosen.
among the candidates, to add to the solution.
Once this decision is made, it cannot be undone.
undo.
The algorithm will end when the set of
selected elements constitute a
solution.
Data Structures and Algorithms
Greedy Algorithms
General Outline
VoraciousFunction(C:set):set;
C is the set of candidates
S The solution is built in the set S
while C ≠ y no solution(S) do
x select(C)
C C–{x}
sifactible(SՍ{x})entonces
S SΣ{x}
If S is the solution, then return S
but there is no solution
At each step, there are the following sets:
Selected candidates for solution S.
Selected candidates but rejected later.
Pending candidates for selection C.
Data Structures and Algorithms
Greedy Algorithms
Components
Greedy Algorithms
Example
Given a monetary system that contains coins of value 10, 6, 5, and 1 and the
the amount that is desired to change is, for example, P=18 : How is it composed of
solution regarding the quantity of coins and their values?
Data Structures and Algorithms
Greedy Algorithms
Example
Initial candidates: all types of available currencies. Value coins
10, 6, 5 and 1
Solution: set of coins that total the amount P.
A solution will be of the form (x1, x2, x3, x4) dondexIit is the number of coins
of the type. It is supposed that the currency is worthi.
Functions:
solution. The present value will be a solution if xi·ciP checks if the value of
the selected coins so far are exactly the value that there is
what to pay.
objective. The function to be minimized is xithe resulting number of coins.
Count the set of coins used in the solution.
select. Choose the highest value coin possible at each step, but
less than the value that remains to be returned.
feasible. A set of coins is feasible if its total value does not exceed the
amount to be paid.
Instead of selecting coins one by one, integer division can be used and
Choose all possible coins of higher value.
Data Structures and Algorithms
Greedy Algorithms
Example
Greedy Algorithms
Applications
Dynamic Programming
Divide and Conquer splits the problem into
independent subproblems, combining
the solutions to solve the problem
original.
Dynamic Programming
Data Structures and Algorithms
Dynamic Programming
Solve subproblems only once.
storing their solutions in a
table for its future use.
Dynamic Programming
General Scheme
optimal solution.
stored information.
Data Structures and Algorithms
Dynamic Programming
Fibonacci sequence
0,1,1,2,3,5,8,13,21,34,55,89…..
Dynamic Programming
Fibonacci sequence
By counting the number of distinct letters in each month, one can know the amount
of total couples that exist up to that month.
Data Structures and Algorithms
Dynamic Programming
Fibonacci - Recursive Solution
Fibonacci (N)
If N = 0
Fibonacci ← 0 // Base Case
else
If N = 1
Fibonacci←1 // Caso Base
else
Fibonacci ← Fibonacci (N-1) +
Fibonacci (N-2)
Complexity O(cte)n )
Data Structures and Algorithms
Dynamic Programming
Fibonacci - Iterative Solution
#include<stdio.h>
#define MAX 10
main()
{int i; int fib[MAX];
fib[0]=1;
fib[1]=1;
for(i=2;i< MAX; i++)
{
fib[i] = fib[i-1] + fib[i-2];
}
}
Complexity O(n)
Data Structures and Algorithms
Dynamic Programming
Applications
Backtracking
The goal is to find
solutions for some problem, they
achieve by building solutions
partials.
It resembles a journey in
depth within a directed graph.
It especially applies to problems of
optimization.
Data Structures and Algorithms
Backtrack
General Scheme
1. As the journey progresses, they go
stops
7. Otherwise, a Backtrack is performed
Data Structures and Algorithms
Backtracking
Backtracking
The journey has no success if there is none.
the stage of the partial solution cannot be
complete.
In a situation of success, the journey
go back. If you return to a node that
it has one or more unexplored neighbors,
continue the course of a solution.
Data Structures and Algorithms
Backtracking
Applications