Skip to content

Commit bdebdb2

Browse files
committed
feat: update
1 parent a6c3d81 commit bdebdb2

File tree

1 file changed

+18
-3
lines changed

1 file changed

+18
-3
lines changed

exercises/algorithm/algorithm10.rs

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
/*
22
graph
3-
This problem requires you to implement a basic graph functio
3+
This problem requires you to implement a basic graph function
44
*/
5-
// I AM NOT DONE
65

76
use std::collections::{HashMap, HashSet};
87
use std::fmt;
@@ -30,6 +29,17 @@ impl Graph for UndirectedGraph {
3029
}
3130
fn add_edge(&mut self, edge: (&str, &str, i32)) {
3231
//TODO
32+
let (left_node, right_node, weight) = edge;
33+
34+
self.add_node(left_node);
35+
self.add_node(right_node);
36+
37+
let table = self.adjacency_table_mutable();
38+
39+
table.get_mut(left_node).unwrap().push((right_node.to_string(), weight));
40+
table.get_mut(right_node).unwrap().push((left_node.to_string(), weight));
41+
42+
3343
}
3444
}
3545
pub trait Graph {
@@ -38,7 +48,12 @@ pub trait Graph {
3848
fn adjacency_table(&self) -> &HashMap<String, Vec<(String, i32)>>;
3949
fn add_node(&mut self, node: &str) -> bool {
4050
//TODO
41-
true
51+
if self.contains(node) {
52+
false
53+
} else {
54+
self.adjacency_table_mutable().insert(node.to_string(), Vec::new());
55+
true
56+
}
4257
}
4358
fn add_edge(&mut self, edge: (&str, &str, i32)) {
4459
//TODO

0 commit comments

Comments
 (0)