Get startedGet started for free

Types of graphs

1. Types of graphs

Great work! We are now going to go a little bit deeper into the NetworkX API and introduce a few more concepts that you can use in network analysis. NetworkX allows us to model different types of graphs.

2. Undirected graphs

For example, there are social graphs like Facebook by Meta, which are undirected graphs. Undirected graphs are named as such because they are comprised of edges that don't have any inherent directionality associated with them. With Facebook, for example, when one user befriends another, the two are automatically connected with an edge. This is commonly drawn as a line with no arrows between two circles. If we explore this in the IPython terminal,

3. Undirected graphs

you can instantiate an empty graph in NetworkX using nx-dot-Graph and ask for its type. Undirected graphs have the type Graph. On the other hand,

4. Directed graphs

Twitter's social graph is a directed network. This is because of the nature of how users interact with one another. For example, one user may follow another, but that user may not necessarily follow back. As such, there is an inherent directionality associated with the graph. If we explore this in the IPython terminal,

5. Directed graphs

you can instantiate an empty directed graph in NetworkX using nx-dot-DiGraph. If you query for its type, it will return a DiGraph object. We can also have graphs in which there are multiple edges permitted between the nodes.

6. Types of graphs

For example, we may want to model trips between bike sharing stations. Each trip may be one edge between the pair of stations. If we explore this in the IPython terminal,

7. Multi-edge (Directed) graphs

we can likewise instantiate a MultiGraph using nx-dot-MultiGraph. If we check for its type, it will be of the MultiGraph class. Likewise for the MultiDiGraph object. Sometimes, for practical reasons, it may be too memory-intensive to model multiple edges per pair of nodes, and so one may choose

8. Weights on graphs

a single edge that contains a metadata summary of the original. For example, we may want to collapse these three edges

9. Weights on graphs

into a single one

10. Weights on graphs

and give them a "weight" metadata with the value "3", indicating that it was originally 3 edges between the pair of nodes. Let's go through one final concept: the idea of

11. Self-loops

self-loops. Self-loops can be used in certain scenarios, such as in bike sharing data, where a trip begins at a station and ends at the same station. One of the exercises you will encounter will leverage what you've learned so far about the NetworkX API to find edges that are self-loops in a graph.

12. Let's practice!

Alright, let's move on to the exercises now!