Skip to content

Commit 387730b

Browse files
authored
9.2 kruskal’s algorithm 1 (#53)
* Update 1 Chapter9.lean * Update 2 Chapter9.lean * Update 3 Chapter9.lean * Update 4 Chapter9.lean * Update Chapter9.lean * Update Final Chapter9.lean * Update namespace SpanningTrees Chapter9.lean * Update Chapter9-Ex.lean * Update Final Chapter9.lean * Update Final 2 Chapter9.lean * Update Final 3 Chapter9.lean * Update Chapter9-Ex.lean * Update FInal Chapter9-Ex.lean * Update final Chapter9.lean
1 parent 77bdd3a commit 387730b

File tree

2 files changed

+77
-1
lines changed

2 files changed

+77
-1
lines changed

Fad/Chapter9-Ex.lean

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import Fad.Chapter3
33

44
namespace Chapter9
55
open Chapter3 (accumArray assocs indices)
6-
6+
open SpanningTrees
77

88
-- # Ex 9.2
99

Fad/Chapter9.lean

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,13 @@
11
import Lean.Data
2+
import Fad.Chapter1
3+
import Fad.«Chapter1-Ex»
4+
import Fad.«Chapter5-Ex»
5+
import Fad.Chapter7
26

37
namespace Chapter9
48

9+
namespace SpanningTrees
10+
511
/- 9.1 Graphs and spanning trees -/
612

713
abbrev Vertex := Nat
@@ -26,4 +32,74 @@ def cost (t : Tree) : Int :=
2632

2733
--#eval cost ([1,2,3,4],[(1,2,5), (2,3,5), (3,4,10)])
2834

35+
end SpanningTrees
36+
37+
open SpanningTrees
38+
open Chapter1 (wrap apply)
39+
open Chapter5 (sortOn₃)
40+
41+
/- 9.2 Kruskal’s algorithm -/
42+
43+
abbrev State := Forest × List Edge
44+
45+
def extract (s : State) : Tree :=
46+
s.1.head!
47+
48+
def done (s : State) : Bool :=
49+
s.1.length = 1
50+
51+
def start₀ (g : Graph) : State :=
52+
(nodes g |>.map (fun v => ([v], [])), edges g)
53+
54+
def find (ts : Forest) (v : Vertex) : Tree :=
55+
(ts.find? (fun t => (nodes t).contains v)).get!
56+
57+
def safeEdge (e : Edge) (ts : Forest) : Bool :=
58+
find ts (souce e) ≠ find ts (target e)
59+
60+
def add (e : Edge) (ts : Forest) : Forest :=
61+
let t1 := find ts (souce e)
62+
let t2 := find ts (target e)
63+
let rest := ts.filter (fun t => t ≠ t1 ∧ t ≠ t2)
64+
((t1.1 ++ t2.1), e :: (t1.2 ++ t2.2)) :: rest
65+
66+
def steps : State → List State
67+
| (ts, es) =>
68+
Chapter7.picks es
69+
|>.filterMap (fun (e, es') =>
70+
if safeEdge e ts then
71+
some (add e ts, es')
72+
else
73+
none)
74+
75+
def spats (g : Graph) : List Tree :=
76+
(Chapter1.until' (fun ss => ss.all done) (fun ss => List.flatMap steps ss)) (wrap (start₀ g))
77+
|>.map extract
78+
79+
def MCC (g : Graph) : Tree :=
80+
Chapter7.minWith cost (spats g)
81+
82+
def gstep (s : State) : State :=
83+
match s with
84+
| (_, []) => s
85+
| (ts, e :: es) =>
86+
let t1 := find ts (souce e)
87+
let t2 := find ts (target e)
88+
let rest := ts.filter (fun t => t ≠ t1 ∧ t ≠ t2)
89+
let ts' := ((t1.1 ++ t2.1), e :: (t1.2 ++ t2.2)) :: rest
90+
if t1 ≠ t2 then
91+
(ts', es)
92+
else
93+
gstep (ts, es)
94+
95+
def start₁ (g : Graph) : State :=
96+
(nodes g |>.map (fun v => ([v], [])), sortOn₃ weight (edges g))
97+
98+
def kruskal₁ (g : Graph) : Tree :=
99+
Chapter1.until' done gstep (start₁ g) |> extract
100+
101+
def kruskal₂ (g : Graph) : Tree :=
102+
let n := (nodes g).length
103+
extract (apply (n - 1) gstep (start₁ g))
104+
29105
end Chapter9

0 commit comments

Comments
 (0)