# Install the "igraph" library unless it is already set up.
install.packages("igraph")
# Use the library.
library(igraph)
# Define the coefficients of the objective function as a matrix.
X = read.table(text = '2 3 5 6
2 1 3 5
3 8 4 6', header=FALSE)
# Size of the problem.
m = nrow(X)
n = ncol(X)
colnames(X) = paste("D", 1:n, sep="")
rownames(X) = paste("S", 1:m, sep="")
X
# Set a graph (network model), consisting of nodes and edges.
# The X matrix is part of the above solution.
# Nodes:
nodes = c(rownames(X),colnames(X))
# Since each of the S nodes is to be connected with each of the D nodes, the from and to nodes# can be generated as follows.
# Create a vector of the source nodes.
from = c('S1','S1','S1','S1','S2','S2','S2','S2','S3','S3','S3','S3')
# Create a vector of the destinations nodes.
to = c('D1','D2','D3','D4','D1','D2','D3','D4','D1','D2','D3','D4')
# Notice that pairs of from and to elements define the edges of the graph.
edges = data.frame(from, to)
# Create the graph.
transp_net = graph_from_data_frame(d = edges, vertices = nodes, directed = TRUE)
# Set X and Y coordinates for the layout (Z is needed for a 3D diagram).
# These are coordinates of a 2 by 7 grid, placing supply nodes in slots
# (0,5), (0,3) and (0,1) and the demand nodes in slots
# (1,6), (1,4), (1,2), and (1,0)
x = c(0,0,0,1,1,1,1)
y = c(5,3,1,6,4,2,0)
# Create a layout matrix.
cust_layout = as.matrix(data.frame(x,y))
# Display the graph.
plot(transp_net, layout = cust_layout)