Skip to content

Commit 1acd735

Browse files
authored
Translated Portuguese comments and added English comments alongside in Dijkstra.java
1 parent 070cc66 commit 1acd735

File tree

1 file changed

+35
-4
lines changed

1 file changed

+35
-4
lines changed

src/java/Dijkstra.java

+35-4
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,29 @@
1-
// Algoritimo de Dijkstra
1+
// Algoritimo de Dijkstra (Dijkstra's Algorithm)
22
// Anderson Carneiro da Silva
33
// https://github.com/AndersonSheep
44

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+
// 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
8+
9+
/* Based on the GeekforGeeks method
10+
A Java program for Dijkstra's single-source shortest path algorithm.
11+
The program is for the representation of the graph's adjacency matrix */
12+
813
import java.io.*;
914
import java.util.*;
1015

1116
class ShortestPath {
1217
// Uma função de utilidade para encontrar o vértice com valor mínimo de distância,
1318
// do conjunto de vértices ainda não incluídos na árvore do caminho mais curto
19+
20+
/* A utility function to find the vertex with the minimum distance value,
21+
from the set of vertices not yet included in the shortest path tree */
1422
static final int V = 9;
1523

1624
int minDistance(int dist[], Boolean sptSet[]) {
1725
// Iniciando um valor minimo
26+
// Initializing a minimum value
1827
int min = Integer.MAX_VALUE, min_index = -1;
1928

2029
for (int v = 0; v < V; v++) {
@@ -28,6 +37,7 @@ int minDistance(int dist[], Boolean sptSet[]) {
2837
}
2938

3039
// Uma função de utilidade para imprimir a matriz de distância construída
40+
// A utility function to print the constructed distance matrix
3141
void printSolution(int dist[]) {
3242
System.out.println("Vertex \t\t Distance from Source");
3343
for (int i = 0; i < V; i++) {
@@ -37,38 +47,57 @@ void printSolution(int dist[]) {
3747

3848
// Função que implementa o caminho mais curto da fonte única de Dijkstra
3949
// algoritmo para um grafo representado usando matriz de adjacência
50+
51+
/* Function that implements Dijkstra's single-source shortest path algorithm
52+
for a graph represented using an adjacency matrix */
4053
void dijkstra(int graph[][], int src) {
4154
// A matriz de saída. dist [i] irá manter a menor distância de src a i
55+
// The output array. dist[i] will hold the shortest distance from src to i
4256
int dist[] = new int[V];
4357

4458
// sptSet [i] será verdadeiro se o vértice i for incluído no mais curto
4559
// árvore do caminho ou distância mais curta de src para i é finalizada
60+
61+
/* sptSet[i] will be true if vertex i is included in the shortest
62+
path tree or the shortest distance from src to i is finalized */
4663
Boolean sptSet[] = new Boolean[V];
4764

4865
// Inicializa todas as distâncias como INFINITE e stpSet [] como falso
66+
// Initialize all distances as INFINITE and sptSet[] as false
4967
for (int i = 0; i < V; i++) {
5068
dist[i] = Integer.MAX_VALUE;
5169
sptSet[i] = false;
5270
}
5371

5472
// A distância do vértice de origem é sempre 0
73+
// The distance of the source vertex is always 0
5574
dist[src] = 0;
5675

5776
// Encontre o caminho mais curto para todos os vértices
77+
// Find the shortest path for all vertices
5878
for (int count = 0; count < V - 1; count++) {
5979
// Escolha o vértice de distância mínima do conjunto de vértices
6080
// ainda não processado. vc é sempre igual a src na primeira iteração.
81+
82+
/* Pick the vertex with the minimum distance from the set of vertices
83+
not yet processed. u is always equal to src in the first iteration. */
6184
int u = minDistance(dist, sptSet);
6285

6386
// Marque o vértice escolhido como processado
87+
// Mark the chosen vertex as processed
6488
sptSet[u] = true;
6589

6690
// Atualize o valor dist dos vértices adjacentes do vértice escolhido.
91+
// Update the value dist for the adjacent vertices of the chosen vertex.
6792
for (int v = 0; v < V; v++)
6893

6994
// Atualize dist [v] apenas se não estiver em sptSet, há um
7095
// borda de u a v, e peso total do caminho de src a
7196
// v a u é menor que o valor atual de dist [v]
97+
98+
/* Update dist[v] only if it's not in sptSet, there is an edge from u to v,
99+
and the total weight of the path from src to v through u is less than the
100+
current value of dist[v] */
72101
if (!sptSet[v]
73102
&& graph[u][v] != 0
74103
&& dist[u] != Integer.MAX_VALUE
@@ -78,11 +107,13 @@ void dijkstra(int graph[][], int src) {
78107
}
79108

80109
// Imprime a matriz de distância construída
110+
// Print the constructed distance matrix
81111
printSolution(dist);
82112
}
83113

84114
public static void main(String[] args) {
85115
// Vamos criar o gráfico de exemplo discutido acima
116+
//Let's create the example graph discussed above
86117
int graph[][] =
87118
new int[][] {
88119
{0, 4, 0, 0, 0, 0, 0, 8, 0},

0 commit comments

Comments
 (0)