DijkstraShortestPath::getPath()   A
last analyzed

Complexity

Conditions 6
Paths 6

Size

Total Lines 22
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 6.1308

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 6
eloc 12
nc 6
nop 2
dl 0
loc 22
ccs 11
cts 13
cp 0.8462
crap 6.1308
rs 9.2222
c 1
b 0
f 0
1
<?php
2
3
namespace Graphp\Alg\Shortestpath;
4
5
use InvalidArgumentException;
6
use Heap\AddressableHeapInterface;
7
use Heap\Tree\PairingHeap;
8
use Graphp\GraphPathInterface;
9
use Graphp\GraphInterface;
10
use Graphp\Vertex\VertexInterface;
11
use Graphp\Util\Supplier;
12
use Graphp\Alg\Interfaces\SingleSourcePathsInterface;
13
14
/**
15
 * Class DijkstraShortestPath
16
 *
17
 * @package Graphp\Alg\Shortestpath
18
 */
19
class DijkstraShortestPath extends AbstractShortestPathAlgorithm
20
{
21
    /**
22
     * The radius
23
     *
24
     * @var float
25
     */
26
    private $radius;
27
28
    /**
29
     * The heap supplier
30
     *
31
     * @var Supplier
32
     */
33
    private $heapSupplier;
34
35
    /**
36
     * Construct a new instance of the algorithm for the given graph
37
     *
38
     * @param GraphInterface $graph - the graph
39
     * @param float $radius - the radius
40
     * @param Supplier $heapSupplier - the heap supplier
41
     */
42 5
    public function __construct(GraphInterface $graph, float $radius = null, ?Supplier $heapSupplier = null)
43
    {
44 5
        parent::__construct($graph);
45
        
46 5
        if (!is_null($radius) && $radius < 0) {
47
            throw new InvalidArgumentException("Radius must be non-negative");
48
        }
49
        
50 5
        $this->radius = $radius ?? INF;
51 5
        $this->heapSupplier = $heapSupplier ?? new Supplier(PairingHeap::class);
52 5
    }
53
54
    /**
55
     * Find path between two vertices, if it exists.
56
     *
57
     * @param GraphInterface $graph - the graph
58
     * @param VertexInterface $source - the source vertex
59
     * @param VertexInterface $sink - the target vertex
60
     *
61
     * @return null|GraphPathInterface
62
     */
63
    public static function findPathBetween(
64
        GraphInterface $graph,
65
        VertexInterface $source,
66
        VertexInterface $sink
67
    ): ?GraphPathInterface {
68
        return (new DijkstraShortestPath($graph))->getPath($source, $sink);
69
    }
70
71
    /**
72
     * Get the path from the source vertex to the target vertex
73
     *
74
     * @param VertexInterface $targetVertex - the source vertex
75
     * @param VertexInterface $sink - the target vertex
76
     *
77
     * @return null|GraphPathInterface
78
     */
79 4
    public function getPath(VertexInterface $source, VertexInterface $sink): ?GraphPathInterface
80
    {
81 4
        if (!$this->graph->containsVertex($source)) {
82
            throw new InvalidArgumentException("Graph must contain the source vertex");
83
        }
84 4
        if (!$this->graph->containsVertex($sink)) {
85
            throw new InvalidArgumentException("Graph must contain the target vertex");
86
        }
87 4
        if ($source->equals($sink)) {
88 1
            return $this->createEmptyPath($source, $sink);
89
        }
90
91 4
        $it = new DijkstraClosestFirstIterator($this->graph, $source, $this->radius, $this->heapSupplier);
92
93 4
        while ($it->hasNext()) {
94 4
            $vertex = $it->next();
95 3
            if ($vertex->equals($sink)) {
96 3
                break;
97
            }
98
        }
99
100 3
        return $it->getPaths()->getPath($sink);
101
    }
102
103
    /**
104
     * Compute all shortest paths starting from a single source vertex.
105
     *
106
     * @param VertexInterface $sourceVertex - the source vertex
107
     *
108
     * @return SingleSourcePathsInterface
109
     */
110 1
    public function getPaths(VertexInterface $source): SingleSourcePathsInterface
111
    {
112 1
        if (!$this->graph->containsVertex($source)) {
113
            throw new InvalidArgumentException("Graph must contain the source vertex");
114
        }
115
116 1
        $it = new DijkstraClosestFirstIterator($this->graph, $source, $this->radius, $this->heapSupplier);
117
118 1
        while ($it->hasNext()) {
119 1
            $it->next();
120
        }
121
122 1
        return $it->getPaths();
123
    }
124
}
125