Skip to content

Commit 8b95900

Browse files
Create Dijkstra's Algorithm.cpp
1 parent 3b663a7 commit 8b95900

File tree

1 file changed

+101
-0
lines changed

1 file changed

+101
-0
lines changed
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
/*
2+
Dijkstra's Algorithm
3+
Send Feedback
4+
Given an undirected, connected and weighted graph G(V, E) with V number of vertices (which are numbered from 0 to V-1) and E number of edges.
5+
Find and print the shortest distance from the source vertex (i.e. Vertex 0) to all other vertices (including source vertex also) using Dijkstra's Algorithm.
6+
Print the ith vertex number and the distance from source in one line separated by space. Print different vertices in different lines.
7+
Note : Order of vertices in output doesn't matter.
8+
Input Format :
9+
First line will contain T(number of test case), each test case follows as.
10+
Line 1: Two Integers V and E (separated by space)
11+
Next E lines : Three integers ei, ej and wi, denoting that there exists an edge between vertex ei and vertex ej with weight wi (separated by space)
12+
Output Format :
13+
In different lines, ith vertex number and its distance from source (separated by space)
14+
Constraints :
15+
1 <= T <= 10
16+
2 <= V, E <= 10^3
17+
Sample Input 1 :
18+
1
19+
4 4
20+
0 1 3
21+
0 3 5
22+
1 2 1
23+
2 3 8
24+
Sample Output 1 :
25+
0 0
26+
1 3
27+
2 4
28+
3 5
29+
*/
30+
31+
32+
33+
#include<iostream>
34+
#include<climits>
35+
#include<algorithm>
36+
using namespace std;
37+
void algo(int** arr, int v, int e, bool* visited, int* dist, int current_vertex)
38+
{
39+
for (int i = 0; i < v - 1; i++)
40+
{
41+
int vertex_with_minimum_distance=-1;
42+
int min_distance = INT_MAX;
43+
for (int j = 0; j < v; j++)
44+
{
45+
if (!visited[j] && min_distance > dist[j])
46+
{
47+
min_distance = dist[j];
48+
vertex_with_minimum_distance = j;
49+
}
50+
}
51+
52+
visited[vertex_with_minimum_distance] = true;
53+
for (int j = 0; j < v; j++)
54+
{
55+
if (!visited[j] && dist[j] > dist[vertex_with_minimum_distance] + arr[vertex_with_minimum_distance][j] && arr[vertex_with_minimum_distance][j] > 0)
56+
{
57+
dist[j] = dist[vertex_with_minimum_distance] + arr[vertex_with_minimum_distance][j];
58+
}
59+
}
60+
}
61+
}
62+
int main()
63+
{
64+
int t;cin>>t;
65+
while(t--){
66+
int v, e;
67+
cin >> v >> e;
68+
int** arr = new int*[v];
69+
for (int i = 0; i < v; i++)
70+
{
71+
arr[i] = new int[v];
72+
for (int j = 0; j < v; j++)
73+
{
74+
arr[i][j] = 0;
75+
}
76+
}
77+
for (int i = 0; i < e; i++)
78+
{
79+
int v1, v2, w;
80+
cin >> v1 >> v2 >> w;
81+
arr[v1][v2] = w;
82+
arr[v2][v1] = w;
83+
}
84+
bool* visited = new bool[v];
85+
for (int i = 0; i < v; i++)
86+
{
87+
visited[i] = false;
88+
}
89+
int* dist = new int[v];
90+
for (int i = 0; i < v; i++)
91+
{
92+
dist[i] = INT_MAX;
93+
}
94+
dist[0] = 0;
95+
algo(arr, v, e, visited, dist, 0);
96+
for (int i = 0; i < v; i++)
97+
{
98+
cout << i << " " << dist[i] << endl;
99+
}
100+
}
101+
}

0 commit comments

Comments
 (0)