Skip to content

Commit 1c22789

Browse files
committed
Add example for Graph with illustration
1 parent 195861a commit 1c22789

File tree

3 files changed

+38
-2
lines changed

3 files changed

+38
-2
lines changed

Makefile

+6-2
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ FEATURES =
77

88
VERSIONS = $(patsubst %,target/VERS/%,$(DOCCRATES))
99

10-
docs: mkdocs subst $(RMDOCS)
10+
docs: mkdocs mksvgs subst $(RMDOCS)
1111

1212
# https://www.gnu.org/software/make/manual/html_node/Automatic-Variables.html
1313
$(VERSIONS): Cargo.toml
@@ -30,5 +30,9 @@ $(RMDOCS): mkdocs
3030
rm -r ./doc/$@
3131
sed -i "/searchIndex\['$@'\]/d" doc/search-index.js
3232

33+
mksvgs: mkdocs graph-example.dot
34+
dot -Tsvg < ./graph-example.dot > graph-example.svg
35+
mv graph-example.svg ./doc/petgraph/graph/
3336

34-
.PHONY: docs mkdocs subst $(DOCCRATES) $(RMDOCS)
37+
38+
.PHONY: docs mkdocs mksvgs subst $(DOCCRATES) $(RMDOCS)

graph-example.dot

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
digraph {
2+
rankdir = "LR";
3+
splines = true;
4+
5+
0 [label="petgraph"]
6+
1 [label="fixedbitset"]
7+
2 [label="quickcheck"]
8+
3 [label="rand"]
9+
4 [label="libc"]
10+
0 -> 1
11+
0 -> 2
12+
2 -> 3
13+
3 -> 4
14+
2 -> 4
15+
}

src/graph.rs

+17
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,23 @@ impl<E, Ix: IndexType = DefIndex> Edge<E, Ix>
205205
///
206206
/// Based on the graph datastructure used in rustc.
207207
///
208+
/// ```
209+
/// use petgraph::Graph;
210+
///
211+
/// let mut deps = Graph::<&str, &str>::new();
212+
/// let pg = deps.add_node("petgraph");
213+
/// let fb = deps.add_node("fixedbitset");
214+
/// let qc = deps.add_node("quickcheck");
215+
/// let rand = deps.add_node("rand");
216+
/// let libc = deps.add_node("libc");
217+
/// deps.extend_with_edges(&[
218+
/// (pg, fb), (pg, qc),
219+
/// (qc, rand), (rand, libc), (qc, libc),
220+
/// ]);
221+
/// ```
222+
///
223+
/// ![graph-example](graph-example.svg)
224+
///
208225
/// ### Graph Indices
209226
///
210227
/// The graph maintains unique indices for nodes and edges, and node and edge

0 commit comments

Comments
 (0)