Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Bellow is a code which is based on Bellman-Ford algorithm, Can you please modify it so that It prints the shortest path from source to

Bellow is a code which is based on Bellman-Ford algorithm,

Can you please modify it so that It prints the shortest path from source to a specific destination.

Example: shortest path of (0,4) will be 0->1->3->2->4

void bellman_ford(int nv,edge e[],int src_graph,int ne) { int u,v,weight,i,j=0; int dis[MAX];

/* initializing array 'dis' with 999. 999 denotes infinite distance */ for(i=0;i

/* distance of source vertex from source vertex is o */ dis[src_graph]=0;

/* relaxing all the edges nv - 1 times */ for(i=0;i

if(dis[u]!=999 && dis[u]+weight < dis[v]) { dis[v]=dis[u]+weight; } }

}

/* checking if negative cycle is present */ for(j=0;j

if(dis[u]+weight < dis[v]) { cout<<" NEGATIVE CYCLE PRESENT..!! "; return; } }

cout<<" Vertex"<<" Distance from source"; for(i=1;i<=nv;i++) { cout<<" "<

int main() { int nv,ne,src_graph; edge e[MAX];

cout<<"Enter the number of vertices: "; cin>>nv;

/* if you enter no of vertices: 5 then vertices will be 1,2,3,4,5. so while giving input enter source and destination vertex accordingly */ cout<<"Enter the source vertex of the graph: "; cin>>src_graph;

cout<<" Enter no. of edges: "; cin>>ne;

for(int i=0;i"; cout<<" Enter source vertex :"; cin>>e[i].src; cout<<"Enter destination vertex :"; cin>>e[i].dest; cout<<"Enter weight :"; cin>>e[i].wt; }

bellman_ford(nv,e,src_graph,ne); //calling bellman_ford() function

return 0; }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Upgrading Oracle Databases Oracle Database New Features

Authors: Charles Kim, Gary Gordhamer, Sean Scott

1st Edition

B0BL12WFP6, 979-8359657501

More Books

Students also viewed these Databases questions

Question

3. List ways to manage relationship dynamics

Answered: 1 week ago