forked from graphp/graph
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUndirected.php
More file actions
104 lines (88 loc) · 2.4 KB
/
Undirected.php
File metadata and controls
104 lines (88 loc) · 2.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
<?php
namespace Graphp\Graph\Edge;
use Graphp\Graph\Exception\InvalidArgumentException;
use Graphp\Graph\Vertex;
use Graphp\Graph\Set\Vertices;
class Undirected extends Base
{
/**
* vertex a
*
* @var Vertex
*/
private $a;
/**
* vertex b
*
* @var Vertex
*/
private $b;
/**
* create a new undirected edge between given vertices
*
* @param Vertex $a
* @param Vertex $b
* @see Vertex::createEdge() instead
*/
public function __construct(Vertex $a, Vertex $b)
{
if ($a->getGraph() !== $b->getGraph()) {
throw new InvalidArgumentException('Vertices have to be within the same graph');
}
$this->a = $a;
$this->b = $b;
$a->getGraph()->addEdge($this);
$a->addEdge($this);
$b->addEdge($this);
}
public function getVerticesTarget()
{
return new Vertices(array($this->b, $this->a));
}
public function getVerticesStart()
{
return new Vertices(array($this->a, $this->b));
}
public function getVertices()
{
return new Vertices(array($this->a, $this->b));
}
public function isConnection(Vertex $from, Vertex $to)
{
// one way or other way
return (($this->a === $from && $this->b === $to) || ($this->b === $from && $this->a === $to));
}
public function isLoop()
{
return ($this->a === $this->b);
}
public function getVertexToFrom(Vertex $startVertex)
{
if ($this->a === $startVertex) {
return $this->b;
} elseif ($this->b === $startVertex) {
return $this->a;
} else {
throw new InvalidArgumentException('Invalid start vertex');
}
}
public function getVertexFromTo(Vertex $endVertex)
{
if ($this->a === $endVertex) {
return $this->b;
} elseif ($this->b === $endVertex) {
return $this->a;
} else {
throw new InvalidArgumentException('Invalid end vertex');
}
}
public function hasVertexStart(Vertex $startVertex)
{
return ($this->a === $startVertex || $this->b === $startVertex);
}
public function hasVertexTarget(Vertex $targetVertex)
{
// same implementation as direction does not matter
return $this->hasVertexStart($targetVertex);
}
}