Skip to contents

This function is useful if you want to create a small (named) graph quickly, it works for both directed and undirected graphs.

Usage

graph_from_literal(..., simplify = TRUE)

from_literal(...)

Arguments

...

For 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

Logical, whether to call simplify() on the created graph. By default the graph is simplified, loop and multiple edges are removed. simplify() is only called when the created graph is not already simple, so the edge order from the formula is preserved whenever no loops or multi-edges are present. When the graph does contain loops or multi-edges (and simplify = TRUE), simplify() reorders the edges into its canonical order.

Value

An igraph graph

Details

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 ‘-’ and ‘+’ characters, the former is for the edges and the latter is used for arrow heads. The edges can be arbitrarily long, i.e. you may use as many ‘-’ characters to “draw” them as you like.

If all edge operators consist of only ‘-’ characters then the graph will be undirected, whereas a single ‘+’ character implies a directed graph.

Let us see some simple examples. Without arguments the function creates an empty graph:

A simple undirected graph with two vertices called ‘A’ and ‘B’ and one edge only:

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 ‘:’ operator can be used to define vertex sets. If an edge operator connects two vertex sets then every vertex from the first set will be connected to every vertex in the second set. The following form creates a full graph, including loop edges:

  graph_from_literal( A:B:C:D -- A:B:C:D )

In directed graphs, edges will be created only if the edge operator includes a arrow head (‘+’) at the end of the edge:

  graph_from_literal( A -+ B -+ C )
  graph_from_literal( A +- B -+ C )
  graph_from_literal( A +- B -- C )

Thus in the third example no edge is created between vertices 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, ‘+’, ‘++’ and ‘+-----+’ have exactly the same meaning.

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 ‘+’ and ‘-’ characters.

See more examples below.

create(), simplify(), famous(), is_simple(), vcount(), empty()

Examples

# A simple undirected graph
g <- graph_from_literal(
  Alice - Bob - Cecil - Alice,
  Daniel - Cecil - Eugene,
  Cecil - Gordon
)
g
#> ── <igraph> ───────────────────────────────────────────────────────── e1d86df ──
#>  undirected · named
#>  6 vertices · 6 edges
#> 
#> ── Attributes ──────────────────────────────────────────────────────────────────
#> → vertex: name <chr>
#> 
#> ── Edges (vertex names) ────────────────────────────────────────────────────────
#> [1] Alice ─ Bob     Bob ─ Cecil     Alice ─ Cecil   Cecil ─ Daniel 
#> [5] Cecil ─ Eugene  Cecil ─ Gordon 

# Another undirected graph, ":" notation
g2 <- graph_from_literal(Alice - Bob:Cecil:Daniel, Cecil:Daniel - Eugene:Gordon)
g2
#> ── <igraph> ───────────────────────────────────────────────────────── 35c03d4 ──
#>  undirected · named
#>  6 vertices · 7 edges
#> 
#> ── Attributes ──────────────────────────────────────────────────────────────────
#> → vertex: name <chr>
#> 
#> ── Edges (vertex names) ────────────────────────────────────────────────────────
#> [1] Alice ─ Bob      Alice ─ Cecil    Alice ─ Daniel   Cecil ─ Eugene  
#> [5] Cecil ─ Gordon   Daniel ─ Eugene  Daniel ─ Gordon 

# A directed graph
g3 <- graph_from_literal(
  Alice +-+ Bob --+ Cecil +-- Daniel,
  Eugene --+ Gordon:Helen
)
g3
#> ── <igraph> ───────────────────────────────────────────────────────── 62720f9 ──
#>  directed · named
#>  7 vertices · 6 edges
#> 
#> ── Attributes ──────────────────────────────────────────────────────────────────
#> → vertex: name <chr>
#> 
#> ── Edges (vertex names) ────────────────────────────────────────────────────────
#> [1] Bob → Alice      Alice → Bob      Bob → Cecil      Daniel → Cecil  
#> [5] Eugene → Gordon  Eugene → Helen  

# A graph with isolate vertices
g4 <- graph_from_literal(Alice -- Bob -- Daniel, Cecil:Gordon, Helen)
g4
#> ── <igraph> ───────────────────────────────────────────────────────── c4380a8 ──
#>  undirected · named
#>  6 vertices · 2 edges
#> 
#> ── Attributes ──────────────────────────────────────────────────────────────────
#> → vertex: name <chr>
#> 
#> ── Edges (vertex names) ────────────────────────────────────────────────────────
#> [1] Alice ─ Bob   Bob ─ Daniel 
V(g4)$name
#> [1] "Alice"  "Bob"    "Daniel" "Cecil"  "Gordon" "Helen" 

# "Arrows" can be arbitrarily long
g5 <- graph_from_literal(Alice +---------+ Bob)
g5
#> ── <igraph> ───────────────────────────────────────────────────────── ab25953 ──
#>  directed · named
#>  2 vertices · 2 edges
#> 
#> ── Attributes ──────────────────────────────────────────────────────────────────
#> → vertex: name <chr>
#> 
#> ── Edges (vertex names) ────────────────────────────────────────────────────────
#> [1] Bob → Alice  Alice → Bob 

# Special vertex names
g6 <- graph_from_literal("+" -- "-", "*" -- "/", "%%" -- "%/%")
g6
#> ── <igraph> ───────────────────────────────────────────────────────── 6aed570 ──
#>  undirected · named
#>  6 vertices · 3 edges
#> 
#> ── Attributes ──────────────────────────────────────────────────────────────────
#> → vertex: name <chr>
#> 
#> ── Edges (vertex names) ────────────────────────────────────────────────────────
#> [1] + ─ -     * ─ /     %% ─ %/%