Book Image

Network Science with Python and NetworkX Quick Start Guide

By : Edward L. Platt
Book Image

Network Science with Python and NetworkX Quick Start Guide

By: Edward L. Platt

Overview of this book

NetworkX is a leading free and open source package used for network science with the Python programming language. NetworkX can track properties of individuals and relationships, find communities, analyze resilience, detect key network locations, and perform a wide range of important tasks. With the recent release of version 2, NetworkX has been updated to be more powerful and easy to use. If you’re a data scientist, engineer, or computational social scientist, this book will guide you in using the Python programming language to gain insights into real-world networks. Starting with the fundamentals, you’ll be introduced to the core concepts of network science, along with examples that use real-world data and Python code. This book will introduce you to theoretical concepts such as scale-free and small-world networks, centrality measures, and agent-based modeling. You’ll also be able to look for scale-free networks in real data and visualize a network using circular, directed, and shell layouts. By the end of this book, you’ll be able to choose appropriate network representations, use NetworkX to build and characterize networks, and uncover insights while working with real-world systems.
Table of Contents (15 chapters)

Types of networks

The networks presented in this chapter so far have just the bare essentials. These networks are called simple networks because, well, they are simple. In NetworkX, simple networks are represented by the Graph class, described in detail in Chapter 2, Working with Networks in NetworkX.

The name Graph comes from the term used in math to describe networks. You'd think it would mean a picture or drawing, but in this case, it just means a network. Mathematicians often use everyday words in very specific ways that are quite different from their everyday meanings, for example, "graph", "bundle", "ring", and "clearly".

Directed networks

Sometimes, it helps to add just a little more detail to a network. The edges we've seen so far don't have any sense of coming from or going to; they are simply connections between two nodes, so they are called symmetric or undirected.

Imagine a network that represents a system of roads (edges) and intersections (nodes). A network with undirected edges would be a good representation, until you came across a one-way street. An undirected edge suggests that you can travel in either direction equally well, while in reality, driving against traffic is likely to be a rather different experience from driving with it.

When direction matters, a network is called directed. In a directed network, each edge has a source node and a target node. Typically, the edge represents a flow of some kind, for example, traffic, from the source to the target. But what if not all connections are one-way? Easy! Two-way connections are made by combining two directed edges going in opposite directions. In directed networks, edges are drawn with arrows pointing toward the target, as shown in the following diagram. In NetworkX, directed networks are represented by the DiGraph class, also described in Chapter 2, Working with Networks in NetworkX:

Example of a directed network

Weighted networks

Returning to the case of undirected networks, sometimes, not all edges are created equal. For example, in a network representing a city's water distribution system, the edges could represent a series of tubes that carry the water from one place to another. Some of these might have greater capacity than others. When edges can have different strengths, the network is called weighted, and the strength is quantified by a number called the weight. Both directed and undirected networks can be weighted. An example of a weighted network is shown in the following diagram. When visualizing a network, edge weights are often indicated by varying the thickness or opacity of the edge. Edge weights can be used to represent many different types of attributes. The most common ones are described in the next section:

Example of a weighted network

Understanding edges

Edges represent the connections and relationships that make a network. The edges and their weights can have different interpretations, depending on what the network represents. Some common interpretations include the following:

  • Friendships
  • Flows
  • Similarity
  • Distance

Social networks

In social networks, edges most often represent friendship or other interpersonal relationships. Edge weights then represent the strength of the friendship, for example, time spent together, messages exchanged, or the number of common interests.

Flow networks

Flow networks describe the movement of something (people, information, fluid, and so on) from place to place. Edge weights might represent capacity—the maximum amount that can be transported between two nodes—or the actual amount that has traveled through/across the connection.

Similarity networks

In similarity networks, connections are less literal and more abstract. Edge weights correspond to how similar two nodes are, often with zero being not at all, and one being identical. For example, one type of similarity between different people could be calculated by taking their top-10 favorite online cat videos and using the fraction of videos that appear for both people. In this case, the edge weight doesn't have anything to do with whether two people have any kind of relationship. It's quite possible to have an edge with a very high weight connecting two individuals who have never even met!

Spatial networks

Edges can also represent distance (or closeness), especially when nodes represent locations in space. When using edge weights to represent distance, the distance of an entire trip can be calculated by adding together all of the edge weights along a path. Using edge weights to represent distance can sometimes be confusing because a larger number means a weaker connection, and non-existent edges are actually edges with an infinite weight. Sometimes, it can be more intuitive to use a measure of closeness, such as the reciprocal of the distance, although that can complicate working with paths across many edges.

The previous examples cover many of the common applications of networks, but they are by no means exhaustive. Whenever a group of things can have any type of relationship or connection with each other, it is possible to capture the structure of those connections using a network.