1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the bisarca/graph package. |
5
|
|
|
* |
6
|
|
|
* (c) Emanuele Minotto <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Bisarca\Graph\Graph\Descriptor; |
13
|
|
|
|
14
|
|
|
use Bisarca\Graph\Edge\Set as EdgeSet; |
15
|
|
|
use Bisarca\Graph\Vertex\Set as VertexSet; |
16
|
|
|
use Bisarca\Graph\Vertex\VertexInterface; |
17
|
|
|
|
18
|
|
|
trait DegreeTrait |
19
|
|
|
{ |
20
|
|
|
/** |
21
|
|
|
* Gets the edges set. |
22
|
|
|
* |
23
|
|
|
* @return EdgeSet |
24
|
|
|
*/ |
25
|
|
|
abstract public function getEdgeSet(): EdgeSet; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* Gets the vertices set. |
29
|
|
|
* |
30
|
|
|
* @return VertexSet |
31
|
|
|
*/ |
32
|
|
|
abstract public function getVertexSet(): VertexSet; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Gets vertex degree. |
36
|
|
|
* |
37
|
|
|
* @param VertexInterface $vertex |
38
|
|
|
* |
39
|
|
|
* @return int |
40
|
|
|
*/ |
41
|
|
|
public function getVertexDegree(VertexInterface $vertex): int |
42
|
|
|
{ |
43
|
|
|
return $this->getVertexInDegree($vertex) + $this->getVertexOutDegree($vertex); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Gets vertex in degree. |
48
|
|
|
* |
49
|
|
|
* @param VertexInterface $vertex |
50
|
|
|
* |
51
|
|
|
* @return int |
52
|
|
|
*/ |
53
|
|
|
public function getVertexInDegree(VertexInterface $vertex): int |
54
|
|
|
{ |
55
|
|
|
$counter = 0; |
56
|
|
|
|
57
|
|
|
foreach ($this->getEdgeSet() as $edge) { |
58
|
|
|
$counter += $edge->hasVertexEnd() && $vertex === $edge->getVertexEnd(); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
return $counter; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* Gets vertex out degree. |
66
|
|
|
* |
67
|
|
|
* @param VertexInterface $vertex |
68
|
|
|
* |
69
|
|
|
* @return int |
70
|
|
|
*/ |
71
|
|
|
public function getVertexOutDegree(VertexInterface $vertex): int |
72
|
|
|
{ |
73
|
|
|
$counter = 0; |
74
|
|
|
|
75
|
|
|
foreach ($this->getEdgeSet() as $edge) { |
76
|
|
|
$counter += $edge->hasVertexStart() && $vertex === $edge->getVertexStart(); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
return $counter; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* Gets maximum degree. |
84
|
|
|
* |
85
|
|
|
* @return int |
86
|
|
|
*/ |
87
|
|
|
public function getMaxDegree(): int |
88
|
|
|
{ |
89
|
|
|
$maximum = 0; |
90
|
|
|
|
91
|
|
|
foreach ($this->getVertexSet() as $vertex) { |
92
|
|
|
$maximum = max($maximum, $this->getVertexDegree($vertex)); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
return $maximum; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* Gets minimum degree. |
100
|
|
|
* |
101
|
|
|
* @return int |
102
|
|
|
*/ |
103
|
|
|
public function getMinDegree(): int |
104
|
|
|
{ |
105
|
|
|
$minimum = PHP_INT_MAX; |
106
|
|
|
|
107
|
|
|
foreach ($this->getVertexSet() as $vertex) { |
108
|
|
|
$minimum = min($minimum, $this->getVertexDegree($vertex)); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
return $minimum; |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* Checks if vertex is isolated. |
116
|
|
|
* |
117
|
|
|
* @param VertexInterface $vertex |
118
|
|
|
* |
119
|
|
|
* @return bool |
120
|
|
|
*/ |
121
|
|
|
public function isIsolatedVertex(VertexInterface $vertex): bool |
122
|
|
|
{ |
123
|
|
|
return 0 === $this->getVertexDegree($vertex); |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* Checks if vertex is a leaf. |
128
|
|
|
* |
129
|
|
|
* @param VertexInterface $vertex |
130
|
|
|
* |
131
|
|
|
* @return bool |
132
|
|
|
*/ |
133
|
|
|
public function isLeafVertex(VertexInterface $vertex): bool |
134
|
|
|
{ |
135
|
|
|
return 1 === $this->getVertexDegree($vertex); |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
/** |
139
|
|
|
* Checks if vertex is dominating. |
140
|
|
|
* |
141
|
|
|
* @param VertexInterface $vertex |
142
|
|
|
* |
143
|
|
|
* @return bool |
144
|
|
|
*/ |
145
|
|
|
public function isDominatingVertex(VertexInterface $vertex): bool |
146
|
|
|
{ |
147
|
|
|
return (count($this->getVertexSet()) - 1) === $this->getVertexDegree($vertex); |
148
|
|
|
} |
149
|
|
|
} |
150
|
|
|
|