| 1 |
------------------------------------------------------------------------ |
| 2 |
-- The Agda standard library |
| 3 |
-- |
| 4 |
-- Directed acyclic multigraphs |
| 5 |
------------------------------------------------------------------------ |
| 6 |
|
| 7 |
-- A representation of DAGs, based on the idea underlying Martin |
| 8 |
-- Erwig's FGL. Note that this representation does not aim to be |
| 9 |
-- efficient. |
| 10 |
|
| 11 |
{-# OPTIONS --cubical-compatible --safe #-} |
| 12 |
|
| 13 |
module Data.Graph.Acyclic where |
| 14 |
|
| 15 |
open import Level using (_⊔_) |
| 16 |
open import Data.Nat.Base as ℕ using (ℕ; zero; suc; _<′_) |
| 17 |
open import Data.Nat.Induction using (<′-rec; <′-Rec) |
| 18 |
import Data.Nat.Properties as ℕ using (≤⇒≤′) |
| 19 |
open import Data.Fin as Fin |
| 20 |
using (Fin; Fin′; zero; suc; #_; toℕ; _≟_; opposite) |
| 21 |
renaming (_ℕ-ℕ_ to _-_) |
| 22 |
open import Data.Fin.Properties as Fin using (nℕ-ℕi≤n) |
| 23 |
open import Data.Product.Base as Prod using (∃; _×_; _,_) |
| 24 |
open import Data.Maybe.Base as Maybe using (Maybe) |
| 25 |
open import Data.Empty using (⊥) |
| 26 |
open import Data.Unit.Base using (⊤; tt) |
| 27 |
open import Data.Vec.Base as Vec using (Vec; []; _∷_) |
| 28 |
open import Data.List.Base as List using (List; []; _∷_) |
| 29 |
open import Function.Base using (_$_; _∘′_; _∘_; id) |
| 30 |
open import Relation.Binary.Consequences using (dec⇒weaklyDec) |
| 31 |
open import Relation.Binary.PropositionalEquality.Core using (_≡_; refl) |
| 32 |
|
| 33 |
------------------------------------------------------------------------ |
| 34 |
-- A lemma |
| 35 |
|
| 36 |
private |
| 37 |
|
| 38 |
lemma : ∀ n (i : Fin n) → n - suc i <′ n |
| 39 |
lemma (suc n) i = ℕ.≤⇒≤′ $ ℕ.s≤s $ Fin.nℕ-ℕi≤n n i |
| 40 |
|
| 41 |
------------------------------------------------------------------------ |
| 42 |
-- Node contexts |
| 43 |
|
| 44 |
record Context {ℓ e} (Node : Set ℓ) (Edge : Set e) (n : ℕ) : Set (ℓ ⊔ e) where |
| 45 |
constructor context |
| 46 |
field |
| 47 |
label : Node |
| 48 |
successors : List (Edge × Fin n) |
| 49 |
|
| 50 |
open Context public |
| 51 |
|
| 52 |
-- Map for contexts. |
| 53 |
|
| 54 |
module _ {ℓ₁ e₁} {N₁ : Set ℓ₁} {E₁ : Set e₁} |
| 55 |
{ℓ₂ e₂} {N₂ : Set ℓ₂} {E₂ : Set e₂} where |
| 56 |
|
| 57 |
cmap : ∀ {n} → (N₁ → N₂) → (List (E₁ × Fin n) → List (E₂ × Fin n)) → |
| 58 |
Context N₁ E₁ n → Context N₂ E₂ n |
| 59 |
cmap f g c = context (f (label c)) (g (successors c)) |
| 60 |
|
| 61 |
------------------------------------------------------------------------ |
| 62 |
-- Graphs |
| 63 |
|
| 64 |
infixr 3 _&_ |
| 65 |
|
| 66 |
-- The DAGs are indexed on the number of nodes. |
| 67 |
|
| 68 |
data Graph {ℓ e} (Node : Set ℓ) (Edge : Set e) : ℕ → Set (ℓ ⊔ e) where |
| 69 |
∅ : Graph Node Edge 0 |
| 70 |
_&_ : ∀ {n} (c : Context Node Edge n) (g : Graph Node Edge n) → |
| 71 |
Graph Node Edge (suc n) |
| 72 |
|
| 73 |
private |
| 74 |
|
| 75 |
example : Graph ℕ ℕ 5 |
| 76 |
example = context 0 [] & |
| 77 |
context 1 ((10 , # 1) ∷ (11 , # 1) ∷ []) & |
| 78 |
context 2 ((12 , # 0) ∷ []) & |
| 79 |
context 3 [] & |
| 80 |
context 4 [] & |
| 81 |
∅ |
| 82 |
|
| 83 |
------------------------------------------------------------------------ |
| 84 |
-- Higher-order functions |
| 85 |
|
| 86 |
module _ {ℓ e} {N : Set ℓ} {E : Set e} {t} where |
| 87 |
|
| 88 |
-- "Fold right". |
| 89 |
|
| 90 |
foldr : (T : ℕ → Set t) → |
| 91 |
(∀ {n} → Context N E n → T n → T (suc n)) → |
| 92 |
T 0 → |
| 93 |
∀ {m} → Graph N E m → T m |
| 94 |
foldr T _∙_ x ∅ = x |
| 95 |
foldr T _∙_ x (c & g) = c ∙ foldr T _∙_ x g |
| 96 |
|
| 97 |
-- "Fold left". |
| 98 |
|
| 99 |
foldl : ∀ {n} (T : ℕ → Set t) → |
| 100 |
((i : Fin n) → T (toℕ i) → Context N E (n - suc i) → |
| 101 |
T (suc (toℕ i))) → |
| 102 |
T 0 → |
| 103 |
Graph N E n → T n |
| 104 |
foldl T f x ∅ = x |
| 105 |
foldl T f x (c & g) = foldl (T ∘′ suc) (f ∘ suc) (f zero x c) g |
| 106 |
|
| 107 |
|
| 108 |
module _ {ℓ₁ e₁} {N₁ : Set ℓ₁} {E₁ : Set e₁} |
| 109 |
{ℓ₂ e₂} {N₂ : Set ℓ₂} {E₂ : Set e₂} where |
| 110 |
|
| 111 |
-- Maps over node contexts. |
| 112 |
|
| 113 |
map : (∀ {n} → Context N₁ E₁ n → Context N₂ E₂ n) → |
| 114 |
∀ {n} → Graph N₁ E₁ n → Graph N₂ E₂ n |
| 115 |
map f = foldr _ (λ c → f c &_) ∅ |
| 116 |
|
| 117 |
-- Maps over node labels. |
| 118 |
|
| 119 |
nmap : ∀ {ℓ₁ ℓ₂ e} {N₁ : Set ℓ₁} {N₂ : Set ℓ₂} {E : Set e} → |
| 120 |
∀ {n} → (N₁ → N₂) → Graph N₁ E n → Graph N₂ E n |
| 121 |
nmap f = map (cmap f id) |
| 122 |
|
| 123 |
-- Maps over edge labels. |
| 124 |
|
| 125 |
emap : ∀ {ℓ e₁ e₂} {N : Set ℓ} {E₁ : Set e₁} {E₂ : Set e₂} → |
| 126 |
∀ {n} → (E₁ → E₂) → Graph N E₁ n → Graph N E₂ n |
| 127 |
emap f = map (cmap id (List.map (Prod.map f id))) |
| 128 |
|
| 129 |
-- Zips two graphs with the same number of nodes. Note that one of the |
| 130 |
-- graphs has a type which restricts it to be completely disconnected. |
| 131 |
|
| 132 |
zipWith : ∀ {ℓ₁ ℓ₂ ℓ e} {N₁ : Set ℓ₁} {N₂ : Set ℓ₂} {N : Set ℓ} {E : Set e} → |
| 133 |
∀ {n} → (N₁ → N₂ → N) → Graph N₁ ⊥ n → Graph N₂ E n → Graph N E n |
| 134 |
zipWith _∙_ ∅ ∅ = ∅ |
| 135 |
zipWith _∙_ (c₁ & g₁) (c₂ & g₂) = |
| 136 |
context (label c₁ ∙ label c₂) (successors c₂) & zipWith _∙_ g₁ g₂ |
| 137 |
|
| 138 |
------------------------------------------------------------------------ |
| 139 |
-- Specific graphs |
| 140 |
|
| 141 |
-- A completeley disconnected graph. |
| 142 |
|
| 143 |
disconnected : ∀ n → Graph ⊤ ⊥ n |
| 144 |
disconnected zero = ∅ |
| 145 |
disconnected (suc n) = context tt [] & disconnected n |
| 146 |
|
| 147 |
-- A complete graph. |
| 148 |
|
| 149 |
complete : ∀ n → Graph ⊤ ⊤ n |
| 150 |
complete zero = ∅ |
| 151 |
complete (suc n) = |
| 152 |
context tt (List.map (tt ,_) $ Vec.toList (Vec.allFin n)) & |
| 153 |
complete n |
| 154 |
|
| 155 |
------------------------------------------------------------------------ |
| 156 |
-- Queries |
| 157 |
|
| 158 |
module _ {ℓ e} {N : Set ℓ} {E : Set e} where |
| 159 |
|
| 160 |
-- The top-most context. |
| 161 |
|
| 162 |
head : ∀ {n} → Graph N E (suc n) → Context N E n |
| 163 |
head (c & g) = c |
| 164 |
|
| 165 |
-- The remaining graph. |
| 166 |
|
| 167 |
tail : ∀ {n} → Graph N E (suc n) → Graph N E n |
| 168 |
tail (c & g) = g |
| 169 |
|
| 170 |
-- Finds the context and remaining graph corresponding to a given node |
| 171 |
-- index. |
| 172 |
|
| 173 |
infix 4 _[_] |
| 174 |
|
| 175 |
_[_] : ∀ {n} → Graph N E n → (i : Fin n) → Graph N E (suc (n - suc i)) |
| 176 |
(c & g) [ zero ] = c & g |
| 177 |
(c & g) [ suc i ] = g [ i ] |
| 178 |
|
| 179 |
-- The nodes of the graph (node number relative to "topmost" node × |
| 180 |
-- node label). |
| 181 |
|
| 182 |
nodes : ∀ {n} → Graph N E n → Vec (Fin n × N) n |
| 183 |
nodes = Vec.zip (Vec.allFin _) ∘ |
| 184 |
foldr (Vec N) (λ c → label c ∷_) [] |
| 185 |
|
| 186 |
private |
| 187 |
|
| 188 |
test-nodes : nodes example ≡ (# 0 , 0) ∷ (# 1 , 1) ∷ (# 2 , 2) ∷ |
| 189 |
(# 3 , 3) ∷ (# 4 , 4) ∷ [] |
| 190 |
test-nodes = refl |
| 191 |
|
| 192 |
|
| 193 |
module _ {ℓ e} {N : Set ℓ} {E : Set e} where |
| 194 |
|
| 195 |
-- Topological sort. Gives a vector where earlier nodes are never |
| 196 |
-- successors of later nodes. |
| 197 |
|
| 198 |
topSort : ∀ {n} → Graph N E n → Vec (Fin n × N) n |
| 199 |
topSort = nodes |
| 200 |
|
| 201 |
-- The edges of the graph (predecessor × edge label × successor). |
| 202 |
-- |
| 203 |
-- The predecessor is a node number relative to the "topmost" node in |
| 204 |
-- the graph, and the successor is a node number relative to the |
| 205 |
-- predecessor. |
| 206 |
|
| 207 |
edges : ∀ {n} → Graph N E n → List (∃ λ i → E × Fin (n - suc i)) |
| 208 |
edges {n} = |
| 209 |
foldl (λ _ → List (∃ λ i → E × Fin (n - suc i))) |
| 210 |
(λ i es c → es List.++ List.map (i ,_) (successors c)) |
| 211 |
[] |
| 212 |
|
| 213 |
private |
| 214 |
|
| 215 |
test-edges : edges example ≡ (# 1 , 10 , # 1) ∷ (# 1 , 11 , # 1) ∷ |
| 216 |
(# 2 , 12 , # 0) ∷ [] |
| 217 |
test-edges = refl |
| 218 |
|
| 219 |
-- The successors of a given node i (edge label × node number relative |
| 220 |
-- to i). |
| 221 |
|
| 222 |
sucs : ∀ {ℓ e} {N : Set ℓ} {E : Set e} → |
| 223 |
∀ {n} → Graph N E n → (i : Fin n) → List (E × Fin (n - suc i)) |
| 224 |
sucs g i = successors $ head (g [ i ]) |
| 225 |
|
| 226 |
private |
| 227 |
|
| 228 |
test-sucs : sucs example (# 1) ≡ (10 , # 1) ∷ (11 , # 1) ∷ [] |
| 229 |
test-sucs = refl |
| 230 |
|
| 231 |
-- The predecessors of a given node i (node number relative to i × |
| 232 |
-- edge label). |
| 233 |
|
| 234 |
preds : ∀ {ℓ e} {N : Set ℓ} {E : Set e} → |
| 235 |
∀ {n} → Graph N E n → (i : Fin n) → List (Fin′ i × E) |
| 236 |
preds g zero = [] |
| 237 |
preds (c & g) (suc i) = |
| 238 |
List._++_ (List.mapMaybe (p i) $ successors c) |
| 239 |
(List.map (Prod.map suc id) $ preds g i) |
| 240 |
where |
| 241 |
p : ∀ {e} {E : Set e} {n} (i : Fin n) → E × Fin n → Maybe (Fin′ (suc i) × E) |
| 242 |
p i (e , j) = Maybe.map (λ{ refl → zero , e }) (dec⇒weaklyDec _≟_ i j) |
| 243 |
|
| 244 |
private |
| 245 |
|
| 246 |
test-preds : preds example (# 3) ≡ |
| 247 |
(# 1 , 10) ∷ (# 1 , 11) ∷ (# 2 , 12) ∷ [] |
| 248 |
test-preds = refl |
| 249 |
|
| 250 |
------------------------------------------------------------------------ |
| 251 |
-- Operations |
| 252 |
|
| 253 |
-- Weakens a node label. |
| 254 |
|
| 255 |
weaken : ∀ {n} {i : Fin n} → Fin (n - suc i) → Fin n |
| 256 |
weaken {n} {i} j = Fin.inject≤ j (Fin.nℕ-ℕi≤n n (suc i)) |
| 257 |
|
| 258 |
-- Labels each node with its node number. |
| 259 |
|
| 260 |
number : ∀ {ℓ e} {N : Set ℓ} {E : Set e} → |
| 261 |
∀ {n} → Graph N E n → Graph (Fin n × N) E n |
| 262 |
number {N = N} {E} = |
| 263 |
foldr (λ n → Graph (Fin n × N) E n) |
| 264 |
(λ c g → cmap (zero ,_) id c & nmap (Prod.map suc id) g) |
| 265 |
∅ |
| 266 |
|
| 267 |
private |
| 268 |
|
| 269 |
test-number : number example ≡ |
| 270 |
(context (# 0 , 0) [] & |
| 271 |
context (# 1 , 1) ((10 , # 1) ∷ (11 , # 1) ∷ []) & |
| 272 |
context (# 2 , 2) ((12 , # 0) ∷ []) & |
| 273 |
context (# 3 , 3) [] & |
| 274 |
context (# 4 , 4) [] & |
| 275 |
∅) |
| 276 |
test-number = refl |
| 277 |
|
| 278 |
-- Reverses all the edges in the graph. |
| 279 |
|
| 280 |
reverse : ∀ {ℓ e} {N : Set ℓ} {E : Set e} → |
| 281 |
∀ {n} → Graph N E n → Graph N E n |
| 282 |
reverse {N = N} {E} g = |
| 283 |
foldl (Graph N E) |
| 284 |
(λ i g′ c → |
| 285 |
context (label c) |
| 286 |
(List.map (Prod.swap ∘ Prod.map opposite id) $ |
| 287 |
preds g i) |
| 288 |
& g′) |
| 289 |
∅ g |
| 290 |
|
| 291 |
private |
| 292 |
|
| 293 |
test-reverse : reverse (reverse example) ≡ example |
| 294 |
test-reverse = refl |
| 295 |
|
| 296 |
------------------------------------------------------------------------ |
| 297 |
-- Views |
| 298 |
|
| 299 |
-- Expands the subgraph induced by a given node into a tree (thus |
| 300 |
-- losing all sharing). |
| 301 |
|
| 302 |
data Tree {ℓ e} (N : Set ℓ) (E : Set e) : Set (ℓ ⊔ e) where |
| 303 |
node : (label : N) (successors : List (E × Tree N E)) → Tree N E |
| 304 |
|
| 305 |
module _ {ℓ e} {N : Set ℓ} {E : Set e} where |
| 306 |
|
| 307 |
toTree : ∀ {n} → Graph N E n → Fin n → Tree N E |
| 308 |
toTree g i = <′-rec Pred expand _ (g [ i ]) |
| 309 |
where |
| 310 |
Pred = λ n → Graph N E (suc n) → Tree N E |
| 311 |
|
| 312 |
expand : (n : ℕ) → <′-Rec Pred n → Pred n |
| 313 |
expand n rec (c & g) = |
| 314 |
node (label c) |
| 315 |
(List.map |
| 316 |
(Prod.map id (λ i → rec (lemma n i) (g [ i ]))) |
| 317 |
(successors c)) |
| 318 |
|
| 319 |
-- Performs the toTree expansion once for each node. |
| 320 |
|
| 321 |
toForest : ∀ {n} → Graph N E n → Vec (Tree N E) n |
| 322 |
toForest g = Vec.map (toTree g) (Vec.allFin _) |
| 323 |
|
| 324 |
private |
| 325 |
|
| 326 |
test-toForest : toForest example ≡ |
| 327 |
let n3 = node 3 [] in |
| 328 |
node 0 [] ∷ |
| 329 |
node 1 ((10 , n3) ∷ (11 , n3) ∷ []) ∷ |
| 330 |
node 2 ((12 , n3) ∷ []) ∷ |
| 331 |
node 3 [] ∷ |
| 332 |
node 4 [] ∷ |
| 333 |
[] |
| 334 |
test-toForest = refl |