Skip to content

Commit af028f6

Browse files
committed
Added Dijkstra and Bellman Ford Shortest Path Algorithms.
1 parent cc0ddbe commit af028f6

File tree

2 files changed

+213
-0
lines changed

2 files changed

+213
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
#include <bits/stdc++.h>
2+
using namespace std;
3+
4+
#define INF 1e9 + 5
5+
6+
struct Edge
7+
{
8+
int src, dest, weight;
9+
Edge()
10+
{
11+
12+
}
13+
Edge(int src, int dest, int weight) :
14+
src(src), dest(dest), weight(weight)
15+
{
16+
// Nothing to do here.
17+
}
18+
};
19+
20+
struct Graph
21+
{
22+
int V, E;
23+
Edge* edges;
24+
Graph(int V, int E) : V(V), E(E)
25+
{
26+
edges = new Edge[E];
27+
}
28+
~Graph()
29+
{
30+
delete[] edges;
31+
}
32+
};
33+
34+
void printArr(int distTo[], int n)
35+
{
36+
cout << "Vertex distancesfrom source: \n";
37+
for (int i = 0; i < n; i++)
38+
cout << i << " " << distTo[i] << "\n";
39+
}
40+
41+
void BellmanFord(Graph* graph, int src)
42+
{
43+
int V = graph->V;
44+
int E = graph->E;
45+
int distTo[V];
46+
47+
// Initialize all distances to infinity and the src to be 0.
48+
for (int i = 0; i < V; i++)
49+
distTo[i] = INF;
50+
distTo[src] = 0;
51+
52+
// Relax all edges V - 1 times.
53+
for (int i = 0; i < V - 1; i++)
54+
{
55+
for (int j = 0; j < E; j++)
56+
{
57+
int u = graph->edges[j].src;
58+
int v = graph->edges[j].dest;
59+
int weight = graph->edges[j].weight;
60+
distTo[v] = min(distTo[v], distTo[u] + weight);
61+
}
62+
}
63+
printArr(distTo, V);
64+
}
65+
66+
int main()
67+
{
68+
69+
int V = 5;
70+
int E = 8;
71+
Graph* graph = new Graph(5, 8);
72+
73+
// add edge 0-1 (or A-B in above figure)
74+
graph->edges[0].src = 0;
75+
graph->edges[0].dest = 1;
76+
graph->edges[0].weight = -1;
77+
78+
// add edge 0-2 (or A-C in above figure)
79+
graph->edges[1].src = 0;
80+
graph->edges[1].dest = 2;
81+
graph->edges[1].weight = 4;
82+
83+
// add edge 1-2 (or B-C in above figure)
84+
graph->edges[2].src = 1;
85+
graph->edges[2].dest = 2;
86+
graph->edges[2].weight = 3;
87+
88+
// add edge 1-3 (or B-D in above figure)
89+
graph->edges[3].src = 1;
90+
graph->edges[3].dest = 3;
91+
graph->edges[3].weight = 2;
92+
93+
// add edge 1-4 (or A-E in above figure)
94+
graph->edges[4].src = 1;
95+
graph->edges[4].dest = 4;
96+
graph->edges[4].weight = 2;
97+
98+
// add edge 3-2 (or D-C in above figure)
99+
graph->edges[5].src = 3;
100+
graph->edges[5].dest = 2;
101+
graph->edges[5].weight = 5;
102+
103+
// add edge 3-1 (or D-B in above figure)
104+
graph->edges[6].src = 3;
105+
graph->edges[6].dest = 1;
106+
graph->edges[6].weight = 1;
107+
108+
// add edge 4-3 (or E-D in above figure)
109+
graph->edges[7].src = 4;
110+
graph->edges[7].dest = 3;
111+
graph->edges[7].weight = -3;
112+
113+
BellmanFord(graph, 0);
114+
115+
return 0;
116+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
#include <bits/stdc++.h>
2+
using namespace std;
3+
4+
#define INF 1e9 + 5
5+
6+
struct Graph
7+
{
8+
int V;
9+
vector<pair<int, int>> *adjList;
10+
Graph(int V) : V(V)
11+
{
12+
adjList = new vector<pair<int, int>>[V];
13+
}
14+
~Graph()
15+
{
16+
delete[] adjList;
17+
}
18+
void addEdge(int src, int dest, int weight);
19+
};
20+
21+
22+
void Graph::addEdge(int src, int dest, int weight)
23+
{
24+
adjList[src].push_back(make_pair(dest, weight));
25+
}
26+
27+
struct comparer
28+
{
29+
bool operator()(pair<int, int> a, pair<int, int> b)
30+
{
31+
return a.second < b.second;
32+
}
33+
};
34+
35+
void DijkstraSP(Graph *graph, int src)
36+
{
37+
int V = graph->V;
38+
39+
int distTo[V];
40+
bool processed[V];
41+
42+
43+
for (int i = 0; i < V; i++)
44+
{
45+
distTo[i] = INF;
46+
processed[i] = false;
47+
}
48+
distTo[src] = 0;
49+
priority_queue<pair<int, int>, vector<pair<int, int>>, comparer> pq;
50+
pq.push(make_pair(src, 0));
51+
while (!pq.empty())
52+
{
53+
int a = pq.top().first;
54+
pq.pop();
55+
if (processed[a]) continue;
56+
processed[a] = true;
57+
for (auto &it : graph->adjList[a])
58+
{
59+
int b = it.first;
60+
int w = it.second;
61+
if (distTo[b] > distTo[a] + w)
62+
{
63+
distTo[b] = distTo[a] + w;
64+
pq.push(make_pair(b, distTo[b]));
65+
}
66+
}
67+
}
68+
69+
cout << "Vertex distancesfrom source: \n";
70+
for (int i = 0; i < V; i++)
71+
cout << i << " " << distTo[i] << "\n";
72+
}
73+
74+
int main()
75+
{
76+
int V = 9;
77+
Graph *graph = new Graph(V);
78+
79+
graph->addEdge(0, 1, 4);
80+
graph->addEdge(0, 7, 8);
81+
graph->addEdge(1, 2, 8);
82+
graph->addEdge(1, 7, 11);
83+
graph->addEdge(2, 3, 7);
84+
graph->addEdge(2, 8, 2);
85+
graph->addEdge(2, 5, 4);
86+
graph->addEdge(3, 4, 9);
87+
graph->addEdge(3, 5, 14);
88+
graph->addEdge(4, 5, 10);
89+
graph->addEdge(5, 6, 2);
90+
graph->addEdge(6, 7, 1);
91+
graph->addEdge(6, 8, 6);
92+
graph->addEdge(7, 8, 7);
93+
94+
DijkstraSP(graph, 0);
95+
96+
return 0;
97+
}

0 commit comments

Comments
 (0)