Presentation is loading. Please wait.

Presentation is loading. Please wait.

Greedy Backtracking ? ? ? ?.  Fast, low complexity, gives acceptable solution (not necessarily the best)  At each step choose the best option considering.

Similar presentations


Presentation on theme: "Greedy Backtracking ? ? ? ?.  Fast, low complexity, gives acceptable solution (not necessarily the best)  At each step choose the best option considering."— Presentation transcript:

1 Greedy Backtracking ? ? ? ?

2

3  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

4

5

6

7 How? Variation: initial processing of set A (sort)

8  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

9 // I: total capacity (q), nr. of objects (n), assigned capacity (c), // [ profit (v) ] // E: solution x void Rucsac_c(float q, int n, float* c, float* x) { float qr; int i,j; qr=q; for(i=0; i 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; } }

10

11

12

13

14

15  Recursive nature => easy recursive implementation  General recursive function ▪ backtracking(i)  if i==n+1  save_solution();  else  for each element j of S i  x[i]=j;  if posibile(i)  backtracking(i+1);

16  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

17

18 // 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(); }

19 // partial condition // I: partial solution (x), number of elements (i) // E: 1 if acceptable, 0 if not acceptable int 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; }

20 // 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; }

21 // I: queen nr.(n), current element (i), solution vector (x), solution count (nr) // E: solution count int 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; }

22 Spor la învăat!


Download ppt "Greedy Backtracking ? ? ? ?.  Fast, low complexity, gives acceptable solution (not necessarily the best)  At each step choose the best option considering."

Similar presentations


Ads by Google