petgraph_examples/
lib.rs

1//! Example petgraph graphs
2extern crate petgraph;
3use petgraph::graph::Graph;
4
5pub fn singleton() -> Graph<String, String, petgraph::Undirected> {
6    let mut g = Graph::new_undirected();
7    g.add_node("only value".to_string());
8    g
9}
10
11pub fn list() -> Graph<String, String, petgraph::Undirected> {
12    let mut g = Graph::new_undirected();
13    let item1 = g.add_node("a".to_string());
14    let item2 = g.add_node("b".to_string());
15    let item3 = g.add_node("c".to_string());
16    g.add_edge(item1, item2, "".to_string());
17    g.add_edge(item2, item3, "".to_string());
18    g
19}
20
21pub fn table() -> Graph<String, String, petgraph::Undirected> {
22    let mut g = Graph::new_undirected();
23    let cellA1 = g.add_node("A1".to_string());
24    let cellA2 = g.add_node("A2".to_string());
25    let cellA3 = g.add_node("A3".to_string());
26
27    let cellB1 = g.add_node("B1".to_string());
28    let cellB2 = g.add_node("B2".to_string());
29    let cellB3 = g.add_node("B3".to_string());
30
31    let cellC1 = g.add_node("C1".to_string());
32    let cellC2 = g.add_node("C2".to_string());
33    let cellC3 = g.add_node("C3".to_string());
34
35    // Columns
36    g.add_edge(cellA1, cellA2, "".to_string());
37    g.add_edge(cellA2, cellA3, "".to_string());
38
39    g.add_edge(cellB1, cellB2, "".to_string());
40    g.add_edge(cellB2, cellB3, "".to_string());
41
42    g.add_edge(cellC1, cellC2, "".to_string());
43    g.add_edge(cellC2, cellC3, "".to_string());
44
45    // Rows
46    g.add_edge(cellA1, cellB1, "".to_string());
47    g.add_edge(cellB1, cellC1, "".to_string());
48
49    g.add_edge(cellA2, cellB2, "".to_string());
50    g.add_edge(cellB2, cellC2, "".to_string());
51
52    g.add_edge(cellA3, cellB3, "".to_string());
53    g.add_edge(cellB3, cellC3, "".to_string());
54    g
55}
56
57pub fn tree() -> Graph<String, String, petgraph::Directed> {
58    let mut g = Graph::new();
59    let tree_item1 = g.add_node("a".to_string());
60    let tree_item2 = g.add_node("b".to_string());
61    let tree_item3 = g.add_node("c".to_string());
62    let tree_item4 = g.add_node("d".to_string());
63    let tree_item5 = g.add_node("e".to_string());
64    g.add_edge(tree_item1, tree_item2, "".to_string());
65    g.add_edge(tree_item1, tree_item3, "".to_string());
66    g.add_edge(tree_item2, tree_item4, "".to_string());
67    g.add_edge(tree_item2, tree_item5, "".to_string());
68    g
69}
70
71pub fn dag() -> Graph<String, String, petgraph::Directed> {
72    let mut g = Graph::new();
73    let dag_item1 = g.add_node("a".to_string());
74    let dag_item2 = g.add_node("b".to_string());
75    let dag_item3 = g.add_node("c".to_string());
76    let dag_item4 = g.add_node("d".to_string());
77    let dag_item5 = g.add_node("e".to_string());
78    let dag_item6 = g.add_node("f".to_string());
79    g.add_edge(dag_item1, dag_item2, "".to_string());
80    g.add_edge(dag_item1, dag_item3, "".to_string());
81    g.add_edge(dag_item2, dag_item4, "".to_string());
82    g.add_edge(dag_item2, dag_item5, "".to_string());
83    g.add_edge(dag_item4, dag_item6, "".to_string());
84    g.add_edge(dag_item5, dag_item6, "".to_string());
85    g
86}
87
88pub fn directed_graph_with_cycle() -> Graph<String, String, petgraph::Directed> {
89    let mut g = Graph::new();
90    let gwc_item1 = g.add_node("a".to_string());
91    let gwc_item2 = g.add_node("b".to_string());
92    let gwc_item3 = g.add_node("c".to_string());
93    let gwc_item4 = g.add_node("d".to_string());
94    let gwc_item5 = g.add_node("e".to_string());
95    let gwc_item6 = g.add_node("f".to_string());
96    g.add_edge(gwc_item1, gwc_item2, "".to_string());
97    g.add_edge(gwc_item1, gwc_item3, "".to_string());
98    g.add_edge(gwc_item2, gwc_item4, "".to_string());
99    g.add_edge(gwc_item2, gwc_item5, "".to_string());
100    g.add_edge(gwc_item4, gwc_item6, "".to_string());
101    g.add_edge(gwc_item5, gwc_item6, "".to_string());
102    g.add_edge(gwc_item6, gwc_item1, "".to_string());
103    g
104}
105
106pub fn directed_graph_with_loop() -> Graph<String, String, petgraph::Directed> {
107    let mut g = Graph::new();
108    let gwc_item1 = g.add_node("a".to_string());
109    let gwc_item2 = g.add_node("b".to_string());
110    let gwc_item3 = g.add_node("c".to_string());
111    let gwc_item4 = g.add_node("d".to_string());
112    let gwc_item5 = g.add_node("e".to_string());
113    let gwc_item6 = g.add_node("f".to_string());
114    g.add_edge(gwc_item1, gwc_item2, "".to_string());
115    g.add_edge(gwc_item1, gwc_item3, "".to_string());
116    g.add_edge(gwc_item2, gwc_item4, "".to_string());
117    g.add_edge(gwc_item2, gwc_item5, "".to_string());
118    g.add_edge(gwc_item4, gwc_item6, "".to_string());
119    g.add_edge(gwc_item5, gwc_item6, "".to_string());
120    g.add_edge(gwc_item6, gwc_item6, "".to_string());
121    g
122}
123
124pub fn ring() -> Graph<String, String, petgraph::Undirected> {
125    let mut g = Graph::new_undirected();
126    let ring_item1 = g.add_node("a".to_string());
127    let ring_item2 = g.add_node("b".to_string());
128    let ring_item3 = g.add_node("c".to_string());
129    let ring_item4 = g.add_node("d".to_string());
130    g.add_edge(ring_item1, ring_item2, "".to_string());
131    g.add_edge(ring_item2, ring_item3, "".to_string());
132    g.add_edge(ring_item3, ring_item4, "".to_string());
133    g.add_edge(ring_item4, ring_item1, "".to_string());
134    g
135}
136
137pub fn dict() -> Graph<String, String, petgraph::Directed> {
138    let mut g = Graph::new();
139    let core = g.add_node("dict".to_string());
140
141    let key1 = g.add_node("key 1".to_string());
142    let key2 = g.add_node("key 2".to_string());
143    let key3 = g.add_node("key 3".to_string());
144
145    let value1 = g.add_node("value 1".to_string());
146    let value2 = g.add_node("value 2".to_string());
147    let value3 = g.add_node("value 3".to_string());
148
149    g.add_edge(core, key1, "".to_string());
150    g.add_edge(core, key2, "".to_string());
151    g.add_edge(core, key3, "".to_string());
152
153    g.add_edge(key1, value1, "".to_string());
154    g.add_edge(key2, value2, "".to_string());
155    g.add_edge(key3, value3, "".to_string());
156    g
157}
158
159use std::fmt;
160
161#[derive(Debug, Clone)]
162pub struct Person {
163    name: String,
164    age: u8,
165}
166
167#[derive(Debug, Copy, Clone)]
168pub enum Relationship {
169    Friend,
170    Parent,
171    Boss,
172}
173
174impl fmt::Display for Person {
175    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
176        write!(f, "{}, {}", self.name, self.age)
177    }
178}
179
180impl fmt::Display for Relationship {
181    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
182        write!(f, "{:?}", self)
183    }
184}
185
186pub fn social() -> Graph<Person, Relationship, petgraph::Directed> {
187    let mut g = Graph::new();
188    let bob = g.add_node(Person {
189        name: "Bob".to_string(),
190        age: 37,
191    });
192    let alice = g.add_node(Person {
193        name: "Alice".to_string(),
194        age: 17,
195    });
196    g.add_edge(bob, alice, Relationship::Parent);
197
198    let lilly = g.add_node(Person {
199        name: "Lilly".to_string(),
200        age: 50,
201    });
202    g.add_edge(lilly, bob, Relationship::Boss);
203
204    let george = g.add_node(Person {
205        name: "George".to_string(),
206        age: 16,
207    });
208    g.add_edge(george, alice, Relationship::Friend);
209    g.add_edge(lilly, george, Relationship::Parent);
210
211    let fred = g.add_node(Person {
212        name: "Fred".to_string(),
213        age: 16,
214    });
215    g.add_edge(george, fred, Relationship::Friend);
216    g.add_edge(alice, fred, Relationship::Friend);
217    g
218}
219
220pub fn multi_component() -> Graph<String, String, petgraph::Undirected> {
221    let mut g = Graph::new_undirected();
222    g.add_node("component a".to_string());
223    g.add_node("component b".to_string());
224    g
225}
226
227pub fn moravia() -> Graph<String, f32, petgraph::Undirected> {
228    let mut g = Graph::new_undirected();
229    let brno = g.add_node("Brno".to_string());
230    let zdlch = g.add_node("Židlochovice".to_string());
231    let pohor = g.add_node("Pohořelice".to_string());
232    let vysko = g.add_node("Vyškov".to_string());
233    let blansk = g.add_node("Blansko".to_string());
234    let trebic = g.add_node("Třebíč".to_string());
235    let mbud = g.add_node("Mor. Buďějovice".to_string());
236    let jihl = g.add_node("Jihlava".to_string());
237    let jemn = g.add_node("Jemnice".to_string());
238    let znojmo = g.add_node("Znojmo".to_string());
239    let novmest = g.add_node("Nové Město".to_string());
240    let mtreb = g.add_node("Mor. Třebová".to_string());
241    g.add_edge(brno, trebic, 87.5);
242    g.add_edge(brno, zdlch, 21.9);
243    g.add_edge(brno, vysko, 43.1);
244    g.add_edge(brno, blansk, 26.4);
245    g.add_edge(pohor, zdlch, 11.7);
246    g.add_edge(pohor, trebic, 80.0);
247    g.add_edge(blansk, mtreb, 61.8);
248    g.add_edge(trebic, mbud, 27.3);
249    g.add_edge(mbud, znojmo, 56.6);
250    g.add_edge(brno, znojmo, 101.6);
251    g.add_edge(mbud, jemn, 39.0);
252    g.add_edge(jihl, trebic, 45.1);
253    g.add_edge(jihl, jemn, 67.3);
254    g.add_edge(jemn, znojmo, 82.6);
255    g.add_edge(pohor, znojmo, 80.8);
256    g.add_edge(novmest, jihl, 64.5);
257    g.add_edge(novmest, brno, 87.6);
258    g.add_edge(novmest, trebic, 70.9);
259    g.add_edge(novmest, blansk, 75.0);
260    g.add_edge(novmest, mtreb, 89.4);
261    g.add_edge(vysko, blansk, 37.0);
262    g.add_edge(vysko, zdlch, 56.9);
263    g
264}