graph_from_literal(..., simplify = TRUE)from_literal(...)
graph_from_literal
the formulae giving the
structure of the graph, see details below. For from_literal
all arguments are passed to graph_from_literal
.simplify
on the created graph. By default the graph is simplified, loop and
multiple edges are removed.graph_from_literal
is very handy for creating small graphs quickly.
You need to supply one or more R expressions giving the structure of
the graph. The expressions consist of vertex names and edge
operators. An edge operator is a sequence of -
+
-
If all edge operators consist of only -
+
Let us see some simple examples. Without arguments the function creates an empty graph: graph_from_literal()
A simple undirected graph with two vertices called
Remember that the length of the edges does not matter, so we could have written the following, this creates the same graph: graph_from_literal( A-----B )
If you have many disconnected components in the graph, separate them with commas. You can also give isolate vertices. graph_from_literal( A--B, C--D, E--F, G--H, I, J, K )
The :
In directed graphs, edges will be created only if the edge operator
includes a arrow head (B
and C
.
Mutual edges can be also created with a simple edge operator:
graph_from_literal( A +-+ B +---+ C ++ D + E)
Note again that the length of the edge operators is arbitrary,
+
++
+-----+
If the vertex names include spaces or other special characters then
you need to quote them:
graph_from_literal( "this is" +- "a silly" -+ "graph here" )
You can include any character in the vertex names this way, even
See more examples below.
atlas
,
graph.atlas
,
graph_from_atlas
;
chordal_ring
,
graph.extended.chordal.ring
,
make_chordal_ring
;
directed_graph
, graph
,
graph.famous
,
make_directed_graph
,
make_graph
,
make_undirected_graph
,
undirected_graph
;
empty_graph
, graph.empty
,
make_empty_graph
;
from_edgelist
,
graph.edgelist
,
graph_from_edgelist
;
full_citation_graph
,
graph.full.citation
,
make_full_citation_graph
;
full_graph
, graph.full
,
make_full_graph
;
graph.lattice
, lattice
,
make_lattice
; graph.ring
,
make_ring
, ring
;
graph.star
, make_star
,
star
; graph.tree
,
make_tree
, tree
# A simple undirected graph
g <- graph_from_literal( Alice-Bob-Cecil-Alice, Daniel-Cecil-Eugene,
Cecil-Gordon )
g
# Another undirected graph, ":" notation
g2 <- graph_from_literal( Alice-Bob:Cecil:Daniel, Cecil:Daniel-Eugene:Gordon )
g2
# A directed graph
g3 <- graph_from_literal( Alice +-+ Bob --+ Cecil +-- Daniel,
Eugene --+ Gordon:Helen )
g3
# A graph with isolate vertices
g4 <- graph_from_literal( Alice -- Bob -- Daniel, Cecil:Gordon, Helen )
g4
V(g4)$name
# "Arrows" can be arbitrarily long
g5 <- graph_from_literal( Alice +---------+ Bob )
g5
# Special vertex names
g6 <- graph_from_literal( "+" -- "-", "*" -- "/", "%%" -- "%/%" )
g6
Run the code above in your browser using DataLab