Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Download

Jupyter notebook Lesson 3.ipynb

27 views
Kernel: Python 2

Lesson 3

Introduction to lists & local network properties

Author: Alissa Tripodoro, Michael Coyne, Naaz Modan

Version: 1

Date: September 23, 2015

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

# 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.

# create an empty graph myGraph = igraph.Graph() # add three nodes or vertices and add labels to them myGraph.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))
Image in a Jupyter notebook

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

#add one nodes or vertice and add label to it myGraph.add_vertices (1) myGraph.vs["label"] = ["0","1","2","3"] #create two edges between this node and rest of graph myGraph.add_edges([(3,2),(3,1)]) #draw the graph igraph.plot(myGraph, layout = myGraph.layout_random(), bbox=(200,200))
Image in a Jupyter notebook
# 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.

myGraph.summary()

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.

# 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: 0.833333333333 Graph diameter: 2

Your turn! Explain what density and diameter tell us.

Density is the ratio between the actual connections between nodes in a graph and the maximum potential number of connections that could exist between those same nodes.

Diameter is the "longest shortest distance;" or the shortest possible path between the two furthest nodes in a graph.

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.

myDegreeList = myGraph.degree() myBetweenessList = myGraph.edge_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: [2, 3, 3, 2] Betweenness of all nodes: [1.5, 1.0, 1.5, 1.5, 1.5] Clustering Coefficient of all nodes: [1.0, 0.6666666666666666, 0.6666666666666666, 1.0]
## <span style="color:orange;">Your turn! Explain the importance of each of these three local graph properties.</span>

The clustering coefficient shows the number of neighbors a node has, and how many of those neighbors are connected to each other. A greater clustering coefficient signifies a stronger network.

Betweeness refers to the number of geodesic (shortest) paths between two nodes in a graph that pass through a given node.

Degree refers to the number of neighbors an individual node has, indicated by edges linking a node to another node(s).

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.

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.

#determine lengths using myGraph and above definitions from professors, NOT new definitions of our own graph betweenessListLength = len(myBetweenessList) print "The length of the betweeness list: ", betweenessListLength clusteringCoefficentListLength = len(myClusteringCoefficientList) print "the length of the clustering coefficent list: ", clusteringCoefficentListLength
The length of the betweeness list: 5 the length of the clustering coefficent list: 4

To determine the degree of a particular node, you need to access a particular cell in the list. You do this by identifying the cell of interest with the list variable.

# print the first cell of the list - notice that the index starts at 0, not 1 print myDegreeList[0] # print the second cell of the list print myDegreeList[1] # print the third cell of the list print myDegreeList[2] #print the fourth cell of the list print myDegreeList[3]
2 3 3 2

Your turn! Determine the betweeness and clustering coefficient for all the nodes in the network.

#First do Betweeness #print the first cell of the list print myBetweenessList[0] #print the second cell of the list print myBetweenessList[1] #print the third cell of the list print myBetweenessList[2] #print the fourth cell of the list print myBetweenessList[3] #Next do Clustering Coefficient #print the first cell of the list print myClusteringCoefficientList[0] #print the second cell of the list print myClusteringCoefficientList[1] #print the third cell of the list print myClusteringCoefficientList[2] #print the fourth cell of the list print myClusteringCoefficientList[3]
1.5 1.0 1.5 1.5 1.0 0.666666666667 0.666666666667 1.0

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

# See if the graph is a weighted graph myGraph.is_weighted()
False

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).

# Add weights to the graph myGraph.es["weight"] = 1 # Check to see if the graph is a weighted graph myGraph.is_weighted()
True
# Print all the edge weights myGraph.es["weight"]
[1, 1, 1, 1, 1]

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

# Make edge(0,1) an edge weight of 2 myGraph[0, 1]=2 # Print all the edge weights myGraph.es["weight"]
[2, 1, 1, 1, 1]

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

# Make edge (1,2) an edge weight of 3 myGraph[1, 2]=3 # Make edge (0,2) an edge weight of 2 myGraph[0, 2]=2 # Make edge (1,3) an edge weight of 4 myGraph[1, 3]=4 # Make edge (2,3) an edge weight of 1.5, if possible (We're legitimately curious if we can do this ) (postscript: apparently we can) myGraph[2, 3]=1.5 #Print all edge weights myGraph.es["weight"]
[2, 3, 2, 1.5, 4]

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

Yes, weighted edges indicate connections of different significance between differnt nodes. For example, the average weight of a connection between node 3 and another node is ((4+1.5)/2)=2.75. In contrast, node 0, though it has the same amount of connections, has a lower average weight of ((2+2)/2)=2.00. As such, node 3 has stronger average connections within this network, and may as such be more significant to the network as a whole–it may be a station with more traffic, a student who has older friendships, an enzyme that is more crucial to various processes, et cetera.