1 / 10

Drumuri minime si drumuri maxime intr -un graf

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 ..;

starbuck
Download Presentation

Drumuri minime si drumuri maxime intr -un graf

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Drumuriminimesidrumurimaximeintr-un graf

  2. 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

  3. 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;

  4. 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

  5. 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);

  6. 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

  7. 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

  8. 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;}

  9. 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);}

  10. 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; }

More Related