Skip to content

Commit 4661257

Browse files
author
Michal Zarnecki
committed
Bellman-Ford code formatting
1 parent e502f58 commit 4661257

File tree

2 files changed

+8
-7
lines changed

2 files changed

+8
-7
lines changed

Graphs/BellmanFord.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
<?php
22

3-
class Edge {
3+
class Edge
4+
{
45
public $start;
56
public $end;
67
public int $weight;
@@ -36,11 +37,11 @@ function bellmanFord(array $verticesNames, array $edges, string $start, bool $ve
3637
}
3738

3839
foreach ($edges[$vertice] as $edge) {
39-
if ($vertices[$edge->end] > $vertices[$vertice] + $edge->weight ) {
40+
if ($vertices[$edge->end] > $vertices[$vertice] + $edge->weight) {
4041
if ($verbose) {
4142
echo "replace $vertice " . $vertices[$edge->end] . " with " . $vertices[$vertice] + $edge->weight . "\n ";
4243
}
43-
$vertices[$edge->end] = $vertices[$vertice] + $edge->weight;
44+
$vertices[$edge->end] = $vertices[$vertice] + $edge->weight;
4445
$change = true;
4546
}
4647
}

tests/Graphs/BellmanFordTest.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,16 +19,16 @@ public function testBellmanFord()
1919
['C', -2, 'B'],
2020
['B', 1, 'A'],
2121
];
22-
$vertices = [ 'S', 'A', 'B', 'C', 'D', 'E',];
22+
$vertices = ['S', 'A', 'B', 'C', 'D', 'E',];
2323

2424
#prepare array of edges listed by edge start to simplify Bellman-Ford updating weights of other edges
2525
$edges = [];
26-
foreach($edgesRaw as $edgeRaw) {
26+
foreach ($edgesRaw as $edgeRaw) {
2727
$edge = new Edge();
2828
$edge->start = $edgeRaw[0];
2929
$edge->end = $edgeRaw[2];
3030
$edge->weight = $edgeRaw[1];
31-
if (! isset($edges[$edgeRaw[0]])) {
31+
if (!isset($edges[$edgeRaw[0]])) {
3232
$edges[$edgeRaw[0]] = [];
3333
}
3434
$edges[$edgeRaw[0]][] = $edge;
@@ -42,7 +42,7 @@ public function testBellmanFord()
4242
'B' => 5,
4343
'C' => 7,
4444
'D' => 9,
45-
'E'=> 8
45+
'E' => 8
4646
]);
4747
}
4848
}

0 commit comments

Comments
 (0)