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

Function dfs

code/chapter08/dfs.rs:84–123  ·  view source on GitHub ↗
(graph: Vec<(Graph, usize)>)

Source from the content-addressed store, hash-verified

82}
83
84fn dfs(graph: Vec<(Graph, usize)>) {
85 let mut gp = graph;
86 let mut nodes: Vec<usize> = Vec::new();
87 let mut temp: Vec<usize> = Vec::new();
88
89 gp[1].1 = 1;
90 let mut curr = gp[1].0.get_first().clone();
91
92 // 打印图
93 print!("{}->",1);
94 while let Some(val) = curr {
95 nodes.insert(0, val.borrow().data);
96 curr = val.borrow().next.clone();
97 }
98
99 // 打印深度优先图
100 loop{
101 if 0 == nodes.len() {
102 break;
103 }else{
104 let data = nodes.pop().unwrap();
105 if 0 == gp[data].1 {
106 gp[data].1 = 1;
107 print!("{data}->");
108
109 // 节点加入 temp
110 let mut curr = gp[data].0.get_first().clone();
111 while let Some(val) = curr {
112 temp.push(val.borrow().data);
113 curr = val.borrow().next.clone();
114 }
115
116 while !temp.is_empty(){
117 nodes.push(temp.pop().unwrap());
118 }
119 }
120 }
121 }
122 println!("");
123}
124
125fn main() {
126 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 6

get_firstMethod · 0.45
insertMethod · 0.45
lenMethod · 0.45
popMethod · 0.45
pushMethod · 0.45
is_emptyMethod · 0.45

Tested by

no test coverage detected