TreeSingleSourcePaths::getPath()   B
last analyzed

Complexity

Conditions 8
Paths 5

Size

Total Lines 33
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 21
CRAP Score 8.006

Importance

Changes 0
Metric Value
cc 8
eloc 21
nc 5
nop 1
dl 0
loc 33
ccs 21
cts 22
cp 0.9545
crap 8.006
rs 8.4444
c 0
b 0
f 0
1
<?php
2
3
namespace Graphp\Alg\Shortestpath;
4
5
use Graphp\GraphInterface;
6
use Graphp\GraphPathInterface;
7
use Graphp\GraphUtils;
8
use Graphp\Vertex\VertexInterface;
9
use Graphp\Path\GraphWalk;
10
use Graphp\Alg\Interfaces\SingleSourcePathsInterface;
11
12
/**
13
 * Class TreeSingleSourcePaths
14
 *
15
 * @package Graphp\Alg\Shortestpath
16
 */
17
class TreeSingleSourcePaths implements SingleSourcePathsInterface
18
{
19
    /**
20
     * The graph
21
     *
22
     * @var GraphInterface
23
     */
24
    protected $graph;
25
26
    /**
27
     * The source vertex
28
     *
29
     * @var VertexInterface
30
     */
31
    protected $source;
32
33
    /**
34
     * A map which keeps for each target vertex the predecessor edge and the total length of the
35
     * shortest path
36
     *
37
     * @var array
38
     */
39
    protected $map;
40
41
    /**
42
     * Construct a new instance
43
     *
44
     * @param GraphInterface $graph - the graph
45
     * @param VertexInterface $source - the source vertex
46
     * @param array $map - the map
47
     */
48 6
    public function __construct(GraphInterface $graph, VertexInterface $source, array $map)
49
    {
50 6
        $this->graph = $graph;
51 6
        $this->source = $source;
52 6
        $this->map = $map;
53 6
    }
54
55
    /**
56
     * Get the graph over which this set of paths is defined
57
     *
58
     * @return GraphInterface
59
     */
60 1
    public function getGraph(): GraphInterface
61
    {
62 1
        return $this->graph;
63
    }
64
65
    /**
66
     * Get the single source vertex
67
     *
68
     * @return GraphInterface
69
     */
70 2
    public function getSourceVertex(): VertexInterface
71
    {
72 2
        return $this->source;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->source returns the type Graphp\Vertex\VertexInterface which is incompatible with the documented return type Graphp\GraphInterface.
Loading history...
73
    }
74
75
    /**
76
     * Get the weight of the path from the source vertex to the target vertex
77
     *
78
     * @param VertexInterface $targetVertex - the target vertex
79
     *
80
     * @return float
81
     */
82 2
    public function getWeight(VertexInterface $targetVertex): ?float
83
    {
84 2
        $id = $targetVertex->getHash();
85 2
        if (array_key_exists($id, $this->map)) {
86 2
            return $this->map[$id][0];
87 2
        } elseif ($this->source->equals($targetVertex)) {
88 1
            return 0.0;
89
        }
90 1
        return INF;
91
    }
92
93
    /**
94
     * Get the path from the source vertex to the target vertex
95
     *
96
     * @param VertexInterface $targetVertex - the target vertex
97
     *
98
     * @return null|GraphPathInterface
99
     */
100 6
    public function getPath(VertexInterface $targetVertex): ?GraphPathInterface
101
    {
102 6
        if ($this->source->equals($targetVertex)) {
103 2
            return GraphWalk::singletonWalk($this->graph, $this->source, 0.0);
104
        }
105
106 6
        $edgeList = [];
107
108 6
        $cur = $targetVertex;
109
        
110 6
        $id = $cur->getHash();
111 6
        if (!array_key_exists($id, $this->map) || $this->map[$id] == INF) {
112 3
            return null;
113
        }
114 6
        $p = $this->map[$id];
115
116 6
        $weight = 0.0;
117 6
        while (!is_null($p) && !$cur->equals($this->source)) {
118 6
            $edge = $p[1];
119 6
            if (is_null($edge)) {
120
                break;
121
            }
122 6
            array_unshift($edgeList, $edge);
123 6
            $weight += $this->graph->getEdgeWeight($edge);
124 6
            $cur = GraphUtils::getOppositeVertex($this->graph, $edge, $cur);
125
126 6
            $id = $cur->getHash();
127 6
            if (!array_key_exists($id, $this->map)) {
128 1
                break;
129
            }
130 6
            $p = $this->map[$id];
131
        }
132 6
        return new GraphWalk($this->graph, $this->source, $targetVertex, null, $edgeList, $weight);
133
    }
134
}
135