Date post: | 30-Dec-2015 |
Category: |
Documents |
Upload: | joanna-horn |
View: | 225 times |
Download: | 0 times |
Consider , , order relation on each
solution space
validation function
result solutions set
partial validation functions
▪ may add a new element with .
Solution space
Greedy – local optimum method
Fast, low complexity, gives acceptable solution (not necessarily the best) At each step choose the best option considering local context,
ignores general context Sometimes the result may be the worst
Used when finding the best solution consumes too much resources
Greedy – local optimum method
11
2233 44
55 6677
5
88
99
1111
1010
215
32 2647
9
12
41
5
20
18 21
1, 3, 6, 2, 5, 9, 11 -> 124
7
1, 4, 8, 11 -> 45
Greedy – local optimum method
Characteristics of problems solved using Greedy
Problem may be imagined as a set with elements;
A possible solution is a subset ( that meets a give criteria ( is acceptable);
There may be more than 1 acceptable subsets (possible solutions), of which one is considered optimal solution, based on a criteria (a max / min function).
Greedy – local optimum method
Operations (not always obvious)
Choose a candidate element from (
Verify acceptability for chosen element: adding it to the partially constructed solution makes it unacceptable?
▪ is acceptable?
Add the chose element to partially constructed solution, if the result is acceptable.
General algorithm
(input) receive set with elements
initialize solution as empty set
repeat (at most) times
▪ choose a candidate element from set
▪ verify if is acceptable partial solution
▪ if yes, add to set :
send set as solution
Greedy – local optimum method
How? How?
Variation: initial processing of set A (sort)
Examples of problems solved through Greedy algorithms: Find the minimum spanning tree (optimal solution always)
▪ Kruskal, Prim algorithms
Knapsack (rucksack) problem
▪ discrete
▪ continuous
Optimal merging of n vectors
Max sum from a set of real values
Payment problem (with unit coin)
Watchman problem
Transposition multiplication
Dijkstra algorithm
Full problem statement in manual
Greedy – local optimum method
Knapsack problem (continuous)
// I: total capacity (q), nr. of objects (n), assigned capacity (c),// [ profit (v) ]// E: solution xvoid Rucsac_c(float q, int n, float* c, float* x){ float qr; int i,j;
qr=q; for(i=0; i<n && qr>0; i++) if(qr>=c[i]) { x[i]=1; qr-=c[i]; //qr-=c[i]*x[i] } else { x[i]=qr/c[i]; qr=0; //qr-=c[i]*x[i] for(j=i+1;j<n;j++) x[j]=0; }}
Backtracking – full search
Slow, high resource consumption, high complexity Verifies (almost) all elements of the solution space
, ,
- partial conditions
build one element at a time is assigned a value from after previous elements were assigned () move to element
▪ otherwise chose another value for chosen value for is consumed set of consumed values
▪ if there are no more available values go back to previous element does not guarantee a full solution (result solution)
Backtracking - data representation
Current configuration
Initial configuration
Solution configuration
Final configuration
Solving a problem: initial config. solution config. … solution config. final config.
Backtracking – general algorithm
initialize //build initial configuration
cît timp //while the configuration is not final dacă //solution configuration?
▪ reține soluția ▪ //return after finding a solution
altfel▪ dacă //are there available values
▪ alege o valoare
▪ dacă satisfac condițiile de continuare //assign and advance
▪ altfel //return
Backtracking - variation
Particularități ale unor probleme
- progresii aritmetice cu valoarea inițială , rația și valoarea finală ▪ uneori ,
toate sînt identice cu mulțimea
Avantaje nu trebuie memorate explicit mulțimile și alegerea unui element neconsumat este ușoară
//first element minus ration, often 0=1-1 while
//deadlock variable while and //chose a value for
▪ //next term in progression▪ posibile(,vb) //partial conditions fulfilled
if //deadlock => return▪
otherwise▪ if
▪ if final_condition(x) //solution configuration? save_solution
▪ otherwise //advance▪
Backtracking - recursive
Recursive nature => easy recursive implementation
General recursive function
▪ backtracking(i) if i==n+1
save_solution(); else
for each element j of Si
x[i]=j; if posibile(i)
backtracking(i+1);
Backtracking
Examples of problems solved through backtracking:
The 8 (n) queens.
Round table knights.
Payment (with/without unit coin).
Generation of all permutations.
Generation of all arrangements.
Generation of all combinations.
Map coloring.
Full problem statements in manual
The 8 queens
® ®
® ®
® ®
® ®
- column for queen on line i
, ,
Partial condition Assigned queen,
▪ is not on the same line as a previous queen▪ default, no verification required
▪ is not on the same column as a previous queen▪ for
▪ is not on the same diagonal as a previous queen▪ for
Final condition Not necessary
The 8 queens
// save a configuration (display)// I: solution nr.(nr), queen number (n), solution vector// E: -void save_solution(int nr, int n, int* x){ int i,j;
printf("\n Solution number %d\n",nr); for(i=1; i<=n; i++) { for(j=1; j<=n; j++) printf("%c",j==x[i]?'Q':'.'); printf("\n"); } if(r=='n') { printf("\n\nNext (n) or Last (l)?"); r=_getch(); }}
The 8 queens
// partial condition// I: partial solution (x), number of elements (i)// E: 1 if acceptable, 0 if not acceptableint possible(int *x, int i){ int j, p; p=1; for( j=1; j<i; j++) if( x[i]==x[j] || abs(i-j)==abs(x[i]-x[j]) ) p=0; return p;}
The 8 queens
// I: queen nr. / table size (n) // E: solution count int queens(int n){ int nr, *x, i, ok; x=new int[n+1]; //solution vector nr=0; //solution count i=1; x[1]=0; //first value minus ratio while(i>0) //while not final configuration { ok=0; while( x[i]<n && !am) //chose next acceptable for x[i] { x[i]++; //next value for x[i] ok=possible(x,i); //is it acceptable? } if(!ok) i--; //deadlock, return else if( i==n ) //solution configuration save_solution(++nr,n,x); else x[++i]=0; //first value minus ratio } delete x; return nr;}
The 8 queens
// I: queen nr.(n), current element (i), solution vector (x), solution count (nr)// E: solution countint queen_r(int n, int i, int* x, int nr){ int j;
if( i==n+1) save_solution(++nr,n,x); else for(j=1; j<=n; j++ ) { x[i]=j; if( posibil(x,i) ) nr=queen_r(n,i+1,x,nr); } return nr;}