# How to convert a path formula to a sequence of vertex pairs?

Reason: Consider the following code.

``````Q3 <- make_graph(~ A-B-C-D-A
, a-b-c-d-a
, A-a, B-b, C-c, D-d
)
Q3\$main="Q3 graph"

hvp <- c( "A","B", "B","b", "b","a", "a","d"
, "d","c", "c","C", "C","D", "D","A"
)
E(Q3)[ get.edge.ids(Q3, hvp)]\$color <- "red"
E(Q3)[-get.edge.ids(Q3, hvp)]\$color <- "black"
plot(Q3)
``````

The definition of the Hamiltonian path looks needlessly complicated. I would like to use the format of the make_graph() function.

``````hpf <- ~ A-B-b-a-d-c-C-D-A
hvf
``````

From the formula,`hvf`, I can create a graph using a hidden function.

``````g2 <- igraph:::graph_from_literal_i(hvp[-1])
g2
``````

But how to create (from hvf) a sequence of vertex pairs as in `hvp`?

1 Like

I am not sufficiently familiar with the R interface of igraph to give advice here, but I wanted to draw a comparison with how Mathematica does this. There is a `HighlightGraph` function which can mark one or several sets of vertices, edges or subgraphs. Visualizing any kind of subgraph (paths, cycles, communities, etc.) is a short one-liner. I wish this could be done this easily in R and Python.

Example:

Note that this did not involve a permanent modification of the graph that one might want to undo later, such as the setting the `color` attribute in the example above.

Well, technically speaking, `HighlightGraph` returns a modified graph which encodes what needs to be highlighted in a special graph attribute (not quite the same as setting the base colours). This paradigm of returning modified objects instead of mutating objects in place fits functional language, so it would be a nice way to handle visualization in R as well, if someone is up for implementing it. If we were to mimic how Mathematica’s system works, there could be a function `highlight_graph(graph, object)` where `object` may be a vertex sequence, an edge sequence, or another graph whose vertices are a subset of that of `graph`. The generalized version would take a list of objects instead of a single object, to highlight multiple things in multiple colours.

Just an idea @tamas.

Very nice idea.

However, easier to implement and nice to have, is `edgelist_from_literal`, analogous to `graph-from_literal`. That would improve the usability of the path class. For example.

`edgelist_from_literal(...) |> graph_from_edgelist() -> g`