PLEASE HELP!!!!!!!! In this programming project, you will be implementing Dijkst
ID: 3825294 • Letter: P
Question
PLEASE HELP!!!!!!!!
In this programming project, you will be implementing Dijkstra’s shortest path algorithm in a directed edge weighted graph. You should use the C++ programming language.
Your program should take one of the following four commands from the standard input, and execute corresponding functions. • S
• R
• W
• P s t
On reading S, the program stops. On reading R, the program reads in an edge weighted directed graph from file GRAPHinput.txt to build the adjacency lists, and waits for the next command.
The file GRAPHinput.txt is a text file. The first line of the file contains two integers n and m, which indicates the number of vertices and the number of edges on the graph, respectively. It is followed by m lines, where each line contains three integers: u, v, and w. These three integers indicate the information of an edge: there is an edge pointing from u to v, with weight w. Please note that the vertices of the graph are indexed from 1 to n (not from 0 to n 1). On reading W, the program writes the graph information to the screen, and waits for the next command.
The screen output format of W is as follows: The first line should contain two integers, n and m, where n is the number of vertices and m is the number of edges. It should be followed by n lines, where each of these n lines has the following format:
u : (v1 w1) (v2 w2) ... (vx, wx)
On reading P s t, the program runs Dijkstra’s shortest path algorithm to compute a shortest s-t path, and prints out the shortest path information from s to t to the screen, and waits for the next command.
The screen output format of P s t is as follows: There are two lines. The first line contains an integer dist, which is the length of the shortest path computed. The next line contains the indexes of all vertices along the path direction, starting from src and ending with dest, in format of:
s v1 v2 ... t
Please note that your program should read in only one graph, but may be asked to compute s-t shortest paths for different pairs of s and t. Therefore, during the computation of the s-t shortest path, your program should not modify the given graph.
You should use modular design. At the minimum, you should have
• the main program as main.cpp and the corresponding main.h;
• the heap functions heap.cpp and the corresponding heap.h;
• the graph functions graph.cpp and the corresponding graph.h;
• various utility functions util.cpp and the corresponding util.h.
WILL UPVOTE AND PRAISE THE CORRECT ANSWER! PLEASE!!!!!!!!!!
Explanation / Answer
#include<iostream.h>
#include<stdio.h>
#include<conio.h>
struct node
{
int cost;
int value;
int from;
}
a[7];
void min_heap(int *b,int i,int n)
{
int j,temp;
temp=b[i];
j=2*i;
while(j<=n)
{
if(j<n &&b[j+1]<b[j])
{
j=j+1;
}
if(temp<b[j])
{
break;
}else if(temp>=b[j])
{
b[j/2]=b[j];
j=2*j;
]
}
b[j/2]=temp;
return;
}
void build_minheap(int *b,int n)
{
int i;
for(i=n/2;i>=1;i--)
{
min_heap(b,i,n);
}
}
void addedge(intam[][7],int src,int dest,int cost)
{
am[src][dest]=cost;
return;
}
void bell(int am[][7])
{
int,i,j,kc=0,temp;
a[0].cost=0;
a[0].from=0;
a[0].value=0;
for(i=1;i<7;i++)
{ a[0].from=0;
a[0].cost=INFINITY;
a[0].value=0;
}
while(c<7)
{
int miin=999;
for(i=0;i<7;i++)
{
if(min>a[i].cost&&a[i].value==0)
{
min=a[i].cost;
}
else
{
continue;
}
}
for(i=0;i<7;i++)
{
if(min==a[i].cost&&a[i].value==0)
{
break;
}
else
{
continue;
}
}
temp=i;
for(k=0;k<7;k++)
{
if(am[temp][k]+a[temp].cost<a[k].cost)
{
a[k].cost=am[temp][k]+a[temp].cost;
a[k].from=temp;
}
else
{
continue;
}
}
a[temp].value=1;
c++;
}
cout<<"cost"<<" "<<"source node"<<endl;
for(j=0;j<7;j++)
{
cout<<a[j].cost<<" "<<a[j].from<<endl;
}
}
int main()
{
int n,am[7][7],c=0,i,j,cost;
for(i=0;i<7;i++)
{
for(j=0;j<7;j++)
{
am[i][j]=INFINITY;
}
}
while(c<12)
{
cout<<"enter the source and destinatio cost of edge";
cin>>i>>j>>cost;
addedge(am,i,j,cost);
c++;
bell(am);
getch();
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.