challenge_3
Ananya Pujary
Degree and Density of a Network
Author

Ananya Pujary

Published

March 6, 2023

Degree

I’ve chosen to work with the got_distances.csv dataset that provides information about the distances between regions in the GOT universe.

Code
got_distances <-read_csv("../posts/_data/got/got_distances.csv")
Rows: 200 Columns: 6
── Column specification ────────────────────────────────────────────────────────
Delimiter: ","
chr (5): Region From, From, To, Mode, Notes
dbl (1): Miles

ℹ Use `spec()` to retrieve the full column specification for this data.
ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
Code
got_distances
# A tibble: 200 × 6
   `Region From` From          To               Miles Mode  Notes   
   <chr>         <chr>         <chr>            <dbl> <chr> <chr>   
 1 Westerlands   Casterly Rock the Golden Tooth   240 land  <NA>    
 2 Westerlands   Casterly Rock Lannisport          40 land  <NA>    
 3 Westerlands   Casterly Rock Kayce              100 land  <NA>    
 4 Westerlands   Casterly Rock Kayce               12 water <NA>    
 5 Westerlands   Casterly Rock Deep Den           240 land  Goldroad
 6 Westerlands   Deep Den      King’s Landing     590 land  Goldroad
 7 Westerlands   Kayce         Faircastle         480 raven <NA>    
 8 Westerlands   Faircastle    the Crag           115 Boat  Island  
 9 Westerlands   the Crag      Ashemark            85 land  <NA>    
10 Westerlands   Ashemark      Casterly Rock      170 land  <NA>    
# … with 190 more rows
Code
got_distances.ig <-graph_from_data_frame(got_distances, directed = TRUE)
Code
# number of edges
ecount(got_distances.ig)
[1] 200
Code
# number of vertices
vcount(got_distances.ig)
[1] 103
Code
# vertex and edge attributes
vertex_attr_names(got_distances.ig)
[1] "name"
Code
edge_attr_names(got_distances.ig)
[1] "To"    "Miles" "Mode"  "Notes"
Code
# network features
is_directed(got_distances.ig)
[1] TRUE
Code
is_bipartite(got_distances.ig)
[1] FALSE
Code
is_weighted(got_distances.ig)
[1] FALSE
Code
# dyad census
igraph::dyad.census(got_distances.ig)
$mut
[1] 0

$asym
[1] 93

$null
[1] 5160
Code
# triad census
triad_census(got_distances.ig)
 [1] 167960   3917   4472    502      0      0      0      0      0      0
[11]      0      0      0      0      0      0

This network has 200 edges and 103 vertices. The vertex attribute is ‘name’ and edge attributes are “To”, “Miles”, “Mode”, and “Notes”. It is directed, not bipartite, and not weighted. Dyad census reveals that there are 0 mutual, 93 asymmetric, and 5160 null relationships in this network of distances between regions in the GOT universe

Code
# number of components
igraph::components(got_distances.ig)$no
[1] 10
Code
# size of components
igraph::components(got_distances.ig)$csize 
 [1]  9 12  7 13  6 10 12 13  1 20

There are 10 components, the smallest of which is 1 node and largest is 20.

Density

Code
# network density
graph.density(got_distances.ig)
[1] 0.01903674
Code
# density without loops
graph.density(got_distances.ig, loops=TRUE)
[1] 0.01885192

The density of this network, a global measure, is 0.01903674 which suggests that it isn’t very dense. Without accounting for loops, the density value decreases to 0.01885192.

Analyzing the average network degree, a vast majority of the nodes have an average degree of 1 or 2, which supports the previous finding of the network not being very dense.

Code
# average network degree
igraph::degree(got_distances.ig)
                   Westerlands                          Reach 
                            14                             19 
                    Riverlands                           Vale 
                            18                             19 
                    Crownlands                     Stormlands 
                            13                             13 
                         Dorne                          North 
                            16                             26 
                  Iron Islands                          Essos 
                            10                             57 
                 Casterly Rock                       Deep Den 
                             5                              1 
                         Kayce                     Faircastle 
                             1                              1 
                      the Crag                       Ashemark 
                             1                              2 
                    Lannisport                      Crakehall 
                             2                              1 
                       Old Oak                     Highgarden 
                             1                              5 
                       Oldtown                     Blackcrown 
                             2                              2 
                  Three Towers      Mouth of the Mander River 
                             1                              1 
                  Golden Grove                     Cider Hall 
                             1                              2 
                     Longtable                       Harroway 
                             2                              7 
                       Seagard                       Riverrun 
                             3                              3 
                     Harrenhal                       Saltpans 
                             2                              1 
                    Maidenpool                         Border 
                             2                              1 
               the Bloody Gate                      Wickenden 
                             1                              2 
                      Gulltown                      Runestone 
                             1                              2 
                    Old Anchor                   Longbow Hall 
                             2                              2 
                    Snakewoods                   Heart’s Home 
                             3                              1 
                   the Sisters                  Pebble Island 
                             2                              1 
                      the Paps                 King’s Landing 
                             1                              7 
                    Duskendale                    Rook’s Rest 
                             2                              1 
                   Sharp Point                    Dragonstone 
                             1                              2 
                     Wendwater                    Bronze Gate 
                             1                              1 
                   Storm’s End                     Summerhall 
                             4                              1 
                    Blackhaven                     Rain House 
                             1                              1 
                      Mistwood                  Weeping Tower 
                             2                              1 
                     Stonehelm                            Wyl 
                             1                              1 
                      Yronwood                        the Tor 
                             1                              1 
                     Godsgrace                      Saltshore 
                             3                              1 
                         Vaith                       Hellholt 
                             1                              3 
                      Skyreach                    Moat Cailin 
                             1                              2 
                        Cerwyn                Greywater Watch 
                             1                              1 
                Flint’s Finger                   White Harbor 
                             1                              2 
                      Ramsgate                  Widow’s Watch 
                             2                              1 
                       Karhold                      Dreadfort 
                             2                              2 
                    Winterfell                 Deepwood Motte 
                             5                              2 
                     Barrowton             the Shield Islands 
                             5                              1 
                     the Arbor the Southern Dornish Coastline 
                             1                              1 
                     Lemonwood                       Sunspear 
                             2                              1 
                        Tyrosh                         Pentos 
                             7                              2 
                       Braavos                         Lorath 
                             6                              1 
                           Lys                       Volantis 
                             2                              5 
                      New Ghis                        Astapor 
                             9                              2 
                        Yunkai                    Dagger Lake 
                             2                              2 
                the Stepstones                            Myr 
                             1                              3 
                      Selhorys                          Qohor 
                             2                              1 
                        Norvos                   Vaes Dothrak 
                             2                              4 
                      Mantarys                        Bhorash 
                             2                              1 
                       Meereen 
                             3 

Creating a data frame of the nodes in the network and finding out the in-degree and out-degree values of each:

Code
nodes_ig<-data.frame(name=V(got_distances.ig)$name, degree=igraph::degree(got_distances.ig,loops=FALSE))
nodes_ig<-nodes_ig %>%
    mutate(indegree=igraph::degree(got_distances.ig, mode="in", loops=FALSE),
           outdegree=igraph::degree(got_distances.ig, mode="out", loops=FALSE))
head(nodes_ig)
                   name degree indegree outdegree
Westerlands Westerlands     14        0        14
Reach             Reach     19        0        19
Riverlands   Riverlands     18        0        18
Vale               Vale     19        0        19
Crownlands   Crownlands     13        0        13
Stormlands   Stormlands     13        0        13
Code
summary(nodes_ig)
     name               degree          indegree       outdegree     
 Length:103         Min.   : 0.000   Min.   :0.000   Min.   : 0.000  
 Class :character   1st Qu.: 1.000   1st Qu.:1.000   1st Qu.: 0.000  
 Mode  :character   Median : 2.000   Median :1.000   Median : 0.000  
                    Mean   : 3.786   Mean   :1.893   Mean   : 1.893  
                    3rd Qu.: 3.000   3rd Qu.:2.000   3rd Qu.: 0.000  
                    Max.   :57.000   Max.   :9.000   Max.   :57.000  

Random Network

Creating a random (Erdos-Renyi) network with the same number of nodes and edges as the GOT distances network:

Code
erdos_renyi.ig <- sample_gnm(103, 200, directed = TRUE, loops = FALSE)

# density of random network
graph.density(erdos_renyi.ig)
[1] 0.01903674
Code
# dyad census of random network
igraph::dyad.census(erdos_renyi.ig)
$mut
[1] 2

$asym
[1] 196

$null
[1] 5055
Code
# triad census of random network
igraph::triad.census(erdos_renyi.ig)
 [1] 157567  18402    183    173    180    313     12      6     11      3
[11]      0      0      0      1      0      0

The density of the random graph is 0.01903674. It has 1 mutual, 198 asymmetric, and 5054 null relationships.

Now, looking at the degree distributions in this random network:

Code
nodes_rand<-data.frame(degree=igraph::degree(erdos_renyi.ig))
nodes_rand<-nodes_rand %>%
    mutate(indegree=igraph::degree(erdos_renyi.ig, mode="in", loops=FALSE),
           outdegree=igraph::degree(erdos_renyi.ig, mode="out", loops=FALSE))
head(nodes_rand)
  degree indegree outdegree
1      3        2         1
2      4        3         1
3      5        2         3
4      3        1         2
5      5        2         3
6      3        1         2
Code
summary(nodes_rand)
     degree          indegree       outdegree    
 Min.   : 0.000   Min.   :0.000   Min.   :0.000  
 1st Qu.: 3.000   1st Qu.:1.000   1st Qu.:1.000  
 Median : 4.000   Median :2.000   Median :2.000  
 Mean   : 3.883   Mean   :1.942   Mean   :1.942  
 3rd Qu.: 5.000   3rd Qu.:3.000   3rd Qu.:3.000  
 Max.   :10.000   Max.   :7.000   Max.   :6.000  

Comparing the random network statistics with those of the got_distances.ig, the density values are comparable, and so are the values from the dyad census. The triad census values for the random network are higher. The in-degree values for the distances network are all 0. At the same time, its values for out-degree and degree are much higher than those in the random network.