{ "cells": [ { "cell_type": "markdown", "id": "dee32b11-7922-44f4-a587-e118c0e1b793", "metadata": {}, "source": [ "# Directed Networks\n", "\n", "As we begin to discuss information networks like citations networks, semantic networks, and especially the Web, being able to work with *directed* graphs becomes an essential skill.\n", "\n", "## DiGraph Objects\n", "\n", "Unlike signed networks, which have no special designation in NetworkX, directed networks use a special `DiGraph()` object that retains directed information about the source and target of each edge." ] }, { "cell_type": "code", "execution_count": 1, "id": "2976a937-0352-4d5d-aaac-6a1ff7f474ed", "metadata": {}, "outputs": [], "source": [ "import networkx as nx\n", "import pandas as pd" ] }, { "cell_type": "code", "execution_count": 2, "id": "c6870e8e-feb5-45dc-89bf-4810c3aee461", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "MultiDiGraph with 297 nodes and 2359 edges\n" ] } ], "source": [ "# Read a DiGraph from a file\n", "# This is actually a \"MultiDiGraph\", with multiple edges between nodes\n", "# This file contains the neural network of C. Elegans\n", "D = nx.read_gml(\"../data/celegansneural.gml\")\n", "print(D)" ] }, { "cell_type": "markdown", "id": "a787d1c1-1992-499c-b056-ef0d9dceda4d", "metadata": {}, "source": [ "Some files (especially `.gml` format) will already indicate that the network is directed. With other files and objects, like edgelists or Pandas DataFrames, you'll need to read or convert the file using `create_using=nx.DiGraph` to make sure that the DiGraph constructor is used to create the object." ] }, { "cell_type": "code", "execution_count": 3, "id": "8bb66da8-6551-4df8-8f0c-99ef1d172fa7", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "DiGraph with 327 nodes and 5818 edges\n" ] } ], "source": [ "# Import an edgelist of high school student interaction data as a DiGraph\n", "HS = nx.read_weighted_edgelist(\"../data/contact-high-school-proj-graph.txt\", create_using=nx.DiGraph)\n", "print(HS)" ] }, { "cell_type": "markdown", "id": "fc625f1b-34fb-4b54-9ae0-daabf685d4f7", "metadata": {}, "source": [ "## In-Degree and Out-Degree\n", "\n", "When you create `DiGraph()` objects, they include methods for in-degree and out-degree as well as standard, undirected degree." ] }, { "cell_type": "code", "execution_count": 4, "id": "4f37501a-7a8f-403a-85aa-edee9e6448f5", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " | neuron_id | \n", "in_degree | \n", "out_degree | \n", "degree | \n", "
---|---|---|---|---|
0 | \n", "1 | \n", "2 | \n", "9 | \n", "11 | \n", "
1 | \n", "51 | \n", "24 | \n", "10 | \n", "34 | \n", "
2 | \n", "72 | \n", "41 | \n", "39 | \n", "80 | \n", "
3 | \n", "77 | \n", "33 | \n", "21 | \n", "54 | \n", "
4 | \n", "78 | \n", "35 | \n", "21 | \n", "56 | \n", "
... | \n", "... | \n", "... | \n", "... | \n", "... | \n", "
292 | \n", "298 | \n", "0 | \n", "1 | \n", "1 | \n", "
293 | \n", "299 | \n", "0 | \n", "1 | \n", "1 | \n", "
294 | \n", "300 | \n", "0 | \n", "1 | \n", "1 | \n", "
295 | \n", "301 | \n", "0 | \n", "1 | \n", "1 | \n", "
296 | \n", "302 | \n", "0 | \n", "1 | \n", "1 | \n", "
297 rows × 4 columns
\n", "