library(igraph)
## 
## Attaching package: 'igraph'
## 
## The following objects are masked from 'package:stats':
## 
##     decompose, spectrum
## 
## The following object is masked from 'package:base':
## 
##     union
# undirected graph
edges = c(1,2, 1,5, 2,3, 2,4, 3,4, 3,5, 3,6)
ug = graph(edges, directed=FALSE)
coords = layout.fruchterman.reingold(ug)
plot(ug, layout=coords, vertex.size = 20, edge.width = 3, edge.color = "black") 

# multigraph
edges = c(1,2, 1,5, 1,5, 1,5, 2,3, 2,3, 2,4, 3,4, 3,5, 3,6, 6,6)
mug = graph(edges, directed=FALSE)
coords = layout.fruchterman.reingold(mug)
plot(mug, layout=coords, vertex.size = 20, edge.width = 3, edge.color = "black") 

# directed graph
edges = c(1,3, 1,4, 2,5, 3,2, 4,4, 4,5, 5,3, 5,6, 5,4)
dg = graph(edges, directed=TRUE)
coords = layout.fruchterman.reingold(dg)
plot(dg, layout=coords, vertex.size = 20, edge.width = 3, edge.color = "black") 

# weighted graph
edges = c(1,2, 1,5, 2,3, 2,4, 3,4, 3,5, 3,6)
wg = graph(edges, directed=FALSE)
E(wg)$weight = c(-1, 0.5, 2, 2.8, -4, 5, 6.2)
coords = layout.fruchterman.reingold(wg)
plot(wg, layout=coords, vertex.size = 20, edge.width = 1, 
     edge.color = "grey", edge.label = E(wg)$weight, edge.label.cex = 1.5) 

# tree
edges = c(1,2, 1,5, 2,3, 2,4, 3,6)
tree = graph(edges, directed=FALSE)
coords = layout.fruchterman.reingold(tree)
plot(tree, layout=coords, vertex.size = 20, edge.width = 3, edge.color = "black") 

# DAG
edges = c(1,2, 1,5, 2,3, 2,4, 3,6, 3,4, 1,3)
dag = graph(edges, directed=TRUE)
plot(dag, layout=coords, vertex.size = 25, edge.width = 3, edge.color = "black", edge.arrow.size = 0.7) 

# projections of a directed graph
edges = c(1,3, 1,4, 2,5, 3,2, 4,4, 4,5, 5,3, 5,6, 5,4)
dg = graph(edges, directed=TRUE)
A = get.adjacency(dg, sparse=FALSE)
P = A %*% t(A)
Q = t(A) %*% A
p = graph.adjacency(P, mode="undirected", weighted="weight")
q = graph.adjacency(Q, mode="undirected", weighted="weight")

coords1 = layout.fruchterman.reingold(dg)
plot(dg, layout=coords1, vertex.size = 25, edge.width = 3, edge.color = "black", edge.arrow.size = 1) 

coords2 = layout.fruchterman.reingold(p)
plot(p, layout=coords2, vertex.size = 25, edge.width = 1, edge.color = "grey", edge.label = E(p)$weight) 

coords3 = layout.fruchterman.reingold(q)
plot(q, layout=coords3, vertex.size = 25, edge.width = 1, edge.color = "grey", edge.label = E(q)$weight) 

# bipartite graph and its projections
g = graph.bipartite(c(rep(TRUE,7), rep(FALSE,4)), c(8,1, 8,2, 8,3, 9,2, 9,3, 9,4, 9,5, 10,4, 10,6, 11,5, 11,6, 11,7), directed=FALSE)
lay = layout.bipartite(g)
#plot(g, layout=lay, vertex.color=c("blue","red")[V(g)$type+1])
plot(g, layout=lay[,2:1], vertex.color=c("skyblue","green")[V(g)$type+1], vertex.size = 20)

proj = bipartite.projection(g)
coords1 = layout.fruchterman.reingold(proj$proj1)
plot(proj$proj1, layout=coords1, vertex.size = 20, vertex.label = c(8,9,10,11), edge.width = 3, edge.color = "black", edge.arrow.size = 1, vertex.color="skyblue") 

coords2 = layout.fruchterman.reingold(proj$proj2)
plot(proj$proj2, layout=coords2, vertex.size = 20, edge.width = 3, edge.color = "black", edge.arrow.size = 1, vertex.color="green") 

# incidence matrix
B = get.incidence(g)
B
##    1 2 3 4 5 6 7
## 8  1 1 1 0 0 0 0
## 9  0 1 1 1 1 0 0
## 10 0 0 0 1 0 1 0
## 11 0 0 0 0 1 1 1
# adjacency matrix
A = get.adjacency(g, sparse=FALSE)
A
##       [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [,11]
##  [1,]    0    0    0    0    0    0    0    1    0     0     0
##  [2,]    0    0    0    0    0    0    0    1    1     0     0
##  [3,]    0    0    0    0    0    0    0    1    1     0     0
##  [4,]    0    0    0    0    0    0    0    0    1     1     0
##  [5,]    0    0    0    0    0    0    0    0    1     0     1
##  [6,]    0    0    0    0    0    0    0    0    0     1     1
##  [7,]    0    0    0    0    0    0    0    0    0     0     1
##  [8,]    1    1    1    0    0    0    0    0    0     0     0
##  [9,]    0    1    1    1    1    0    0    0    0     0     0
## [10,]    0    0    0    1    0    1    0    0    0     0     0
## [11,]    0    0    0    0    1    1    1    0    0     0     0