Conditions | 16 |
Paths | 26 |
Total Lines | 111 |
Lines | 0 |
Ratio | 0 % |
Changes | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | <?php |
||
23 | public static function findShortestPaths(Vertex $startVertex, Vertex $endVertex) |
||
24 | { |
||
25 | $totalCostOfCheapestPathTo = []; |
||
26 | // start node distance |
||
27 | $totalCostOfCheapestPathTo[$startVertex->getId()] = 0; |
||
28 | |||
29 | $endVertexId = $endVertex->getId(); |
||
30 | |||
31 | // just to get the cheapest vertex in the correct order |
||
32 | $cheapestVertex = new SplPriorityQueue(); |
||
33 | $cheapestVertex->setExtractFlags(SplPriorityQueue::EXTR_BOTH); |
||
34 | $cheapestVertex->insert($startVertex, 0); |
||
35 | |||
36 | // predecessors |
||
37 | $predecesEdgeOfCheapestPathTo = []; |
||
38 | |||
39 | // mark vertices when their cheapest path has been found |
||
40 | $usedVertices = [$startVertex->getId() => true]; |
||
41 | |||
42 | $isFirst = true; |
||
43 | |||
44 | // Repeat until all vertices have been marked |
||
45 | $totalCountOfVertices = count($startVertex->getGraph()->getVertices()); |
||
46 | for ($i = 0; $i < $totalCountOfVertices; ++$i) { |
||
47 | $currentVertex = null; |
||
48 | $currentVertexId = null; |
||
49 | $isEmpty = false; |
||
50 | if ($isFirst) { |
||
51 | $currentVertex = $startVertex; |
||
52 | $currentCost = 0; |
||
53 | $currentVertexId = $currentVertex->getId(); |
||
54 | } else { |
||
55 | do { |
||
56 | // if the priority queue is empty there are isolated vertices, but the algorithm visited all other vertices |
||
57 | if ($cheapestVertex->isEmpty()) { |
||
58 | $isEmpty = true; |
||
59 | break; |
||
60 | } |
||
61 | // Get cheapest unmarked vertex |
||
62 | $cheapestResult = $cheapestVertex->extract(); |
||
63 | $currentVertex = $cheapestResult['data']; |
||
64 | $currentCost = $cheapestResult['priority']; |
||
65 | $currentVertexId = $currentVertex->getId(); |
||
66 | // Vertices can be in the priority queue multiple times, with different path costs (if vertex is already marked, this is an old unvalid entry) |
||
67 | } while (isset($usedVertices[$currentVertexId])); |
||
68 | } |
||
69 | |||
70 | // Check premature end condition |
||
71 | // If the end vertex is marked as done and the next lowest possible weight is bigger than end vertix, |
||
72 | // we are done processing. |
||
73 | |||
74 | |||
75 | // catch "algorithm ends" condition |
||
76 | if ($isEmpty) { |
||
77 | break; |
||
78 | } |
||
79 | |||
80 | if ($isFirst) { |
||
81 | $isFirst = false; |
||
82 | } |
||
83 | |||
84 | // mark this vertex |
||
85 | $usedVertices[$currentVertexId] = true; |
||
86 | |||
87 | if (isset($usedVertices[$endVertexId]) && $totalCostOfCheapestPathTo[$endVertexId] < -$currentCost) { |
||
88 | break; |
||
89 | } |
||
90 | |||
91 | // check for all edges of current vertex if there is a cheaper path (or IN OTHER WORDS: Add reachable nodes from currently added node and refresh the current possible distances) |
||
92 | foreach ($currentVertex->getEdgesOut() as $edge) { |
||
93 | $weight = $edge->getWeight(); |
||
94 | if ($weight < 0) { |
||
95 | throw new UnexpectedValueException('Dijkstra not supported for negative weights - Consider using MooreBellmanFord'); |
||
96 | } |
||
97 | |||
98 | $targetVertex = $edge->getVertexToFrom($currentVertex); |
||
99 | $targetVertexId = $targetVertex->getId(); |
||
100 | |||
101 | // if the targetVertex is marked, the cheapest path for this vertex has already been found (no negative edges) { |
||
102 | if (!isset($usedVertices[$targetVertexId])) { |
||
103 | // calculate new cost to vertex |
||
104 | $newCostsToTargetVertex = $totalCostOfCheapestPathTo[$currentVertexId] + $weight; |
||
105 | |||
106 | if ((!isset($predecesEdgeOfCheapestPathTo[$targetVertexId])) |
||
107 | // is the new path cheaper? |
||
108 | || $totalCostOfCheapestPathTo[$targetVertexId] > $newCostsToTargetVertex) { |
||
109 | |||
110 | // Not an update, just a new insert with lower cost |
||
111 | $cheapestVertex->insert($targetVertex, -$newCostsToTargetVertex); |
||
112 | // so the lowest cost will be extracted first |
||
113 | // and higher cost will be skipped during extraction |
||
114 | |||
115 | // update/set costs found with the new connection |
||
116 | $totalCostOfCheapestPathTo[$targetVertexId] = $newCostsToTargetVertex; |
||
117 | // update/set predecessor vertex from the new connection |
||
118 | $predecesEdgeOfCheapestPathTo[$targetVertexId] = [$edge]; |
||
119 | } elseif ($totalCostOfCheapestPathTo[$targetVertexId] == $newCostsToTargetVertex) { |
||
120 | // Same length paths. We need to add the predecessor to the list of possible predecessors. |
||
121 | $predecesEdgeOfCheapestPathTo[$targetVertexId][] = $edge; |
||
122 | } |
||
123 | } |
||
124 | } |
||
125 | } |
||
126 | |||
127 | if (!isset($totalCostOfCheapestPathTo[$endVertexId])) { |
||
128 | throw new MultiDijkstraNoPathException("No path found between vertex '".$startVertex->getId()."' and vertex '".$endVertex->getId()."'"); |
||
129 | } |
||
130 | |||
131 | // algorithm is done, return resulting edges |
||
132 | return $predecesEdgeOfCheapestPathTo; |
||
133 | } |
||
134 | |||
201 |
This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.