0% found this document useful (0 votes)
51 views4 pages

Algoritmul Lui Dijkstra

The document describes Dijkstra's algorithm for finding the shortest paths between nodes in a graph. It provides two variations of implementing the algorithm in code. The first variation only outputs the distances, while the second variation also outputs the paths by tracing back the parent nodes. The algorithm works by assigning initial distances from the source node, then iteratively relaxing edges and updating distances and parents until all shortest paths are found.

Uploaded by

Grig Nestian
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
51 views4 pages

Algoritmul Lui Dijkstra

The document describes Dijkstra's algorithm for finding the shortest paths between nodes in a graph. It provides two variations of implementing the algorithm in code. The first variation only outputs the distances, while the second variation also outputs the paths by tracing back the parent nodes. The algorithm works by assigning initial distances from the source node, then iteratively relaxing edges and updating distances and parents until all shortest paths are found.

Uploaded by

Grig Nestian
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

Algoritmul lui Dijkstra

Scurta descriere a algoritmului

Varianta pbinfo-588
cod dijkstra.in dijkstra.out
#include <iostream> 54 3 1 4 0 -1
#define inf 2000 131
#include <fstream> 212
using namespace std; 421
ifstream fin("dijkstra.in"); 438
ofstream fout("dijkstra.out"); 535
int n,m,c[101][101],d[101],s[101],t[101],p; 542
void citire()
{
int i,j,cost;
fin>>n>>p;
for(i=1; i<=n; i++)
for(j=1; j<=n; j++)
if(i!=j)
c[i][j]=inf;
while(fin>>i>>j>>cost)
{
c[i][j]=cost;
}
}
void Dijkstra(int p)
{
int i,j,k=0,mini;
s[p]=1;
t[p]=0;
for(i=1; i<=n; i++)
{
d[i]=c[p][i];
if(i!=p&&d[i]<inf)
t[i]=p;
}
for(j=1; j<=n-1; j++)
{
mini=inf;
for(i=1; i<=n; i++)
if(d[i]<mini && s[i]==0)
{
mini=d[i];
k=i;
}
s[k]=1;
for(i=1; i<=n; i++)
if(d[i]>d[k]+c[k][i]&& s[i]==0)
{
d[i]=d[k]+c[k][i];
t[i]=k;
}
}
}

int main()
{
int i;
citire();
Dijkstra(p);
for(i=1; i<=n; i++)
if(d[i]==inf)
fout<<-1<<" ";
else
fout<<d[i]<<" ";
return 0;
}

Varianta cu afisarea traseelor


cod dijkstra.in dijkstra.out
#include <iostream> 54
#define inf 2000 131 D41: cost=3
#include <fstream> 212 Traseu:4 2
using namespace std; 421 D42: cost=1
ifstream fin("dijkstra.in"); 438 Traseu:4
ofstream fout("dijkstra.out"); 535 D43: cost=4
int n,m,c[101][101],d[101],s[101],t[101],p; 542 Traseu:4 2 1
void citire() D44: cost=0
{ Traseu:0
int i,j,cost; Nu exista drum de
fin>>n>>p; la 4 la 5
for(i=1; i<=n; i++)
for(j=1; j<=n; j++)
if(i!=j)
c[i][j]=inf;
while(fin>>i>>j>>cost)
{
c[i][j]=cost;
}
}
void Dijkstra(int p)
{
int i,j,k=0,mini;
s[p]=1;
t[p]=0;
for(i=1; i<=n; i++)
{
d[i]=c[p][i];
if(i!=p&&d[i]<inf)
t[i]=p;
}
for(j=1; j<=n-1; j++)
{
mini=inf;
for(i=1; i<=n; i++)
if(d[i]<mini && s[i]==0)
{
mini=d[i];
k=i;
}
s[k]=1;
for(i=1; i<=n; i++)
if(d[i]>d[k]+c[k][i]&& s[i]==0)
{
d[i]=d[k]+c[k][i];
t[i]=k;
}
}
}
void drum(int i)
{
if(t[i]!=0)
drum(t[i]);
fout<<i<<" ";
}
int main()
{
int i;
citire();
Dijkstra(p);
for(i=1; i<=n; i++)
if(d[i]==inf)
fout<<"\nNu exista drum de la "<<p<<" la
"<<i<<endl;
else
{
fout<<"\nD"<<p<<i<<": cost="<<d[i]<<"
";
fout<<" Traseu:";
drum(t[i]);
}

return 0;
}

You might also like