# les miserables graph

*label*Programming

*timer*Asked: Mar 8th, 2017

**Question description**

Q1. One premise of the Strength of Weak Tie theory is that, the stronger the tie between two people is, the more likely their contacts will overlap so that they will have common ties with the same third parties. Test this hypothesis for the les miserables graph we studied in class. The following code block prints the number of shared neighbors for the endpoints of every edge:

for(i in E(g)){

a = ends(g, i)[1]

b = ends(g, i)[2]

source_neighbors = neighbors(g, a)

target_neighbors = neighbors(g, b)

num_overlap_neighbors = length(intersection(source_neighbors, target_neighbors))

print(num_overlap_neighbors)

}

1. Rewrite it as a function that takes i as its input (an edge i), and gives the number of overlapping neighbors of edge i's endpoints as output. For example if the function name is get_overlap then given the input 24 the output should be

get_overlap(24)

[1] 6

Hint: to define a function in R

get_overlap = function(i){

a = ends(g, i)[1]

b = ends(g, i)[2]

source_neighbors = neighbors(g, a)

target_neighbors = neighbors(g, b)

num_overlap_neighbors = length(intersection(source_neighbors, target_neighbors))

return(num_overlap_neighbors)

}

2. use sapply() to apply the function to E(g) to get a vector as output: sapply(E(g), get_overlap)

3. Run a regression with the tie strength (measured as the edge weight, which is the value attribute) as the dependent variable and number of shared neighbors as the independent variable. What is your conclusion? Submit a single Word/Pdf file with R code attached at the end. Please DO NOT submit a zip file.

Hint:

To run a linear regression in R use lm() function

lm(y ~ x)

where y would be E(g)$value, and x would be the vector you constructed in step 2.