-
Notifications
You must be signed in to change notification settings - Fork 143
/
Graph.php
223 lines (179 loc) · 6.16 KB
/
Graph.php
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
<?php
namespace DataStructure\Graph;
use DataStructure\Spl\SplPriorityQueue;
class Graph
{
public static function BFS(&$graph, int $start, array $visited) : \SplQueue
{
$queue = new \SplQueue;
$path = new \SplQueue;
$queue->enqueue($start);
$visited[$start] = 1;
while (!$queue->isEmpty()) {
$node = $queue->dequeue();
$path->enqueue($node);
foreach ($graph[$node] as $key => $vertex) {
if (!$visited[$key] && $vertex == 1) {
$visited[$key] = 1;
$queue->enqueue($key);
}
}
}
return $path;
}
public static function DFS(&$graph, int $start, array $visited) : \SplQueue
{
$stack = new \SplStack();
$path = new \SplQueue();
$stack->push($start);
$visited[$start] = 1;
while (!$stack->isEmpty()) {
$node = $stack->pop();
$path->enqueue($node);
foreach ($graph[$node] as $key => $vertex) {
if (!$visited[$key] && $vertex == 1) {
$visited[$key] = 1;
$stack->push($key);
}
}
}
return $path;
}
public static function topologicalSort(array $matrix): \SplQueue
{
$order = new \SplQueue();
$queue = new \SplQueue();
$size = count($matrix);
$incoming = array_fill(0, $size, 0);
//找到所有入度为0的顶点
for ($i = 0; $i < $size; $i++) {
//遍历所有到顶点$i的边
for ($j = 0; $j < $size; $j++) {
if ($matrix[$j][$i]) {
//入度加1
$incoming[$i]++;
}
}
//如果没有到$i的边
if ($incoming[$i] == 0) {
//将$i放入队列
$queue->enqueue($i);
}
}
while (!$queue->isEmpty()) {
//从队列中拿一个顶点
$node = $queue->dequeue();
for ($i = 0; $i < $size; $i++) {
//如果这个顶点到任何一个其他顶点都有边
if ($matrix[$node][$i] == 1) {
//删除这个边
$matrix[$node][$i] = 0;
//另外这个顶点入度-1
$incoming[$i]--;
//如果这个顶点入度为0加入队列
if ($incoming[$i] == 0) {
$queue->enqueue($i);
}
}
}
$order->enqueue($node);
}
if ($order->count() != $size) { //cycle detected
return new SplQueue();
}
return $order;
}
protected static function topologicalSortV2(array $matrix): \SplQueue
{
//这次使用数组存储顶点
$sorted = [];
$nodes = [];
$size = count($matrix);
//找到所有入度为0的顶点
for ($i = 0; $i < $size; $i++) {
$sum = 0;
for ($j = 0; $j < $size; $j++) {
$sum += $matrix[$j][$i];
}
if (!$sum) {
array_push($nodes, $i);
}
}
while($nodes) {
//从所有入度为0的顶点中拿一个
$node = array_shift($nodes);
//放到排序的数组中
array_push($sorted, $node);
foreach($matrix[$node] as $index => $hasEdge) {
//扫描这个这个顶点所有指向的顶点
if ($hasEdge) {
//如果有边,就删除这个边
$matrix[$node][$index] = 0;
$sum = 0;
//接着检查被删除的边指向的顶点,看是不是入度为0
for ($i = 0; $i < $size; $i++) {
$sum += $matrix[$i][$index];
}
if (!$sum) {
//如果入度为0的话就放入排序的数组
array_push($sorted, $index);
}
}
}
}
return $sorted;
}
public static function floydWarshall(array $graph): array
{
$dist = [];
$dist = $graph;
$size = count($dist);
for ($k = 0; $k < $size; $k++) {
for ($i = 0; $i < $size; $i++) {
for ($j = 0; $j < $size; $j++) {
$dist[$i][$j] = min($dist[$i][$j], $dist[$i][$k] + $dist[$k][$j]);
}
}
}
return $dist;
}
public static function Dijkstra(array $graph, string $source, string $target)
{
$dist = [];
$prev = [];
$queue = new \SplPriorityQueue();
foreach ($graph as $vertex => $weight) {
$dist[$vertex] = PHP_INT_MAX;
$prev[$vertex] = null;
$queue->insert($vertex, min($weight));
}
$dist[$source] = 0;
while (!$queue->isEmpty()) {
$current = $queue->extract();
if (!empty($graph[$current])) {
foreach($graph[$current] as $vertex => $weight) {
//如果源点到$vertx的距离大于到源点到$current的距离再加上从$current到$vertex的距离
if ($dist[$current] + $weight < $dist[$vertex]) {
//修正源点到$vertex的最短距离
$dist[$vertex] = $dist[$current] + $weight;
//设置最短距离中的上一个节点
$prev[$vertex] = $current;
}
}
}
}
$stack = new \SplStack();
$distance = 0;
while(isset($prev[$target]) && $prev[$target]) {
$stack->push($target);
$distance += $graph[$target][$prev[$target]];
$target = $prev[$target];
}
if ($stack->isEmpty()) {
return ["distance" => 0, "path" => $stack];
} else {
$stack->push($source);
return ["distance" => $distance, "path" => $stack];
}
}
}