Handling Graphs with NetworkX

Python offers the library NetworkX for manipulating graphs. You can learn more here:

https://networkx.github.io/

https://networkx.github.io/documentation/stable/tutorial.html

Creating a graph

Add nodes to the graph

Draw the graph

Add edges to the graph

Adding an edge with a new node will create the node Charlie in the graph

A graph is a dictionary with nodes as the keys

Each node is a dictionary with the neighbors as the keys, and the edge properties as values

Creating a graph from edges

Reading a graph from a file with list of edges

https://networkx.org/documentation/stable/reference/readwrite/index.html

Graph attributes

You can assign attributes and values to the nodes of the graph

You can also add attributes and values to the edges in the graph

Weighted graphs

A special attribute of a an edge is the "weight". When adding weighted edges, you enter triples consisting of the two edge endpoints and the weight of the edge. This weight is stored in an attribute "weight" by default.

Directed Graphs

You can transform a directed graph into an undirected on using to_undirected()

Graph Operations

Some common graph operations and algorithms are implemented in networkx library.

http://networkx.readthedocs.io/en/networkx-1.11/reference/algorithms.html

Neighbors, degrees and adjancency matrix

Neighbors and degrees for directed or weighted graphs

Connected components

Get the connected component subgraphs

Get the largest connected component

Shortest paths

https://networkx.github.io/documentation/stable/reference/algorithms/link_analysis.html

Pagerank

The result of the Pagerank algorithm is a dictionary with the node ids as keys, and the pagerank values as values.

HITS

Pesronalized Pagerank

We will see the example we presented in class

To define the personalized Pagerank, we will use the "personalization" parameter, were we define the jump vector as a dictionary, with node ids as the keys, and the jump probability to each node as the values

We will now change the jump probability. To change the jump probability we will set the parameter alpha.

Attention: The parameter alpha is not the jump probability as we have described in class, but the probability of following an out-link. The jump probability is 1-alpha.

Betweeness

Drawing Graphs

https://networkx.org/documentation/stable/reference/drawing.html

An example

Change the size of the nodes depending on their pagerank value

A PageRank implementation

We will now do our own implementation of Pagerank. Pagerank values for node $i$ are computed by iterating the following formula:

$$p_i = 0.85\sum_{j\rightarrow i}\frac{p_j}{d_{out}(j)} +0.15\frac 1n$$

We will associate each node with two values: the old pagerank in the previous step and the new one that is currently computed. We initialize the old pagerank to $1/n$

The algorithm goes over the edges in the graph, and for each edge (x,y) transfers a fraction of the Pagerank of x to y (and vice versa since the graph is undirected).

For convergece check we want the maximum difference between old and new Pagerank values to be less than eps.

We got essentially the same values as for the Pagerank vector.