1
- // Algoritimo de Dijkstra
1
+ // Dijkstra's Algorithm
2
2
// Anderson Carneiro da Silva
3
3
// https://github.com/AndersonSheep
4
4
5
- // Baseado no método do GeekforGeeks
6
- // Um programa Java para o algoritmo de caminho mais curto de fonte única de Dijkstra .
7
- // O programa é para representação da matriz de adjacência do grafo
5
+ // Based on the GeekforGeeks method
6
+ // A Java program for Dijkstra's single-source shortest path algorithm .
7
+ // The program is for the representation of the graph's adjacency matrix.
8
8
import java .io .*;
9
9
import java .util .*;
10
10
11
11
class ShortestPath {
12
- // Uma função de utilidade para encontrar o vértice com valor mínimo de distância ,
13
- // do conjunto de vértices ainda não incluídos na árvore do caminho mais curto
12
+ // A utility function to find the vertex with the minimum distance value ,
13
+ // from the set of vertices not yet included in the shortest path tree
14
14
static final int V = 9 ;
15
15
16
16
int minDistance (int dist [], Boolean sptSet []) {
17
- // Iniciando um valor minimo
17
+ // Initialize a minimum value
18
18
int min = Integer .MAX_VALUE , min_index = -1 ;
19
19
20
20
for (int v = 0 ; v < V ; v ++) {
@@ -27,48 +27,48 @@ int minDistance(int dist[], Boolean sptSet[]) {
27
27
return min_index ;
28
28
}
29
29
30
- // Uma função de utilidade para imprimir a matriz de distância construída
30
+ // A utility function to print the constructed distance matrix
31
31
void printSolution (int dist []) {
32
32
System .out .println ("Vertex \t \t Distance from Source" );
33
33
for (int i = 0 ; i < V ; i ++) {
34
34
System .out .println (i + " \t \t " + dist [i ]);
35
35
}
36
36
}
37
37
38
- // Função que implementa o caminho mais curto da fonte única de Dijkstra
39
- // algoritmo para um grafo representado usando matriz de adjacência
38
+ // Function that implements Dijkstra's single-source shortest path algorithm
39
+ // for a graph represented using an adjacency matrix
40
40
void dijkstra (int graph [][], int src ) {
41
- // A matriz de saída . dist [i] irá manter a menor distância de src a i
41
+ // The output array . dist[i] will hold the shortest distance from src to i
42
42
int dist [] = new int [V ];
43
43
44
- // sptSet [i] será verdadeiro se o vértice i for incluído no mais curto
45
- // árvore do caminho ou distância mais curta de src para i é finalizada
44
+ // sptSet[i] will be true if vertex i is included in the shortest
45
+ // path tree or the shortest distance from src to i is finalized
46
46
Boolean sptSet [] = new Boolean [V ];
47
47
48
- // Inicializa todas as distâncias como INFINITE e stpSet [] como falso
48
+ // Initialize all distances as INFINITE and sptSet [] as false
49
49
for (int i = 0 ; i < V ; i ++) {
50
50
dist [i ] = Integer .MAX_VALUE ;
51
51
sptSet [i ] = false ;
52
52
}
53
53
54
- // A distância do vértice de origem é sempre 0
54
+ // The distance of the source vertex is always 0
55
55
dist [src ] = 0 ;
56
56
57
- // Encontre o caminho mais curto para todos os vértices
57
+ // Find the shortest path for all vertices
58
58
for (int count = 0 ; count < V - 1 ; count ++) {
59
- // Escolha o vértice de distância mínima do conjunto de vértices
60
- // ainda não processado. vc é sempre igual a src na primeira iteração .
59
+ // Pick the vertex with the minimum distance from the set of vertices
60
+ // not yet processed. u is always equal to src in the first iteration .
61
61
int u = minDistance (dist , sptSet );
62
62
63
- // Marque o vértice escolhido como processado
63
+ // Mark the chosen vertex as processed
64
64
sptSet [u ] = true ;
65
65
66
- // Atualize o valor dist dos vértices adjacentes do vértice escolhido.
66
+ // Update the value of dist for the adjacent vertices of the chosen vertex
67
67
for (int v = 0 ; v < V ; v ++)
68
68
69
- // Atualize dist [v] apenas se não estiver em sptSet, há um
70
- // borda de u a v, e peso total do caminho de src a
71
- // v a u é menor que o valor atual de dist [v]
69
+ // Update dist[v] only if it's not in sptSet, there is an edge from u to v,
70
+ // and the total weight of the path from src to v through u is less than the
71
+ // current value of dist[v]
72
72
if (!sptSet [v ]
73
73
&& graph [u ][v ] != 0
74
74
&& dist [u ] != Integer .MAX_VALUE
@@ -77,12 +77,12 @@ void dijkstra(int graph[][], int src) {
77
77
}
78
78
}
79
79
80
- // Imprime a matriz de distância construída
80
+ // Print the constructed distance matrix
81
81
printSolution (dist );
82
82
}
83
83
84
84
public static void main (String [] args ) {
85
- // Vamos criar o gráfico de exemplo discutido acima
85
+ // Let's create the example graph discussed above
86
86
int graph [][] =
87
87
new int [][] {
88
88
{0 , 4 , 0 , 0 , 0 , 0 , 0 , 8 , 0 },
0 commit comments