110 likes | 233 Views
Drumuri minime si drumuri maxime intr -un graf. Consideram un graf orientat G=(V,U) cu n varfuri,in care fiecarui arc, ii asociem un cost (nr. intreg ) , semnificatia acestui cost poate fi de exemplu : costul deplasarii unui autoturism ..;
E N D
Consideram un graforientat G=(V,U) cu n varfuri,in care fiecarui arc, ii asociem un cost (nr. intreg) , semnificatiaacestui cost poatefi de exemplu: costuldeplasariiunuiautoturism..; Matriceacosturiloreste o matrice A cu n liniisi m coloane care poateavea 2 forme: c, dacaexista arc de cost c>0 de la i la j; A= 0, dacai=j; +∞,daca nu exista arc; F1
F2 c, dacaexista arc de cost de la i la j; A= 0, dacai=j; -∞, daca nu exista arc de cost de la i la j;
V={(1,2) (1,4) (2,4) (3,2) (3,4)} u1 u2 u3 u4 u5 u1=7; u3=4; u5=2; u2=3; u4=6; 0 7 +∞ 3 A= +∞ 0 +∞ 4 +∞ 6 0 2 +∞ +∞ +∞ 0 1 2 3 4
Un drum este minim atuncicandlungimealuiesteceamai mica si se numeste drum de cost minim. Un drum este maxim candlungimealuiesteceamai mare si se numeste drum de cost maxim. Exista 3 posibilitati de a determinadrumuloptim: -sursasigura, destinatieunica (intre 2 noduri date); -sursaunica , destinatiemultipla (Dijkstra); -sursamultipla, destinatiemultipla (Roy-Floyd);
V={(1,2)(1,4)(2,3)(3,2)(4,3)(5,1)} u1 u2 u3 u4 u5 u6 u1= 4; u2=6; u3=9; u4=1 ; u5=7; u6=3; 1 2 5 4 3
0 4 +∞ 6 +∞ +∞ 0 1 +∞ +∞ pentrudrumuri A= +∞ 9 0 7 +∞ minime +∞ +∞ +∞ 0 +∞ 3 +∞ +∞ +∞ 0 0 4 -∞ 6 -∞ pentrudrumuri -∞ 0 1 -∞ -∞ maxime A= -∞ 9 0 7 -∞ -∞ -∞ -∞ 0 -∞ 3 -∞ -∞ -∞ 0
Algoritmullui Roy-Floyd # include<iostream.h> float a[50][50]; int n; void drum(inti , int j) {int k=1, gasit=0; while ((k<=n) &&(gasit==0)) {if ((i!=k) && (j!=k)) a[i][j]=a[i][k]+a[k][j]; { drum(i,k); drum (k,j); gasit=1;} k++;{ if(gasit==0) cout<< j<<“ ”;} void scrie_drum(intnod_initial, intnod_final) { if (a[nod_initial][nod_final]<pinfint) {cout<<“drumul de la”<<nod_initial<<“la”<<nod_final<<“are lungimea”<<a[nod_initial][nod_final]<<endl; cout<<nod_initial<<“ ”; drum(nod_initial, nod_final);} else cout<<“nu exista drum de la”<<nod_initial<<“la”<<nod_final;}
void lungime_drumuri() { inti , j , k; for ( k=1; k<=n; k++) for ( i=1; i<=n; i++) for ( j=1; j<=n; j++) if ( a[i][j]>a[i][k]+a[k][j] ) a[i][j]=a[i][k]+a[k][j];} void citire_cost() { cout<< “ n= “; cin >> n ; for (i=1 ; i<=n; i++ ) for (j=1; j<=n; j++) cin >>a[i][j]; } void main () { citire_cost(); lungime_drumuri(); scriu_drum( 5,p);}
AlgoritmulluiDijkstra # include<iostream.h> float a[50][50] ,d[50], min ; int s[50], t[50], n, i, j, r, poz ; void drum ( inti) { if t[i]==1) drum ( t[i] ); cout<< i<<“ ”; } void main () { cout<<“dati nr. de noduri ”; cin>> n ; for(i=1; i<=n; i++) for (j=1; j<=n; j++) cin>> a[i] [j] ; } cout<<“ datinodul de plecare ”; cin>> r ; s[ r]=1; for( i=1; i<=n; i++) {d [ i]= a[ r][ i] ; if ( i != r) if ( d[ i]< pinfinit ) t [ i]= r; } for (i=1; i<=n-1; i++) {min= pinfinit ; for (j=1; j<=n; j++) if ( s[j ]==0) if ( d[ j]<min ) { min = d[ j ] ; poz=j; } s [poz]=1; for (j=1; j<=n; j++) if (s[ j]==0) if (d [j]> d [poz]+a[poz][ j]) { d [ j]=d[poz]+a[poz][ j] ; t[ j]=poz ; } } for (i=1; i<=n; i++) cout<< d[ i]<<“ ”; cout<<endl; for (i=1; i<=n; i++) if (i != r) if (t[ i]==1) cout<<“distanta de la”<< r<<“la”<< i<<“este”<< d [ i] <<endl ; drum ( i) ; else cout<<“nu exista drum de la”<< r<<“ la ”<< i<<endl; }