Given an undirected graph G having N (1<N<=1000) vertices and positive weights. Find the shortest path from vertex 1 to vertex N, or state that such path doesn't exist.
Hint: At each step, among the vertices which weren't yet checked and for which a path from vertex 1 was found, take the one which has the shortest path, from vertex 1 to it, yet found.
Uses Prims/Dijikstra Algorithms....
#include<iostream>
#include<cstdlib>
#include<limits.h>
using namespace std;
struct Graph
{
int V;
int E;
int **Matrix;
};
struct Table
{
int Dist;
int Pvertex;
};
void Create_Matrix(Graph *G)
{
G->Matrix=new int*[G->V];
for(int i=0;i<G->V;i++)
G->Matrix[i]=new int[G->V];
for(int i=0;i<G->V;i++)
for(int j=0;j<G->V;j++)
G->Matrix[i][j]=-1;
int u,v,cost;
for(int i=0;i<G->E;i++)
{
cin>>u>>v>>cost;
u--,v--;
G->Matrix[u][v]=cost;
}
}
void Heapify(int PQ[], Table DT[],int size)
{
if(size<2)return;
int left=0,right=0,max=0;
int parent=(size-1)/2;
if(2*parent+2<=size)
{right=2*parent+2;left=2*parent+1;}
else if(2*parent+1<=size)
left=2*parent+1;
if(right)
if(DT[PQ[left]].Dist<DT[PQ[right]].Dist)
max=PQ[left];
else
max=PQ[right];
else if(left)
max=PQ[left];
if(max)
{
size=PQ[parent];
PQ[parent]=PQ[max];
PQ[max]=size;
Heapify(PQ,DT,parent);
}
}
void Prims(Graph *G,int s)
{
int *PQ=new int[G->V];
Table *DT=new Table[G->V];
for(int i=0;i<G->V;i++)
DT[i].Dist=DT[i].Pvertex=PQ[i]=INT_MAX;
DT[s].Dist=DT[s].Pvertex=0;
int top=-1,v,d;
PQ[++top]=DT[s].Dist;
while(top>=0)
{
v=PQ[top--];
Heapify(PQ,DT,top);
for(int j=0;j<G->V;j++)
{
if(G->Matrix[v][j]>0)
{ d=G->Matrix[v][j]+DT[v].Dist;
if(d<DT[j].Dist)
{ PQ[++top]=j;
DT[j].Dist=d;
DT[j].Pvertex=v;
Heapify(PQ,DT,top);
}
}
}
}
int src,dest;
cin>>src>>dest;
src--,dest--;
for(int i=0;i<G->V;i++)
PQ[i]=INT_MAX;
top=-1;
if(DT[dest].Pvertex!=INT_MAX)
{PQ[++top]=dest;
while(PQ[top]!=src)
{
v=top;
PQ[++top]=DT[PQ[v]].Pvertex;
}
}
else
{cout<<"PATH DOES NOT EXIST";return;}
while(top>=0)
cout<<PQ[top--]+1<<" ";
}
main()
{
Graph *G=new Graph;
G->Matrix=NULL;
cin>>G->V>>G->E;
Create_Matrix(G);
Prims(G,0);
return 0;
}
Hint: At each step, among the vertices which weren't yet checked and for which a path from vertex 1 was found, take the one which has the shortest path, from vertex 1 to it, yet found.
Uses Prims/Dijikstra Algorithms....
#include<iostream>
#include<cstdlib>
#include<limits.h>
using namespace std;
struct Graph
{
int V;
int E;
int **Matrix;
};
struct Table
{
int Dist;
int Pvertex;
};
void Create_Matrix(Graph *G)
{
G->Matrix=new int*[G->V];
for(int i=0;i<G->V;i++)
G->Matrix[i]=new int[G->V];
for(int i=0;i<G->V;i++)
for(int j=0;j<G->V;j++)
G->Matrix[i][j]=-1;
int u,v,cost;
for(int i=0;i<G->E;i++)
{
cin>>u>>v>>cost;
u--,v--;
G->Matrix[u][v]=cost;
}
}
void Heapify(int PQ[], Table DT[],int size)
{
if(size<2)return;
int left=0,right=0,max=0;
int parent=(size-1)/2;
if(2*parent+2<=size)
{right=2*parent+2;left=2*parent+1;}
else if(2*parent+1<=size)
left=2*parent+1;
if(right)
if(DT[PQ[left]].Dist<DT[PQ[right]].Dist)
max=PQ[left];
else
max=PQ[right];
else if(left)
max=PQ[left];
if(max)
{
size=PQ[parent];
PQ[parent]=PQ[max];
PQ[max]=size;
Heapify(PQ,DT,parent);
}
}
void Prims(Graph *G,int s)
{
int *PQ=new int[G->V];
Table *DT=new Table[G->V];
for(int i=0;i<G->V;i++)
DT[i].Dist=DT[i].Pvertex=PQ[i]=INT_MAX;
DT[s].Dist=DT[s].Pvertex=0;
int top=-1,v,d;
PQ[++top]=DT[s].Dist;
while(top>=0)
{
v=PQ[top--];
Heapify(PQ,DT,top);
for(int j=0;j<G->V;j++)
{
if(G->Matrix[v][j]>0)
{ d=G->Matrix[v][j]+DT[v].Dist;
if(d<DT[j].Dist)
{ PQ[++top]=j;
DT[j].Dist=d;
DT[j].Pvertex=v;
Heapify(PQ,DT,top);
}
}
}
}
int src,dest;
cin>>src>>dest;
src--,dest--;
for(int i=0;i<G->V;i++)
PQ[i]=INT_MAX;
top=-1;
if(DT[dest].Pvertex!=INT_MAX)
{PQ[++top]=dest;
while(PQ[top]!=src)
{
v=top;
PQ[++top]=DT[PQ[v]].Pvertex;
}
}
else
{cout<<"PATH DOES NOT EXIST";return;}
while(top>=0)
cout<<PQ[top--]+1<<" ";
}
main()
{
Graph *G=new Graph;
G->Matrix=NULL;
cin>>G->V>>G->E;
Create_Matrix(G);
Prims(G,0);
return 0;
}
No comments:
Post a Comment