Lesson 3

Introduction to lists & local network properties

Author: Patrick Egan, Eng Gin Moe, Chris Wadibia

Version: 1

Date: September 23, 2015

GOOD CODING PRACTICE - Import all your packages before you begin writing your program code.

In [65]:
# Import packages
import igraph

Recall from last time that we created a graph with 3 nodes.

GOOD CODING PRACTICE - Comment different parts of your code.

In [66]:
# create an empty graph
freshGraph = igraph.Graph()
    
# add three nodes or vertices and add labels to them
freshGraph.add_vertices(3)
myGraph.vs["label"] = ["0","1","2"]

# create three edges among these nodes
myGraph.add_edges([(0,1),(1,2),(2,0)])

# draw the graph
igraph.plot(myGraph, layout = myGraph.layout_random(), bbox=(200,200))
Out[66]:

Your turn! Add another node to the graph and 2 edges from the new node to one of the other nodes.

In [53]:
import igraph
freshGraph =igraph.Graph()
freshGraph.add_vertices(4)
freshGraph.vs["label"] = ["0","1","2","3"]
freshGraph.add_edges([(0,1),(1,2),(2,0),(3,1),(3,2)])
igraph.plot(freshGraph, layout =freshGraph.layout_random(), bbox=(200,200))
Out[53]:

We can view a summary of the network using the summary function. This summary tells us that we have an undirected graph with 3 nodes and 3 edges. It also tells us that the graph has labels on its nodes. By default, nodes are usually labeled with numbers.

In [54]:
myGraph.summary()
Out[54]:
'IGRAPH U-W- 4 11 -- \n+ attr: label (v), weight (e)'

Last time, we also learned how to compute some global graph properties, density and diameter. We stored the results in a variable and then printed the density and diameter on the screen.

In [55]:
# Use igraph to compute the density and diameter of the network
myDensity = myGraph.density()
myDiameter = myGraph.diameter()

# Display the density and diameter of the graph
print "Graph density: ", myDensity
print "Graph diameter: ", myDiameter
Graph density:  1.83333333333
Graph diameter:  2

Your turn! Explain what density and diameter tell us.

Density = if all nodes were connected, density shows us how many connections there would be. It shows us how connected a graph is. Diameter = Is the largest geodesic distance in the graph. Diameter tells us how to travel the farthest distance between two nodes using the fewest number of edges. Diameter shows how nodes are connected across the network.

#### We can also compute local graph properties. Here we compute degree, betweenness and clustering coefficient for all the nodes in the graph. #### GOOD CODING PRACTICE - Use meaningful variable names.
In [56]:
myDegreeList = myGraph.degree()
myBetweenessList = myGraph.betweenness()
myClusteringCoefficientList = myGraph.transitivity_local_undirected()

print "Degree of all nodes: ", myDegreeList
print "Betweenness of all nodes: ", myBetweenessList
print "Clustering Coefficient of all nodes:", myClusteringCoefficientList
Degree of all nodes:  [6, 7, 7, 2]
Betweenness of all nodes:  [0.0, 0.5, 0.5, 0.0]
Clustering Coefficient of all nodes: [0.06666666666666667, 0.09523809523809523, 0.09523809523809523, 1.0]

Your turn! Explain the importance of each of these three local graph properties.

Your turn! Explain the importance of each of these three local graph properties.</span

Degree = the number of nodes that a node is connected to, or the number of neighbors it has. Degree is important to show how connected a node is to the other nodes. Betweeness = betweeness is the total number of shortest paths between all pairs of nodes that pass through node i. Betweeness is important in findng out which nodes are highly relevant to the graph and are often used in these shortest paths. Clustering coefficient = subset of node i's neighbors that are connected. Clustering coefficient is important because it is indicative of the relationships existing between i's neighbors, which in turn details the nature and depth of the relationships amongst i's neighbors

#### The variables myDegreeList, myBetweenessList, myClusteringCoefficientList are all variables that store a list of values. Each of these variables are lists of size four since there are 4 nodes in the graph (assuming you added a node). If we did not know the size of the list, we would use the len() function to determine it.
In [71]:
degreeListLength = len(myDegreeList)

print "The length of the degree list: ", degreeListLength
The length of the degree list:  4

Your turn! Write code to determine the length of the betweeness list and the clustering coefficient list.

In [72]:
myBetweenessList = myGraph.betweenness()
betweenessListLength = len(myBetweenessList)
print "The length of the betweeness list: ", betweenessListLength
The length of the betweeness list:  4
In [73]:
myClusteringCoefficientList = myGraph.transitivity_local_undirected()
print "Clustering Coefficient of all nodes:", myClusteringCoefficientList
clusteringCoefficientListLength = len(myClusteringCoefficientList)
print "The length of the clustering coefficent list: ", clusteringCoefficientListLength
Clustering Coefficient of all nodes: [0.03571428571428571, 0.05555555555555555, 0.05555555555555555, 1.0]
The length of the clustering coefficent list:  4

Our current graph is an unweighted graph. If we did not know that, we could check to see if it is weighted.

In [74]:
# See if the graph is a weighted graph
myGraph.is_weighted()
Out[74]:
True

Let's add weights to the graph. We do this by adding an attribute "weight" to the set of edges (es) in the graph (myGraph).

In [75]:
# Add weights to the graph
myGraph.es["weight"] = 1

# Check to see if the graph is a weighted graph
myGraph.is_weighted()
Out[75]:
True
In [76]:
# Print all the edge weights
myGraph.es["weight"]
Out[76]:
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]

Let's change the weight of the edge between nodes 0 and 1 to a weight of 2.

In [78]:
# Make edge(0,1) an edge weight of 2
myGraph[0, 1]=2

# Print all the edge weights
myGraph.es["weight"]
Out[78]:
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 1, 1]

Your turn! Change the edge weights for all the other edges in the network. Then print the edge weights.

In [79]:
freshGraph.es["weight"] = 3
freshGraph.is_weighted()
freshGraph.es["weight"]
freshGraph [0,1]=4
freshGraph [1,2]=2
freshGraph [2,0]=5
freshGraph [3,1]=.5
freshGraph [3,2]=3.5
freshGraph.es["weight"]
Out[79]:
[4, 2, 5, 0.5, 3.5]

Your turn! Now that the edges have different weights, does that change the importance of the different nodes in the network. Explain.

Yes,introducing different weights alters the importance of existing nodes in the network. Given that weights correspond to the strength of relationships, new weights directly relate to changed strength of relationships, which, in turn, lessens the importance of some nodes while increasing the importance of other nodes. An edge weight that changes from 3 to 5 would become more important becuase the network is emphasizing that edge more that others in the network.

In [ ]:
 
In [ ]: