Skip to content

Commit e99fc6a

Browse files
henrik-wolfgdalle
andauthored
added short paragraph in docs on the dangers of deleting vertices (#179)
* added short paragraph on the dangers of deleting vertices * Update docs/src/first_steps/access.md --------- Co-authored-by: Guillaume Dalle <[email protected]>
1 parent 783f8be commit e99fc6a

File tree

1 file changed

+39
-0
lines changed

1 file changed

+39
-0
lines changed

docs/src/first_steps/access.md

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,3 +27,42 @@ The following is an overview of functions for accessing graph properties.
2727
- `src(e)` gives the source vertex `s` of an edge `(s, d)`.
2828
- `dst(e)` gives the destination vertex `d` of an edge `(s, d)`.
2929
- `reverse(e)` creates a new edge `(d, s)` from edge `(s, d)`.
30+
31+
## Persistence of vertex indices
32+
Adding a vertex to the graph with `add_vertex!(g)` adds it (if successful) to the end of the "vertex-list". Therefore, it is possible to access the index of the recently added vertex by using `nv(g)`:
33+
```julia-repl
34+
julia> g = SimpleGraph(10)
35+
{10, 0} undirected simple Int64 graph
36+
37+
julia> add_vertex!(g)
38+
true
39+
40+
julia> last_added_vertex = nv(g)
41+
11
42+
```
43+
Note that this index is NOT persistent if vertices added earlier are removed. When `rem_vertex!(g, v)` is called, `v` is "switched" with the last vertex before being deleted. As edges are identified by vertex indices, one has to be careful with edges as well. An edge added as `add_edge!(g, 3, 11)` can not be expected to always pass the `has_edge(g, 3, 11)` check:
44+
```julia-repl
45+
julia> g = SimpleGraph(10)
46+
{10, 0} undirected simple Int64 graph
47+
48+
julia> add_vertex!(g)
49+
true
50+
51+
julia> add_edge!(g, 3, 11)
52+
true
53+
54+
julia> g
55+
{11, 1} undirected simple Int64 graph
56+
57+
julia> has_edge(g, 3, 11)
58+
true
59+
60+
julia> rem_vertex!(g, 7)
61+
true
62+
63+
julia> has_edge(g, 3, 11)
64+
false
65+
66+
julia> has_edge(g, 3, 7) # vertex number 11 "renamed" to vertex number 7
67+
true
68+
```

0 commit comments

Comments
 (0)