Skip to content

Commit 2bdcf29

Browse files
committed
Update
1 parent 7c50eb4 commit 2bdcf29

File tree

63 files changed

+180
-868
lines changed

Some content is hidden

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

63 files changed

+180
-868
lines changed

docs/chapters/01-networkrepresentations.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -404,7 +404,7 @@ <h3 class="anchored" data-anchor-id="networks-as-graphs">Networks as Graphs</h3>
404404
</div>
405405
</div>
406406
<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>
407-
<div class="cell" data-execution_count="1" id="39f1d0d2">
407+
<div class="cell" data-execution_count="1" id="c02907cb">
408408
<details class="code-fold">
409409
<summary>Show code</summary>
410410
<div class="sourceCode cell-code" id="cb1"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb1-1"><a aria-hidden="true" href="#cb1-1" tabindex="-1"></a><span class="im">from</span> matplotlib <span class="im">import</span> pyplot <span class="im">as</span> plt</span>
@@ -704,7 +704,7 @@ <h4 class="anchored" data-anchor-id="creating-a-graph-dictionary">Creating a gra
704704
</div>
705705
</div></div></div>
706706
<p>It’s also possible to “go backwards” and obtain the dictionary representation from the graph object using</p>
707-
<div class="cell" data-execution_count="8" id="a6a6ae00">
707+
<div class="cell" data-execution_count="8" id="7ffa2212">
708708
<div class="sourceCode cell-code" id="cb9"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb9-1"><a aria-hidden="true" href="#cb9-1" tabindex="-1"></a>nx.to_dict_of_lists(G)</span></code><button class="code-copy-button" title="Copy to Clipboard"><i class="bi"></i></button></pre></div>
709709
<div class="cell-output cell-output-display" data-execution_count="8">
710710
<pre><code>{1: [2], 2: [1, 3, 4], 3: [2, 4], 4: [2, 3]}</code></pre>
-13 Bytes
Loading
434 Bytes
Loading
-1.49 KB
Loading
1018 Bytes
Loading
10.1 KB
Loading
683 Bytes
Loading
-1.39 KB
Loading

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

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -425,7 +425,7 @@ <h2 class="anchored" data-anchor-id="degree-of-a-node">Degree of a Node</h2>
425425
<section class="level3 page-columns page-full" id="computing-with-degrees">
426426
<h3 class="anchored" data-anchor-id="computing-with-degrees">Computing with Degrees</h3>
427427
<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>
428-
<div class="cell" data-execution_count="1" id="a0fecae4">
428+
<div class="cell" data-execution_count="1" id="0af9922c">
429429
<details class="code-fold">
430430
<summary>Code</summary>
431431
<div class="sourceCode cell-code" data-lst-cap="hidden code ahoy!" id="cb1"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb1-1"><a aria-hidden="true" href="#cb1-1" tabindex="-1"></a><span class="im">from</span> matplotlib <span class="im">import</span> pyplot <span class="im">as</span> plt</span>
@@ -465,7 +465,7 @@ <h3 class="anchored" data-anchor-id="computing-with-degrees">Computing with Degr
465465
</div>
466466
<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>
467467
<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 class="quarto-xref" href="#def-degree">Definition <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>
468-
<div class="cell" data-execution_count="3" id="b4fe516c">
468+
<div class="cell" data-execution_count="3" id="f91b15e5">
469469
<div class="sourceCode cell-code" id="cb3"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb3-1"><a aria-hidden="true" href="#cb3-1" tabindex="-1"></a><span class="co"># computing degrees directly from the adjacency matrix</span></span>
470470
<span id="cb3-2"><a aria-hidden="true" href="#cb3-2" tabindex="-1"></a></span>
471471
<span id="cb3-3"><a aria-hidden="true" href="#cb3-3" tabindex="-1"></a>A <span class="op">=</span> nx.adjacency_matrix(G_LesMis) <span class="co"># extract the adjacency matrix</span></span>
@@ -476,12 +476,12 @@ <h3 class="anchored" data-anchor-id="computing-with-degrees">Computing with Degr
476476
</div>
477477
</div>
478478
<p>Networkx also has a helpful built-in function to calculate the complete set of degrees:</p>
479-
<div class="cell" data-execution_count="4" id="bc89cbef">
479+
<div class="cell" data-execution_count="4" id="db5b2f27">
480480
<div class="sourceCode cell-code" id="cb5"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb5-1"><a aria-hidden="true" href="#cb5-1" tabindex="-1"></a>degree_view <span class="op">=</span> G_LesMis.degree()</span></code><button class="code-copy-button" title="Copy to Clipboard"><i class="bi"></i></button></pre></div>
481481
</div>
482482
<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>
483483
<p>Let’s take a moment to compare the first few nodes to make sure that our two methods agree:</p>
484-
<div class="cell" data-execution_count="5" id="da0485ff">
484+
<div class="cell" data-execution_count="5" id="ab410450">
485485
<div class="sourceCode cell-code" id="cb6"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb6-1"><a aria-hidden="true" href="#cb6-1" tabindex="-1"></a>i <span class="op">=</span> <span class="dv">0</span></span>
486486
<span id="cb6-2"><a aria-hidden="true" href="#cb6-2" tabindex="-1"></a><span class="cf">for</span> name, degree <span class="kw">in</span> <span class="bu">dict</span>(degree_view).items():</span>
487487
<span id="cb6-3"><a aria-hidden="true" href="#cb6-3" tabindex="-1"></a> <span class="cf">if</span> i <span class="op">&lt;</span> <span class="dv">5</span>:</span>
@@ -575,7 +575,7 @@ <h3 class="anchored" data-anchor-id="degree-in-directed-graphs">Degree in direct
575575
</div>
576576
</div>
577577
<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>
578-
<div class="cell" data-execution_count="7" id="29918aea">
578+
<div class="cell" data-execution_count="7" id="aa2062c9">
579579
<details class="code-fold">
580580
<summary>Code</summary>
581581
<div class="sourceCode cell-code" id="cb9"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb9-1"><a aria-hidden="true" href="#cb9-1" tabindex="-1"></a>A <span class="op">=</span> nx.adjacency_matrix(G_Hamilton) <span class="co"># grab the adjacency matrix</span></span>
@@ -626,7 +626,7 @@ <h3 class="anchored" data-anchor-id="regular-graphs">Regular Graphs</h3>
626626
</div>
627627
</div>
628628
<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>
629-
<div class="cell page-columns page-full" data-execution_count="8" id="0a31d75d">
629+
<div class="cell page-columns page-full" data-execution_count="8" id="69caf930">
630630
<details class="code-fold">
631631
<summary>Code</summary>
632632
<div class="sourceCode cell-code" id="cb11"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb11-1"><a aria-hidden="true" href="#cb11-1" 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>
@@ -678,7 +678,7 @@ <h2 class="anchored" data-anchor-id="density-and-sparsity">Density and Sparsity<
678678
<li>Calculating directly using mean degree and number of nodes;</li>
679679
<li>Using the built-in NetworkX function <code>nx.density()</code>.</li>
680680
</ul>
681-
<div class="cell" data-execution_count="9" id="d421dbcd">
681+
<div class="cell" data-execution_count="9" id="553792e1">
682682
<details class="code-fold">
683683
<summary>Code</summary>
684684
<div class="sourceCode cell-code" id="cb12"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb12-1"><a aria-hidden="true" href="#cb12-1" tabindex="-1"></a><span class="im">from</span> matplotlib <span class="im">import</span> pyplot <span class="im">as</span> plt</span>
@@ -877,7 +877,7 @@ <h3 class="anchored" data-anchor-id="paths">Paths</h3>
877877
</div>
878878
</div>
879879
<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>
880-
<div class="cell page-columns page-full" data-execution_count="11" id="265f7efe">
880+
<div class="cell page-columns page-full" data-execution_count="11" id="e066d893">
881881
<details class="code-fold">
882882
<summary>Code</summary>
883883
<div class="sourceCode cell-code" id="cb15"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb15-1"><a aria-hidden="true" href="#cb15-1" 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>
@@ -980,7 +980,7 @@ <h2 class="anchored" data-anchor-id="cyclic-and-acyclic-graphs">Cyclic and Acycl
980980
</div>
981981
</div>
982982
<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>
983-
<div class="cell" data-execution_count="12" id="009bd865">
983+
<div class="cell" data-execution_count="12" id="c49be497">
984984
<div class="sourceCode cell-code" id="cb16"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb16-1"><a aria-hidden="true" href="#cb16-1" tabindex="-1"></a><span class="kw">def</span> is_cyclic(G):</span>
985985
<span id="cb16-2"><a aria-hidden="true" href="#cb16-2" tabindex="-1"></a> <span class="cf">while</span> G.number_of_nodes() <span class="op">&gt;</span> <span class="dv">0</span>:</span>
986986
<span id="cb16-3"><a aria-hidden="true" href="#cb16-3" 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>
@@ -1066,7 +1066,7 @@ <h3 class="anchored" data-anchor-id="trees">Trees</h3>
10661066
</div>
10671067
<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>
10681068
<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>
1069-
<div class="cell page-columns page-full" data-execution_count="15" data-out.width="80%" id="377b7fb2">
1069+
<div class="cell page-columns page-full" data-execution_count="15" data-out.width="80%" id="1037a6c0">
10701070
<details class="code-fold">
10711071
<summary>Code</summary>
10721072
<div class="sourceCode cell-code" id="cb21"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb21-1"><a aria-hidden="true" href="#cb21-1" 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>
805 Bytes
Loading

0 commit comments

Comments
 (0)