8000 feat: Add Bellman Ford explanation · JSSpagh/Algorithms-Explanation@97772c0 · GitHub
[go: up one dir, main page]

Skip to content

Commit 97772c0

Browse files
committed
feat: Add Bellman Ford explanation
1 parent eaf399f commit 97772c0

File tree

1 file changed

+105
-0
lines changed

1 file changed

+105
-0
lines changed
Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
# Bellman Ford
2+
3+
#### Declaración de problema
4+
5+
Dado un gráfico dirigido ponderado `G(V,E)` y un vértice de origen s ∈ V, determine para cada `v v v ∈ V` el trayecto más corto entre `s` y `v`.
6+
7+
#### Enfoque
8+
9+
- Inicializar la distancia de la fuente a todos los vértices como infinito.
10+
- Inicializar la distancia a sí mismo como 0.
11+
- Crear una matriz dist[] de tamaño | V| con todos los valores como infinitos excepto dist[s].
12+
- Repita los siguientes |V| - 1 vez, dónde |V| es el número de vértices.
13+
- Crear otro bucle para ir a través de cada borde `(u, v)` en E y hacer lo siguiente:
14+
1. `dist[v] = minimum(dist[v], dist[u] + peso de borde`.
15+
- Por último, iterar a través de todos los bordes en la última vez, para asegurarse de que no hay ciclos ponderados negativamente.
16+
17+
#### Complejidad horaria
18+
19+
`O(VE)`
20+
21+
#### Complejidad espacial
22+
23+
`O(V^2)`
24+
25+
#### Nombre del Fundador
26+
27+
- Richard Bellman & Lester Ford, Jr.
28+
29+
#### Ejemplo
30+
31+
```markdown
32+
# de vértices en el gráfico = 5 [A, B, C, D, E]
33+
# de bordes en gráfico = 8
34+
35+
bordes [A->B, A->C, B->C, B->D, B->E, D->C, D->B, E->D]
36+
peso [ -1, 4, 3, 2, 2, 5, 1, -4 ]
37+
fuente [ A, A, B, B, B, D, D, E ]
38+
39+
borde A->B
40+
graph->edge[0].src = A
41+
graph->edge[0].dest = B
42+
graph->edge[0].weight = -1
43+
44+
borde A->C
45+
graph->edge[1] .src = A
46+
graph->edge[1].dest = C
47+
gráfico->edge[1] .weight = 4
48+
49+
borde B->C
50+
graph->edge[2].src = B
51+
graph->edge[2].dest = C
52+
gráfico->edge[2].peso = 3
53+
54+
borde B->D
55+
gráfico->edge[3] .src = B
56+
graph->edge[3] .dest = D
57+
gráfico->edge[3] .peso = 2
58+
59+
borde B->E
60+
graph->edge[4].src = B
61+
graph->edge[4].dest = E
62+
gráfico->edge[4].peso = 2
63+
64+
borde D->C
65+
graph->edge[5].src = D
66+
graph->edge[5].dest = C
67+
gráfico->edge[5].peso = 5
68+
69+
borde D->B
70+
graph->edge[6] .src = D
71+
graph->edge[6].dest = B
72+
gráfico->edge[6].weight = 1
73+
74+
borde E->D
75+
graph->edge[7] .src = E
76+
graph->edge[7].dest = D
77+
gráfico->edge[7].weight = -3
78+
79+
para la fuente = A
80+
81+
Distancia de vértice desde la fuente
82+
A 0 A->A
83+
B -1 A->B
84+
C 2 A->B->C = -1 + 3
85+
D -2 A->B->E->D = -1 + 2 + -3
86+
E 1 A->B->E = -1 + 2
87+
```
88+
89+
#### Enlaces de implementación de código
90+
91+
- [Java](https://github.com/TheAlgorithms/Java/blob/master/DataStructures/Graphs/BellmanFord.java)
92+
- [C++](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/Dynamic%20Programming/Bellman-Ford.cpp)
93+
- [Python](https://github.com/TheAlgorithms/Python/blob/master/data_structures/graph/bellman_ford.py)
94+
- [C](https://github.com/TheAlgorithms/C/blob/master/data_structures/graphs/Bellman-Ford.c)
95+
96+
#### Explicación de vídeo
97+
98+
[Un video explicando el algoritmo Bellman Ford](https://www.youtube.com/watch?v=hxMWBBCpR6A)
99+
100+
#### Otros
101+
102+
Fuentes utilizadas:
103+
104+
- <https://www.geeksforgeeks.org/bellman-ford-algorithm-dp-23/>
105+
- <https://en.wikipedia.org/wiki/Bellman%E2%80%93Ford_algorithm>

0 commit comments

Comments
 (0)
0