<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:itunes="http://www.itunes.com/dtds/podcast-1.0.dtd" xmlns:googleplay="http://www.google.com/schemas/play-podcasts/1.0"><channel><title><![CDATA[Simon’s Newsletter: For Posterity]]></title><description><![CDATA[Random, unpolished posts meant for linking to other people.]]></description><link>https://www.simonberens.com/s/for-posterity</link><image><url>https://www.simonberens.com/img/substack.png</url><title>Simon’s Newsletter: For Posterity</title><link>https://www.simonberens.com/s/for-posterity</link></image><generator>Substack</generator><lastBuildDate>Tue, 19 May 2026 12:34:52 GMT</lastBuildDate><atom:link href="https://www.simonberens.com/feed" rel="self" type="application/rss+xml"/><copyright><![CDATA[Simon]]></copyright><language><![CDATA[en]]></language><webMaster><![CDATA[sberens@substack.com]]></webMaster><itunes:owner><itunes:email><![CDATA[sberens@substack.com]]></itunes:email><itunes:name><![CDATA[Simon]]></itunes:name></itunes:owner><itunes:author><![CDATA[Simon]]></itunes:author><googleplay:owner><![CDATA[sberens@substack.com]]></googleplay:owner><googleplay:email><![CDATA[sberens@substack.com]]></googleplay:email><googleplay:author><![CDATA[Simon]]></googleplay:author><itunes:block><![CDATA[Yes]]></itunes:block><item><title><![CDATA[Generating Sparse And Dense Random Graphs]]></title><description><![CDATA[Suppose you want to generate a graph with V vertices and E edges, such that each graph (i.e.]]></description><link>https://www.simonberens.com/p/generating-sparse-and-dense-random-graphs</link><guid isPermaLink="false">https://www.simonberens.com/p/generating-sparse-and-dense-random-graphs</guid><dc:creator><![CDATA[Simon]]></dc:creator><pubDate>Tue, 04 Oct 2022 17:16:46 GMT</pubDate><content:encoded><![CDATA[<p>Suppose you want to generate a graph with <code>V</code> vertices and <code>E</code> edges, such that each graph (i.e. each configuration of edges) is equally likely to be generated. For sparse graphs, you can just pick two vertices, and connect them if they&#8217;re not connected already. If they are connected, pick another two vertices. (Repeat <code>E</code> times.) For dense graphs one solution is to generate all edges, randomly permute them, and then pick the first <code>E</code>. The time complexity of both of these algorithms is <code>O(V+E)</code>) (for sparse and dense graphs respectively).</p><p>However, if you want to handle both sparse and dense graphs in a single function you end up having to mash the two algorithms together with an if statement checking if the graph is sparse or dense. Here is a cleaner way:</p><div class="github-gist" data-attrs="{&quot;innerHTML&quot;:&quot;<div id=\&quot;gist118693707\&quot; class=\&quot;gist\&quot;>\n    <div class=\&quot;gist-file\&quot; translate=\&quot;no\&quot;>\n      <div class=\&quot;gist-data\&quot;>\n        <div class=\&quot;js-gist-file-update-container js-task-list-container file-box\&quot;>\n  <div id=\&quot;file-random_graph-py\&quot; class=\&quot;file my-2\&quot;>\n    \n    <div itemprop=\&quot;text\&quot; class=\&quot;Box-body p-0 blob-wrapper data type-python  \&quot;>\n\n        \n<div class=\&quot;js-check-bidi js-blob-code-container blob-code-content\&quot;>\n\n  <template class=\&quot;js-file-alert-template\&quot;>\n  <div data-view-component=\&quot;true\&quot; class=\&quot;flash flash-warn flash-full d-flex flex-items-center\&quot;>\n  <svg aria-hidden=\&quot;true\&quot; height=\&quot;16\&quot; viewBox=\&quot;0 0 16 16\&quot; version=\&quot;1.1\&quot; width=\&quot;16\&quot; data-view-component=\&quot;true\&quot; class=\&quot;octicon octicon-alert\&quot;>\n    <path fill-rule=\&quot;evenodd\&quot; d=\&quot;M8.22 1.754a.25.25 0 00-.44 0L1.698 13.132a.25.25 0 00.22.368h12.164a.25.25 0 00.22-.368L8.22 1.754zm-1.763-.707c.659-1.234 2.427-1.234 3.086 0l6.082 11.378A1.75 1.75 0 0114.082 15H1.918a1.75 1.75 0 01-1.543-2.575L6.457 1.047zM9 11a1 1 0 11-2 0 1 1 0 012 0zm-.25-5.25a.75.75 0 00-1.5 0v2.5a.75.75 0 001.5 0v-2.5z\&quot;></path>\n</svg>\n    <span>\n      This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.\n      <a href=\&quot;https://github.co/hiddenchars\&quot; target=\&quot;_blank\&quot;>Learn more about bidirectional Unicode characters</a>\n    </span>\n\n\n  <div data-view-component=\&quot;true\&quot; class=\&quot;flash-action\&quot;>        <a href=\&quot;{{ revealButtonHref }}\&quot; data-view-component=\&quot;true\&quot; class=\&quot;btn-sm btn\&quot;>    Show hidden characters\n</a>\n</div>\n</div></template>\n<template class=\&quot;js-line-alert-template\&quot;>\n  <span aria-label=\&quot;This line has hidden Unicode characters\&quot; data-view-component=\&quot;true\&quot; class=\&quot;line-alert tooltipped tooltipped-e\&quot;>\n    <svg aria-hidden=\&quot;true\&quot; height=\&quot;16\&quot; viewBox=\&quot;0 0 16 16\&quot; version=\&quot;1.1\&quot; width=\&quot;16\&quot; data-view-component=\&quot;true\&quot; class=\&quot;octicon octicon-alert\&quot;>\n    <path fill-rule=\&quot;evenodd\&quot; d=\&quot;M8.22 1.754a.25.25 0 00-.44 0L1.698 13.132a.25.25 0 00.22.368h12.164a.25.25 0 00.22-.368L8.22 1.754zm-1.763-.707c.659-1.234 2.427-1.234 3.086 0l6.082 11.378A1.75 1.75 0 0114.082 15H1.918a1.75 1.75 0 01-1.543-2.575L6.457 1.047zM9 11a1 1 0 11-2 0 1 1 0 012 0zm-.25-5.25a.75.75 0 00-1.5 0v2.5a.75.75 0 001.5 0v-2.5z\&quot;></path>\n</svg>\n</span></template>\n\n  <table data-hpc class=\&quot;highlight tab-size js-file-line-container js-code-nav-container js-tagsearch-file\&quot; data-tab-size=\&quot;8\&quot; data-paste-markdown-skip data-tagsearch-lang=\&quot;Python\&quot; data-tagsearch-path=\&quot;random_graph.py\&quot;>\n        <tr>\n          <td id=\&quot;file-random_graph-py-L1\&quot; class=\&quot;blob-num js-line-number js-code-nav-line-number js-blob-rnum\&quot; data-line-number=\&quot;1\&quot;></td>\n          <td id=\&quot;file-random_graph-py-LC1\&quot; class=\&quot;blob-code blob-code-inner js-file-line\&quot;><span class=pl-k>import</span> <span class=pl-s1>random</span></td>\n        </tr>\n        <tr>\n          <td id=\&quot;file-random_graph-py-L2\&quot; class=\&quot;blob-num js-line-number js-code-nav-line-number js-blob-rnum\&quot; data-line-number=\&quot;2\&quot;></td>\n          <td id=\&quot;file-random_graph-py-LC2\&quot; class=\&quot;blob-code blob-code-inner js-file-line\&quot;><span class=pl-k>import</span> <span class=pl-s1>math</span></td>\n        </tr>\n        <tr>\n          <td id=\&quot;file-random_graph-py-L3\&quot; class=\&quot;blob-num js-line-number js-code-nav-line-number js-blob-rnum\&quot; data-line-number=\&quot;3\&quot;></td>\n          <td id=\&quot;file-random_graph-py-LC3\&quot; class=\&quot;blob-code blob-code-inner js-file-line\&quot;>\n</td>\n        </tr>\n        <tr>\n          <td id=\&quot;file-random_graph-py-L4\&quot; class=\&quot;blob-num js-line-number js-code-nav-line-number js-blob-rnum\&quot; data-line-number=\&quot;4\&quot;></td>\n          <td id=\&quot;file-random_graph-py-LC4\&quot; class=\&quot;blob-code blob-code-inner js-file-line\&quot;>\n</td>\n        </tr>\n        <tr>\n          <td id=\&quot;file-random_graph-py-L5\&quot; class=\&quot;blob-num js-line-number js-code-nav-line-number js-blob-rnum\&quot; data-line-number=\&quot;5\&quot;></td>\n          <td id=\&quot;file-random_graph-py-LC5\&quot; class=\&quot;blob-code blob-code-inner js-file-line\&quot;><span class=pl-k>def</span> <span class=pl-en>random_pick</span>(<span class=pl-s1>n</span>, <span class=pl-s1>m</span>):</td>\n        </tr>\n        <tr>\n          <td id=\&quot;file-random_graph-py-L6\&quot; class=\&quot;blob-num js-line-number js-code-nav-line-number js-blob-rnum\&quot; data-line-number=\&quot;6\&quot;></td>\n          <td id=\&quot;file-random_graph-py-LC6\&quot; class=\&quot;blob-code blob-code-inner js-file-line\&quot;>    <span class=pl-s>&amp;quot;&amp;quot;&amp;quot;Pick m integers from a bag of the integers in [0, n) without replacement&amp;quot;&amp;quot;&amp;quot;</span></td>\n        </tr>\n        <tr>\n          <td id=\&quot;file-random_graph-py-L7\&quot; class=\&quot;blob-num js-line-number js-code-nav-line-number js-blob-rnum\&quot; data-line-number=\&quot;7\&quot;></td>\n          <td id=\&quot;file-random_graph-py-LC7\&quot; class=\&quot;blob-code blob-code-inner js-file-line\&quot;>    <span class=pl-s1>d</span> <span class=pl-c1>=</span> {<span class=pl-s1>i</span> : <span class=pl-s1>i</span> <span class=pl-k>for</span> <span class=pl-s1>i</span> <span class=pl-c1>in</span> <span class=pl-en>range</span>(<span class=pl-s1>m</span>)} <span class=pl-c># For now, just pick the first m integers</span></td>\n        </tr>\n        <tr>\n          <td id=\&quot;file-random_graph-py-L8\&quot; class=\&quot;blob-num js-line-number js-code-nav-line-number js-blob-rnum\&quot; data-line-number=\&quot;8\&quot;></td>\n          <td id=\&quot;file-random_graph-py-LC8\&quot; class=\&quot;blob-code blob-code-inner js-file-line\&quot;>    <span class=pl-s1>res</span> <span class=pl-c1>=</span> []</td>\n        </tr>\n        <tr>\n          <td id=\&quot;file-random_graph-py-L9\&quot; class=\&quot;blob-num js-line-number js-code-nav-line-number js-blob-rnum\&quot; data-line-number=\&quot;9\&quot;></td>\n          <td id=\&quot;file-random_graph-py-LC9\&quot; class=\&quot;blob-code blob-code-inner js-file-line\&quot;>    <span class=pl-k>for</span> <span class=pl-s1>i</span> <span class=pl-c1>in</span> <span class=pl-en>range</span>(<span class=pl-s1>m</span>): <span class=pl-c># Pick the i-th number</span></td>\n        </tr>\n        <tr>\n          <td id=\&quot;file-random_graph-py-L10\&quot; class=\&quot;blob-num js-line-number js-code-nav-line-number js-blob-rnum\&quot; data-line-number=\&quot;10\&quot;></td>\n          <td id=\&quot;file-random_graph-py-LC10\&quot; class=\&quot;blob-code blob-code-inner js-file-line\&quot;>        <span class=pl-s1>j</span> <span class=pl-c1>=</span> <span class=pl-s1>random</span>.<span class=pl-en>randrange</span>(<span class=pl-s1>i</span>, <span class=pl-s1>n</span>)</td>\n        </tr>\n        <tr>\n          <td id=\&quot;file-random_graph-py-L11\&quot; class=\&quot;blob-num js-line-number js-code-nav-line-number js-blob-rnum\&quot; data-line-number=\&quot;11\&quot;></td>\n          <td id=\&quot;file-random_graph-py-LC11\&quot; class=\&quot;blob-code blob-code-inner js-file-line\&quot;>        <span class=pl-c># Pick whatever is in the j-th slot. If there is nothing, then pick j.</span></td>\n        </tr>\n        <tr>\n          <td id=\&quot;file-random_graph-py-L12\&quot; class=\&quot;blob-num js-line-number js-code-nav-line-number js-blob-rnum\&quot; data-line-number=\&quot;12\&quot;></td>\n          <td id=\&quot;file-random_graph-py-LC12\&quot; class=\&quot;blob-code blob-code-inner js-file-line\&quot;>        <span class=pl-k>if</span> <span class=pl-s1>j</span> <span class=pl-c1>not</span> <span class=pl-c1>in</span> <span class=pl-s1>d</span>:</td>\n        </tr>\n        <tr>\n          <td id=\&quot;file-random_graph-py-L13\&quot; class=\&quot;blob-num js-line-number js-code-nav-line-number js-blob-rnum\&quot; data-line-number=\&quot;13\&quot;></td>\n          <td id=\&quot;file-random_graph-py-LC13\&quot; class=\&quot;blob-code blob-code-inner js-file-line\&quot;>            <span class=pl-s1>d</span>[<span class=pl-s1>j</span>] <span class=pl-c1>=</span> <span class=pl-s1>j</span></td>\n        </tr>\n        <tr>\n          <td id=\&quot;file-random_graph-py-L14\&quot; class=\&quot;blob-num js-line-number js-code-nav-line-number js-blob-rnum\&quot; data-line-number=\&quot;14\&quot;></td>\n          <td id=\&quot;file-random_graph-py-LC14\&quot; class=\&quot;blob-code blob-code-inner js-file-line\&quot;>        <span class=pl-s1>d</span>[<span class=pl-s1>i</span>], <span class=pl-s1>d</span>[<span class=pl-s1>j</span>] <span class=pl-c1>=</span> <span class=pl-s1>d</span>[<span class=pl-s1>j</span>], <span class=pl-s1>d</span>[<span class=pl-s1>i</span>] <span class=pl-c># Swap the contents of the i-th and j-th slot</span></td>\n        </tr>\n        <tr>\n          <td id=\&quot;file-random_graph-py-L15\&quot; class=\&quot;blob-num js-line-number js-code-nav-line-number js-blob-rnum\&quot; data-line-number=\&quot;15\&quot;></td>\n          <td id=\&quot;file-random_graph-py-LC15\&quot; class=\&quot;blob-code blob-code-inner js-file-line\&quot;>        <span class=pl-s1>res</span>.<span class=pl-en>append</span>(<span class=pl-s1>d</span>[<span class=pl-s1>i</span>])</td>\n        </tr>\n        <tr>\n          <td id=\&quot;file-random_graph-py-L16\&quot; class=\&quot;blob-num js-line-number js-code-nav-line-number js-blob-rnum\&quot; data-line-number=\&quot;16\&quot;></td>\n          <td id=\&quot;file-random_graph-py-LC16\&quot; class=\&quot;blob-code blob-code-inner js-file-line\&quot;>    <span class=pl-k>return</span> <span class=pl-s1>res</span></td>\n        </tr>\n        <tr>\n          <td id=\&quot;file-random_graph-py-L17\&quot; class=\&quot;blob-num js-line-number js-code-nav-line-number js-blob-rnum\&quot; data-line-number=\&quot;17\&quot;></td>\n          <td id=\&quot;file-random_graph-py-LC17\&quot; class=\&quot;blob-code blob-code-inner js-file-line\&quot;>\n</td>\n        </tr>\n        <tr>\n          <td id=\&quot;file-random_graph-py-L18\&quot; class=\&quot;blob-num js-line-number js-code-nav-line-number js-blob-rnum\&quot; data-line-number=\&quot;18\&quot;></td>\n          <td id=\&quot;file-random_graph-py-LC18\&quot; class=\&quot;blob-code blob-code-inner js-file-line\&quot;><span class=pl-k>def</span> <span class=pl-en>gen_random_graph</span>(<span class=pl-v>V</span>, <span class=pl-v>E</span>):</td>\n        </tr>\n        <tr>\n          <td id=\&quot;file-random_graph-py-L19\&quot; class=\&quot;blob-num js-line-number js-code-nav-line-number js-blob-rnum\&quot; data-line-number=\&quot;19\&quot;></td>\n          <td id=\&quot;file-random_graph-py-LC19\&quot; class=\&quot;blob-code blob-code-inner js-file-line\&quot;>    <span class=pl-s>&amp;quot;&amp;quot;&amp;quot;Generate an undirected graph in the form of an adjacency list with no duplicate edges or self loops&amp;quot;&amp;quot;&amp;quot;</span></td>\n        </tr>\n        <tr>\n          <td id=\&quot;file-random_graph-py-L20\&quot; class=\&quot;blob-num js-line-number js-code-nav-line-number js-blob-rnum\&quot; data-line-number=\&quot;20\&quot;></td>\n          <td id=\&quot;file-random_graph-py-LC20\&quot; class=\&quot;blob-code blob-code-inner js-file-line\&quot;>    <span class=pl-s1>g</span> <span class=pl-c1>=</span> [[] <span class=pl-k>for</span> <span class=pl-s1>_</span> <span class=pl-c1>in</span> <span class=pl-en>range</span>(<span class=pl-v>V</span>)]</td>\n        </tr>\n        <tr>\n          <td id=\&quot;file-random_graph-py-L21\&quot; class=\&quot;blob-num js-line-number js-code-nav-line-number js-blob-rnum\&quot; data-line-number=\&quot;21\&quot;></td>\n          <td id=\&quot;file-random_graph-py-LC21\&quot; class=\&quot;blob-code blob-code-inner js-file-line\&quot;>    <span class=pl-s1>edges</span> <span class=pl-c1>=</span> <span class=pl-en>random_pick</span>(<span class=pl-s1>math</span>.<span class=pl-en>comb</span>(<span class=pl-v>V</span>, <span class=pl-c1>2</span>), <span class=pl-v>E</span>) <span class=pl-c># Pick E integers that represent the edges</span></td>\n        </tr>\n        <tr>\n          <td id=\&quot;file-random_graph-py-L22\&quot; class=\&quot;blob-num js-line-number js-code-nav-line-number js-blob-rnum\&quot; data-line-number=\&quot;22\&quot;></td>\n          <td id=\&quot;file-random_graph-py-LC22\&quot; class=\&quot;blob-code blob-code-inner js-file-line\&quot;>    <span class=pl-k>for</span> <span class=pl-s1>e</span> <span class=pl-c1>in</span> <span class=pl-s1>edges</span>: <span class=pl-c># Decode the edges into their vertices</span></td>\n        </tr>\n        <tr>\n          <td id=\&quot;file-random_graph-py-L23\&quot; class=\&quot;blob-num js-line-number js-code-nav-line-number js-blob-rnum\&quot; data-line-number=\&quot;23\&quot;></td>\n          <td id=\&quot;file-random_graph-py-LC23\&quot; class=\&quot;blob-code blob-code-inner js-file-line\&quot;>        <span class=pl-s1>u</span> <span class=pl-c1>=</span> <span class=pl-en>int</span>((<span class=pl-c1>1</span> <span class=pl-c1>+</span> <span class=pl-s1>math</span>.<span class=pl-en>sqrt</span>(<span class=pl-c1>1</span> <span class=pl-c1>+</span> <span class=pl-c1>8</span> <span class=pl-c1>*</span> <span class=pl-s1>e</span>)) <span class=pl-c1>/</span> <span class=pl-c1>2</span>)</td>\n        </tr>\n        <tr>\n          <td id=\&quot;file-random_graph-py-L24\&quot; class=\&quot;blob-num js-line-number js-code-nav-line-number js-blob-rnum\&quot; data-line-number=\&quot;24\&quot;></td>\n          <td id=\&quot;file-random_graph-py-LC24\&quot; class=\&quot;blob-code blob-code-inner js-file-line\&quot;>        <span class=pl-s1>v</span> <span class=pl-c1>=</span> <span class=pl-s1>e</span> <span class=pl-c1>-</span> <span class=pl-s1>math</span>.<span class=pl-en>comb</span>(<span class=pl-s1>u</span>, <span class=pl-c1>2</span>)</td>\n        </tr>\n        <tr>\n          <td id=\&quot;file-random_graph-py-L25\&quot; class=\&quot;blob-num js-line-number js-code-nav-line-number js-blob-rnum\&quot; data-line-number=\&quot;25\&quot;></td>\n          <td id=\&quot;file-random_graph-py-LC25\&quot; class=\&quot;blob-code blob-code-inner js-file-line\&quot;>        <span class=pl-s1>g</span>[<span class=pl-s1>u</span>].<span class=pl-en>append</span>(<span class=pl-s1>v</span>)</td>\n        </tr>\n        <tr>\n          <td id=\&quot;file-random_graph-py-L26\&quot; class=\&quot;blob-num js-line-number js-code-nav-line-number js-blob-rnum\&quot; data-line-number=\&quot;26\&quot;></td>\n          <td id=\&quot;file-random_graph-py-LC26\&quot; class=\&quot;blob-code blob-code-inner js-file-line\&quot;>        <span class=pl-s1>g</span>[<span class=pl-s1>v</span>].<span class=pl-en>append</span>(<span class=pl-s1>u</span>)</td>\n        </tr>\n        <tr>\n          <td id=\&quot;file-random_graph-py-L27\&quot; class=\&quot;blob-num js-line-number js-code-nav-line-number js-blob-rnum\&quot; data-line-number=\&quot;27\&quot;></td>\n          <td id=\&quot;file-random_graph-py-LC27\&quot; class=\&quot;blob-code blob-code-inner js-file-line\&quot;>    <span class=pl-k>return</span> <span class=pl-s1>g</span></td>\n        </tr>\n        <tr>\n          <td id=\&quot;file-random_graph-py-L28\&quot; class=\&quot;blob-num js-line-number js-code-nav-line-number js-blob-rnum\&quot; data-line-number=\&quot;28\&quot;></td>\n          <td id=\&quot;file-random_graph-py-LC28\&quot; class=\&quot;blob-code blob-code-inner js-file-line\&quot;>\n</td>\n        </tr>\n        <tr>\n          <td id=\&quot;file-random_graph-py-L29\&quot; class=\&quot;blob-num js-line-number js-code-nav-line-number js-blob-rnum\&quot; data-line-number=\&quot;29\&quot;></td>\n          <td id=\&quot;file-random_graph-py-LC29\&quot; class=\&quot;blob-code blob-code-inner js-file-line\&quot;><span class=pl-c># The complete graph on 4 vertices</span></td>\n        </tr>\n        <tr>\n          <td id=\&quot;file-random_graph-py-L30\&quot; class=\&quot;blob-num js-line-number js-code-nav-line-number js-blob-rnum\&quot; data-line-number=\&quot;30\&quot;></td>\n          <td id=\&quot;file-random_graph-py-LC30\&quot; class=\&quot;blob-code blob-code-inner js-file-line\&quot;><span class=pl-en>print</span>(<span class=pl-en>gen_random_graph</span>(<span class=pl-c1>4</span>, <span class=pl-c1>6</span>)) <span class=pl-c># [[3, 2, 1], [3, 2, 0], [1, 0, 3], [0, 1, 2]]</span></td>\n        </tr>\n  </table>\n</div>\n\n\n    </div>\n\n  </div>\n</div>\n\n      </div>\n      <div class=\&quot;gist-meta\&quot;>\n        <a href=\&quot;https://gist.github.com/SimonBerens/174f74338d6a6a713fa414cb9673174b/raw/d735e69d1db28875dc5427818d6dd60e433ab806/random_graph.py\&quot; style=\&quot;float:right\&quot;>view raw</a>\n        <a href=\&quot;https://gist.github.com/SimonBerens/174f74338d6a6a713fa414cb9673174b#file-random_graph-py\&quot;>\n          random_graph.py\n        </a>\n        hosted with &amp;#10084; by <a href=\&quot;https://github.com\&quot;>GitHub</a>\n      </div>\n    </div>\n</div>\n&quot;,&quot;stylesheet&quot;:&quot;https://github.githubassets.com/assets/gist-embed-d68a95cf247f.css&quot;}" data-component-name="GitgistToDOM"><link rel="stylesheet" href="https://github.githubassets.com/assets/gist-embed-d68a95cf247f.css"><div id="gist118693707" class="gist">
    <div class="gist-file">
      <div class="gist-data">
        <div class="js-gist-file-update-container js-task-list-container file-box">
  <div id="file-random_graph-py" class="file my-2">
    
    <div itemprop="text" class="Box-body p-0 blob-wrapper data type-python  ">

        
<div class="js-check-bidi js-blob-code-container blob-code-content">

  
  <div data-view-component="true" class="flash flash-warn flash-full d-flex flex-items-center">
  
    

    <span>
      This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      <a href="https://github.co/hiddenchars" target="_blank">Learn more about bidirectional Unicode characters</a>
    </span>


  <div data-view-component="true" class="flash-action">        <a href="{{ revealButtonHref }}" data-view-component="true" class="btn-sm btn">    Show hidden characters
</a>
</div>
</div>

  <span data-view-component="true" class="line-alert tooltipped tooltipped-e">
    
    

</span>

  <table data-hpc="" class="highlight tab-size js-file-line-container js-code-nav-container js-tagsearch-file" data-tab-size="8" data-paste-markdown-skip="" data-tagsearch-lang="Python" data-tagsearch-path="random_graph.py">
        <tbody><tr>
          <td id="file-random_graph-py-L1" class="blob-num js-line-number js-code-nav-line-number js-blob-rnum" data-line-number="1"></td>
          <td id="file-random_graph-py-LC1" class="blob-code blob-code-inner js-file-line"><span class="pl-k">import</span> <span class="pl-s1">random</span></td>
        </tr>
        <tr>
          <td id="file-random_graph-py-L2" class="blob-num js-line-number js-code-nav-line-number js-blob-rnum" data-line-number="2"></td>
          <td id="file-random_graph-py-LC2" class="blob-code blob-code-inner js-file-line"><span class="pl-k">import</span> <span class="pl-s1">math</span></td>
        </tr>
        <tr>
          <td id="file-random_graph-py-L3" class="blob-num js-line-number js-code-nav-line-number js-blob-rnum" data-line-number="3"></td>
          <td id="file-random_graph-py-LC3" class="blob-code blob-code-inner js-file-line">
</td>
        </tr>
        <tr>
          <td id="file-random_graph-py-L4" class="blob-num js-line-number js-code-nav-line-number js-blob-rnum" data-line-number="4"></td>
          <td id="file-random_graph-py-LC4" class="blob-code blob-code-inner js-file-line">
</td>
        </tr>
        <tr>
          <td id="file-random_graph-py-L5" class="blob-num js-line-number js-code-nav-line-number js-blob-rnum" data-line-number="5"></td>
          <td id="file-random_graph-py-LC5" class="blob-code blob-code-inner js-file-line"><span class="pl-k">def</span> <span class="pl-en">random_pick</span>(<span class="pl-s1">n</span>, <span class="pl-s1">m</span>):</td>
        </tr>
        <tr>
          <td id="file-random_graph-py-L6" class="blob-num js-line-number js-code-nav-line-number js-blob-rnum" data-line-number="6"></td>
          <td id="file-random_graph-py-LC6" class="blob-code blob-code-inner js-file-line">    <span class="pl-s">"""Pick m integers from a bag of the integers in [0, n) without replacement"""</span></td>
        </tr>
        <tr>
          <td id="file-random_graph-py-L7" class="blob-num js-line-number js-code-nav-line-number js-blob-rnum" data-line-number="7"></td>
          <td id="file-random_graph-py-LC7" class="blob-code blob-code-inner js-file-line">    <span class="pl-s1">d</span> <span class="pl-c1">=</span> {<span class="pl-s1">i</span> : <span class="pl-s1">i</span> <span class="pl-k">for</span> <span class="pl-s1">i</span> <span class="pl-c1">in</span> <span class="pl-en">range</span>(<span class="pl-s1">m</span>)} <span class="pl-c"># For now, just pick the first m integers</span></td>
        </tr>
        <tr>
          <td id="file-random_graph-py-L8" class="blob-num js-line-number js-code-nav-line-number js-blob-rnum" data-line-number="8"></td>
          <td id="file-random_graph-py-LC8" class="blob-code blob-code-inner js-file-line">    <span class="pl-s1">res</span> <span class="pl-c1">=</span> []</td>
        </tr>
        <tr>
          <td id="file-random_graph-py-L9" class="blob-num js-line-number js-code-nav-line-number js-blob-rnum" data-line-number="9"></td>
          <td id="file-random_graph-py-LC9" class="blob-code blob-code-inner js-file-line">    <span class="pl-k">for</span> <span class="pl-s1">i</span> <span class="pl-c1">in</span> <span class="pl-en">range</span>(<span class="pl-s1">m</span>): <span class="pl-c"># Pick the i-th number</span></td>
        </tr>
        <tr>
          <td id="file-random_graph-py-L10" class="blob-num js-line-number js-code-nav-line-number js-blob-rnum" data-line-number="10"></td>
          <td id="file-random_graph-py-LC10" class="blob-code blob-code-inner js-file-line">        <span class="pl-s1">j</span> <span class="pl-c1">=</span> <span class="pl-s1">random</span>.<span class="pl-en">randrange</span>(<span class="pl-s1">i</span>, <span class="pl-s1">n</span>)</td>
        </tr>
        <tr>
          <td id="file-random_graph-py-L11" class="blob-num js-line-number js-code-nav-line-number js-blob-rnum" data-line-number="11"></td>
          <td id="file-random_graph-py-LC11" class="blob-code blob-code-inner js-file-line">        <span class="pl-c"># Pick whatever is in the j-th slot. If there is nothing, then pick j.</span></td>
        </tr>
        <tr>
          <td id="file-random_graph-py-L12" class="blob-num js-line-number js-code-nav-line-number js-blob-rnum" data-line-number="12"></td>
          <td id="file-random_graph-py-LC12" class="blob-code blob-code-inner js-file-line">        <span class="pl-k">if</span> <span class="pl-s1">j</span> <span class="pl-c1">not</span> <span class="pl-c1">in</span> <span class="pl-s1">d</span>:</td>
        </tr>
        <tr>
          <td id="file-random_graph-py-L13" class="blob-num js-line-number js-code-nav-line-number js-blob-rnum" data-line-number="13"></td>
          <td id="file-random_graph-py-LC13" class="blob-code blob-code-inner js-file-line">            <span class="pl-s1">d</span>[<span class="pl-s1">j</span>] <span class="pl-c1">=</span> <span class="pl-s1">j</span></td>
        </tr>
        <tr>
          <td id="file-random_graph-py-L14" class="blob-num js-line-number js-code-nav-line-number js-blob-rnum" data-line-number="14"></td>
          <td id="file-random_graph-py-LC14" class="blob-code blob-code-inner js-file-line">        <span class="pl-s1">d</span>[<span class="pl-s1">i</span>], <span class="pl-s1">d</span>[<span class="pl-s1">j</span>] <span class="pl-c1">=</span> <span class="pl-s1">d</span>[<span class="pl-s1">j</span>], <span class="pl-s1">d</span>[<span class="pl-s1">i</span>] <span class="pl-c"># Swap the contents of the i-th and j-th slot</span></td>
        </tr>
        <tr>
          <td id="file-random_graph-py-L15" class="blob-num js-line-number js-code-nav-line-number js-blob-rnum" data-line-number="15"></td>
          <td id="file-random_graph-py-LC15" class="blob-code blob-code-inner js-file-line">        <span class="pl-s1">res</span>.<span class="pl-en">append</span>(<span class="pl-s1">d</span>[<span class="pl-s1">i</span>])</td>
        </tr>
        <tr>
          <td id="file-random_graph-py-L16" class="blob-num js-line-number js-code-nav-line-number js-blob-rnum" data-line-number="16"></td>
          <td id="file-random_graph-py-LC16" class="blob-code blob-code-inner js-file-line">    <span class="pl-k">return</span> <span class="pl-s1">res</span></td>
        </tr>
        <tr>
          <td id="file-random_graph-py-L17" class="blob-num js-line-number js-code-nav-line-number js-blob-rnum" data-line-number="17"></td>
          <td id="file-random_graph-py-LC17" class="blob-code blob-code-inner js-file-line">
</td>
        </tr>
        <tr>
          <td id="file-random_graph-py-L18" class="blob-num js-line-number js-code-nav-line-number js-blob-rnum" data-line-number="18"></td>
          <td id="file-random_graph-py-LC18" class="blob-code blob-code-inner js-file-line"><span class="pl-k">def</span> <span class="pl-en">gen_random_graph</span>(<span class="pl-v">V</span>, <span class="pl-v">E</span>):</td>
        </tr>
        <tr>
          <td id="file-random_graph-py-L19" class="blob-num js-line-number js-code-nav-line-number js-blob-rnum" data-line-number="19"></td>
          <td id="file-random_graph-py-LC19" class="blob-code blob-code-inner js-file-line">    <span class="pl-s">"""Generate an undirected graph in the form of an adjacency list with no duplicate edges or self loops"""</span></td>
        </tr>
        <tr>
          <td id="file-random_graph-py-L20" class="blob-num js-line-number js-code-nav-line-number js-blob-rnum" data-line-number="20"></td>
          <td id="file-random_graph-py-LC20" class="blob-code blob-code-inner js-file-line">    <span class="pl-s1">g</span> <span class="pl-c1">=</span> [[] <span class="pl-k">for</span> <span class="pl-s1">_</span> <span class="pl-c1">in</span> <span class="pl-en">range</span>(<span class="pl-v">V</span>)]</td>
        </tr>
        <tr>
          <td id="file-random_graph-py-L21" class="blob-num js-line-number js-code-nav-line-number js-blob-rnum" data-line-number="21"></td>
          <td id="file-random_graph-py-LC21" class="blob-code blob-code-inner js-file-line">    <span class="pl-s1">edges</span> <span class="pl-c1">=</span> <span class="pl-en">random_pick</span>(<span class="pl-s1">math</span>.<span class="pl-en">comb</span>(<span class="pl-v">V</span>, <span class="pl-c1">2</span>), <span class="pl-v">E</span>) <span class="pl-c"># Pick E integers that represent the edges</span></td>
        </tr>
        <tr>
          <td id="file-random_graph-py-L22" class="blob-num js-line-number js-code-nav-line-number js-blob-rnum" data-line-number="22"></td>
          <td id="file-random_graph-py-LC22" class="blob-code blob-code-inner js-file-line">    <span class="pl-k">for</span> <span class="pl-s1">e</span> <span class="pl-c1">in</span> <span class="pl-s1">edges</span>: <span class="pl-c"># Decode the edges into their vertices</span></td>
        </tr>
        <tr>
          <td id="file-random_graph-py-L23" class="blob-num js-line-number js-code-nav-line-number js-blob-rnum" data-line-number="23"></td>
          <td id="file-random_graph-py-LC23" class="blob-code blob-code-inner js-file-line">        <span class="pl-s1">u</span> <span class="pl-c1">=</span> <span class="pl-en">int</span>((<span class="pl-c1">1</span> <span class="pl-c1">+</span> <span class="pl-s1">math</span>.<span class="pl-en">sqrt</span>(<span class="pl-c1">1</span> <span class="pl-c1">+</span> <span class="pl-c1">8</span> <span class="pl-c1">*</span> <span class="pl-s1">e</span>)) <span class="pl-c1">/</span> <span class="pl-c1">2</span>)</td>
        </tr>
        <tr>
          <td id="file-random_graph-py-L24" class="blob-num js-line-number js-code-nav-line-number js-blob-rnum" data-line-number="24"></td>
          <td id="file-random_graph-py-LC24" class="blob-code blob-code-inner js-file-line">        <span class="pl-s1">v</span> <span class="pl-c1">=</span> <span class="pl-s1">e</span> <span class="pl-c1">-</span> <span class="pl-s1">math</span>.<span class="pl-en">comb</span>(<span class="pl-s1">u</span>, <span class="pl-c1">2</span>)</td>
        </tr>
        <tr>
          <td id="file-random_graph-py-L25" class="blob-num js-line-number js-code-nav-line-number js-blob-rnum" data-line-number="25"></td>
          <td id="file-random_graph-py-LC25" class="blob-code blob-code-inner js-file-line">        <span class="pl-s1">g</span>[<span class="pl-s1">u</span>].<span class="pl-en">append</span>(<span class="pl-s1">v</span>)</td>
        </tr>
        <tr>
          <td id="file-random_graph-py-L26" class="blob-num js-line-number js-code-nav-line-number js-blob-rnum" data-line-number="26"></td>
          <td id="file-random_graph-py-LC26" class="blob-code blob-code-inner js-file-line">        <span class="pl-s1">g</span>[<span class="pl-s1">v</span>].<span class="pl-en">append</span>(<span class="pl-s1">u</span>)</td>
        </tr>
        <tr>
          <td id="file-random_graph-py-L27" class="blob-num js-line-number js-code-nav-line-number js-blob-rnum" data-line-number="27"></td>
          <td id="file-random_graph-py-LC27" class="blob-code blob-code-inner js-file-line">    <span class="pl-k">return</span> <span class="pl-s1">g</span></td>
        </tr>
        <tr>
          <td id="file-random_graph-py-L28" class="blob-num js-line-number js-code-nav-line-number js-blob-rnum" data-line-number="28"></td>
          <td id="file-random_graph-py-LC28" class="blob-code blob-code-inner js-file-line">
</td>
        </tr>
        <tr>
          <td id="file-random_graph-py-L29" class="blob-num js-line-number js-code-nav-line-number js-blob-rnum" data-line-number="29"></td>
          <td id="file-random_graph-py-LC29" class="blob-code blob-code-inner js-file-line"><span class="pl-c"># The complete graph on 4 vertices</span></td>
        </tr>
        <tr>
          <td id="file-random_graph-py-L30" class="blob-num js-line-number js-code-nav-line-number js-blob-rnum" data-line-number="30"></td>
          <td id="file-random_graph-py-LC30" class="blob-code blob-code-inner js-file-line"><span class="pl-en">print</span>(<span class="pl-en">gen_random_graph</span>(<span class="pl-c1">4</span>, <span class="pl-c1">6</span>)) <span class="pl-c"># [[3, 2, 1], [3, 2, 0], [1, 0, 3], [0, 1, 2]]</span></td>
        </tr>
  </tbody></table>
</div>


    </div>

  </div>
</div>

      </div>
      <div class="gist-meta">
        <a href="https://gist.github.com/SimonBerens/174f74338d6a6a713fa414cb9673174b/raw/d735e69d1db28875dc5427818d6dd60e433ab806/random_graph.py" style="float:right">view raw</a>
        <a href="https://gist.github.com/SimonBerens/174f74338d6a6a713fa414cb9673174b#file-random_graph-py">
          random_graph.py
        </a>
        hosted with &#10084; by <a href="https://github.com">GitHub</a>
      </div>
    </div>
</div>
</div><p>This algorithm is similar to the algorithm for dense graphs, but instead of generating all edges and permuting up front, you do it one edge at a time. The key is an &#8220;implicit&#8221; list of edges, stored as a dictionary of integers -&gt; edges (we encode edges as integers). We can think of the list in two parts: indices <code>[0,i)</code> where we have finalized what edges we picked, and indices <code>[i,n)</code> which store the remaining edges that we can pick from. Thus at each step to pick an edge, we pick a random index from <code>[i,n)</code> and move that edge into index i (while moving what was stored at index i into the selected index).</p><p>The edge -&gt; integer encoding/decoding scheme is as follows: </p><div class="latex-rendered" data-attrs="{&quot;persistentExpression&quot;:&quot;0, 1, 2, 3, &#8230;, { V \\choose 2 } -1 \\rightarrow (1, 0), (2, 0), (2, 1), (3, 0), &#8230;, (V-1, V-2)&quot;,&quot;id&quot;:&quot;GAHJHRHYOS&quot;}" data-component-name="LatexBlockToDOM"></div><p></p><p>How do we decode an integer into the vertices it represents? First, let&#8217;s figure out what the first vertex is (e.g. <code>a</code> is the first vertex in the edge <code>(a,b)</code> ). In a graph with <code>V</code> vertices and <code>E</code> edges, given an edge represented as an integer <code>e</code> (sorry), if the first vertex was <code>u</code> then </p><div class="latex-rendered" data-attrs="{&quot;persistentExpression&quot;:&quot; \\begin{align} e &amp;\\geq 1 + 2 + 3 + &#8230; + (u - 1) \\\\ &amp;= \\frac{1}{2} \\cdot u \\cdot (u -1)  \\\\ 2e &amp; \\geq u^2 - u \\end{align}&quot;,&quot;id&quot;:&quot;WMVWNEEEVC&quot;}" data-component-name="LatexBlockToDOM"></div><p>Solving for the roots and throwing out the solution where u&#8804;0 we get u&#8805;12&#8901;(1+1+8e). Since we want the largest u that still satisfies this condition, we round it down to the nearest integer.</p><p>There are <code>1+2+&#8230;+(u&#8722;1)=(u^2)</code> edges with their first vertex being <code>&lt;u</code>. After that, the edges look like <code>(u,0),(u,1),&#8230;,(u,u&#8722;1),(u+1,0),&#8230;)</code>, so it is sufficient to subtract (u2) from e to obtain the second vertex v.</p><p>Generating the base adjacency list is <code>O(V)</code>, generating the encoded edges is <code>O(E)</code>, and decoding each edge is <code>O(1)</code> so the total time complexity is <code>O(V+E)</code>.</p>]]></content:encoded></item><item><title><![CDATA[The Final Boss Of Hinge]]></title><description><![CDATA[Hates slow walkers]]></description><link>https://www.simonberens.com/p/the-final-boss-of-hinge</link><guid isPermaLink="false">https://www.simonberens.com/p/the-final-boss-of-hinge</guid><dc:creator><![CDATA[Simon]]></dc:creator><pubDate>Sun, 18 Sep 2022 00:57:21 GMT</pubDate><content:encoded><![CDATA[<ul><li><p>Hates slow walkers</p></li><li><p>Wants to debate</p><ul><li><p>cats vs dogs</p></li><li><p>pineapple on pizza</p></li><li><p>spoon vs fork for eating mac &amp; cheese</p></li></ul></li><li><p>Her mantra is everything happens for a reason</p></li><li><p>The most spontaneous thing she&#8217;s done is skydiving</p></li><li><p>Together we could try free samples at Trader Joe&#8217;s</p></li><li><p>This year she really wants to travel</p></li><li><p>Her unusual skill is that she can say the alphabet backwards</p></li><li><p>She won&#8217;t shut up about anything</p></li><li><p>Her simple pleasures are NPR Tiny Desk Concerts</p></li><li><p>A green flag she looks for is a good flirt to roast ratio</p></li><li><p>She geeks out on marvel</p></li><li><p>Her irrational fear is birds</p></li><li><p>The best way to ask her out is to give her a place and a time</p></li><li><p>The key to her heart is</p><ul><li><p>sushi</p></li><li><p>boba</p></li></ul></li><li><p>Will fall for you if you trip her</p></li><li><p>Is obsessed with pickles</p></li><li><p>Is weirdly attracted to</p><ul><li><p>men</p></li><li><p>Danny Devito</p></li></ul></li></ul><p>For the love of god, these tell me nothing about you.</p>]]></content:encoded></item></channel></rss>