-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDjikstra.h
37 lines (29 loc) · 1008 Bytes
/
Djikstra.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#pragma once
#include <limits.h>
#include <iostream>
void Djikstra(int **graph, int V, int src){
int *dist = new int[V];
bool *visited = new bool[V];
for (int i = 0; i < V; i++)
dist[i] = INT_MAX, visited[i] = false;
dist[src] = 0;
for (int count = 0; count < V - 1; count++)
{
int min = INT_MAX, min_index;
// Finding Minimum Distance
for (int v = 0; v < V; v++){
if (visited[v] == false && dist[v] <= min)
min = dist[v], min_index = v;
}
visited[min_index] = true;
for (int v = 0; v < V; v++){
if (!visited[v] && graph[min_index][v] && dist[min_index] != INT_MAX && dist[min_index] + graph[min_index][v] < dist[v]){
dist[v] = dist[min_index] + graph[min_index][v];
}
}
}
using namespace std;
cout << "Vertices \t Distance from Source\n" << endl;
for (int i = 0; i < V; i++)
cout << src << "-->" << i << " : " << dist[i] << endl;
}