MCPcopy Create free account
hub / github.com/codemistic/Data-Structures-and-Algorithms / bellman_ford

Function bellman_ford

graph/code17.cpp:21–54  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

19};
20
21void bellman_ford(vector<pair<int, int>> vec[], int v, int source)
22{
23 vector<node> edges;
24 vector<int> dist(v, inf);
25 dist[source] = 0;
26 for (int i = 0; i < v; i++)
27 {
28 for (int j = 0; j < vec[i].size(); j++)
29 {
30 edges.push_back({i, vec[i][j].first, vec[i][j].second});
31 }
32 }
33
34 for (int i = 0; i < v - 1; i++)
35 {
36 for (int j = 0; j < edges.size(); j++)
37 {
38 node curr = edges[j];
39
40 int curr_u = curr.u, curr_v = curr.v, curr_weight = curr.weight;
41 if (dist[curr_v] > dist[curr_u] + curr_weight)
42 {
43 dist[curr_v] = dist[curr_u] + curr_weight;
44 }
45 }
46 }
47
48 cout << "FINAL SHORTEST DISTANCES VIA BELLMAN FORD ALGORITHM: " << endl;
49 for (int i = 0; i < dist.size(); i++)
50 {
51 cout << dist[i] << " ";
52 }
53 cout << endl;
54}
55
56int main()
57{

Callers 1

mainFunction · 0.85

Calls 2

push_backMethod · 0.80
sizeMethod · 0.45

Tested by

no test coverage detected