Question
i want a google test for this code #include using namespace std; #define INF 0x3f3f3f3f typedef pair iPair; class Graph { int V; list *
i want a google test for this code
#include
using namespace std;
#define INF 0x3f3f3f3f
typedef pair
class Graph
{
int V;
list
public:
Graph(int V)
{
this->V = V;
adj = new list
}
void addEdge(int u, int v, int w);
void shortestPathingraph(int s);
};
void Graph::addEdge(int u, int v, int w)
{
adj[u].push_back(make_pair(v, w));
adj[v].push_back(make_pair(u, w));
}
void Graph::shortestPathingraph(int src)
{
priority_queue
vector
pq.push(make_pair(0, src));
dist[src] = 0;
while (!pq.empty()) {
int u = pq.top().second;
pq.pop();
list
for (i = adj[u].begin(); i != adj[u].end(); ++i) {
int v = (*i).first;
int weight = (*i).second;
if (dist[v] > dist[u] + weight) {
dist[v] = dist[u] + weight;
pq.push(make_pair(dist[v], v));
}
}
}
printf("Vertex \tDistance from Source ");
for (int i = 0; i < V; ++i)
printf("%d \t\t %d ", i, dist[i]);
}
int main()
{
int V = 9;
Graph g(V);
g.addEdge(0, 1, 1);
g.addEdge(0, 7, 2);
g.addEdge(1, 2, 3);
g.addEdge(1, 7, 4);
g.addEdge(2, 3, 5);
g.addEdge(2, 8, 6);
g.addEdge(2, 5, 7);
g.addEdge(3, 4, 8);
g.addEdge(3, 5, 9);
g.addEdge(4, 5, 10);
g.addEdge(5, 6, 12);
g.addEdge(6, 7, 13);
g.addEdge(6, 8, 16);
g.addEdge(7, 8, 17);
g.shortestPathingraph(0);
return 0;
}
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started