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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
//! Example petgraph graphs
extern crate petgraph;
use petgraph::graph::Graph;

pub fn singleton() -> Graph<String, String, petgraph::Undirected> {
    let mut g = Graph::new_undirected();
    g.add_node("only value".to_string());
    g
}

pub fn list() -> Graph<String, String, petgraph::Undirected> {
    let mut g = Graph::new_undirected();
    let item1 = g.add_node("a".to_string());
    let item2 = g.add_node("b".to_string());
    let item3 = g.add_node("c".to_string());
    g.add_edge(item1, item2, "".to_string());
    g.add_edge(item2, item3, "".to_string());
    g
}

pub fn table() -> Graph<String, String, petgraph::Undirected> {
    let mut g = Graph::new_undirected();
    let cellA1 = g.add_node("A1".to_string());
    let cellA2 = g.add_node("A2".to_string());
    let cellA3 = g.add_node("A3".to_string());

    let cellB1 = g.add_node("B1".to_string());
    let cellB2 = g.add_node("B2".to_string());
    let cellB3 = g.add_node("B3".to_string());

    let cellC1 = g.add_node("C1".to_string());
    let cellC2 = g.add_node("C2".to_string());
    let cellC3 = g.add_node("C3".to_string());

    // Columns
    g.add_edge(cellA1, cellA2, "".to_string());
    g.add_edge(cellA2, cellA3, "".to_string());

    g.add_edge(cellB1, cellB2, "".to_string());
    g.add_edge(cellB2, cellB3, "".to_string());

    g.add_edge(cellC1, cellC2, "".to_string());
    g.add_edge(cellC2, cellC3, "".to_string());

    // Rows
    g.add_edge(cellA1, cellB1, "".to_string());
    g.add_edge(cellB1, cellC1, "".to_string());

    g.add_edge(cellA2, cellB2, "".to_string());
    g.add_edge(cellB2, cellC2, "".to_string());

    g.add_edge(cellA3, cellB3, "".to_string());
    g.add_edge(cellB3, cellC3, "".to_string());
    g
}

pub fn tree() -> Graph<String, String, petgraph::Directed> {
    let mut g = Graph::new();
    let tree_item1 = g.add_node("a".to_string());
    let tree_item2 = g.add_node("b".to_string());
    let tree_item3 = g.add_node("c".to_string());
    let tree_item4 = g.add_node("d".to_string());
    let tree_item5 = g.add_node("e".to_string());
    g.add_edge(tree_item1, tree_item2, "".to_string());
    g.add_edge(tree_item1, tree_item3, "".to_string());
    g.add_edge(tree_item2, tree_item4, "".to_string());
    g.add_edge(tree_item2, tree_item5, "".to_string());
    g
}

pub fn dag() -> Graph<String, String, petgraph::Directed> {
    let mut g = Graph::new();
    let dag_item1 = g.add_node("a".to_string());
    let dag_item2 = g.add_node("b".to_string());
    let dag_item3 = g.add_node("c".to_string());
    let dag_item4 = g.add_node("d".to_string());
    let dag_item5 = g.add_node("e".to_string());
    let dag_item6 = g.add_node("f".to_string());
    g.add_edge(dag_item1, dag_item2, "".to_string());
    g.add_edge(dag_item1, dag_item3, "".to_string());
    g.add_edge(dag_item2, dag_item4, "".to_string());
    g.add_edge(dag_item2, dag_item5, "".to_string());
    g.add_edge(dag_item4, dag_item6, "".to_string());
    g.add_edge(dag_item5, dag_item6, "".to_string());
    g
}

pub fn directed_graph_with_cycle() -> Graph<String, String, petgraph::Directed> {
    let mut g = Graph::new();
    let gwc_item1 = g.add_node("a".to_string());
    let gwc_item2 = g.add_node("b".to_string());
    let gwc_item3 = g.add_node("c".to_string());
    let gwc_item4 = g.add_node("d".to_string());
    let gwc_item5 = g.add_node("e".to_string());
    let gwc_item6 = g.add_node("f".to_string());
    g.add_edge(gwc_item1, gwc_item2, "".to_string());
    g.add_edge(gwc_item1, gwc_item3, "".to_string());
    g.add_edge(gwc_item2, gwc_item4, "".to_string());
    g.add_edge(gwc_item2, gwc_item5, "".to_string());
    g.add_edge(gwc_item4, gwc_item6, "".to_string());
    g.add_edge(gwc_item5, gwc_item6, "".to_string());
    g.add_edge(gwc_item6, gwc_item1, "".to_string());
    g
}

pub fn directed_graph_with_loop() -> Graph<String, String, petgraph::Directed> {
    let mut g = Graph::new();
    let gwc_item1 = g.add_node("a".to_string());
    let gwc_item2 = g.add_node("b".to_string());
    let gwc_item3 = g.add_node("c".to_string());
    let gwc_item4 = g.add_node("d".to_string());
    let gwc_item5 = g.add_node("e".to_string());
    let gwc_item6 = g.add_node("f".to_string());
    g.add_edge(gwc_item1, gwc_item2, "".to_string());
    g.add_edge(gwc_item1, gwc_item3, "".to_string());
    g.add_edge(gwc_item2, gwc_item4, "".to_string());
    g.add_edge(gwc_item2, gwc_item5, "".to_string());
    g.add_edge(gwc_item4, gwc_item6, "".to_string());
    g.add_edge(gwc_item5, gwc_item6, "".to_string());
    g.add_edge(gwc_item6, gwc_item6, "".to_string());
    g
}

pub fn ring() -> Graph<String, String, petgraph::Undirected> {
    let mut g = Graph::new_undirected();
    let ring_item1 = g.add_node("a".to_string());
    let ring_item2 = g.add_node("b".to_string());
    let ring_item3 = g.add_node("c".to_string());
    let ring_item4 = g.add_node("d".to_string());
    g.add_edge(ring_item1, ring_item2, "".to_string());
    g.add_edge(ring_item2, ring_item3, "".to_string());
    g.add_edge(ring_item3, ring_item4, "".to_string());
    g.add_edge(ring_item4, ring_item1, "".to_string());
    g
}

pub fn dict() -> Graph<String, String, petgraph::Directed> {
    let mut g = Graph::new();
    let core = g.add_node("dict".to_string());

    let key1 = g.add_node("key 1".to_string());
    let key2 = g.add_node("key 2".to_string());
    let key3 = g.add_node("key 3".to_string());

    let value1 = g.add_node("value 1".to_string());
    let value2 = g.add_node("value 2".to_string());
    let value3 = g.add_node("value 3".to_string());

    g.add_edge(core, key1, "".to_string());
    g.add_edge(core, key2, "".to_string());
    g.add_edge(core, key3, "".to_string());

    g.add_edge(key1, value1, "".to_string());
    g.add_edge(key2, value2, "".to_string());
    g.add_edge(key3, value3, "".to_string());
    g
}

use std::fmt;

#[derive(Debug, Clone)]
pub struct Person {
    name: String,
    age: u8,
}

#[derive(Debug, Copy, Clone)]
pub enum Relationship {
    Friend,
    Parent,
    Boss,
}

impl fmt::Display for Person {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "{}, {}", self.name, self.age)
    }
}

impl fmt::Display for Relationship {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "{:?}", self)
    }
}

pub fn social() -> Graph<Person, Relationship, petgraph::Directed> {
    let mut g = Graph::new();
    let bob = g.add_node(Person {
        name: "Bob".to_string(),
        age: 37,
    });
    let alice = g.add_node(Person {
        name: "Alice".to_string(),
        age: 17,
    });
    g.add_edge(bob, alice, Relationship::Parent);

    let lilly = g.add_node(Person {
        name: "Lilly".to_string(),
        age: 50,
    });
    g.add_edge(lilly, bob, Relationship::Boss);

    let george = g.add_node(Person {
        name: "George".to_string(),
        age: 16,
    });
    g.add_edge(george, alice, Relationship::Friend);
    g.add_edge(lilly, george, Relationship::Parent);

    let fred = g.add_node(Person {
        name: "Fred".to_string(),
        age: 16,
    });
    g.add_edge(george, fred, Relationship::Friend);
    g.add_edge(alice, fred, Relationship::Friend);
    g
}

pub fn multi_component() -> Graph<String, String, petgraph::Undirected> {
    let mut g = Graph::new_undirected();
    g.add_node("component a".to_string());
    g.add_node("component b".to_string());
    g
}

pub fn moravia() -> Graph<String, f32, petgraph::Undirected> {
    let mut g = Graph::new_undirected();
    let brno = g.add_node("Brno".to_string());
    let zdlch = g.add_node("Židlochovice".to_string());
    let pohor = g.add_node("Pohořelice".to_string());
    let vysko = g.add_node("Vyškov".to_string());
    let blansk = g.add_node("Blansko".to_string());
    let trebic = g.add_node("Třebíč".to_string());
    let mbud = g.add_node("Mor. Buďějovice".to_string());
    let jihl = g.add_node("Jihlava".to_string());
    let jemn = g.add_node("Jemnice".to_string());
    let znojmo = g.add_node("Znojmo".to_string());
    let novmest = g.add_node("Nové Město".to_string());
    let mtreb = g.add_node("Mor. Třebová".to_string());
    g.add_edge(brno, trebic, 87.5);
    g.add_edge(brno, zdlch, 21.9);
    g.add_edge(brno, vysko, 43.1);
    g.add_edge(brno, blansk, 26.4);
    g.add_edge(pohor, zdlch, 11.7);
    g.add_edge(pohor, trebic, 80.0);
    g.add_edge(blansk, mtreb, 61.8);
    g.add_edge(trebic, mbud, 27.3);
    g.add_edge(mbud, znojmo, 56.6);
    g.add_edge(brno, znojmo, 101.6);
    g.add_edge(mbud, jemn, 39.0);
    g.add_edge(jihl, trebic, 45.1);
    g.add_edge(jihl, jemn, 67.3);
    g.add_edge(jemn, znojmo, 82.6);
    g.add_edge(pohor, znojmo, 80.8);
    g.add_edge(novmest, jihl, 64.5);
    g.add_edge(novmest, brno, 87.6);
    g.add_edge(novmest, trebic, 70.9);
    g.add_edge(novmest, blansk, 75.0);
    g.add_edge(novmest, mtreb, 89.4);
    g.add_edge(vysko, blansk, 37.0);
    g.add_edge(vysko, zdlch, 56.9);
    g
}