Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

#include using namespace std; #define INF 0x3f3f3f3f typedef pair iPair; class Graph { int V; list * adj; public: Graph(int V) { this->V = V;

#include

using namespace std;

#define INF 0x3f3f3f3f

typedef pair iPair;

class Graph

{

int V;

list>* adj;

public:

Graph(int V)

{

this->V = V;

adj = new list[V];

}

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, greater> pq;

vector dist(V, INF);

pq.push(make_pair(0, src));

dist[src] = 0;

while (!pq.empty()) {

int u = pq.top().second;

pq.pop();

list>::iterator i;

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;

}

i want a unite tests files and cmake files c++

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

Step: 3

blur-text-image

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

Probabilistic Databases

Authors: Dan Suciu, Dan Olteanu, Christopher Re, Christoph Koch

1st Edition

3031007514, 978-3031007514

More Books

Students also viewed these Databases questions