-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbfs.php
More file actions
46 lines (42 loc) · 675 Bytes
/
bfs.php
File metadata and controls
46 lines (42 loc) · 675 Bytes
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
<?php
$adj = [
'1' => ['6',],
'2' => ['4','8',],
'3' => ['5','6',],
'4' => ['2','5','7',],
'5' => ['3','4','7',],
'6' => ['1','3',],
'7' => ['4','5','8',],
'8' => ['2','7',],
];
$discovered = [
'1' => false,
'2' => false,
'3' => false,
'4' => false,
'5' => false,
'6' => false,
'7' => false,
'8' => false,
];
$chosen = '5';
$l = [];
$l[] = [$chosen];
$discovered[$chosen] = true;
$i = 0;
$tree = [];
while (!empty($l[$i])) {
$l[$i + 1] = [];
foreach ($l[$i] as $u) {
foreach ($adj[$u] as $e) {
if ($discovered[$e] !== false) {
continue;
}
$discovered[$e] = true;
$tree[] = [$u, $e];
$l[$i+1][] = $e;
}
}
$i++;
}
print_r($tree);