You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/chapters/01-networkrepresentations.html
+2-2Lines changed: 2 additions & 2 deletions
Original file line number
Diff line number
Diff line change
@@ -404,7 +404,7 @@ <h3 class="anchored" data-anchor-id="networks-as-graphs">Networks as Graphs</h3>
404
404
</div>
405
405
</div>
406
406
<divclass="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><divclass="no-row-height column-margin column-container"><spanclass="margin-aside">The code in this collapsed cell imports packages and sets up various plotting options</span></div></div>
<h3class="anchored" data-anchor-id="computing-with-degrees">Computing with Degrees</h3>
427
427
<divclass="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><divclass="no-row-height column-margin column-container"><spanclass="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>
@@ -465,7 +465,7 @@ <h3 class="anchored" data-anchor-id="computing-with-degrees">Computing with Degr
465
465
</div>
466
466
<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>
467
467
<divclass="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 <aclass="quarto-xref" href="#def-degree">Definition <span>2.1</span></a>. With convenient functions from NetworkX and NumPy, this is a two-liner: </p><divclass="no-row-height column-margin column-container"><spanclass="margin-aside">For undirected graphs, we could equally do <code>np.sum(A, axis = 0)</code> because <spanclass="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>
<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>
483
483
<p>Let’s take a moment to compare the first few nodes to make sure that our two methods agree:</p>
<spanid="cb6-3"><aaria-hidden="true" href="#cb6-3" tabindex="-1"></a><spanclass="cf">if</span> i <spanclass="op"><</span><spanclass="dv">5</span>:</span>
@@ -575,7 +575,7 @@ <h3 class="anchored" data-anchor-id="degree-in-directed-graphs">Degree in direct
575
575
</div>
576
576
</div>
577
577
<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>
<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 <spanclass="math inline">\((n-1)\)</span>-regular).</p>
@@ -980,7 +980,7 @@ <h2 class="anchored" data-anchor-id="cyclic-and-acyclic-graphs">Cyclic and Acycl
980
980
</div>
981
981
</div>
982
982
<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>
<p>All trees are necessarily simple graphs, because self- and multiedges would create cycles. Trees always have exactly <spanclass="math inline">\(n-1\)</span> edges, as can be proven via induction. Furthermore, any connected graph with <spanclass="math inline">\(n-1\)</span> edges is a tree.</p>
1068
1068
<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>
0 commit comments