Skip to content

Commit b97eb5c

Browse files
committed
Add References to Ford-Fulkerson
1 parent 3d329d4 commit b97eb5c

File tree

7 files changed

+51
-24
lines changed

7 files changed

+51
-24
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ Programming assignment solutions are not available in order to respect Coursera
5454
- Bellman-Ford
5555
- Floyd-Warshall
5656
- Johnson (APSP)
57+
- Ford-Fulkerson
5758

5859
To visualize some Pathfinding Algorithms check: [Pathfinding Visualizer](https://lyanghiga.github.io/pathfinding-visualizer/)
5960

data-structures/graph/README.md

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,8 @@
33
- BFS
44
- [BFS' lectures](https://www.coursera.org/learn/algorithms-graphs-data-structures/lecture/JZRXz/breadth-first-search-bfs-the-basics)
55
- [Algorithm Design 1st Edition](https://www.amazon.com/Algorithm-Design-Jon-Kleinberg/dp/0321295358)
6-
- Undirected connectivity
7-
- [BFS and Undirected Connectivity - Lecture
8-
](https://www.coursera.org/learn/algorithms-graphs-data-structures/lecture/BTVWn/bfs-and-undirected-connectivity)
9-
- [Algorithm Design 1st Edition](https://www.amazon.com/Algorithm-Design-Jon-Kleinberg/dp/0321295358)
6+
- Undirected connectivity - [BFS and Undirected Connectivity - Lecture
7+
](https://www.coursera.org/learn/algorithms-graphs-data-structures/lecture/BTVWn/bfs-and-undirected-connectivity) - [Algorithm Design 1st Edition](https://www.amazon.com/Algorithm-Design-Jon-Kleinberg/dp/0321295358)
108
- DFS
119
- [DFS' lectures](https://www.coursera.org/learn/algorithms-graphs-data-structures/lecture/pKr0Y/depth-first-search-dfs-the-basics)
1210
- [Algorithm Design 1st Edition](https://www.amazon.com/Algorithm-Design-Jon-Kleinberg/dp/0321295358)
@@ -36,3 +34,7 @@
3634
- Johnson
3735
- [Johnson's Algorithm Lectures](https://www.coursera.org/learn/algorithms-npcomplete/lecture/eT0Xt/johnsons-algorithm-i)
3836
- [Introduction to Algorithms, 3rd Edition](https://en.wikipedia.org/wiki/Introduction_to_Algorithms)
37+
- Ford-Fulkerson
38+
- [Ford-Fulkerson Algorithm Lectures](https://ocw.mit.edu/courses/electrical-engineering-and-computer-science/6-046j-design-and-analysis-of-algorithms-spring-2015/lecture-videos/lecture-13-incremental-improvement-max-flow-min-cut/)
39+
- [Introduction to Algorithms, 3rd Edition](https://en.wikipedia.org/wiki/Introduction_to_Algorithms)
40+
- [Algorithm Design 1st Edition](https://www.amazon.com/Algorithm-Design-Jon-Kleinberg/dp/0321295358)

data-structures/graph/graph.js

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -189,50 +189,61 @@ class Graph {
189189
}
190190
return min;
191191
};
192+
// Returns the initial Residual Graph (Gr)
193+
// with original edges, and residual edges with zero capacity
192194
this.createResidualGraph = () => {
193195
const rg = new Graph(true);
194196
for (let [u, vertexList] of this.list) {
195197
for (let v in vertexList) {
196-
// original edge = c(u,v) - f(u,v)
198+
// original edge = c(u,v)
197199
rg.addVertecesAndEdge(u, vertexList[v].node, vertexList[v].weight);
200+
// residual edges
198201
rg.addVertecesAndEdge(vertexList[v].node, u, 0);
199202
}
200203
}
201204
return rg;
202205
};
206+
// Update the edeges (orignal and residual) of Gr that are used in this path
207+
// using the bottleneck value
203208
this.updateResidualGraph = (g, path, bottleneck) => {
204209
// for each edge of this path
205210
for (let i = 0; i < path.length - 1; i++) {
206-
// we need to check if edge(u,v) is from the original Graph or from the Residual Graph
207-
// let isResidual = true;
211+
// nodes u,v from the edge (u,v)
208212
const u = path[i];
209213
const v = path[i + 1];
214+
// ajd list (capacity of each edge that remains) of u and v
210215
const listU = g.list.get(u);
211216
const listV = g.list.get(v);
217+
// values that will be updated
212218
let updatedEdgeUV;
213219
let updatedEdgeVU;
214-
// search for edge(u,v) in residual graph
220+
// serach for v in adj list of u
215221
for (let i = 0; i < listU.length; i++) {
216222
const t = listU[i];
217223
if (v == t.node) {
224+
// remove <bottleneck> from the remaining capacity of the edge (u,v)
218225
updatedEdgeUV = {
219226
node: t.node,
220227
weight: t.weight - bottleneck,
221228
};
229+
// updated adj list of u and set in Gr's map
222230
const updatedList = [...listU];
223231
updatedList[i] = updatedEdgeUV;
224232
g.list.set(u, updatedList);
225233
break;
226234
}
227235
}
228-
// search for edge(v,v) in residual graph
236+
// search for u in the adj list of v
229237
for (let i = 0; i < listV.length; i++) {
230238
const t = listV[i];
231239
if (u == t.node) {
240+
// add <bottleneck> from the remaining capacity of the edge (v,u)
241+
// edges (u,v) and (v,u) are complementary, residual and original
232242
updatedEdgeVU = {
233243
node: t.node,
234244
weight: t.weight + bottleneck,
235245
};
246+
// update adj list of v and set to Gr's map
236247
const updatedList = [...listV];
237248
updatedList[i] = updatedEdgeVU;
238249
g.list.set(v, updatedList);
@@ -841,23 +852,24 @@ class Graph {
841852
};
842853
// Returns the final Residual Graph
843854
this.fordFulkerson = (s, t) => {
855+
// Net flow must be a directed graph
844856
if (!this.directed)
845857
return false;
846858
const flow = new Map();
847-
// Initially f(e)=0 for all e in G
848-
// for each edge of G f(u,v) = 0
859+
// Initially f(e)=0
860+
// for each edge(u,v) of G f(e) = 0
849861
for (let [u, vertexList] of this.list) {
850862
flow.set(u, new Map());
851863
for (let v of vertexList) {
852864
flow.get(u).set(v.node, 0);
853865
}
854866
}
855867
let residualGraph = this.createResidualGraph();
856-
residualGraph.print();
857868
let { exists, path } = this.augmentingPath(s, t, residualGraph);
858869
// if there is no path between s - t return false
859870
if (!exists || !path)
860871
return false;
872+
// min val of flow that still can pass through
861873
const bottleneck = this.bottleneck(path, residualGraph);
862874
// while exists a path do augmenting path in Gr
863875
while (exists) {

data-structures/graph/graph.ts

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,8 @@ class Graph<T> {
205205
return min;
206206
};
207207

208+
// Returns the initial Residual Graph (Gr)
209+
// with original edges, and residual edges with zero capacity
208210
createResidualGraph = () => {
209211
const rg = new Graph<T>(true);
210212
for (let [u, vertexList] of this.list) {
@@ -218,39 +220,47 @@ class Graph<T> {
218220
return rg;
219221
};
220222

223+
// Update the edeges (orignal and residual) of Gr that are used in this path
224+
// using the bottleneck value
221225
updateResidualGraph = (g: Graph<T>, path: Array<T>, bottleneck: number) => {
222226
// for each edge of this path
223227
for (let i = 0; i < path.length - 1; i++) {
224-
// we need to check if edge(u,v) is from the original Graph or from the Residual Graph
225-
// let isResidual = true;
228+
// nodes u,v from the edge (u,v)
226229
const u = path[i];
227230
const v = path[i + 1];
231+
// ajd list (capacity of each edge that remains) of u and v
228232
const listU = g.list.get(u)!;
229233
const listV = g.list.get(v)!;
234+
// values that will be updated
230235
let updatedEdgeUV: Vertex<T>;
231236
let updatedEdgeVU: Vertex<T>;
232-
// search for edge(u,v) in residual graph
237+
// serach for v in adj list of u
233238
for (let i = 0; i < listU.length; i++) {
234239
const t = listU[i];
235240
if (v == t.node) {
241+
// remove <bottleneck> from the remaining capacity of the edge (u,v)
236242
updatedEdgeUV = {
237243
node: t.node,
238244
weight: t.weight - bottleneck,
239245
};
246+
// updated adj list of u and set in Gr's map
240247
const updatedList = [...listU];
241248
updatedList[i] = updatedEdgeUV;
242249
g.list.set(u, updatedList);
243250
break;
244251
}
245252
}
246-
// search for edge(v,v) in residual graph
253+
// search for u in the adj list of v
247254
for (let i = 0; i < listV.length; i++) {
248255
const t = listV[i];
249256
if (u == t.node) {
257+
// add <bottleneck> from the remaining capacity of the edge (v,u)
258+
// edges (u,v) and (v,u) are complementary, residual and original
250259
updatedEdgeVU = {
251260
node: t.node,
252261
weight: t.weight + bottleneck,
253262
};
263+
// update adj list of v and set to Gr's map
254264
const updatedList = [...listV];
255265
updatedList[i] = updatedEdgeVU;
256266
g.list.set(v, updatedList);
@@ -1052,22 +1062,22 @@ class Graph<T> {
10521062

10531063
// Returns the final Residual Graph
10541064
fordFulkerson = (s: T, t: T) => {
1065+
// Net flow must be a directed graph
10551066
if (!this.directed) return false;
10561067
const flow = new Map<T, Map<T, number>>();
1057-
// Initially f(e)=0 for all e in G
1058-
// for each edge of G f(u,v) = 0
1068+
// Initially f(e)=0
1069+
// for each edge(u,v) of G f(e) = 0
10591070
for (let [u, vertexList] of this.list) {
10601071
flow.set(u, new Map<T, number>());
10611072
for (let v of vertexList) {
10621073
flow.get(u)!.set(v.node, 0);
10631074
}
10641075
}
10651076
let residualGraph: Graph<T> = this.createResidualGraph();
1066-
residualGraph.print();
10671077
let { exists, path } = this.augmentingPath(s, t, residualGraph);
10681078
// if there is no path between s - t return false
10691079
if (!exists || !path) return false;
1070-
1080+
// min val of flow that still can pass through
10711081
const bottleneck = this.bottleneck(path, residualGraph);
10721082
// while exists a path do augmenting path in Gr
10731083
while (exists) {

greedy/README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,7 @@ I used a lot of references for each problem, in this section I will try to point
1919
- [Huffman coding Lecture](https://www.coursera.org/learn/algorithms-greedy/lecture/Jo6gK/a-greedy-algorithm)
2020
- [Algorithm Design 1st Edition](https://www.amazon.com/Algorithm-Design-Jon-Kleinberg/dp/0321295358)
2121
- [Introduction to Algorithms, 3rd Edition](https://www.amazon.com/Introduction-Algorithms-3rd-MIT-Press/dp/0262033844)
22+
- Ford-Fulkerson
23+
- [Ford-Fulkerson Algorithm Lectures](https://ocw.mit.edu/courses/electrical-engineering-and-computer-science/6-046j-design-and-analysis-of-algorithms-spring-2015/lecture-videos/lecture-13-incremental-improvement-max-flow-min-cut/)
24+
- [Introduction to Algorithms, 3rd Edition](https://en.wikipedia.org/wiki/Introduction_to_Algorithms)
25+
- [Algorithm Design 1st Edition](https://www.amazon.com/Algorithm-Design-Jon-Kleinberg/dp/0321295358)

greedy/ford-fulkerson/exe.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,7 @@ const test = (file) => {
88
const g = graph_1.default.createDirected(file, true, false);
99
const rg = g.fordFulkerson("s", "t");
1010
if (rg) {
11-
// console.log("LALALALALALALLA");
12-
// rg.print();
11+
rg.print();
1312
}
1413
};
1514
test("test.txt");

greedy/ford-fulkerson/exe.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,7 @@ const test = (file: string) => {
44
const g = Graph.createDirected(file, true, false);
55
const rg = g.fordFulkerson("s", "t");
66
if (rg) {
7-
// console.log("LALALALALALALLA");
8-
// rg.print();
7+
rg.print();
98
}
109
};
1110

0 commit comments

Comments
 (0)