forked from m9rco/algorithm-php
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBFSQuery.php
More file actions
174 lines (161 loc) · 5.1 KB
/
BFSQuery.php
File metadata and controls
174 lines (161 loc) · 5.1 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
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
<?php
/**
* 广度优先搜索
*
* @author ShaoWei Pu <[email protected]>
* @date 2017/6/17
* @license MIT
* -------------------------------------------------------------
* 思路分析: BFS并不使用经验法则算法。从算法的观点,所有因为展开节点而得到的子节点都会被加进一个先进先出的队列中
* 时间复杂度:O(n)
* -------------------------------------------------------------
* 宽度优先搜索算法(又称广度优先搜索)是最简便的图的搜索算法之一,这一算法也是很多重要的图的算法的原型。
* Dijkstra单源最短路径算法和Prim最小生成树算法都采用了和宽度优先搜索类似的思想。
* 其别名又叫BFS,属于一种盲目搜寻法,目的是系统地展开并检查图中的所有节点,以找寻结果。
* 换句话说,它并不考虑结果的可能位置,彻底地搜索整张图,直到找到结果为止。
*/
class BFSQuery
{
/**
* @var array 关系网络
*/
protected $relationship;
/**
* @var \SplQueue 处理队列
*/
protected $queue;
/**
* @var string 搜索结果
*/
protected $target;
/**
* BFSQuery constructor.
*
* @param array $relationship
* @param string $target
*/
public function __construct(array $relationship, $target)
{
$this->relationship = $relationship;
$this->queue = new SplQueue();
$this->target = $target;
$this->generator($this->relationship);
}
/**
* 开始入列
*
* @param array $relation
* @return \Generator
*/
public function generator($relation)
{
foreach ($relation as $value) {
$this->schedule($value);
}
}
/**
* 队列入队
*
* @param $item
* @return int
*/
public function schedule($item)
{
$this->queue->enqueue($item);
}
/**
* 队列中查找符合条件
*
* @return string
*/
public function run()
{
$result = $this->target . '没有人有~!';
while (!$this->queue->isEmpty()) {
// 出队列
$item = $this->queue->dequeue();
if (!isset($item['friend'])) {
continue;
}
if (!isset($item['fruit'])) {
continue;
}
$totalFruit = count($item['fruit']);
$mark = 0;
while ($totalFruit > $mark) {
if ($item['fruit'][$mark] === $this->target) {
$result = '找到了~!';
break 2;
}
$mark++;
}
$this->generator($item['friend']);
}
return $result;
}
}
// +--------------------------------------------------------------------------
// | 方案测试
// +--------------------------------------------------------------------------
// 你现在需要一个 `mango` ,所以你需要在你的朋友圈里搜刮,你可以先从Jack 与 tom 身上找,
// 然后再从他们的朋友身上找,然后再从他们朋友的朋友哪里找
$me = array (
'jack' => array (
'fruit' => array ('apple', 'banana', 'dragon'),
'friend' => array (
'lucy' => array (
'fruit' => array ('bear', 'watermelon'),
'friend' => array (
'marco' => array (
'fruit' => array ('mango', 'cherry'), // Mango 在这儿
'friend' => array (
'...',
)
),
),
),
'bob' => array (
'fruit' => array ('orange', 'mangosteen', 'peach'),
'friend' => array (
'',
),
),
),
),
'tom' => array (
'fruit' => array (
'apple',
'banana',
),
'friend' => array (
'lucy' => array (
'fruit' => array (),
'friend' => array (
'lucy' => array (
'fruit' => array ('bear', 'watermelon'),
'friend' => array (
'marco' => array (
'fruit' => array ('mango', 'cherry'), // Mango 在这儿也有
'friend' => array (
'...',
)
),
),
),
),
),
'bob' => array (
'fruit' => array ('apple', 'peach'),
'friend' => array (
'marco' => array (
'fruit' => array ('mango', 'cherry'), // Mango 在这儿也有
'friend' => array (
'Marco 有无数多的盆友...',
)
),
)
),
),
)
);
echo (new BFSQuery($me, 'mango'))->run();