You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardexpand all lines: src/go/dijkstra.go
+36-36
Original file line number
Diff line number
Diff line change
@@ -1,12 +1,12 @@
1
1
/*
2
-
* Grafos - Algoritmo de Dijkstra em Go
3
-
* Complexidade: Teta(n^2)
4
-
* Implementação utilizando matriz de adjacências (matriz de distância)
2
+
* Graphs - Dijkstra's Algorithm in Go
3
+
* Complexity: Theta(n^2)
4
+
* Implementation using adjacency matrix (distance matrix)
5
5
*
6
-
* 1 para todos - Arestas de pesos não negativo - Algoritmo guloso
7
-
* Encontra o caminho mais curto de um vértice (inicio) a outro (destino)
6
+
* One to all - Non-negative weighted edges - Greedy algorithm
7
+
* Finds the shortest path from one vertex (start) to another (destination)
8
8
*
9
-
* Grafo com 5 vértices e 6 arestas
9
+
* Graph with 5 vertices and 6 edges
10
10
*
11
11
* 6
12
12
* (0)-----------------(1)
@@ -19,18 +19,18 @@
19
19
* \ /
20
20
* -----(4)-----
21
21
*
22
-
* Matriz de Distância
22
+
* Distance Matrix
23
23
* 0 1 2 3 4
24
24
* 0 0 6 10 - -
25
25
* 1 6 0 - 2 -
26
26
* 2 10 - 0 1 3
27
27
* 3 - 2 1 0 8
28
28
* 4 - - 3 8 0
29
29
*
30
-
* O objetivo é sair do ponto inicial (0) e chegar ao destino (4) pelo caminho mais curto
31
-
* Resposta: (0)->(1)->(3)->(2)->(4) = 12
30
+
* The goal is to start from the initial point (0) and reach the destination (4) by the shortest path
31
+
* Answer: (0)->(1)->(3)->(2)->(4) = 12
32
32
*
33
-
* link Go PlayGround: https://play.golang.org/p/HyWAcYJ3qXY
33
+
* Go PlayGround link: https://play.golang.org/p/HyWAcYJ3qXY
34
34
*/
35
35
36
36
package main
@@ -39,56 +39,56 @@ import "fmt"
39
39
40
40
varnroVertices=5
41
41
42
-
typeMatriz [][]int
42
+
typeMatrix [][]int
43
43
44
44
varmaxInt=4294967295
45
45
46
-
// Algoritmo de Dijkstra recebe como parâmetro a matriz de distância e o número de vértices
47
-
funcDijkstra(matrizMatriz, nint) {
48
-
visitados:=make([]bool, n) // Variável que guarda true para os vértices visitados
49
-
// O valor 'i' do for abaixo não é utilizado, pois o for serve apenas para percorrer todo o número de colunas da matriz
50
-
fori:=1; i<n; i++ { // Começa em 1 pois não precisa comparar o vértice com ele mesmo
46
+
// Dijkstra's Algorithm takes the distance matrix and the number of vertices as parameters
47
+
funcDijkstra(matrixMatrix, nint) {
48
+
visited:=make([]bool, n) // Variable that stores true for visited vertices
49
+
// The 'i' value in the for loop below is not used, as the loop is only used to iterate over the number of columns in the matrix
50
+
fori:=1; i<n; i++ { // Starts at 1 because there is no need to compare the vertex with itself
51
51
52
-
min:=-1// Variável que guarda a posição do menor valor, inicia em -1 pois é uma posição inválida
53
-
minValor:=maxInt// Variável que guarda o menor valor encontrado, inicia com 'infinito', assim, sempre na primeira passada o valor será menor que esta variável
52
+
min:=-1// Variable that stores the position of the smallest value, starts at -1 because it is an invalid position
53
+
minValue:=maxInt// Variable that stores the smallest value found, starts with 'infinity', so in the first pass the value will be smaller than this variable
54
54
55
-
// For que percorre todas as linhas na coluna [0]
55
+
// For loop that iterates over all rows in column [0]
56
56
forj:=1; j<n; j++ {
57
-
// Se o vertice ainda não foi visitado e o valor for menor que o 'MinValor'
58
-
if!visitados[j] &&matriz[j][0] <minValor {
59
-
min=j// Guarda a posição do menor
60
-
minValor=matriz[j][0] // Guarda o menor valor
57
+
// If the vertex has not been visited yet and the value is smaller than 'minValue'
58
+
if!visited[j] &&matrix[j][0] <minValue {
59
+
min=j// Stores the position of the smallest value
60
+
minValue=matrix[j][0] // Stores the smallest value
61
61
}
62
62
}
63
63
64
-
visitados[min] =true// Marca o valor a posição do minimo como visitado
64
+
visited[min] =true// Marks the position of the minimum as visited
65
65
66
-
// For de 1 até n
66
+
// For loop from 1 to n
67
67
forj:=1; j<n; j++ {
68
-
// Se o valor da coluna [0] + o valor da coluna que está passando for menor que o valor da linha que está passando e coluna [0]
69
-
// Atualiza a primeira coluna da matriz, que será utilizado para as próximas iterações
70
-
if (matriz[min][0] +matriz[min][j]) <matriz[j][0] {
71
-
matriz[j][0] =matriz[min][0] +matriz[min][j]
68
+
// If the value of column [0] + the value of the current column is smaller than the value of the current row and column [0]
69
+
// Updates the first column of the matrix, which will be used for the next iterations
70
+
if (matrix[min][0] +matrix[min][j]) <matrix[j][0] {
71
+
matrix[j][0] =matrix[min][0] +matrix[min][j]
72
72
}
73
73
}
74
74
}
75
75
}
76
76
77
77
funcmain() {
78
-
matriz:=Matriz{
78
+
matrix:=Matrix{
79
79
{0, 6, 10, maxInt, maxInt},
80
80
{6, 0, maxInt, 2, maxInt},
81
81
{10, maxInt, 0, 1, 3},
82
82
{maxInt, 2, 1, 0, 8},
83
83
{maxInt, maxInt, 3, 8, 0},
84
84
}
85
85
86
-
Dijkstra(matriz, nroVertices)
86
+
Dijkstra(matrix, nroVertices)
87
87
88
-
fmt.Printf("Total caminho mais curto do vertice 0 ao 4: %v\n\n", matriz[4][0]) // Caminho total mais curto
88
+
fmt.Printf("Total shortest path from vertex 0 to 4: %v\n\n", matrix[4][0]) // Total shortest path
0 commit comments