1 | <?php |
||
2 | |||
3 | namespace Graphp\Path; |
||
4 | |||
5 | use ArrayObject; |
||
6 | use Graphp\GraphUtils; |
||
7 | use Graphp\GraphInterface; |
||
8 | use Graphp\GraphPathInterface; |
||
9 | use Graphp\Vertex\VertexInterface; |
||
10 | |||
11 | /** |
||
12 | * Class AbstractGraphPath |
||
13 | * |
||
14 | * @package Graphp\Path |
||
15 | */ |
||
16 | abstract class AbstractGraphPath implements GraphPathInterface |
||
17 | { |
||
18 | /** |
||
19 | * The graph over which the path is defined |
||
20 | * |
||
21 | * @var GraphInterface |
||
22 | */ |
||
23 | protected $graph; |
||
24 | |||
25 | /** |
||
26 | * The start vertex in the path |
||
27 | * |
||
28 | * @var VertexInterface |
||
29 | */ |
||
30 | protected $startVertex; |
||
31 | |||
32 | /** |
||
33 | * The end vertex in the path |
||
34 | * |
||
35 | * @var VertexInterface |
||
36 | */ |
||
37 | protected $endVertex; |
||
38 | |||
39 | /** |
||
40 | * The weight of the path |
||
41 | * |
||
42 | * @var float |
||
43 | */ |
||
44 | protected $weight; |
||
45 | |||
46 | /** |
||
47 | * Get the graph over which the path is defined |
||
48 | * |
||
49 | * @return GraphInterface |
||
50 | */ |
||
51 | 4 | public function getGraph(): GraphInterface |
|
52 | { |
||
53 | 4 | return $this->graph; |
|
54 | } |
||
55 | |||
56 | /** |
||
57 | * Get the start vertex in the path |
||
58 | * |
||
59 | * @return VertexInterface |
||
60 | */ |
||
61 | 6 | public function getStartVertex(): VertexInterface |
|
62 | { |
||
63 | 6 | return $this->startVertex; |
|
64 | } |
||
65 | |||
66 | /** |
||
67 | * Get the end vertex in the path |
||
68 | * |
||
69 | * @return VertexInterface |
||
70 | */ |
||
71 | 5 | public function getEndVertex(): VertexInterface |
|
72 | { |
||
73 | 5 | return $this->endVertex; |
|
74 | } |
||
75 | |||
76 | /** |
||
77 | * Get the edges making up the path |
||
78 | * |
||
79 | * @return array |
||
80 | */ |
||
81 | 2 | public function getEdgeList(): array |
|
82 | { |
||
83 | 2 | $vertexList = $this->getVertexList(); |
|
84 | 2 | if (count($vertexList) < 2) { |
|
85 | return []; |
||
86 | } |
||
87 | |||
88 | 2 | $graph = $this->getGraph(); |
|
89 | 2 | $edgeList = []; |
|
90 | |||
91 | 2 | $vertexIterator = (new ArrayObject($vertexList))->getIterator(); |
|
92 | 2 | $sourceVertex = $vertexIterator->current(); |
|
93 | 2 | $vertexIterator->next(); |
|
94 | 2 | while ($vertexIterator->valid()) { |
|
95 | 2 | $targetVertex = $vertexIterator->current(); |
|
96 | 2 | $edgeList[] = $graph->getEdge($sourceVertex, $targetVertex); |
|
97 | 2 | $sourceVertex = $targetVertex; |
|
0 ignored issues
–
show
Unused Code
introduced
by
![]() |
|||
98 | 2 | $vertexIterator->next(); |
|
99 | 2 | $sourceVertex = $targetVertex; |
|
100 | } |
||
101 | 2 | return $edgeList; |
|
102 | } |
||
103 | |||
104 | /** |
||
105 | * Get the vertices making up the path |
||
106 | * |
||
107 | * @return array |
||
108 | */ |
||
109 | 1 | public function getVertexList(): array |
|
110 | { |
||
111 | 1 | $edgeList = $this->getEdgeList(); |
|
112 | |||
113 | 1 | if (empty($edgeList)) { |
|
114 | $startVertex = $this->getStartVertex(); |
||
115 | if (!is_null($startVertex) && $startVertex->equals($this->getEndVertex())) { |
||
116 | return [$startVertex]; |
||
117 | } |
||
118 | return []; |
||
119 | } |
||
120 | |||
121 | 1 | $graph = $this->getGraph(); |
|
122 | 1 | $vertexList = []; |
|
123 | 1 | $vertex = $this->getStartVertex(); |
|
124 | 1 | $vertexList[] = $vertex; |
|
125 | 1 | foreach ($edgeList as $edge) { |
|
126 | 1 | $vertex = GraphUtils::getOppositeVertex($graph, $edge, $vertex); |
|
127 | 1 | $vertexList[] = $vertex; |
|
128 | } |
||
129 | 1 | return $vertexList; |
|
130 | } |
||
131 | |||
132 | /** |
||
133 | * Get the weight of the path, which is typically the sum of the weights of the edges |
||
134 | * |
||
135 | * @return float |
||
136 | */ |
||
137 | 8 | public function getWeight(): float |
|
138 | { |
||
139 | 8 | return $this->weight; |
|
140 | } |
||
141 | |||
142 | /** |
||
143 | * Set the weight of the path |
||
144 | * |
||
145 | * @param float $weight - the weight of the path |
||
146 | */ |
||
147 | public function setWeight(float $weight): void |
||
148 | { |
||
149 | $this->weight = $weight; |
||
150 | } |
||
151 | |||
152 | /** |
||
153 | * Get the length of the path, measured in the number of edges |
||
154 | * |
||
155 | * @return int |
||
156 | */ |
||
157 | public function getLength(): int |
||
158 | { |
||
159 | return count($this->getEdgeList()); |
||
160 | } |
||
161 | } |
||
162 |