130 likes | 477 Views
Algoritmul lui ROY-FLOYD. #include<iostream.h> float a[50][50]; int n void citire_cost() {cout<<"n="; cin>>n; cout<<"dati matricea costurilor"; for(i=1;i<=n;i++) for(j=1;j<=n;j++) cin>>a[i][j];} void drum(int i,int j) {int k=1,gasit=0; while((k<=n)&&(gasit==0))
E N D
Algoritmul lui ROY-FLOYD #include<iostream.h> float a[50][50]; int n voidcitire_cost() {cout<<"n="; cin>>n; cout<<"dati matricea costurilor"; for(i=1;i<=n;i++) for(j=1;j<=n;j++) cin>>a[i][j];} void drum(int i,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 scriu_drum(int nod_initial,int nod_final) {if(a[nod_initial][nod_final]<Pinfinit) {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() {int i,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];} int main() {citire_cost(); lungime_drumuri(); scriu_drum(5,3);}
Algoritmul de parcurgere in adancime(D.F.) -se construieste vectorul viz ale carui elemente sunt viz[i]=1,daca nodul i este vizitat; 0,altfel; -se va folosi o stiva care ne permite sa plecam in fiecare moment de la nodul curent spre primul dintre vecinii sai nevizitati,acesta fiind plasat in varful stivei,cu el se va continua in acelasi mod;
#include<iostream.h> int a[50][50],viz[50],c[50],np,i,j,n; void adancime() { cout<<i<<endl; for(j=1;j<=n;j++) if((a[i][j]==1)&&(viz[j]==0)) viz[j]=1; adancime(j);}} void main() {cout<<"dati nr de noduri"; cin>>n; cout<<"matricea este"; for(i=2;i<=n;i++) for(j=1;j<=i-1;j++) {cin>>a[i][j]; a[j][i]=a[i][j];} for(i=1;i<=n;i++) viz[i]=0; cout<<"dati nod de plecare"; cin>>np; viz[np]=1; adancime(np);}
Algoritmul lui Roy warshall #include<iostream.h> int n,a[50][50],i,j,ok=1; void roy() {int k,i,j; for(k=1;k<=n;k++) for(i=1;i<=n;i++) for(j=1;j<=n;j++) if((i!=k)&&(j!=k)) if(a[i][j]==0) a[i][j]=a[i][k]*a[k][j];} void main() {cout<<"dati numarul de noduri"; for(i=2;i<=n;i++) for(j=1;j<=i-1;j++) {cin>>a[i][j]; a[j][i]=a[i][j];} for(i=1;i<=n;i++) a[i][j]=0; roy(); for(i=1;i<=n;i++) for(j=1;j<=n;j++) if(a[i][j]==0) ok=0; if(ok==1) cout<<"graful este conex"; else cout<<"graful nu este conex";}}
Comis-voiajor • Se dau n orase intre care exista sau nu legaturi directe. • Un comis-voiajor pleaca din orasul 1 si doreste sa viziteze cele n orase,pentru a prezenta produsele • firmei sale.Stiind ca el nu vrea sa treaca de 2 ori prin acelasi oras si doreste sa se intoarca in • orasul de plecare sa se determine toate posibilitatile de vizitare a celor n orase.
#include<iostream.h> int n,a[50][50],i,x[50],k,gasit; int valid(int k) {int ok; ok=1; for(i=1;i<=k-1;i++) if(x[k]==x[j]) ok=0; if(k>1) if(a[x[k]][x[k-1]]==0) ok=0; if(k>1) if(a[x[k]][x[k-1]]==0) ok=0; if(a[x[1]][x[n]]==0) ok=0; void afisare() {int i; for(i=1;i<=n;i++) cout<<x[i]<<" "; cout<<"1";} void main() {cout<<"dati numarul de orase"; cin>>n; cout<<"citim matricea de adiacenta"; for(i=2;i<=n;i++) for(j=1;j<=i-1;j++) {cin>>a[i][j]; a[j][i]=a[i][j];} for(i=1;i<=n;i++) a[i][i]=0; k=1; x[k]=1; while(k>1) {gasit=0; while((gasit==0)&&(x[k]<n)) x[k]=x[k]+1; if(valid(k)==1) gasit=1;} if(gasit==1) if(k==n) afisare(); else{k++; x[k]=0;} else{k--;}}}