# Create edge list representing seat adjacency
edge_list <- data.frame(
from = c("A", "A","A", "B", "B", "B", "B", "C", "C", "C", "C", "C", "D", "D", "D", "D", "D"),
to = c("2", "B","C", "A", "D", "C", "6", "B", "A", "D", "3", "4", "5", "6", "3", "B", "C")
)
print(edge_list)
## from to
## 1 A 2
## 2 A B
## 3 A C
## 4 B A
## 5 B D
## 6 B C
## 7 B 6
## 8 C B
## 9 C A
## 10 C D
## 11 C 3
## 12 C 4
## 13 D 5
## 14 D 6
## 15 D 3
## 16 D B
## 17 D C
# Load network analysis library
library(igraph)
## Warning: 程辑包'igraph'是用R版本4.3.3 来建造的
##
## 载入程辑包:'igraph'
## The following objects are masked from 'package:stats':
##
## decompose, spectrum
## The following object is masked from 'package:base':
##
## union
# Create graph object
bus_graph <- graph_from_data_frame(edge_list, directed = FALSE)
# Function to calculate centrality for a given vertex
calculate_centrality <- function(graph, vertex) {
data.frame(
degree = degree(graph, vertex),
closeness = closeness(graph, vertex),
betweenness = betweenness(graph, vertex)
)
}
# Calculate centrality for each open seat
centrality_A <- calculate_centrality(bus_graph, "A")
centrality_B <- calculate_centrality(bus_graph, "B")
centrality_C <- calculate_centrality(bus_graph, "C")
centrality_D <- calculate_centrality(bus_graph, "D")
# Display the results
print(centrality_A);
## degree closeness betweenness
## A 5 0.07142857 7
print(centrality_B);
## degree closeness betweenness
## B 7 0.08333333 5
print(centrality_C);
## degree closeness betweenness
## C 8 0.09090909 11.5
print(centrality_D)
## degree closeness betweenness
## D 7 0.08333333 9.5
# Put results in dataframe
centrality <- data.frame(rbind(centrality_A,centrality_B,centrality_C, centrality_D))
print(centrality)
## degree closeness betweenness
## A 5 0.07142857 7.0
## B 7 0.08333333 5.0
## C 8 0.09090909 11.5
## D 7 0.08333333 9.5
Considering all three measures of centrality, C is the seat that has the maximum reach in the network. Therefore I would pick seat C. Below is a graph of the network with each vertex representing a seat.
# Set vertex labels according to seats
V(bus_graph)$label <- V(bus_graph)$name
# Plot the graph with centrality size and labels
plot(bus_graph, vertex.size = degree(bus_graph) * 5, vertex.label = V(bus_graph)$label)