Website/src/teaching/cse-250/2022fa/slide/19b-Orderings.erb

570 lines
14 KiB
Plaintext

---
template: templates/cse250_2022_slides.erb
title: Graph Exploration
date: Oct 14, 2022
textbookTBD: Ch. 15.3
cat: graphics/19b/cat.png
---
<section>
<h4 class="slide_title">Examples</h4>
<ul>
<li>$(B, 10)$, $(D, 3)$, $(E, 40)$</li>
<li class="fragment"><b>“A+”</b>, <b>“C”</b>, <b>“B-”</b></li>
<li class="fragment">Taco Tuesday, Fish Friday, Meatless Monday</li>
<li class="fragment">The Princess Bride, Crouching Tiger Hidden Dragon, Rob Roy</li>
<li class="fragment">Aardvark, Baboon, Capybara, Donkey, Echidna, ...</li>
</ul>
</section>
<section>
<h4 class="slide_title">Ordering</h4>
<p>An ordering $(A, \leq)$ (an ordering over type $A$) is ...</p>
<ul>
<li>A collection (set) of things of type $A$</li>
<li>A "relation" or comparator $\leq$ that relates two things</li>
</ul>
</section>
<section>
<dl>
<div>
<dt>$5 \leq 30 \leq 999$</dt>
<dd>Numerical order</dd>
</div>
<div>
<dt>$(E, 40) \leq (B, 10) \leq (D, 3)$</dt>
<dd>Reverse-numerical order on the 2nd field</dd>
</div>
<div>
<dt>$C+ \leq B- \leq B \leq B+ \leq A- \leq A$</dt>
<dd>Letter grades</dd>
</div>
<div>
<dt>$AA \leq AM \leq BZ \leq CA \leq CD$</dt>
<dd>Compare 1st letter, if same compare 2nd, if same compare 3rd, ... <span class="fragment">("Lexical Order")</span></dd>
</div>
</dl>
</section>
<section>
<h4 class="slide_title">Ordering Properties</h4>
<dl>
<div class="fragment">
<dt>Crouching Tiger Hidden Dragon $\leq$ Princess Bride</dt>
<dd>Carey Elwes and Mandy Patankin have amazing banter</dd>
</div>
<div class="fragment">
<dt>Princess Bride $\leq$ Rob Roy</dt>
<dd>Rob Roy: Best climactic duel of all time</dd>
</div>
<div class="fragment">
<dt>Rob Roy $\leq$ Crouching Tiger Hidden Dragon</dt>
<dd>A ton of amazingly technical fights</dd>
</div>
</dl>
</section>
<section>
<h4 class="slide_title">Ordering Properties</h4>
<svg data-src="graphics/19b/films-not-transitive.svg"/>
</section>
<section>
<h4 class="slide_title">Ordering Properties</h4>
<p>An ordering needs to be...</p>
<dl>
<dt>Reflexive</dt>
<dd>$x \leq x$</dd>
<dt>Antisymmetric</dt>
<dd>If $x \leq y$ and $y \leq x$ then $x = y$</dd>
<dt>Transitive</dt>
<dd>If $x \leq y$ and $y \leq z$ then $x \leq z$</dd>
</dl>
</section>
<section>
<h4 class="slide_title">Ordering Properties</h4>
<p>Course 1 $\leq$ Course 2 iff Course 1 is a prereq of Course 2</p>
<ul>
<li>CSE 115 $\leq$ CSE 116</li>
<li>CSE 116 $\leq$ CSE 250</li>
<li>CSE 115 $\leq$ CSE 191</li>
<li>CSE 191 $\leq$ CSE 250</li>
</ul>
</section>
<section>
<h4 class="slide_title">Ordering Properties</h4>
<svg data-src="graphics/19b/partial-order.svg"/>
</section>
<section>
<p>Is this a valid ordering?</p>
<p class="fragment"><b>Yes!</b></p>
<p class="fragment">(<u>Partial order</u>, as opposed to <u>Total order</u>)</p>
</section>
<section>
<h4 class="slide_title">(Partial)Ordering Properties</h4>
<p>A partial ordering needs to be...</p>
<dl>
<dt>Reflexive</dt>
<dd>$x \leq x$</dd>
<dt>Antisymmetric</dt>
<dd>If $x \leq y$ and $y \leq x$ then $x = y$</dd>
<dt>Transitive</dt>
<dd>If $x \leq y$ and $y \leq z$ then $x \leq z$</dd>
</dl>
</section>
<section>
<h4 class="slide_title">(Total)Ordering Properties</h4>
<p>A total ordering needs to be...</p>
<dl>
<dt>Reflexive</dt>
<dd>$x \leq x$</dd>
<dt>Antisymmetric</dt>
<dd>If $x \leq y$ and $y \leq x$ then $x = y$</dd>
<dt>Transitive</dt>
<dd>If $x \leq y$ and $y \leq z$ then $x \leq z$</dd>
<dt style="color: blue">Complete</dt>
<dd style="color: blue">Either $x \leq y$ or $y \leq x$ for any $x, y \in A$.</dd>
</dl>
</section>
<section>
<h4 class="slide_title">Formally...</h4>
<p>For a sort order $(A, \leq)$</p>
<dl>
<dt>Greatest</dt>
<dd>An element $x \in A$ s.t. there is no $y \in A$, where $x \leq y$</dd>
<dt>Least</dt>
<dd>An element $x \in A$ s.t. there is no $y \in A$, where $y \leq x$</dd>
</dl>
<p class="fragment">A <i>partial</i> order may not have a <i>unique</i> greatest/least element</p>
</section>
<section>
<h4 class="slide_title">Formally...</h4>
<p>$\leq$ can be described <b>explicitly, by a set of tuples</b>.</p>
$$\{\; (a, a), (a, b), (a, c), \ldots, (b, b), \ldots, (z, z) \;\}$$
<p class="fragment">If $(x, y)$ is in the set, $x \leq y$</p>
</section>
<section>
<h4 class="slide_title">Formally...</h4>
<p>$\leq$ can be described <b>by a mathematical rule</b>.</p>
$$\{\; (x, y) \;|\; x, y \in \mathbb Z, \exists a \in \mathbb Z_0^{+} : x + a = y \;\}$$
<p class="fragment">$x \leq y$ iff $x, y$ are integers, and there is a non-negative integer $a$ s.t. $x + a = y$</p>
</section>
<section>
<h4 class="slide_title">Formally...</h4>
<p>Multiple orderings can be defined for the same set.</p>
<ul>
<li>RottenTomatoes vs Metacritic vs Box Office Gross.</li>
<li>"Best Movie" first vs "Worst Movie" first.</li>
<li>Rank by number of air ducts crawled through.</li>
</ul>
<p class="fragment">We use a subscripts to separate orderings (e.g., $\leq_1$, $\leq_2$, $\leq_3$)</p>
</section>
<section>
<h4 class="slide_title">Formally...</h4>
<p>We can transform orderings.</p>
<p class="fragment"><b>Reverse:</b> If $x \leq_1 y$, then define $y \leq_r x$</p>
<p class="fragment"><b>Lexical:</b> Given $\leq_1, \leq_2, \leq_3, \ldots$
<ul class="fragment">
<li>If $x \leq_1 y$ then $x \leq_\ell y$</li>
<li>If $x =_1 y$ and $x \leq_2 y$ then $x \leq_\ell y$</li>
<li>If $x =_2 y$ and $x \leq_3 y$ then $x \leq_\ell y$</li>
<li>....</li>
</ul>
</p>
</section>
<section>
<h4 class="slide_title">Examples of Lexical Ordering</h4>
<ul>
<li><b>Names: </b> First letter, then second, then third...</li>
<li><b>Movies: </b> Average of reviews, then number of reviews...</li>
<li><b>Tuples: </b> First field, then second field, then third...</li>
</ul>
</section>
<section>
<h4 class="slide_title">Formally...</h4>
<p>$\leq$ can be described <b>an ordering over a <u>key</u> derived from the element</b>.</p>
<p>$x \leq_{edge} y$ iff $weight(x) \leq weight(y)$</p>
<p>$x \leq_{student} y$ iff $name(x) \leq_{lex} name(y)$</p>
<p class="fragment">We say that the weight (resp., name) is a key.</p>
</section>
<section>
<h4 class="slide_title">Topological Sort</h4>
<p>A <b>Topological Sort</b> $(A, \leq_2)$ is <i>any</i> total order over $x$ that "agrees" with a partial order $(A, \leq_1)$</p>
<p>For any two elements $x, y \in X$:
<ul>
<li>If $x \leq_1 y$ then $x \leq_2 y$</li>
<li>If $y \leq_1 x$ then $y \leq_2 x$</li>
<li>Otherwise, either $x \leq_2 y$ or $y \leq_2 x$</li>
</ul>
</p>
</section>
<section>
<h4 class="slide_title">Topological Sort</h4>
<p>The following are all topological sorts over our partial order example:</p>
<ul>
<li>CSE 115, CSE 116, CSE 191, CSE 241, CSE 250</li>
<li>CSE 241, CSE 115, CSE 116, CSE 191, CSE 250</li>
<li>CSE 115, CSE 191, CSE 116, CSE 250, CSE 241</li>
</ul>
<p class="fragment">(If the partial order is a schedule requirement, each topological sort is a possible schedule)</p>
</section>
<section>
<p>And now, for an ordering-based ADT...</p>
</section>
<section>
<h2>Priority Queues</h2>
</section>
<section>
<h4 class="slide_title">PriorityQueue[A <: Ordering]</h4>
<dl>
<dt>enqueue(v: A): Unit</dt>
<dd>Insert a value $v$ into the priority queue.</dd>
<dt>dequeue: A</dt>
<dd>Remove the greatest element in the priority queue.</dd>
<dt>head: A</dt>
<dd>Peek at the greatest element in the priority queue.</dd>
</dl>
</section>
<section>
<ul style="font-size: 50%; width: 300px; display: inline-block;">
<li class="fragment">enqueue(5)</li>
<li class="fragment">enqueue(9)</li>
<li class="fragment">enqueue(2)</li>
<li class="fragment">enqueue(7)</li>
<li class="fragment">head <span class="fragment">→ 9</span></li>
<li class="fragment">dequeue <span class="fragment">→ 9</span></li>
<li class="fragment">size <span class="fragment">→ 3</span></li>
<li class="fragment">head <span class="fragment">→ 7</span></li>
<li class="fragment">dequeue <span class="fragment">→ 7</span></li>
<li class="fragment">dequeue <span class="fragment">→ 5</span></li>
<li class="fragment">dequeue <span class="fragment">→ 2</span></li>
<li class="fragment">isEmpty <span class="fragment">→ true</span></li>
</ul>
<div style="display: inline-block;" class="fragment">
<p>How do we store these items?</p>
<p class="fragment">5, 9, 2, 7?</p>
<p class="fragment">9, 7, 5, 2?</p>
<p class="fragment">2, 5, 7, 9?</p>
</div>
</section>
<section>
<h4 class="slide_title">Two mentalities...</h4>
<dl>
<dt>Lazy: Keep everything in a mess</dt>
<dd class="fragment">"Selection Sort"</dd>
<dt>Proactive: Keep everything organized</dt>
<dd class="fragment">"Insertion Sort"</dd>
</dl>
</section>
<section>
<h4 class="slide_title">Lazy Priority Queue</h4>
<p><b>Base Data Structure: </b> Linked List</p>
<dl>
<dt>enqueue(t:A)</dt>
<dd class="fragment">Append $t$ to the end of the linked list. <span class="fragment">$O(1)$</span></dd>
<dt>head/dequeue</dt>
<dd class="fragment">Traverse the list to find the largest value. <span class="fragment">$O(n)$</span></dd>
</dl>
</section>
<section>
<h4 class="slide_title">Sort</h4>
<pre><code class="scala">
def pqueueSort[A](items: Seq[A], pqueue: PriorityQueue[A]): Seq[A] =
{
val out = new Array[A](items.size)
for(item <- items){ pqueue.enqueue(item) }
i = out.size - 1
while(!pqueue.isEmpty) { buffer(i) = pqueue.dequeue; i-- }
return out.toSeq
}
</code></pre>
</section>
<section>
<h4 class="slide_title">Selection Sort</h4>
<table style="font-size: 50%">
<tr>
<th></th>
<th>Seq/Buffer</th>
<th>PQueue</th>
</tr>
<tr>
<td>Input</td>
<td>(7, 4, 8, 2, 5, 3, 9)</td>
<td>()</td>
</tr>
<tr class="fragment">
<td>Step 1</td>
<td>(4, 8, 2, 5, 3, 9)</td>
<td>(7)</td>
</tr>
<tr class="fragment">
<td>Step 2</td>
<td>(8, 2, 5, 3, 9)</td>
<td>(7, 4)</td>
</tr>
<tr class="fragment">
<td colspan=3>፧ ፧ ፧</td>
</tr>
<tr class="fragment">
<td>Step N</td>
<td>[_, _, _, _, _, _, _]</td>
<td>(7, 4, 8, 2, 5, 3, 9)</td>
</tr>
<tr class="fragment">
<td>Step N+1</td>
<td>[_, _, _, _, _, _, 9]</td>
<td>(7, 4, 8, 2, 5, 3)</td>
</tr>
<tr class="fragment">
<td>Step N+2</td>
<td>[_, _, _, _, _, 8, 9]</td>
<td>(7, 4, 2, 5, 3)</td>
</tr>
<tr class="fragment">
<td>Step N+3</td>
<td>[_, _, _, _, 7, 8, 9]</td>
<td>(4, 2, 5, 3)</td>
</tr>
<tr class="fragment">
<td>Step N+4</td>
<td>[_, _, _, 5, 7, 8, 9]</td>
<td>(4, 2, 3)</td>
</tr>
<tr class="fragment">
<td>Step N+5</td>
<td>[_, _, 4, 5, 7, 8, 9]</td>
<td>(2, 3)</td>
</tr>
<tr class="fragment">
<td>Step N+6</td>
<td>[_, 3, 4, 5, 7, 8, 9]</td>
<td>(2)</td>
</tr>
<tr class="fragment">
<td>Step 2N</td>
<td>[2, 3, 4, 5, 7, 8, 9]</td>
<td>()</td>
</table>
</section>
<section>
<h4 class="slide_title">Selection Sort</h4>
<pre><code class="scala">
def pqueueSort[A](items: Seq[A], pqueue: PriorityQueue[A]): Seq[A] =
{
val out = new Array[A](items.size)
for(item <- items){ pqueue.enqueue(item) }
i = out.size - 1
while(!pqueue.isEmpty) { buffer(i) = pqueue.dequeue; i-- }
return out.toSeq
}
</code></pre>
<p>What's the complexity?</p>
</section>
<section>
<h4 class="slide_title">Proactive Priority Queue</h4>
<p><b>Base Data Structure: </b> Linked List</p>
<dl>
<dt>enqueue(t:A)</dt>
<dd class="fragment">Find $t$'s position in reverse sorted order. <span class="fragment">$O(n)$</span></dd>
<dt>head/dequeue</dt>
<dd class="fragment">Refer to the head item in the list. <span class="fragment">$O(1)$</span></dd>
</dl>
</section>
<section>
</section><section>
<h4 class="slide_title">Insertion Sort</h4>
<table style="font-size: 50%">
<tr>
<th></th>
<th>Seq/Buffer</th>
<th>PQueue</th>
</tr>
<tr>
<td>Input</td>
<td>(7, 4, 8, 2, 5, 3, 9)</td>
<td>()</td>
</tr>
<tr class="fragment">
<td>Step 1</td>
<td>(4, 8, 2, 5, 3, 9)</td>
<td>(7)</td>
</tr>
<tr class="fragment">
<td>Step 2</td>
<td>(8, 2, 5, 3, 9)</td>
<td>(7, 4)</td>
</tr>
<tr class="fragment">
<td>Step 3</td>
<td>(2, 5, 3, 9)</td>
<td>(8, 7, 4)</td>
</tr>
<tr class="fragment">
<td>Step 4</td>
<td>(5, 3, 9)</td>
<td>(8, 7, 4, 2)</td>
</tr>
<tr class="fragment">
<td>Step 5</td>
<td>(3, 9)</td>
<td>(8, 7, 5, 4, 2)</td>
</tr>
<tr class="fragment">
<td>Step 6</td>
<td>(9)</td>
<td>(8, 7, 5, 4, 3, 2)</td>
</tr>
<tr class="fragment">
<td>Step N</td>
<td>[_, _, _, _, _, _, _]</td>
<td>(9, 8, 7, 5, 4, 3, 2)</td>
</tr>
<tr class="fragment">
<td>Step N+1</td>
<td>[_, _, _, _, _, _, 9]</td>
<td>(8, 7, 5, 4, 3, 2)</td>
</tr>
<tr class="fragment">
<td colspan=3>፧ ፧ ፧</td>
</tr>
<tr class="fragment">
<td>Step 2N</td>
<td>[2, 3, 4, 5, 7, 8, 9]</td>
<td>()</td>
</tr>
</table>
</section>
<section>
<h4 class="slide_title">Insertion Sort</h4>
<pre><code class="scala">
def pqueueSort[A](items: Seq[A], pqueue: PriorityQueue[A]): Seq[A] =
{
val out = new Array[A](items.size)
for(item <- items){ pqueue.enqueue(item) }
i = out.size - 1
while(!pqueue.isEmpty) { buffer(i) = pqueue.dequeue; i-- }
return out.toSeq
}
</code></pre>
<p>What's the complexity?</p>
</section>
<section>
<h4 class="slide_title">Priority Queues</h4>
<table>
<tr>
<th>Operation</th>
<th>Lazy</th>
<th>Proactive</th>
</tr>
<tr>
<td>enqueue</td>
<td>$O(1)$</td>
<td>$O(n)$</td>
</tr>
<tr>
<td>dequeue</td>
<td>$O(n)$</td>
<td>$O(1)$</td>
</tr>
<tr>
<td>head</td>
<td>$O(n)$</td>
<td>$O(1)$</td>
</tr>
</table>
<p class="fragment">Can we do better?</p>
</section>