MCPcopy Create free account
hub / github.com/QMHTMY/RustBook / bfs

Function bfs

code/chapter08/bfs.rs:87–119  ·  view source on GitHub ↗
(graph: Vec<(Graph, usize)>)

Source from the content-addressed store, hash-verified

85}
86
87fn bfs(graph: Vec<(Graph, usize)>) {
88 let mut gp = graph;
89 let mut nodes = Vec::new();
90
91 gp[1].1 = 1;
92 let mut curr = gp[1].0.get_first().clone();
93
94 // 打印图
95 print!("{}->", 1);
96 while let Some(val) = curr {
97 nodes.push(val.borrow().data);
98 curr = val.borrow().next.clone();
99 }
100
101 // 打印宽度优先图
102 loop {
103 if 0 == nodes.len() {
104 break;
105 } else {
106 let data = nodes.remove(0);
107 if 0 == gp[data].1 {
108 gp[data].1 = 1;
109 print!("{data}->");
110 let mut curr = gp[data].0.get_first().clone();
111 while let Some(val) = curr {
112 nodes.push(val.borrow().data);
113 curr = val.borrow().next.clone();
114 }
115 }
116 }
117 }
118 println!("");
119}
120
121fn main() {
122 let data = [[1,2],[2,1],[1,3],[3,1],[2,4],[4,2],[2,5],[5,2],[3,6],[6,3],[3,7],[7,3],[4,5],[5,4],[6,7],[7,6],[5,8],[8,5],[6,8],[8,6]];

Callers 1

mainFunction · 0.70

Calls 4

get_firstMethod · 0.45
pushMethod · 0.45
lenMethod · 0.45
removeMethod · 0.45

Tested by

no test coverage detected