Skip to content

Commit afa6fa5

Browse files
committed
Update
1 parent 85c6d4e commit afa6fa5

File tree

51 files changed

+427
-292
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

51 files changed

+427
-292
lines changed

docs/chapters/01-networkrepresentations.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -444,7 +444,7 @@ <h3 class="anchored" data-anchor-id="networks-as-graphs">Networks as Graphs</h3>
444444
</div>
445445
</div>
446446
<div class="page-columns page-full"><p>Here’s an example of a famous graph: the Zachary Karate Club. Each node represents a member of a university karate club, and each edge represents a friendly social relationship between them. This graph is supplied by <code>networkx</code>, the Python software package that we’ll use throughout these notes for network analysis. </p><div class="no-row-height column-margin column-container"><span class="margin-aside">The code in this collapsed cell imports packages and sets up various plotting options</span></div></div>
447-
<div id="9734b964" class="cell" data-execution_count="1">
447+
<div id="ef606d63" class="cell" data-execution_count="1">
448448
<details class="code-fold">
449449
<summary>Show code</summary>
450450
<div class="sourceCode cell-code" id="cb1"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb1-1"><a href="#cb1-1" aria-hidden="true" tabindex="-1"></a><span class="im">from</span> matplotlib <span class="im">import</span> pyplot <span class="im">as</span> plt</span>
@@ -749,7 +749,7 @@ <h4 class="anchored" data-anchor-id="creating-a-graph-dictionary">Creating a gra
749749
</div>
750750
</div></div></div>
751751
<p>It’s also possible to “go backwards” and obtain the dictionary representation from the graph object using</p>
752-
<div id="072067d6" class="cell" data-execution_count="8">
752+
<div id="514ad04e" class="cell" data-execution_count="8">
753753
<div class="sourceCode cell-code" id="cb9"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb9-1"><a href="#cb9-1" aria-hidden="true" tabindex="-1"></a>nx.to_dict_of_lists(G)</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
754754
<div class="cell-output cell-output-display" data-execution_count="8">
755755
<pre><code>{1: [2], 2: [1, 3, 4], 3: [2, 4], 4: [2, 3]}</code></pre>
-687 Bytes
Loading
-83 Bytes
Loading
2.14 KB
Loading
1.26 KB
Loading
14.3 KB
Loading
-350 Bytes
Loading
349 Bytes
Loading

docs/chapters/02-degree-walks-paths.html

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -465,7 +465,7 @@ <h2 class="anchored" data-anchor-id="degree-of-a-node">Degree of a Node</h2>
465465
<section id="computing-with-degrees" class="level3 page-columns page-full">
466466
<h3 class="anchored" data-anchor-id="computing-with-degrees">Computing with Degrees</h3>
467467
<div class="page-columns page-full"><p>Let’s grab some sample data. We’ll use the <em>Les Miserables</em> network, which is a network of coappearances of characters in the book <em>Les Miserables</em> by Victor Hugo. Nodes represent characters and edges represent characters who appear within the same chapter. This data set is supplied as a built-in example in NetworkX, and for this reason we’ll use it several times throughout these notes. </p><div class="no-row-height column-margin column-container"><span class="margin-aside">This hidden code cell imports several packages and defines an <code>unweight</code> function which we’ll use to convert the network from its native weighted format to an unweighted format.</span></div></div>
468-
<div id="74b2d5ff" class="cell" data-execution_count="1">
468+
<div id="60dd15dd" class="cell" data-execution_count="1">
469469
<details class="code-fold">
470470
<summary>Code</summary>
471471
<div class="sourceCode cell-code" id="cb1" data-lst-cap="hidden code ahoy!"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb1-1"><a href="#cb1-1" aria-hidden="true" tabindex="-1"></a><span class="im">from</span> matplotlib <span class="im">import</span> pyplot <span class="im">as</span> plt</span>
@@ -505,7 +505,7 @@ <h3 class="anchored" data-anchor-id="computing-with-degrees">Computing with Degr
505505
</div>
506506
<p>Now let’s compute the degree of each node. There are multiple ways to achieve this task: we can work directly using the adjacency matrix, or we can use the built-in NetworkX function <code>nx.degree()</code>.</p>
507507
<div class="page-columns page-full"><p>One way to compute the degrees of the nodes in a graph is to use the adjacency matrix, as directly described by <a href="#def-degree" class="quarto-xref">Definition&nbsp;<span>2.1</span></a>. With convenient functions from NetworkX and NumPy, this is a two-liner: </p><div class="no-row-height column-margin column-container"><span class="margin-aside">For undirected graphs, we could equally do <code>np.sum(A, axis = 0)</code> because <span class="math inline">\(\mathbf{A}\)</span> is a symmetric matrix. When we discuss directed graphs soon, it will become necessary to be careful!</span></div></div>
508-
<div id="f5bc4cc7" class="cell" data-execution_count="3">
508+
<div id="ba439e1b" class="cell" data-execution_count="3">
509509
<div class="sourceCode cell-code" id="cb3"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb3-1"><a href="#cb3-1" aria-hidden="true" tabindex="-1"></a><span class="co"># computing degrees directly from the adjacency matrix</span></span>
510510
<span id="cb3-2"><a href="#cb3-2" aria-hidden="true" tabindex="-1"></a></span>
511511
<span id="cb3-3"><a href="#cb3-3" aria-hidden="true" tabindex="-1"></a>A <span class="op">=</span> nx.adjacency_matrix(G_LesMis) <span class="co"># extract the adjacency matrix</span></span>
@@ -516,12 +516,12 @@ <h3 class="anchored" data-anchor-id="computing-with-degrees">Computing with Degr
516516
</div>
517517
</div>
518518
<p>Networkx also has a helpful built-in function to calculate the complete set of degrees:</p>
519-
<div id="a169f2a1" class="cell" data-execution_count="4">
519+
<div id="68b01d83" class="cell" data-execution_count="4">
520520
<div class="sourceCode cell-code" id="cb5"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb5-1"><a href="#cb5-1" aria-hidden="true" tabindex="-1"></a>degree_view <span class="op">=</span> G_LesMis.degree()</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
521521
</div>
522522
<p>The result is a <code>DegreeView</code> object which behaves much like a Python dictionary (and which can be easily converted to a dictionary using the <code>dict</code> constructor).</p>
523523
<p>Let’s take a moment to compare the first few nodes to make sure that our two methods agree:</p>
524-
<div id="4fb24391" class="cell" data-execution_count="5">
524+
<div id="3ff8d10f" class="cell" data-execution_count="5">
525525
<div class="sourceCode cell-code" id="cb6"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb6-1"><a href="#cb6-1" aria-hidden="true" tabindex="-1"></a>i <span class="op">=</span> <span class="dv">0</span></span>
526526
<span id="cb6-2"><a href="#cb6-2" aria-hidden="true" tabindex="-1"></a><span class="cf">for</span> name, degree <span class="kw">in</span> <span class="bu">dict</span>(degree_view).items():</span>
527527
<span id="cb6-3"><a href="#cb6-3" aria-hidden="true" tabindex="-1"></a> <span class="cf">if</span> i <span class="op">&lt;</span> <span class="dv">5</span>:</span>
@@ -615,7 +615,7 @@ <h3 class="anchored" data-anchor-id="degree-in-directed-graphs">Degree in direct
615615
</div>
616616
</div>
617617
<p>Let’s calculate the in-degrees of the nodes in the Hamilton network using the adjacency matrix and compare to the networkx built-ins.</p>
618-
<div id="47beea73" class="cell" data-execution_count="7">
618+
<div id="72e30ae4" class="cell" data-execution_count="7">
619619
<details class="code-fold">
620620
<summary>Code</summary>
621621
<div class="sourceCode cell-code" id="cb9"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb9-1"><a href="#cb9-1" aria-hidden="true" tabindex="-1"></a>A <span class="op">=</span> nx.adjacency_matrix(G_Hamilton) <span class="co"># grab the adjacency matrix</span></span>
@@ -666,7 +666,7 @@ <h3 class="anchored" data-anchor-id="regular-graphs">Regular Graphs</h3>
666666
</div>
667667
</div>
668668
<p>Some special cases of regular graphs are <strong>lattices</strong> (e.g., a square lattice is 4-regular) and the <strong>complete graph</strong> where every node is connected to every other node (which is <span class="math inline">\((n-1)\)</span>-regular).</p>
669-
<div id="a77ef0ac" class="cell page-columns page-full" data-execution_count="8">
669+
<div id="9a65a565" class="cell page-columns page-full" data-execution_count="8">
670670
<details class="code-fold">
671671
<summary>Code</summary>
672672
<div class="sourceCode cell-code" id="cb11"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb11-1"><a href="#cb11-1" aria-hidden="true" tabindex="-1"></a>fig, ax <span class="op">=</span> plt.subplots(<span class="dv">1</span>, <span class="dv">1</span>, figsize <span class="op">=</span> (<span class="dv">2</span>, <span class="dv">2</span>))</span>
@@ -719,7 +719,7 @@ <h2 class="anchored" data-anchor-id="density-and-sparsity">Density and Sparsity<
719719
<li>Calculating directly using mean degree and number of nodes;</li>
720720
<li>Using the built-in NetworkX function <code>nx.density()</code>.</li>
721721
</ul>
722-
<div id="5fc4cac4" class="cell" data-execution_count="9">
722+
<div id="da03d155" class="cell" data-execution_count="9">
723723
<details class="code-fold">
724724
<summary>Code</summary>
725725
<div class="sourceCode cell-code" id="cb12"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb12-1"><a href="#cb12-1" aria-hidden="true" tabindex="-1"></a><span class="im">from</span> matplotlib <span class="im">import</span> pyplot <span class="im">as</span> plt</span>
@@ -919,7 +919,7 @@ <h3 class="anchored" data-anchor-id="paths">Paths</h3>
919919
</div>
920920
</div>
921921
<p>Here is an example of a geodesic path between two nodes in Zachary’s Karate Club network. The geodesic distance between these two nodes is 5.</p>
922-
<div id="c3c49ffd" class="cell page-columns page-full" data-execution_count="11">
922+
<div id="5322d602" class="cell page-columns page-full" data-execution_count="11">
923923
<details class="code-fold">
924924
<summary>Code</summary>
925925
<div class="sourceCode cell-code" id="cb15"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb15-1"><a href="#cb15-1" aria-hidden="true" tabindex="-1"></a><span class="co"># code example adapted from: https://stackoverflow.com/questions/24024411/highlighting-the-shortest-path-in-a-networkx-graph</span></span>
@@ -1022,7 +1022,7 @@ <h2 class="anchored" data-anchor-id="cyclic-and-acyclic-graphs">Cyclic and Acycl
10221022
</div>
10231023
</div>
10241024
<p>Now that we’re convinced that the algorithm works, let’s go ahead and implement it in Python. Our implementation accepts a NetworkX <code>DiGraph</code> object as an argument, returning <code>True</code> if the network is cyclic and <code>False</code> if the network is acyclic.</p>
1025-
<div id="7e9cd7ba" class="cell" data-execution_count="12">
1025+
<div id="a621c4d0" class="cell" data-execution_count="12">
10261026
<div class="sourceCode cell-code" id="cb16"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb16-1"><a href="#cb16-1" aria-hidden="true" tabindex="-1"></a><span class="kw">def</span> is_cyclic(G):</span>
10271027
<span id="cb16-2"><a href="#cb16-2" aria-hidden="true" tabindex="-1"></a> <span class="cf">while</span> G.number_of_nodes() <span class="op">&gt;</span> <span class="dv">0</span>:</span>
10281028
<span id="cb16-3"><a href="#cb16-3" aria-hidden="true" tabindex="-1"></a> zero_out_degree <span class="op">=</span> [node <span class="cf">for</span> node <span class="kw">in</span> G.nodes <span class="cf">if</span> G.out_degree(node) <span class="op">==</span> <span class="dv">0</span>]</span>
@@ -1110,7 +1110,7 @@ <h3 class="anchored" data-anchor-id="trees">Trees</h3>
11101110
</div>
11111111
<p>All trees are necessarily simple graphs, because self- and multiedges would create cycles. Trees always have exactly <span class="math inline">\(n-1\)</span> edges, as can be proven via induction. Furthermore, any connected graph with <span class="math inline">\(n-1\)</span> edges is a tree.</p>
11121112
<p>Trees are often drawn as <em>rooted trees</em> with a <em>root node</em> at the top and <em>leaf nodes</em> below. Any node can be chosen as the root of a tree.</p>
1113-
<div id="00f696ef" class="cell page-columns page-full" data-out.width="80%" data-execution_count="15">
1113+
<div id="b60f832c" class="cell page-columns page-full" data-out.width="80%" data-execution_count="15">
11141114
<details class="code-fold">
11151115
<summary>Code</summary>
11161116
<div class="sourceCode cell-code" id="cb21"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb21-1"><a href="#cb21-1" aria-hidden="true" tabindex="-1"></a>edges <span class="op">=</span> [(<span class="dv">0</span>,<span class="dv">1</span>), (<span class="dv">0</span>,<span class="dv">2</span>), (<span class="dv">1</span>,<span class="dv">3</span>), (<span class="dv">1</span>,<span class="dv">4</span>), (<span class="dv">2</span>,<span class="dv">5</span>), (<span class="dv">2</span>,<span class="dv">6</span>), (<span class="dv">2</span>, <span class="dv">7</span>), (<span class="dv">7</span>, <span class="dv">8</span>)]</span>
-671 Bytes
Loading

0 commit comments

Comments
 (0)