Test Failed
Push — master ( 492ea4...9c8472 )
by Bingo
04:47
created

TreeSingleSourcePaths::getPath()   B

Complexity

Conditions 8
Paths 5

Size

Total Lines 33
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 8
eloc 21
nc 5
nop 1
dl 0
loc 33
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
    public function __construct(GraphInterface $graph, VertexInterface $source, array $map)
49
    {
50
        $this->graph = $graph;
51
        $this->source = $source;
52
        $this->map = $map;
53
    }
54
55
    /**
56
     * Get the graph over which this set of paths is defined
57
     *
58
     * @return GraphInterface
59
     */
60
    public function getGraph(): GraphInterface
61
    {
62
        return $this->graph;
63
    }
64
65
    /**
66
     * Get the single source vertex
67
     *
68
     * @return GraphInterface
69
     */
70
    public function getSourceVertex(): VertexInterface
71
    {
72
        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
    public function getWeight(VertexInterface $targetVertex): ?float
83
    {
84
        $id = $targetVertex->getHash();
85
        if (array_key_exists($id, $this->map)) {
86
            return $this->map[$id][0];
87
        } elseif ($this->source->equals($targetVertex)) {
88
            return 0.0;
89
        }
90
        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
    public function getPath(VertexInterface $targetVertex): ?GraphPathInterface
101
    {
102
        if ($this->source->equals($targetVertex)) {
103
            return GraphWalk::singletonWalk($this->graph, $this->source, 0.0);
104
        }
105
106
        $edgeList = [];
107
108
        $cur = $targetVertex;
109
        
110
        $id = $cur->getHash();
111
        if (!array_key_exists($id, $this->map) || $this->map[$id] == INF) {
112
            return null;
113
        }
114
        $p = $this->map[$id];
115
116
        $weight = 0.0;
117
        while (!is_null($p) && !$cur->equals($this->source)) {
118
            $edge = $p[1];
119
            if (is_null($edge)) {
120
                break;
121
            }
122
            array_unshift($edgeList, $edge);
123
            $weight += $this->graph->getEdgeWeight($edge);
124
            $cur = GraphUtils::getOppositeVertex($this->graph, $edge, $cur);
125
126
            $id = $cur->getHash();
127
            if (!array_key_exists($id, $this->map)) {
128
                break;
129
            }
130
            $p = $this->map[$id];
131
        }
132
        return new GraphWalk($this->graph, $this->source, $targetVertex, null, $edgeList, $weight);
133
    }
134
}
135