# excel files
stGraph = read_excel("../data/quiz2nancyc.xlsx",sheet = 1)
stVertices = read_excel("../data/quiz2nancyc.xlsx",2)
stEdges = read_excel("../data/quiz2nancyc.xlsx",3)
# Create a digraph from these three data frames.
st = graph_from_data_frame(stEdges, TRUE, stVertices)
(st = graph_attr_from_df(st, stGraph))
## IGRAPH e8f3086 DNW- 22 24 --
## + attr: Graph Name (g/c), Description (g/c), name (v/c), label (v/c),
## | color (v/c), weight (e/n), labels (e/n), color (e/c)
## + edges from e8f3086 (vertex names):
## [1] KM->MA MA->FE KM->MG AN->WL WL->ME BB->WL UN->FE MD->FE MD->MK SM->MK
## [11] SM->RD FE->SM FE->MG MG->FE MG->BB CH->JF MK->JK JF->FE JF->LA LA->MD
## [21] WL->NG NG->MG LA->PC PC->LA
st_name = (stGraph)[1,1]
# plot of network
igraph_options(vertex.size = 20, edge.arrow.size = 0.3)
set.seed(23) ; plot(st, main = st_name)

#edge.label = E(st)$weight)
################################################################################
# A description of the network; what do the vertices/edges represent?
# This network shows the main streets in the Ironbound section of Newark, NJ
# and how they are connected to one another. The vertices represent streets and
# edges represent what streets are connected. Edges going from vertex A into
# vertex B means that you can go from street A to street B. Edges that go in
# both directions represent a two way street.
# State whether it is directed or undirected and give its order and size.
is_directed(st) # true
## [1] TRUE
# This is a directed graph.
gorder(st) # 22 vertices (streets)
## [1] 22
gsize(st) # 24 edges (connections)
## [1] 24
###############################################################################
# Any additional information, such as whether it’s a special graph
# state whether its connected
# Not a special graph.
# If I wanted to make it a special graph, more specifically, a multi-graph, I
# could have each edge represent how many lanes from street A go into street B, etc.
is_connected(st) # false
## [1] FALSE
# This means not every vertex is reachable from every other vertex.
is_connected(st,mode="weak") # false
## [1] FALSE
# Even ignoring the direction of the edges, you cannot go from all main streets
# to all other main streets. This means not all main streets in the Ironbound are
# connected even by walking (meaning you need to go thorugh other streets that
# are not shown in the diagram to get to another main street.
is_connected(st,mode="strong") # false
## [1] FALSE
###############################################################################
# Explain your graph attributes, vertex attributes, and edge attributes.
graph_attr(st)
## $`Graph Name`
## [1] "Streets in Newark's Ironbound Section"
##
## $Description
## [1] "This network shows several main roads from Newark New Jersey's Ironbound section in the East Ward and how they connect to each other."
# This network shows several main roads from Newark New Jersey's Ironbound
# section in the East Ward and how they connect to each other.
vertex_attr(st)
## $name
## [1] "AN" "BB" "CH" "FE" "JK" "JF" "KM" "LA" "MD" "MG" "MA" "MK" "ME" "NP" "NG"
## [16] "PC" "PK" "RD" "RE" "SM" "UN" "WL"
##
## $label
## [1] "Ann" "Barbara" "Chestnut" "Ferry" "Jackson" "Jefferson"
## [7] "Komorn" "Lafayette" "Madison" "Magazine" "Main" "Market"
## [13] "Marne" "Napoleon" "Niagara" "Pacific" "Polk" "Raymond"
## [19] "Rome" "Somme" "Union" "Wilson"
##
## $color
## [1] "yellow" "yellow" "yellow" "yellow" "yellow" "yellow" "yellow" "yellow"
## [9] "yellow" "yellow" "yellow" "yellow" "yellow" "yellow" "yellow" "yellow"
## [17] "yellow" "yellow" "yellow" "yellow" "yellow" "yellow"
# Gives the color of all vertex, in this graph the color has no meaning.
edge_attr(st)
## $weight
## [1] 1 2 2 3 2 2 2 2 2 3 3 3 3 3 2 2 2 2 2 2 2 2 3 3
##
## $labels
## [1] 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
##
## $color
## [1] "red" "red" "red" "red" "red" "red" "red" "red" "red" "red" "red" "red"
## [13] "red" "red" "red" "red" "red" "red" "red" "red" "red" "red" "red" "red"
# Gives the color of all edges, in this graph the color has no meaning.
################################################################################
# Provide the degree distribution and its plot. Explain what it means and any
# observations you can deduce from it.
# In
degree(st, mode = "in")
## AN BB CH FE JK JF KM LA MD MG MA MK ME NP NG PC PK RD RE SM UN WL
## 0 1 0 5 1 1 0 2 1 3 1 2 1 0 1 1 0 1 0 1 0 2
mean(degree(st, mode = "in"))
## [1] 1.090909
# number of ways to enter each street
st_dd_in = degree_distribution(st, mode = "in")
names(st_dd_in) = 0:(length(st_dd_in)-1)
plot(st_dd_in~names(st_dd_in), type="h", xlab="Degree", ylab="Relative Frequency",
main="Degree Distribution (In)")

# The frequency of the degree 0 is just below 0.32, which means that 32% of the
# vertices have an in degree of 0. In context, this means that you cannot go into
# 32% of streets in the diagram from another main street in the Ironbound.
# On average, each main street has 1.09 main stretss connecting inward to it.
# The streets that has the most number of streets connecting to it inward is
# Ferry St (5 streets). As one may predict, Ferry St is in the center of the
# Ironbound and runs through the enter length of it.
degree(st, mode = "out")
## AN BB CH FE JK JF KM LA MD MG MA MK ME NP NG PC PK RD RE SM UN WL
## 1 1 1 2 0 2 2 2 2 2 1 1 0 0 1 1 0 0 0 2 1 2
mean(degree(st, mode = "out")) #same as for in, as expected
## [1] 1.090909
st_dd_out = degree_distribution(st, mode = "out")
names(st_dd_out) = 0:(length(st_dd_out)-1)
plot(st_dd_out~names(st_dd_out), type="h", xlab="Degree", ylab="Relative Frequency",
main="Degree Distribution (Out)", ylim = c(0.001, 0.5))

################################################################################
# If your graph is weighted, provide the weighted degree distribution and its
# plot, and make observations.
# weighted
is_weighted(st) # true
## [1] TRUE
# Yes there are weights and they represent how busy the street typically is, in a
# particular direction, on a scale from 1-3 (1 not very busy, 2 busy, 3 very busy).
table(strength(st))
##
## 0 2 3 4 6 7 9 10 12 17
## 3 4 4 2 3 1 2 1 1 1
# This gives the total weight of vertices, meaning 3 vertices have no weight (no
# edges), 1 vertex has the total weight of 17, meaning that the total "busyness"
# of all the streets connecting to it is 17, etc.
# weighted degree distribution
(st_wd = weight_distribution(st))
## 0 1 2 3 4 5 6
## 0.13636364 0.00000000 0.18181818 0.18181818 0.09090909 0.00000000 0.13636364
## 7 8 9 10 11 12 13
## 0.04545455 0.00000000 0.09090909 0.04545455 0.00000000 0.04545455 0.00000000
## 14 15 16 17
## 0.00000000 0.00000000 0.00000000 0.04545455
plot(st_wd~names(st_wd),xlab="Strength",ylab=NA,type='h',
main = 'Street "Busyness" / Weighted Degree Dist.')

# Most vertices have strength less than 10, but 1 has a strength of 17. This is
# Ferry St which is also the street with the highest (unweighted) degree.
# This means it is the street connected to the highest number of other man streets,
# so it is no surprise that it is also the one with that has the highest overall
# score in "busyness" which is told to us by the weighted degree dustribution.
################################################################################
# Give its density.
edge_density(st, loops = FALSE) # 0.05
## [1] 0.05194805
# In this context, the edge density refers to the proportion of actual connections
# between the streets compared to the total number of possible connections
# between them.
# A high edge density means that there are many connections between the streets
# in the graph, while a low edge density indicates that there are relatively few
# connections between the streets.
################################################################################
# Is your network dense or sparse, or extremely sparse?
# Since 0.05 is close to 0, my network is extremely sparse. This means there are
# very few connections between the streets compared to all the possible
# connections, therefore, in context, the main streets in the Ironbound are
# spread out and are rarely directly connected to one another, which is expected.
################################################################################
# If your graph is disconnected, show its components. Point out if you have a
# giant component; you may select the giant to continue if you want.
is_connected(st) # false
## [1] FALSE
# Therefore, the graph is disconnected.
# show its components
(st_co = components(st))
## $membership
## AN BB CH FE JK JF KM LA MD MG MA MK ME NP NG PC PK RD RE SM UN WL
## 1 1 1 1 1 1 1 1 1 1 1 1 1 2 1 1 3 1 4 1 1 1
##
## $csize
## [1] 19 1 1 1
##
## $no
## [1] 4
# Plot the components
set.seed(23) ; plot(st, vertex.color = st_co$membership, main="Components")

table(st_co$csize)
##
## 1 19
## 3 1
# The first one is a giant component, consisting of 19 out of 22 streets.
# This means that most main streets in the Ironbound are directly connected
# to each other.
st_co$membership[st_co$membership!=1]
## NP PK RE
## 2 3 4
# Only Napoleon,Rome, and Polk are not part of the giant component.
#Break graph into component graphs
st_comps = get_subgraphs(st,st_co$membership)
st_giant = st_comps[[1]]
plot(st_giant, main = "Giant Component")

################################################################################
# Provide all the centrality measures applicable to your graph. Explain them.
# Degree Centrality
sort(degree(st_giant),decr=T)
## FE MG LA WL JF MD MK SM BB KM MA NG PC AN CH JK ME RD UN
## 7 5 4 4 3 3 3 3 2 2 2 2 2 1 1 1 1 1 1
# Normalize:
round2(sort(degree(st_giant),decr=T)/(gorder(st_giant)-1),2)
## FE MG LA WL JF MD MK SM BB KM MA NG PC AN CH JK
## 0.39 0.28 0.22 0.22 0.17 0.17 0.17 0.17 0.11 0.11 0.11 0.11 0.11 0.06 0.06 0.06
## ME RD UN
## 0.06 0.06 0.06
# Ferry St. was the most important for degree centrality.
# It had the most connections to other vertices, and therefore has a greater
# potential to influence the flow of information or resources in the network.
# Betweenness Centrality
round2(sort(betweenness(st_giant),decr=T),3)
## FE MG WL BB SM MD NG LA MK JF MA AN CH JK KM ME PC RD UN
## 77 61 38 32 32 24 20 19 15 13 5 0 0 0 0 0 0 0 0
# Normalize:
round2(sort(betweenness(st_giant),decr=T) /
((gorder(st_giant)-1)*(gorder(st_giant)-2)/2),2)
## FE MG WL BB SM MD NG LA MK JF MA AN CH JK KM ME
## 0.50 0.40 0.25 0.21 0.21 0.16 0.13 0.12 0.10 0.08 0.03 0.00 0.00 0.00 0.00 0.00
## PC RD UN
## 0.00 0.00 0.00
# Ferry St. was the most important for betweenness centrality.
# It lies on many of the shortest paths in the network, and therefore has
# a greater potential to control the flow of information or resources between
# other vertices.
###############################################################################
# Pick a central vertex and create an egocentric network around it. Plot this.
f = which.max(degree(st_giant))
st_ego = make_ego_graph(st_giant,1,f)[[1]]
plot(st_ego,vertex.size=20, edge.arrow.size = 1,
main = "Streets Connected to Ferry St." )

# This is an egocentric graph around Ferry St.
###############################################################################
# Other things to mention :
# Distance
distances(st_ego, weights=NA, mode = "out")
## FE JF MD MG MA SM UN
## FE 0 Inf Inf 1 Inf 1 Inf
## JF 1 0 Inf 2 Inf 2 Inf
## MD 1 Inf 0 2 Inf 2 Inf
## MG 1 Inf Inf 0 Inf 2 Inf
## MA 1 Inf Inf 2 0 2 Inf
## SM Inf Inf Inf Inf Inf 0 Inf
## UN 1 Inf Inf 2 Inf 2 0
# Diameter
diameter(st_ego, weights = NA) # 2 (the longest shortest path of st_ego)
## [1] 2
diameter(st_giant, weights = NA) # 7 (the longest shortest path of st_giant)
## [1] 7
V(st_ego)$name[get_diameter(st_ego)] # Ann St to Jackson St
## [1] "MG" "FE" "SM"