|
1 | 1 | /*
|
2 |
| -* Grafos - Algoritmo de Dijkstra em C |
| 2 | +* Graphs - Dijkstra Algorithm in C |
3 | 3 | * Kelvin Salton do Prado - 2015
|
4 |
| -* Complexidade: Teta(n^2) |
| 4 | +* Complexity: Theta(n^2) |
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 | +* 1 for all - Edges with non-negative weights - 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 | 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 |
| -* Para valores infinitos será considerado o valor: 4294967295 |
31 |
| -* O objetivo é sair do ponto inicial (0) e chegar ao destino (4) pelo caminho mais curto |
32 |
| -* Resposta: (0)->(1)->(3)->(2)->(4) = 12 |
| 30 | +* For infinite values, the value will be considered: 4294967295 |
| 31 | +* The objective is to leave the starting point (0) and reach the destination (4) by the shortest route |
| 32 | +* Response: (0)->(1)->(3)->(2)->(4) = 12 |
33 | 33 | *
|
34 | 34 | */
|
35 | 35 |
|
36 | 36 | #include <stdio.h>
|
37 | 37 | #include <stdbool.h>
|
38 | 38 |
|
39 |
| -#define nroVertices 5 // Define uma constante 5 que é a quantidade de vértices do grafo |
| 39 | +#define noVertices 5 // Defines a constant 5 which is the number of vertices in the graph |
40 | 40 |
|
41 |
| -// Algoritmo de Dijkstra recebe como parâmetro a matriz de distância e o número de vértices |
42 |
| -void Dijkstra(unsigned long int matriz[nroVertices][nroVertices], int n){ |
43 |
| - bool visitados[n]; // Variável que guarda true para os vértices visitados |
| 41 | +// Dijkstra's algorithm takes as parameters the distance matrix and the number of vertices |
| 42 | +void Dijkstra(unsigned long int matrix[noVertices][noVertices], int n){ |
| 43 | + |
| 44 | +bool visited[n]; // Variable that holds true for visited vertices |
44 | 45 |
|
45 |
| - // O valor 'i' do for abaixo não é utilizado, pois o for serve apenas para percorrer todo o número de colunas da matriz |
46 |
| - for(int i = 1; i < n; i++){ // Começa em 1 pois não precisa comparar o vértice com ele mesmo |
| 46 | + // The value 'i' from the for below is not used, as the for is only used to traverse the entire number of columns in the matrix |
| 47 | + for(int i = 1; i < n; i++){ // Starts at 1 because you don't need to compare the vertex with itself |
47 | 48 |
|
48 |
| - int min = -1; // Variável que guarda a posição do menor valor, inicia em -1 pois é uma posição inválida |
49 |
| - unsigned long int MinValor = 4294967295; // Variável que guarda o menor valor encontrado, inicia com 'infinito', assim, sempre na primeira passada o valor será menor que esta variável |
50 |
| - |
51 |
| - // For que percorre todas as linhas na coluna [0] |
| 49 | +int min = -1; // Variable that stores the position of the smallest value, starts at -1 as it is an invalid position |
| 50 | + unsigned long int MinValue = 4294967295; // Variable that stores the smallest value found, starts with 'infinity', so always on the first pass the value will be smaller than this variable |
| 51 | + // For that loops through all rows in column [0] |
52 | 52 | for(int j = 1; j < n; j++){
|
53 |
| - // Se o vertice ainda não foi visitado e o valor for menor que o 'MinValor' |
54 |
| - if( !visitados[j] && matriz[j][0] < MinValor ){ |
55 |
| - min = j; // Guarda a posição do menor |
56 |
| - MinValor = matriz[j][0]; // Guarda o menor valor |
| 53 | + // If the vertex has not yet been visited and the value is less than the 'MinValor' |
| 54 | + if( !visited[j] && matrix[j][0] < MinValue ){ |
| 55 | + min = j; // Saves the position of the smallest |
| 56 | + MinValue = matrix[j][0]; // Save the smallest value |
57 | 57 | }
|
58 | 58 | }
|
59 | 59 |
|
60 |
| - visitados[min] = true; // Marca o valor a posição do minimo como visitado |
| 60 | + visited[min] = true; // Mark the value of the minimum position as visited |
61 | 61 |
|
62 |
| - // For de 1 até n |
| 62 | + // Goes from 1 to n |
63 | 63 | for(int j = 1; j < n; j++){
|
64 |
| - // 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] |
65 |
| - // Atualiza a primeira coluna da matriz, que será utilizado para as próximas iterações |
66 |
| - if( (matriz[min][0] + matriz[min][j]) < matriz[j][0] ){ |
67 |
| - matriz[j][0] = matriz[min][0] + matriz[min][j]; |
| 64 | + // If the value of column [0] + the value of the passing column is less than the value of the passing row and column [0] |
| 65 | + // Update the first column of the matrix, which will be used for the next iterations |
| 66 | + if( (matrix[min][0] + matrix[min][j]) < matrix[j][0] ){ |
| 67 | + matrix[j][0] = matrix[min][0] + matrix[min][j]; |
68 | 68 | }
|
69 | 69 | }
|
70 | 70 | }
|
71 | 71 | }
|
72 | 72 |
|
73 | 73 | int main(){
|
74 | 74 |
|
75 |
| - unsigned long int Matriz[nroVertices][nroVertices] = {{ 0, 6, 10, 4294967295, 4294967295 }, |
| 75 | + unsigned long int Matrix[noVertices][noVertices] = {{ 0, 6, 10, 4294967295, 4294967295 }, |
76 | 76 | { 6, 0, 4294967295, 2, 4294967295 },
|
77 | 77 | { 10, 4294967295, 0, 1, 3 },
|
78 | 78 | { 4294967295, 2, 1, 0, 8 },
|
79 | 79 | { 4294967295, 4294967295, 3, 8, 0 }};
|
80 | 80 |
|
81 |
| - Dijkstra(Matriz, nroVertices); |
| 81 | + Dijkstra(Matrix, noVertices); |
82 | 82 |
|
83 |
| - printf("Total caminho mais curto do vertice 0 ao 4: %lu\n", Matriz[4][0]); // Caminho total mais curto |
| 83 | + printf("Total shortest path from vertex 0 to 4: %lu\n", Matrix[4][0]); // Shortest total path |
84 | 84 |
|
85 |
| - // Da print na matriz com os valores atualizados |
86 |
| - printf("Matriz:\n"); |
87 |
| - for (int i = 0; i < nroVertices; ++i){ |
88 |
| - for (int e = 0; e < nroVertices; ++e){ |
89 |
| - if( Matriz[i][e] < 10 ) |
90 |
| - printf(" %lu ", Matriz[i][e]); |
| 85 | + // Print the matrix with the updated values |
| 86 | + printf("Matrix:\n"); |
| 87 | + for (int i = 0; i < noVertices; ++i){ |
| 88 | + for (int e = 0; e < noVertices; ++e){ |
| 89 | + if( Matrix[i][e] < 10 ) |
| 90 | + printf(" %lu ", Matrix[i][e]); |
91 | 91 | else
|
92 |
| - printf("%lu ", Matriz[i][e]); |
| 92 | + printf("%lu ", Matrix[i][e]); |
93 | 93 | }
|
94 | 94 | printf("\n");
|
95 | 95 | }
|
|
0 commit comments