Knowledge Garden

Search

Search IconIcon to open search

Graphs in Rust

Last updated Dec 1, 2022 Edit Source

# Rust Graphs

https://www.youtube.com/watch?v=3DLrUNbKhjQ

  1
  2
  3
  4
  5
  6
  7
  8
  9
 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
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
use std::collections::HashMap;

  

////

/// VId = vertex ID

/// E = Edge

/// V = Vertex

/// Adjacency = edges

pub struct Graph<VId, E = (), V = ()> {

vertices: HashMap<VId, V>,

adjacency: HashMap<VId, Vec<(VId, E)>>,

}

  
  

impl<VId, E, V> Graph<VId, E, V>

where

VId: Eq + Hash,

V: Hash,

{

pub fn new() -> Graph<VId, E, V> {

Graph { vertices: HashMap::new(), adjacency: HashMap::new() }

}

pub fn push_vertex(se;f: &mut Graph<VId, E, V>, vid: VId, vertex: V) {

self.vertices.insert(vid,vertex);

}

pub fn push_edge(self: &mut Self, from: VId, to: VId, edge: E) {

let adjacent_to_from = self.adjacency.entry(from).or_default();

adjacent_to_from.push((to, edge));

}

}

  

// pub trait Simple_Graph<VId,E,()> {

// where

// VId: Eq + Hash,

// {

// pub fn push_vid(self: &mut Self, vid: VId) {

// self.vertices.insert(vid, ());

// }

// }

// }

  

/// MAZE

  

Graph<VId = &str, E = Direction> {

vertices: {

"A": (),

"B": (),

"C": (),

"D": (),

},

adjacency: {

"A": [("B", Right)],

"B": [("E", Down)],

},

}

https://www.youtube.com/watch?v=UEAg4qCALb8 mazes

https://www.youtube.com/watch?v=q6paRBbLgNw