Skip to content

Commit 565c367

Browse files
committed
Update
1 parent e1c12ed commit 565c367

File tree

71 files changed

+461
-248
lines changed

Some content is hidden

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

71 files changed

+461
-248
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="dfb5e9ee" class="cell" data-execution_count="1">
447+
<div id="fceaac15" 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>
@@ -756,7 +756,7 @@ <h4 class="anchored" data-anchor-id="creating-a-graph-dictionary">Creating a gra
756756
</div>
757757
</div></div></div>
758758
<p>It’s also possible to “go backwards” and obtain the dictionary representation from the graph object using</p>
759-
<div id="da0b545c" class="cell" data-execution_count="8">
759+
<div id="c057f5ed" class="cell" data-execution_count="8">
760760
<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>
761761
<div class="cell-output cell-output-display" data-execution_count="8">
762762
<pre><code>{1: [2], 2: [1, 3, 4], 3: [2, 4], 4: [2, 3]}</code></pre>
1.14 KB
Loading
-923 Bytes
Loading
-341 Bytes
Loading
132 Bytes
Loading
10.8 KB
Loading
-839 Bytes
Loading
-2.7 KB
Loading

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

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -464,7 +464,7 @@ <h2 class="anchored" data-anchor-id="degree-of-a-node">Degree of a Node</h2>
464464
<section id="computing-with-degrees" class="level3 page-columns page-full">
465465
<h3 class="anchored" data-anchor-id="computing-with-degrees">Computing with Degrees</h3>
466466
<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>
467-
<div id="f3d5bfb6" class="cell" data-execution_count="1">
467+
<div id="d1b103f5" class="cell" data-execution_count="1">
468468
<details class="code-fold">
469469
<summary>Code</summary>
470470
<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>
@@ -506,7 +506,7 @@ <h3 class="anchored" data-anchor-id="computing-with-degrees">Computing with Degr
506506
<section id="degrees-from-the-adjacency-matrix" class="level4 page-columns page-full">
507507
<h4 class="anchored" data-anchor-id="degrees-from-the-adjacency-matrix">Degrees from the Adjacency Matrix</h4>
508508
<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>
509-
<div id="202f5a25" class="cell" data-execution_count="3">
509+
<div id="8087c928" class="cell" data-execution_count="3">
510510
<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>
511511
<span id="cb3-2"><a href="#cb3-2" aria-hidden="true" tabindex="-1"></a></span>
512512
<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>
@@ -517,12 +517,12 @@ <h4 class="anchored" data-anchor-id="degrees-from-the-adjacency-matrix">Degrees
517517
</div>
518518
</div>
519519
<p>Networkx also has a helpful built-in function to calculate the complete set of degrees:</p>
520-
<div id="146dd265" class="cell" data-execution_count="4">
520+
<div id="5f38e0e5" class="cell" data-execution_count="4">
521521
<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>
522522
</div>
523523
<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>
524524
<p>Let’s take a moment to compare the first few nodes to make sure that our two methods agree:</p>
525-
<div id="6117b838" class="cell" data-execution_count="5">
525+
<div id="107ac8ef" class="cell" data-execution_count="5">
526526
<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>
527527
<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>
528528
<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>
@@ -617,7 +617,7 @@ <h3 class="anchored" data-anchor-id="degree-in-directed-graphs">Degree in direct
617617
</div>
618618
</div>
619619
<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>
620-
<div id="301a6427" class="cell" data-execution_count="7">
620+
<div id="7e6d5d80" class="cell" data-execution_count="7">
621621
<details class="code-fold">
622622
<summary>Code</summary>
623623
<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>
@@ -665,7 +665,7 @@ <h3 class="anchored" data-anchor-id="degree-in-directed-graphs">Degree in direct
665665
</div>
666666
</div>
667667
<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>
668-
<div id="b2753ccb" class="cell page-columns page-full" data-execution_count="8">
668+
<div id="3a928547" class="cell page-columns page-full" data-execution_count="8">
669669
<details class="code-fold">
670670
<summary>Code</summary>
671671
<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>
@@ -718,7 +718,7 @@ <h2 class="anchored" data-anchor-id="density-and-sparsity">Density and Sparsity<
718718
<li>Calculating directly using mean degree and number of nodes;</li>
719719
<li>Using the built-in NetworkX function <code>nx.density()</code>.</li>
720720
</ul>
721-
<div id="b46490f0" class="cell" data-execution_count="9">
721+
<div id="a15bd7be" class="cell" data-execution_count="9">
722722
<details class="code-fold">
723723
<summary>Code</summary>
724724
<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>
@@ -918,7 +918,7 @@ <h3 class="anchored" data-anchor-id="paths">Paths</h3>
918918
</div>
919919
</div>
920920
<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>
921-
<div id="ac52de85" class="cell page-columns page-full" data-execution_count="11">
921+
<div id="b85e2eaf" class="cell page-columns page-full" data-execution_count="11">
922922
<details class="code-fold">
923923
<summary>Code</summary>
924924
<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>
@@ -1021,7 +1021,7 @@ <h2 class="anchored" data-anchor-id="cyclic-and-acyclic-graphs">Cyclic and Acycl
10211021
</div>
10221022
</div>
10231023
<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>
1024-
<div id="5acd7c33" class="cell" data-execution_count="12">
1024+
<div id="0d911762" class="cell" data-execution_count="12">
10251025
<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>
10261026
<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>
10271027
<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>
@@ -1109,7 +1109,7 @@ <h3 class="anchored" data-anchor-id="trees">Trees</h3>
11091109
</div>
11101110
<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>
11111111
<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>
1112-
<div id="06da8757" class="cell page-columns page-full" data-out.width="80%" data-execution_count="15">
1112+
<div id="6565ebd7" class="cell page-columns page-full" data-out.width="80%" data-execution_count="15">
11131113
<details class="code-fold">
11141114
<summary>Code</summary>
11151115
<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>
-1.58 KB
Loading

0 commit comments

Comments
 (0)