Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Visualizing graph/network with 3 layeres (tripartite) in R/igraph

Tags:

r

igraph

I have a "layered" network, with 3 layers, let's say parents(P), children(C), grandchildren(G). The edges are always directed and towards a younger generation (either patent->child, child->grandchild or parent->grandchild). No edges between vertices in the same generation. The graph is represented by 3 edge lists (P_C, C_G, P_C). A short example is given bellow.

1) What is the proper term for this sort of graph/network? tripartite graph? As such, I suppose it is a particular case because of the lack of backward connections.

2) How do I represent it as a graph object in R (igraph)?

3) Can I plot the graph in a way that depicts the "layers", with all the vertices for each group (P,C,GC) aligned at the same x coordinates, going from P on the left, C in the middle and GC on the rigth ?

4) Can I check for graph isomorphisms between graphs with this structure, thaking into account the layered nature of the data. (I know for regular graphs it would be the graph.isomorphic() function).

edge_P_C <- read.table(text="P C
A B
A C", header=T)

edge_C_G <- read.table(text="C G
B D
B E
C F", header=T)

edge_P_G <- read.table(text="P G
A G", header=T)
like image 640
LucasMation Avatar asked Mar 10 '15 21:03

LucasMation


1 Answers

1. Term

I think you could say it is a tripartite graph but I am not sure if the term is used for directed graphs.

2. Create graph

To create a graph object (with igraph package) just rbind all the edges and create it with igraph.data.frame. Before binding the column names must match.

all_edges <- do.call(rbind,
  lapply( list(edge_C_G, edge_P_C, edge_P_G), function(x) setNames(x, c("1","2")) )
)

g1 <- graph.data.frame(d = all_edges, directed = TRUE)

3. Plot

You need to set the layer attribute on every vertex. As I understand, the layer is implicitly defined by input data (three tables):

v_layers_df <- unique( rbind(
  expand.grid( ID = edge_P_C$P, Layer = 1),
  expand.grid( ID = edge_P_G$P, Layer = 1),
  expand.grid( ID = edge_P_C$C, Layer = 2),
  expand.grid( ID = edge_C_G$C, Layer = 2),
  expand.grid( ID = edge_C_G$G, Layer = 3),
  expand.grid( ID = edge_P_G$G, Layer = 3)
))

v_layers <- setNames( v_layers_df$Layer, v_layers_df$ID)
V(g1)$layer <- v_layers[V(g1)$name]

With the layer attribute on the vertices you can use it in your own layout function (modified Sugiyama):

layout.k_partite <- function(g) {
  l <- layout.sugiyama(g)$layout[,2:1]
  l[,1] <- V(g1)$layer
  l[,2] <- - l[,2] + 1 + max(l[,2])
  l
}

And use it this way:

plot(g1, layout = layout.k_partite(g1))

enter image description here

4. Isomorphisms

The graph.isomorphic and other functions from igraph package should perform just fine.

like image 61
bergant Avatar answered Nov 06 '22 10:11

bergant