code-examples/current/algorithms/rust_algo/src/graph.rs

39 lines
751 B
Rust

use std::iter::Map;
use std::collections::HashMap;
pub struct Vertex {
id: usize,
name: String
}
pub struct Graph {
vertices: HashMap<usize, Vertex>,
edges: HashMap<usize, Vec<usize>>
}
impl Graph {
pub fn new() -> Self {
return Graph { vertices: HashMap::new(), edges: HashMap::new() }
}
pub fn add_node(&mut self, vid: usize, name: String) {
let v = Vertex { id: vid, name: name };
self.vertices.insert(vid, v);
}
pub fn connect(&mut self, i: usize, j: usize) {
let res = self.edges.get_mut(&i);
match res {
Some(l) => {
l.push(j)
},
None => {}
}
}
pub fn DFS(&self, i: usize, j: usize) {
}
}