R Markdown

options(repos = c(CRAN = "https://cran.r-project.org"))
#HW 4 Rachel Konshok 08/02/2025

install.packages("igraph")
## Installing package into 'C:/Users/Rache/AppData/Local/R/win-library/4.4'
## (as 'lib' is unspecified)
## package 'igraph' successfully unpacked and MD5 sums checked
## Warning: cannot remove prior installation of package 'igraph'
## Warning in file.copy(savedcopy, lib, recursive = TRUE): problem copying
## C:\Users\Rache\AppData\Local\R\win-library\4.4\00LOCK\igraph\libs\x64\igraph.dll
## to C:\Users\Rache\AppData\Local\R\win-library\4.4\igraph\libs\x64\igraph.dll:
## Permission denied
## Warning: restored 'igraph'
## 
## The downloaded binary packages are in
##  C:\Users\Rache\AppData\Local\Temp\RtmpioaerR\downloaded_packages
library(igraph)
## Warning: package 'igraph' was built under R version 4.4.3
## 
## Attaching package: 'igraph'
## The following objects are masked from 'package:stats':
## 
##     decompose, spectrum
## The following object is masked from 'package:base':
## 
##     union
edges <- c(1,3, 1,4, 3,4, 2,9, 5,6, 5,8)


# Create the graph with 10 nodes (undirected)
g <- graph(edges, n = 10, directed = FALSE)
## Warning: `graph()` was deprecated in igraph 2.1.0.
## ℹ Please use `make_graph()` instead.
## This warning is displayed once every 8 hours.
## Call `lifecycle::last_lifecycle_warnings()` to see where this warning was
## generated.
# Set node labels
V(g)$name <- as.character(1:10)

# Manually define the layout
layout_coords <- matrix(c(
  1, 1,     # Node 1
  -1.8, 2.8,    # Node 2
  1, 2,     # Node 3
  0, 1.5,   # Node 4
  -.5, 4.8,     # Node 5
  -1.5, 5,  # Node 6
  3.5, 2,   # Node 7
  1, 4.5,   # Node 8
  -2, 3.5,    # Node 9
  3, 3.8  # Node 10
), ncol = 2, byrow = TRUE)

# Plot the graph
plot(g, layout = layout_coords, 
     vertex.color = "orange", 
     vertex.label = V(g)$name, 
     vertex.size = 25,
     edge.width = 2)

#degree of a node is number of edges connected to it
# Compute degree of each node
deg <- degree(g)

# Display degree for each node
data.frame(Node = V(g)$name, Degree = deg)
##    Node Degree
## 1     1      2
## 2     2      1
## 3     3      2
## 4     4      2
## 5     5      2
## 6     6      1
## 7     7      0
## 8     8      1
## 9     9      1
## 10   10      0
all_cliques <- cliques(g, min = 2)

all_cliques
## [[1]]
## + 2/10 vertices, named, from 2c75dc3:
## [1] 3 4
## 
## [[2]]
## + 2/10 vertices, named, from 2c75dc3:
## [1] 5 8
## 
## [[3]]
## + 2/10 vertices, named, from 2c75dc3:
## [1] 5 6
## 
## [[4]]
## + 2/10 vertices, named, from 2c75dc3:
## [1] 2 9
## 
## [[5]]
## + 2/10 vertices, named, from 2c75dc3:
## [1] 1 3
## 
## [[6]]
## + 3/10 vertices, named, from 2c75dc3:
## [1] 1 3 4
## 
## [[7]]
## + 2/10 vertices, named, from 2c75dc3:
## [1] 1 4
#All cliques says there are 7 different cliques
#the minimum was 2 so obvioulsy 1 ,3 and 4 make up a 3 node clique and 2, 9 make a 2 node clique
#technically 5 and 8 are a subgraph of 5 , 6, 8 and form a 2 node clique, as does 5 and 6
#going off this so does 3 and 4, 4 and 1, and 1 and 3. Therefore with a minimum of 2 nodes there are 7 different cliques in this network
# if the minimum is 3 nodes this network only has 1 clique 1, 3, and 4

components(g)
## $membership
##  1  2  3  4  5  6  7  8  9 10 
##  1  2  1  1  3  3  4  3  2  5 
## 
## $csize
## [1] 3 2 3 1 1
## 
## $no
## [1] 5
count_components(g)
## [1] 5
#this graph has five components 
#their respective sizes are 3, 2, 3, 1, and 1

#this graph has four trees 1,3, and 4 are cyclic which makes them not a tree
#everything else is a tree so node 7 is a tree as is node 10 
#node 5, 6, and 8 make up 1 tree and nodes 2 and 9 make up another
#total there is 4 trees


#this network is NOT a forest as it contains one cyclic component
#since a forest is a disjoint union of trees the cyclic component disqualifes it
#althout it is close if you were to get rid of nodes 1, 4, and 3 this network would be a forest

#degree centrality
deg/vcount(g)
##   1   2   3   4   5   6   7   8   9  10 
## 0.2 0.1 0.2 0.2 0.2 0.1 0.0 0.1 0.1 0.0
#below is the closeness for the network
closeness(g,V(g))
##         1         2         3         4         5         6         7         8 
## 0.5000000 1.0000000 0.5000000 0.5000000 0.5000000 0.3333333       NaN 0.3333333 
##         9        10 
## 1.0000000       NaN
#Below calculates the betweeness between the vertices
betweenness(g,V(g))
##  1  2  3  4  5  6  7  8  9 10 
##  0  0  0  0  1  0  0  0  0  0
#turning the node color red
plot.igraph(g, vertex.color = "red")

#changing names of each node to letter
lab=c("A","B","C","D","E","F","G","H","I","J")
plot.igraph(g, vertex.label=lab)

#below are all the algorithms given for different network layouts
ran = layout.random(g)
## Warning: `layout.random()` was deprecated in igraph 2.1.0.
## ℹ Please use `layout_randomly()` instead.
## This warning is displayed once every 8 hours.
## Call `lifecycle::last_lifecycle_warnings()` to see where this warning was
## generated.
plot(g, layout = ran)

lrt = layout.reingold.tilford(g)    
## Warning: `layout.reingold.tilford()` was deprecated in igraph 2.1.0.
## ℹ Please use `layout_as_tree()` instead.
## This warning is displayed once every 8 hours.
## Call `lifecycle::last_lifecycle_warnings()` to see where this warning was
## generated.
plot(g, layout = lrt)     

cir = layout.circle(g)
## Warning: `layout.circle()` was deprecated in igraph 2.1.0.
## ℹ Please use `layout_in_circle()` instead.
## This warning is displayed once every 8 hours.
## Call `lifecycle::last_lifecycle_warnings()` to see where this warning was
## generated.
plot(g, layout = cir)

sph = layout.sphere(g)
## Warning: `layout.sphere()` was deprecated in igraph 2.1.0.
## ℹ Please use `layout_on_sphere()` instead.
## This warning is displayed once every 8 hours.
## Call `lifecycle::last_lifecycle_warnings()` to see where this warning was
## generated.
plot(g, layout = sph)

lol = layout.lgl(g)
## Warning: `layout.lgl()` was deprecated in igraph 2.1.0.
## ℹ Please use `layout_with_lgl()` instead.
## This warning is displayed once every 8 hours.
## Call `lifecycle::last_lifecycle_warnings()` to see where this warning was
## generated.
## Warning in layout_with_lgl(structure(list(10, FALSE, c(2, 3, 3, 8, 5, 7), : At
## vendor/cigraph/src/layout/large_graph.c:179 : LGL layout does not support
## disconnected graphs yet.
#Above does not support disconnected graphs

star = layout_as_star(g)
plot(g, layout = star)

nice = layout_nicely(g)
plot(g, layout = nice)

grid = layout_on_grid(g)
plot(g, layout = grid)

drl = layout_with_drl(g)
plot(g, layout = drl)

gem = layout_with_gem(g)
plot(g, layout = gem)

graph = layout_with_graphopt(g)
plot(g, layout = graph)

mds = layout_with_mds(g)
plot(g, layout = mds)

#personal network analysis
#I've decided to analyze the Plant Pathology Department at NDSU since I work there
#I'm making 12 groups for each Plant Pathology advisor that has a student
#Each node will represent a student within that advisor's lab

set.seed(57)  # For reproducibility

#defining students and group sizes
students = paste0("Student", 1:57)
group_sizes = c(4, 6, 5, 12, 2, 6, 4, 7, 6, 2, 1, 2)
num_projects = length(group_sizes)

#adding students to a group while making sure no student is put into a group twice
group_membership = list()
start = 1
for (i in 1:num_projects) {
  end = start + group_sizes[i] - 1
  group = students[start:end]
  group_membership[[paste0("Lab", i)]] = group
  start = end + 1
}

#have to create an empty graph with students as verticies otherwise the lone student won't be in the final plot since they don't have any edges
g = make_empty_graph(n = 0, directed = FALSE)
g = add_vertices(g, nv = length(students), name = students)

#adding edges have to ignore the lone student here or it creates an error
collaborations = list()
for (group in group_membership) {
  if (length(group) < 2) next  
  edges = combn(group, 2, simplify = FALSE)
  collaborations <- c(collaborations, edges)
}


if (length(collaborations) > 0) {
  edge_list = do.call(rbind, lapply(collaborations, function(x) c(x[1], x[2])))
  g = add_edges(g, t(edge_list))
}

#adding color to each node
lab_colors = rainbow(num_projects)
V(g)$color = "gray"

for (i in seq_along(group_membership)) {
  members = group_membership[[i]]
  V(g)$color[V(g)$name %in% members] <- lab_colors[i]
}

#finally plotting out the network
plot(g,
     vertex.label.cex = 0.3,
     vertex.size = 6,
     vertex.color = V(g)$color,
     edge.width = 1,
     main = "Labs in Plant Pathology at NDSU")

#the degree of each node, this can be thought of as other students in each lab
#to check student 55 is zero which makes sense as this is the lone student
#as another check there are six students with 1 degree and this makes sense looking at the graph and seeing 3 two node connections
degree(g)
##  Student1  Student2  Student3  Student4  Student5  Student6  Student7  Student8 
##         3         3         3         3         5         5         5         5 
##  Student9 Student10 Student11 Student12 Student13 Student14 Student15 Student16 
##         5         5         4         4         4         4         4        11 
## Student17 Student18 Student19 Student20 Student21 Student22 Student23 Student24 
##        11        11        11        11        11        11        11        11 
## Student25 Student26 Student27 Student28 Student29 Student30 Student31 Student32 
##        11        11        11         1         1         5         5         5 
## Student33 Student34 Student35 Student36 Student37 Student38 Student39 Student40 
##         5         5         5         3         3         3         3         6 
## Student41 Student42 Student43 Student44 Student45 Student46 Student47 Student48 
##         6         6         6         6         6         6         5         5 
## Student49 Student50 Student51 Student52 Student53 Student54 Student55 Student56 
##         5         5         5         5         1         1         0         1 
## Student57 
##         1
#finding the cliques
#I set the minimum at 11 because anything lower had too high of an output, setting at 10 output 79 results
#I do not know all the possible cliques for this as the code stops at 3268 entries
cliques(g, min = 11)
## [[1]]
## + 11/57 vertices, named, from 2da86f8:
##  [1] Student17 Student18 Student19 Student20 Student21 Student22 Student23
##  [8] Student24 Student25 Student26 Student27
## 
## [[2]]
## + 11/57 vertices, named, from 2da86f8:
##  [1] Student16 Student17 Student18 Student19 Student20 Student21 Student22
##  [8] Student23 Student24 Student25 Student26
## 
## [[3]]
## + 12/57 vertices, named, from 2da86f8:
##  [1] Student16 Student17 Student18 Student19 Student20 Student21 Student22
##  [8] Student23 Student24 Student25 Student26 Student27
## 
## [[4]]
## + 11/57 vertices, named, from 2da86f8:
##  [1] Student16 Student17 Student18 Student19 Student20 Student21 Student22
##  [8] Student23 Student24 Student25 Student27
## 
## [[5]]
## + 11/57 vertices, named, from 2da86f8:
##  [1] Student16 Student17 Student18 Student19 Student20 Student21 Student22
##  [8] Student23 Student24 Student26 Student27
## 
## [[6]]
## + 11/57 vertices, named, from 2da86f8:
##  [1] Student16 Student17 Student18 Student19 Student20 Student21 Student22
##  [8] Student23 Student25 Student26 Student27
## 
## [[7]]
## + 11/57 vertices, named, from 2da86f8:
##  [1] Student16 Student17 Student18 Student19 Student20 Student21 Student22
##  [8] Student24 Student25 Student26 Student27
## 
## [[8]]
## + 11/57 vertices, named, from 2da86f8:
##  [1] Student16 Student17 Student18 Student19 Student20 Student21 Student23
##  [8] Student24 Student25 Student26 Student27
## 
## [[9]]
## + 11/57 vertices, named, from 2da86f8:
##  [1] Student16 Student17 Student18 Student19 Student20 Student22 Student23
##  [8] Student24 Student25 Student26 Student27
## 
## [[10]]
## + 11/57 vertices, named, from 2da86f8:
##  [1] Student16 Student17 Student18 Student19 Student21 Student22 Student23
##  [8] Student24 Student25 Student26 Student27
## 
## [[11]]
## + 11/57 vertices, named, from 2da86f8:
##  [1] Student16 Student17 Student18 Student20 Student21 Student22 Student23
##  [8] Student24 Student25 Student26 Student27
## 
## [[12]]
## + 11/57 vertices, named, from 2da86f8:
##  [1] Student16 Student17 Student19 Student20 Student21 Student22 Student23
##  [8] Student24 Student25 Student26 Student27
## 
## [[13]]
## + 11/57 vertices, named, from 2da86f8:
##  [1] Student16 Student18 Student19 Student20 Student21 Student22 Student23
##  [8] Student24 Student25 Student26 Student27
#there are four components in this network that are trees as they are the only acyclic components

#this network is not a forest due to the cyclical components 

components(g)
## $membership
##  Student1  Student2  Student3  Student4  Student5  Student6  Student7  Student8 
##         1         1         1         1         2         2         2         2 
##  Student9 Student10 Student11 Student12 Student13 Student14 Student15 Student16 
##         2         2         3         3         3         3         3         4 
## Student17 Student18 Student19 Student20 Student21 Student22 Student23 Student24 
##         4         4         4         4         4         4         4         4 
## Student25 Student26 Student27 Student28 Student29 Student30 Student31 Student32 
##         4         4         4         5         5         6         6         6 
## Student33 Student34 Student35 Student36 Student37 Student38 Student39 Student40 
##         6         6         6         7         7         7         7         8 
## Student41 Student42 Student43 Student44 Student45 Student46 Student47 Student48 
##         8         8         8         8         8         8         9         9 
## Student49 Student50 Student51 Student52 Student53 Student54 Student55 Student56 
##         9         9         9         9        10        10        11        12 
## Student57 
##        12 
## 
## $csize
##  [1]  4  6  5 12  2  6  4  7  6  2  1  2
## 
## $no
## [1] 12
#there are 12 components that represent the 12 labs with students in plant pathology
#the size is the nodes which represents the students in each lab
count_components(g)
## [1] 12
#degree centrality
deg/vcount(g)
##          1          2          3          4          5          6          7 
## 0.03508772 0.01754386 0.03508772 0.03508772 0.03508772 0.01754386 0.00000000 
##          8          9         10 
## 0.01754386 0.01754386 0.00000000
#below is the closeness for the network
closeness(g,V(g))
##   Student1   Student2   Student3   Student4   Student5   Student6   Student7 
## 0.33333333 0.33333333 0.33333333 0.33333333 0.20000000 0.20000000 0.20000000 
##   Student8   Student9  Student10  Student11  Student12  Student13  Student14 
## 0.20000000 0.20000000 0.20000000 0.25000000 0.25000000 0.25000000 0.25000000 
##  Student15  Student16  Student17  Student18  Student19  Student20  Student21 
## 0.25000000 0.09090909 0.09090909 0.09090909 0.09090909 0.09090909 0.09090909 
##  Student22  Student23  Student24  Student25  Student26  Student27  Student28 
## 0.09090909 0.09090909 0.09090909 0.09090909 0.09090909 0.09090909 1.00000000 
##  Student29  Student30  Student31  Student32  Student33  Student34  Student35 
## 1.00000000 0.20000000 0.20000000 0.20000000 0.20000000 0.20000000 0.20000000 
##  Student36  Student37  Student38  Student39  Student40  Student41  Student42 
## 0.33333333 0.33333333 0.33333333 0.33333333 0.16666667 0.16666667 0.16666667 
##  Student43  Student44  Student45  Student46  Student47  Student48  Student49 
## 0.16666667 0.16666667 0.16666667 0.16666667 0.20000000 0.20000000 0.20000000 
##  Student50  Student51  Student52  Student53  Student54  Student55  Student56 
## 0.20000000 0.20000000 0.20000000 1.00000000 1.00000000        NaN 1.00000000 
##  Student57 
## 1.00000000
#Below calculates the betweeness between the vertices
betweenness(g,V(g))
##  Student1  Student2  Student3  Student4  Student5  Student6  Student7  Student8 
##         0         0         0         0         0         0         0         0 
##  Student9 Student10 Student11 Student12 Student13 Student14 Student15 Student16 
##         0         0         0         0         0         0         0         0 
## Student17 Student18 Student19 Student20 Student21 Student22 Student23 Student24 
##         0         0         0         0         0         0         0         0 
## Student25 Student26 Student27 Student28 Student29 Student30 Student31 Student32 
##         0         0         0         0         0         0         0         0 
## Student33 Student34 Student35 Student36 Student37 Student38 Student39 Student40 
##         0         0         0         0         0         0         0         0 
## Student41 Student42 Student43 Student44 Student45 Student46 Student47 Student48 
##         0         0         0         0         0         0         0         0 
## Student49 Student50 Student51 Student52 Student53 Student54 Student55 Student56 
##         0         0         0         0         0         0         0         0 
## Student57 
##         0