Skip to content

Commit

Permalink
Updates material and slides for Lesson 06 (recursion)
Browse files Browse the repository at this point in the history
  • Loading branch information
hlapp committed Nov 14, 2023
1 parent d06104a commit a904659
Show file tree
Hide file tree
Showing 6 changed files with 219 additions and 3 deletions.
98 changes: 97 additions & 1 deletion Lesson-06.qmd
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
title: "Biostat 823 - Recursive data structures in SQL"
author: "Hilmar Lapp"
institute: "Duke University, Department of Biostatistics & Bioinformatics"
date: "Nov 16, 2023"
date: "Nov 14, 2023"
format:
revealjs:
slide-number: true
Expand Down Expand Up @@ -284,3 +284,99 @@ GROUP BY a.ID, a.Name
HAVING COUNT(n.Name) > 1
ORDER BY Min_Path_Len;
```

## Directed Acyclic Graph (DAG)

:::: {.columns}

::: {.column width="50%"}

![Edges = directed from "start" to "end"](images/Recursion/DAG.png)
:::

::: {.column width="50%"}

![Edges can have a label or type](images/Recursion/DAG with edge types.png)
:::

::::

## Directed Graph as relational model {.smaller}

:::: {.columns}

::: {.column width="45%"}

```{mermaid}
%%| fig-cap: "Relational model of a Directed Graph: Adjacency table connecting nodes to each other. (Note that acyclic property of graph cannot be enforced by a relational database.)"
erDiagram
Node ||--o{ Edge : "edge start"
Node ||--o{ Edge : "edge end"
Node {
integer ID PK
string Label
}
Edge {
integer startNode FK
string Label
integer endNode FK
}
```
:::

::: {.column width="45%"}

```{mermaid}
%%| fig-cap: "Relational model of a Directed Graph with edge type normalized as a node entity. (This could also be a separate table.)"
erDiagram
Node ||--o{ Edge : "edge start"
Node ||--o{ Edge : "edge end"
Node ||--o{ Edge : "edge type"
Node {
integer ID PK
string Label
}
Edge {
integer startNode FK
integer type FK
integer endNode FK
}
```
:::

::::

## Paths through directed graph

* Define recursively: $path(x,y,t,l) := \{ edge(x,y,t) \cup\ edge(path(x,z,t,l-1), y, t) \}$
- There is a path from $x$ to $y$ of type $t$ and length $l$ iff there is an edge from $x$ to $y$ of type $t$, or if there is path from $x$ to $z$ of type $t$ and length $l-1$ and an egde of type $t$ from $z$ to $y$.
* The set of all paths is called the transitive closure.

## Transitive closure for directed graph

```{mermaid}
erDiagram
%%| fig-cap: "Relational model of a Directed Graph with a path table for transitive closure."
Node ||--o{ Edge : "edge start"
Node ||--o{ Edge : "edge end"
Node ||--o{ Edge : "edge type"
Node ||--o{ Path : "path start"
Node ||--o{ Path : "path end"
Node ||--o{ Path : "path type"
Node {
integer ID PK
string Label
boolean isTransitive
}
Edge {
integer startNode FK
integer type FK
integer endNode FK
}
Path {
integer startNode FK
integer type FK
integer endNode FK
integer length
}
```
Binary file added images/Recursion/DAG with edge types.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added images/Recursion/DAG.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
124 changes: 122 additions & 2 deletions pub/Lesson-06.html
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
<meta name="generator" content="quarto-1.3.353">

<meta name="author" content="Hilmar Lapp">
<meta name="dcterms.date" content="2023-11-16">
<meta name="dcterms.date" content="2023-11-14">
<title>Biostat 823 - Recursive data structures in SQL</title>
<meta name="apple-mobile-web-app-capable" content="yes">
<meta name="apple-mobile-web-app-status-bar-style" content="black-translucent">
Expand Down Expand Up @@ -407,7 +407,7 @@ <h1 class="title">Biostat 823 - Recursive data structures in SQL</h1>
</div>
</div>

<p class="date">2023-11-16</p>
<p class="date">2023-11-14</p>
</section>
<section id="recursive-data-structure" class="slide level2">
<h2>Recursive data structure</h2>
Expand Down Expand Up @@ -1208,6 +1208,126 @@ <h2>Transitive closure using CTE (III)</h2>
</table>
</div>
</div>
</section>
<section id="directed-acyclic-graph-dag" class="slide level2">
<h2>Directed Acyclic Graph (DAG)</h2>
<div class="columns">
<div class="column" style="width:50%;">
<div class="quarto-figure quarto-figure-center">
<figure>
<p><img data-src="images/Recursion/DAG.png"></p>
<figcaption>Edges = directed from “start” to “end”</figcaption>
</figure>
</div>
</div><div class="column" style="width:50%;">
<div class="quarto-figure quarto-figure-center">
<figure>
<p><img data-src="images/Recursion/DAG%20with%20edge%20types.png"></p>
<figcaption>Edges can have a label or type</figcaption>
</figure>
</div>
</div>
</div>
</section>
<section id="directed-graph-as-relational-model" class="slide level2 smaller">
<h2>Directed Graph as relational model</h2>
<div class="columns">
<div class="column" style="width:45%;">
<div class="cell" data-reveal="true">
<div class="cell-output-display">
<div>
<div>
<pre class="mermaid mermaid-js">erDiagram
Node ||--o{ Edge : "edge start"
Node ||--o{ Edge : "edge end"
Node {
integer ID PK
string Label
}
Edge {
integer startNode FK
string Label
integer endNode FK
}
</pre>
</div>
<p>Relational model of a Directed Graph: Adjacency table connecting nodes to each other. (Note that acyclic property of graph cannot be enforced by a relational database.)</p>
</div>
</div>
</div>
</div><div class="column" style="width:45%;">
<div class="cell" data-reveal="true">
<div class="cell-output-display">
<div>
<div>
<pre class="mermaid mermaid-js">erDiagram
Node ||--o{ Edge : "edge start"
Node ||--o{ Edge : "edge end"
Node ||--o{ Edge : "edge type"
Node {
integer ID PK
string Label
}
Edge {
integer startNode FK
integer type FK
integer endNode FK
}
</pre>
</div>
<p>Relational model of a Directed Graph with edge type normalized as a node entity. (This could also be a separate table.)</p>
</div>
</div>
</div>
</div>
</div>
</section>
<section id="paths-through-directed-graph" class="slide level2">
<h2>Paths through directed graph</h2>
<ul>
<li>Define recursively: <span class="math inline">\(path(x,y,t,l) := \{ edge(x,y,t) \cup\ edge(path(x,z,t,l-1), y, t) \}\)</span>
<ul>
<li>There is a path from <span class="math inline">\(x\)</span> to <span class="math inline">\(y\)</span> of type <span class="math inline">\(t\)</span> and length <span class="math inline">\(l\)</span> iff there is an edge from <span class="math inline">\(x\)</span> to <span class="math inline">\(y\)</span> of type <span class="math inline">\(t\)</span>, or if there is path from <span class="math inline">\(x\)</span> to <span class="math inline">\(z\)</span> of type <span class="math inline">\(t\)</span> and length <span class="math inline">\(l-1\)</span> and an egde of type <span class="math inline">\(t\)</span> from <span class="math inline">\(z\)</span> to <span class="math inline">\(y\)</span>.</li>
</ul></li>
<li>The set of all paths is called the transitive closure.</li>
</ul>
</section>
<section id="transitive-closure-for-directed-graph" class="slide level2">
<h2>Transitive closure for directed graph</h2>
<div class="cell" data-reveal="true">
<div class="cell-output-display">
<div>
<div>

<pre class="mermaid mermaid-js">erDiagram
%%| fig-cap: "Relational model of a Directed Graph with a path table for transitive closure."
Node ||--o{ Edge : "edge start"
Node ||--o{ Edge : "edge end"
Node ||--o{ Edge : "edge type"
Node ||--o{ Path : "path start"
Node ||--o{ Path : "path end"
Node ||--o{ Path : "path type"
Node {
integer ID PK
string Label
boolean isTransitive
}
Edge {
integer startNode FK
integer type FK
integer endNode FK
}
Path {
integer startNode FK
integer type FK
integer endNode FK
integer length
}
</pre>
</div>
</div>
</div>
</div>
<div class="footer footer-default">

</div>
Expand Down
Binary file added pub/images/Recursion/DAG with edge types.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added pub/images/Recursion/DAG.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit a904659

Please sign in to comment.