figure
A graph simulates a set of connections, consisting of nodes and edges. A node may be directly connected to many nodes, called neighbors.
Breadth-first search
Breadth-first search is A graph algorithm that mainly solves two kinds of problems: 1. Is there A path to node B from node A? 2. Which is the shortest route from node A to node B?
Mango seller problem
Suppose you run a mango farm and need to find a seller to sell mangoes to. The best way to find this seller is to search through your network.
[Python]
Plain text view
Copy the code
?
|
01
02
03
04
05
06
07
08
09
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
|
fromcollections importdeque
defdo_map():
""" Implementation Diagram ""
graph =dict(a)
graph["you"] =["zhangsan"."lisi"."wangwu"]
graph["zhangsan"] =["xiaoming"."xiaohong"]
graph["lisi"] =["liniu"."you"]
graph["wangwu"] =["wangniu"."mango"]
graph["xiaoming"] =[]
graph["xiaohong"] =[]
graph["liniu"] =[]
graph["wangniu"] =[]
graph["mango"] =[]
returngraph
defcheck_person(person):
""" Check if you are a seller. ""
ifperson =='mango': # Give a simple condition, as long as the name of mango is the seller
returnTrue
returnFalse
defsearch(name, graph):
""" Breadth-first Search - Vendors """
|
More learning materials to hang on hu: Gzitcast