R Markdown

This is an R Markdown document. Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents. For more details on using R Markdown see http://rmarkdown.rstudio.com.

library(igraph)
## 
## Attaching package: 'igraph'
## The following objects are masked from 'package:stats':
## 
##     decompose, spectrum
## The following object is masked from 'package:base':
## 
##     union
# Create an undirected network graph by specifying nodes and links and list its properties
undirected_g <- graph_from_literal (1-2,1-3,1-6,2-5,2-3,3-4,4-6,4-5,6-7,7-8,8-5) 
V(undirected_g) # creates vertices (nodes, labeled by their numbers)
## + 8/8 vertices, named, from 4a7bdbf:
## [1] 1 2 3 6 5 4 7 8
E(undirected_g)
## + 11/11 edges from 4a7bdbf (vertex names):
##  [1] 1--2 1--3 1--6 2--3 2--5 3--4 6--4 6--7 5--4 5--8 7--8
print_all(undirected_g) # prints the edges and nodes
## IGRAPH 4a7bdbf UN-- 8 11 -- 
## + attr: name (v/c)
## + edges (vertex names):
## 1 -- 2, 3, 6
## 2 -- 1, 3, 5
## 3 -- 1, 2, 4
## 6 -- 1, 4, 7
## 5 -- 2, 4, 8
## 4 -- 3, 6, 5
## 7 -- 6, 8
## 8 -- 5, 7
V(undirected_g)$size= c(15,15,15,15,15,15,15,15) 
V(undirected_g)$color = c('red','goldenrod1','chartreuse','skyblue','brown','pink','yellow','purple')
plot(undirected_g)

## print adj. matrix

as_adjacency_matrix(undirected_g)
## 8 x 8 sparse Matrix of class "dgCMatrix"
##   1 2 3 6 5 4 7 8
## 1 . 1 1 1 . . . .
## 2 1 . 1 . 1 . . .
## 3 1 1 . . . 1 . .
## 6 1 . . . . 1 1 .
## 5 . 1 . . . 1 . 1
## 4 . . 1 1 1 . . .
## 7 . . . 1 . . . 1
## 8 . . . . 1 . 1 .

Find density

edge_density(undirected_g)
## [1] 0.3928571

Draw a complete network with 8 nodes and a cycle/ring network with 8 nodes using both igraph and network. Compare the densities of these networks with that of the network in Figure 2.

full_g <- make_full_graph(8, directed = FALSE)
ring_g <- make_ring(8, directed = FALSE)
par(mfrow=c(1,2))
plot(full_g)
plot(ring_g)

## Find densities and compare with Fig 2

cat("The density of Figure 2 is", edge_density(undirected_g), "\n") 
## The density of Figure 2 is 0.3928571
cat("The density of the complete network is", edge_density(full_g), "\n") 
## The density of the complete network is 1
cat("The density of the ring network is", edge_density(ring_g), "\n") 
## The density of the ring network is 0.2857143
cat("Density comparison:", edge_density(ring_g), "<", edge_density(undirected_g), "<", edge_density(full_g))
## Density comparison: 0.2857143 < 0.3928571 < 1
cat("\n", "The complete network has the highest density with a density of 1.  Both the ring and undirected figure 4 network are sparse networks. The ring network has the lowest at 0.29, because of how few edges it has, and the undirected network has a value slightly higher than 0.29 at 0.39, meaning there are more edges relative to how many nodes it has.")
## 
##  The complete network has the highest density with a density of 1.  Both the ring and undirected figure 4 network are sparse networks. The ring network has the lowest at 0.29, because of how few edges it has, and the undirected network has a value slightly higher than 0.29 at 0.39, meaning there are more edges relative to how many nodes it has.