AbstractShortestPathAlgorithm::createEmptyPath()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 2.0625

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 3
nc 2
nop 2
dl 0
loc 6
ccs 3
cts 4
cp 0.75
crap 2.0625
rs 10
c 1
b 0
f 0
1
<?php
2
3
namespace Graphp\Alg\Shortestpath;
4
5
use InvalidArgumentException;
6
use Graphp\GraphPathInterface;
7
use Graphp\GraphInterface;
8
use Graphp\Vertex\VertexInterface;
9
use Graphp\Path\GraphWalk;
10
use Graphp\Alg\Interfaces\ShortestPathAlgorithmInterface;
11
use Graphp\Alg\Interfaces\SingleSourcePathsInterface;
12
13
/**
14
 * Class AbstractShortestPathAlgorithm
15
 *
16
 * @package Graphp\Alg\Shortestpath
17
 */
18
abstract class AbstractShortestPathAlgorithm implements ShortestPathAlgorithmInterface
19
{
20
    /**
21
     * The graph
22
     *
23
     * @var GraphInterface
24
     */
25
    protected $graph;
26
27
    /**
28
     * Construct a new instance of the algorithm for the given graph
29
     *
30
     * @param GraphInterface $graph - the graph
31
     */
32 5
    public function __construct(GraphInterface $graph)
33
    {
34 5
        $this->graph = $graph;
35 5
    }
36
37
    /**
38
     * Compute all shortest paths starting from a single source vertex.
39
     *
40
     * @param VertexInterface $sourceVertex - the source vertex
41
     *
42
     * @return SingleSourcePathsInterface
43
     */
44
    public function getPaths(VertexInterface $source): SingleSourcePathsInterface
45
    {
46
        if (!$this->graph->containsVertex($source)) {
47
            throw new InvalidArgumentException("graph must contain the source vertex");
48
        }
49
50
        $paths = [];
51
        $vertices = $this->graph->vertexSet();
52
        foreach ($vertices as $vertex) {
53
            $paths[$vertex->getHash()] = $this->getPath($source, $vertex);
54
        }
55
        return new ListSingleSourcePaths($this->graph, $source, $paths);
56
    }
57
58
    /**
59
     * Get the path from the source vertex to the target vertex
60
     *
61
     * @param VertexInterface $source - the source vertex
62
     * @param VertexInterface $sink - the target vertex
63
     *
64
     * @return null|GraphPathInterface
65
     */
66
    abstract public function getPath(VertexInterface $source, VertexInterface $sink): ?GraphPathInterface;
67
68
    /**
69
     * Get the weight of the path from the source vertex to the target vertex.
70
     *
71
     * @param VertexInterface $sourceVertex - the source vertex
72
     * @param VertexInterface $targetVertex - the target vertex
73
     *
74
     * @return float
75
     */
76 1
    public function getPathWeight(VertexInterface $source, VertexInterface $sink): float
77
    {
78 1
        $p = $this->getPath($source, $sink);
79 1
        if (is_null($p)) {
80 1
            return INF;
81
        }
82 1
        return $p->getWeight();
83
    }
84
85
    /**
86
     * Create emppty path from the source vertex to the target vertex.
87
     *
88
     * @param VertexInterface $sourceVertex - the source vertex
89
     * @param VertexInterface $targetVertex - the target vertex
90
     *
91
     * @return null|GraphPathInterface
92
     */
93 1
    protected function createEmptyPath(VertexInterface $source, VertexInterface $sink): ?GraphPathInterface
94
    {
95 1
        if ($source->equals($sink)) {
96 1
            return GraphWalk::singletonWalk($this->graph, $source, 0.0);
97
        }
98
        return null;
99
    }
100
}
101