File size: 24,320 Bytes
5d5048e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
import OAI.Combinatorics.ListHadwiger.BagContraction

namespace OAI

namespace LinearListHadwiger
open SimpleGraph PathPacking
open scoped Classical

namespace BagRouting
variable {V I J : Type} {G : SimpleGraph V} {s : I → V} {B : J → Set V}

def used (D : BagRouting G s B) : Set V :=
  {v | ∃ i, v ∈ (D.paths i).2.val.support}

def Minimal [Fintype I] (D : BagRouting G s B) : Prop :=
  ∀ E : BagRouting G s B, D.totalLength ≤ E.totalLength

lemma exists_minimal [Fintype I] (h : Nonempty (BagRouting G s B)) :
    ∃ D : BagRouting G s B, D.Minimal := by
  classical
  have hex : ∃ n : ℕ, ∃ D : BagRouting G s B, D.totalLength = n := by
    obtain ⟨D⟩ := h
    exact ⟨D.totalLength, D, rfl⟩
  obtain ⟨D, hd⟩ := Nat.find_spec hex
  refine ⟨D, fun E => ?_⟩
  rw [hd]
  exact Nat.find_min' hex ⟨E, rfl⟩

noncomputable def replace [DecidableEq I] (D : BagRouting G s B) (i : I)
    (p : G.Path (s i) (D.paths i).1)
    (hp : ∀ j, i ≠ j → ∀ v ∈ p.val.support,
      v ∈ (D.paths j).2.val.support → v = s i ∧ v = s j) : BagRouting G s B := by
  let paths : ∀ a, Σ w, G.Path (s a) w :=
    fun a => if h : a = i then h.symm ▸ (⟨(D.paths i).1, p⟩ : Σ w, G.Path (s i) w) else D.paths a
  refine ⟨paths, D.bag, D.bag_injective, ?_, ?_⟩
  · intro a
    by_cases ha : a = i
    · subst a
      have he : paths i = ⟨(D.paths i).1, p⟩ := dite_eq_left rfl
      rw [he]
      exact D.endpoint_mem i
    · have he : paths a = D.paths a := dite_eq_right ha
      rw [he]
      exact D.endpoint_mem a
  · intro a b hab v hva hvb
    by_cases ha : a = i
    · subst a
      have he : paths i = ⟨(D.paths i).1, p⟩ := dite_eq_left rfl
      have he' : paths b = D.paths b := dite_eq_right (Ne.symm hab)
      rw [he] at hva
      rw [he'] at hvb
      exact hp b hab v hva hvb
    · have he : paths a = D.paths a := dite_eq_right ha
      rw [he] at hva
      by_cases hb : b = i
      · subst b
        have he' : paths i = ⟨(D.paths i).1, p⟩ := dite_eq_left rfl
        rw [he'] at hvb
        exact (hp a (Ne.symm ha) v hvb hva).symm
      · have he' : paths b = D.paths b := dite_eq_right hb
        rw [he'] at hvb
        exact D.overlap a b hab v hva hvb

lemma replace_shorter [Fintype I] [DecidableEq I] (D : BagRouting G s B) (i : I)
    (p : G.Path (s i) (D.paths i).1)
    (hp : ∀ j, i ≠ j → ∀ v ∈ p.val.support,
      v ∈ (D.paths j).2.val.support → v = s i ∧ v = s j)
    (hlt : p.val.length < (D.paths i).2.val.length) :
    (D.replace i p hp).totalLength < D.totalLength := by
  have hi : (D.replace i p hp).paths i = ⟨(D.paths i).1, p⟩ := dite_eq_left rfl
  have ha : ∀ a, a ≠ i → (D.replace i p hp).paths a = D.paths a := fun a h => dite_eq_right h
  apply Finset.sum_lt_sum
  · intro a _
    by_cases h : a = i
    · subst a
      rw [hi]
      exact hlt.le
    · rw [ha a h]
  · refine ⟨i, Finset.mem_univ _, ?_⟩
    rw [hi]
    exact hlt

lemma Minimal.walk_length_le [Fintype I] [DecidableEq V] [DecidableEq I]
    {D : BagRouting G s B} (hD : D.Minimal) (i : I)
    (p : G.Walk (s i) (D.paths i).1)
    (hp : ∀ j, i ≠ j → ∀ v ∈ p.support,
      v ∈ (D.paths j).2.val.support → v = s i ∧ v = s j) :
    (D.paths i).2.val.length ≤ p.length := by
  have hp' : ∀ j, i ≠ j → ∀ v ∈ p.toPath.val.support,
      v ∈ (D.paths j).2.val.support → v = s i ∧ v = s j := by
    intro j hij v hv hvj
    exact hp j hij v (p.support_toPath_subset_support hv) hvj
  have hle : (D.paths i).2.val.length ≤ p.toPath.val.length := by
    by_contra hn
    exact (not_lt_of_ge (hD (D.replace i p.toPath hp')))
      (D.replace_shorter i p.toPath hp' (Nat.lt_of_not_ge hn))
  exact hle.trans p.length_bypass_le_length

lemma Minimal.no_long_shortcut [Fintype I] [DecidableEq V] [DecidableEq I]
    {D : BagRouting G s B} (hD : D.Minimal) (i : I) (v : V) (hv : v ∉ D.used)
    (a b : ℕ) (ha : a ≤ (D.paths i).2.val.length) (hb : b ≤ (D.paths i).2.val.length)
    (hva : G.Adj ((D.paths i).2.val.getVert a) v)
    (hvb : G.Adj v ((D.paths i).2.val.getVert b)) : b ≤ a + 2 := by
  let p := (D.paths i).2.val
  let q : G.Walk (s i) (D.paths i).1 := ((p.take a).concat hva).append (Walk.cons hvb (p.drop b))
  change a ≤ p.length at ha
  change b ≤ p.length at hb
  have hq := hD.walk_length_le i q (by
    intro j hij w hw hwj
    have hw' : w ∈ p.support ∨ w = v := by
      simp only [q, Walk.mem_support_append_iff, Walk.support_concat, Walk.support_cons,
        List.mem_append, List.mem_cons, List.not_mem_nil, or_false] at hw
      rcases hw with (hw | hw) | (hw | hw)
      · left
        rw [Walk.support_take] at hw
        exact List.mem_of_mem_take hw
      · exact Or.inr hw
      · exact Or.inr hw
      · left
        rw [Walk.drop_support_eq_support_drop_min] at hw
        exact List.mem_of_mem_drop hw
    rcases hw' with hw | rfl
    · exact D.overlap i j hij w hw hwj
    · exact (hv ⟨j, hwj⟩).elim)
  have hlen : q.length = a + 2 + (p.length - b) := by
    simp [q, Walk.take_length, Nat.min_eq_left ha, Walk.drop_length]
    omega
  change p.length ≤ q.length at hq
  rw [hlen] at hq
  omega

lemma Minimal.path_neighbors_le_three [Fintype I] [DecidableEq V] [DecidableEq I]
    [DecidableRel G.Adj]
    {D : BagRouting G s B} (hD : D.Minimal) (i : I) (v : V) (hv : v ∉ D.used) :
    ((D.paths i).2.val.support.toFinset.filter fun w => G.Adj v w).card ≤ 3 := by
  classical
  let p := (D.paths i).2.val
  let R := (Finset.range (p.length + 1)).filter fun a => G.Adj v (p.getVert a)
  have hR : R.card ≤ 3 := by
    obtain he | hn := R.eq_empty_or_nonempty
    · simp [he]
    let a := R.min' hn
    have haR : a ∈ R := R.min'_mem hn
    have ha : a ≤ p.length := by
      have hh := (Finset.mem_filter.mp haR).1
      simp only [Finset.mem_range] at hh
      omega
    have hsub : R ⊆ Finset.Icc a (a + 2) := by
      intro b hbR
      have hb : b ≤ p.length := by
        have hh := (Finset.mem_filter.mp hbR).1
        simp only [Finset.mem_range] at hh
        omega
      have hab := hD.no_long_shortcut i v hv a b ha hb
        (Finset.mem_filter.mp haR).2.symm (Finset.mem_filter.mp hbR).2
      exact Finset.mem_Icc.mpr ⟨R.min'_le b hbR, hab⟩
    exact (Finset.card_le_card hsub).trans (by simp; omega)
  have hsub : (p.support.toFinset.filter fun w => G.Adj v w) ⊆ R.image p.getVert := by
    intro w hw
    obtain ⟨hwp, hvw⟩ := Finset.mem_filter.mp hw
    obtain ⟨a, ha, hlen⟩ := Walk.mem_support_iff_exists_getVert.mp (List.mem_toFinset.mp hwp)
    refine Finset.mem_image.mpr ⟨a, Finset.mem_filter.mpr ⟨?_, ?_⟩, ha⟩
    · exact Finset.mem_range.mpr (by omega)
    · rwa [ha]
  exact (Finset.card_le_card hsub).trans ((Finset.card_image_le).trans hR)

noncomputable def usedFinset [Fintype I] [DecidableEq V] (D : BagRouting G s B) : Finset V :=
  Finset.univ.biUnion fun i => (D.paths i).2.val.support.toFinset

lemma mem_usedFinset [Fintype I] [DecidableEq V] (D : BagRouting G s B) (v : V) :
    v ∈ D.usedFinset ↔ v ∈ D.used := by
  simp [usedFinset, used]

lemma Minimal.used_neighbors_le [Fintype I] [DecidableEq V] [DecidableEq I]
    [DecidableRel G.Adj]
    {D : BagRouting G s B} (hD : D.Minimal) (v : V) (hv : v ∉ D.used) :
    (D.usedFinset.filter fun w => G.Adj v w).card ≤ 3 * Fintype.card I := by
  classical
  have he : D.usedFinset.filter (fun w => G.Adj v w) =
      Finset.univ.biUnion (fun i => (D.paths i).2.val.support.toFinset.filter fun w => G.Adj v w) := by
    ext w
    simp only [Finset.mem_filter, usedFinset, Finset.mem_biUnion, Finset.mem_univ, true_and]
    constructor
    · rintro ⟨⟨i, hi⟩, hw⟩
      exact ⟨i, hi, hw⟩
    · rintro ⟨i, hi, hw⟩
      exact ⟨⟨i, hi⟩, hw⟩
  rw [he]
  calc
    _ ≤ ∑ i : I, ((D.paths i).2.val.support.toFinset.filter fun w => G.Adj v w).card := Finset.card_biUnion_le
    _ ≤ ∑ _i : I, 3 := Finset.sum_le_sum (fun i _ => hD.path_neighbors_le_three i v hv)
    _ = 3 * Fintype.card I := by simp [Nat.mul_comm]

lemma mem_take_indices {u w : V} (p : G.Walk u w) (a : ℕ) {v : V}
    (hv : v ∈ (p.take a).support) :
    ∃ n, n ≤ a ∧ n ≤ p.length ∧ p.getVert n = v := by
  obtain ⟨n, hn, hlen⟩ := Walk.mem_support_iff_exists_getVert.mp hv
  rw [Walk.take_length] at hlen
  have hna := (Nat.le_min.mp hlen).1
  have hnl := (Nat.le_min.mp hlen).2
  rw [Walk.take_getVert, Nat.min_eq_right hna] at hn
  exact ⟨n, hna, hnl, hn⟩

lemma mem_drop_indices {u w : V} (p : G.Walk u w) (b : ℕ) (hb : b ≤ p.length)
    {v : V} (hv : v ∈ (p.drop b).support) :
    ∃ n, b ≤ n ∧ n ≤ p.length ∧ p.getVert n = v := by
  obtain ⟨n, hn, hlen⟩ := Walk.mem_support_iff_exists_getVert.mp hv
  rw [Walk.drop_length] at hlen
  rw [Walk.drop_getVert] at hn
  exact ⟨b + n, by omega, by omega, hn⟩

lemma take_drop_disjoint {u w : V} (p : G.Path u w) (a b : ℕ)
    (hab : a < b) (hb : b ≤ p.val.length) :
    Disjoint {v | v ∈ (p.val.take a).support} {v | v ∈ (p.val.drop b).support} := by
  rw [Set.disjoint_left]
  intro v hv ht
  obtain ⟨c, hca, hcl, hc⟩ := mem_take_indices p.val a hv
  obtain ⟨d, hbd, hdl, hd⟩ := mem_drop_indices p.val b hb ht
  have he := p.property.getVert_injOn hcl hdl (hc.trans hd.symm)
  omega

lemma source_not_mem_drop {u w : V} (p : G.Path u w) (b : ℕ)
    (hpos : 0 < b) (hb : b ≤ p.val.length) : u ∉ (p.val.drop b).support := by
  intro hu
  obtain ⟨n, hbn, hnl, hn⟩ := mem_drop_indices p.val b hb hu
  have h0 : p.val.getVert 0 = u := p.val.getVert_zero
  have he := p.property.getVert_injOn (show 0 ≤ p.val.length by omega) hnl (h0.trans hn.symm)
  omega

structure Exchange (D : BagRouting G s B) where
  active : Finset I
  e : Equiv.Perm I
  fixed : ∀ i, i ∉ active → e i = i
  stable : ∀ i, i ∈ active ↔ e i ∈ active
  a : I → ℕ
  b : I → ℕ
  bounds : ∀ i ∈ active, a i + 2 ≤ b i ∧ b i ≤ (D.paths i).2.val.length
  edge : ∀ i ∈ active,
    G.Adj ((D.paths i).2.val.getVert (a i)) ((D.paths (e i)).2.val.getVert (b (e i)))

namespace Exchange
variable [DecidableEq V] [DecidableEq I] {D : BagRouting G s B}

noncomputable def front (E : D.Exchange) (i : I) : Set V :=
  if i ∈ E.active then {v | v ∈ ((D.paths i).2.val.take (E.a i)).support}
  else {v | v ∈ (D.paths i).2.val.support}

noncomputable def tail (E : D.Exchange) (i : I) : Set V :=
  if i ∈ E.active then {v | v ∈ ((D.paths i).2.val.drop (E.b i)).support} else ∅

lemma front_subset {V I J : Type} {G : SimpleGraph V} {s : I → V} {B : J → Set V}
    [DecidableEq V] [DecidableEq I] {D : BagRouting G s B}
    (E : D.Exchange) (i : I) : E.front i ⊆ {v | v ∈ (D.paths i).2.val.support} := by
  intro v hv
  by_cases hi : i ∈ E.active
  · rw [front, ite_eq_left hi] at hv
    rw [Walk.support_take] at hv
    exact List.mem_of_mem_take hv
  · simpa [front, hi] using hv

lemma tail_subset {V I J : Type} {G : SimpleGraph V} {s : I → V} {B : J → Set V}
    [DecidableEq V] [DecidableEq I] {D : BagRouting G s B}
    (E : D.Exchange) (i : I) : E.tail i ⊆ {v | v ∈ (D.paths i).2.val.support} := by
  intro v hv
  by_cases hi : i ∈ E.active
  · rw [tail, ite_eq_left hi] at hv
    rw [Walk.drop_support_eq_support_drop_min] at hv
    exact List.mem_of_mem_drop hv
  · simp [tail, hi] at hv

lemma not_source_tail {V I J : Type} {G : SimpleGraph V} {s : I → V} {B : J → Set V}
    [DecidableEq V] [DecidableEq I] {D : BagRouting G s B}
    (E : D.Exchange) (i : I) : s i ∉ E.tail i := by
  by_cases hi : i ∈ E.active
  · rw [tail, ite_eq_left hi]
    exact source_not_mem_drop _ _ (by have := (E.bounds i hi).1; omega) (E.bounds i hi).2
  · simp [tail, hi]

lemma front_tail_disjoint {V I J : Type} {G : SimpleGraph V} {s : I → V} {B : J → Set V}
    [DecidableEq V] [DecidableEq I] {D : BagRouting G s B}
    (E : D.Exchange) (i : I) : Disjoint (E.front i) (E.tail i) := by
  by_cases hi : i ∈ E.active
  · rw [front, tail, ite_eq_left hi, ite_eq_left hi]
    exact take_drop_disjoint _ _ _ (by have := (E.bounds i hi).1; omega) (E.bounds i hi).2
  · simp [tail, hi]

noncomputable def walk (E : D.Exchange) (i : I) (hi : i ∈ E.active) :
    G.Walk (s i) (D.paths (E.e i)).1 :=
  (((D.paths i).2.val.take (E.a i)).concat (E.edge i hi)).append
    ((D.paths (E.e i)).2.val.drop (E.b (E.e i)))

lemma walk_support {V I J : Type} {G : SimpleGraph V} {s : I → V} {B : J → Set V}
    [DecidableEq V] [DecidableEq I] {D : BagRouting G s B}
    (E : D.Exchange) (i : I) (hi : i ∈ E.active) {v : V}
    (hv : v ∈ (E.walk i hi).support) : v ∈ E.front i ∪ E.tail (E.e i) := by
  have hei := (E.stable i).mp hi
  rw [front, tail, ite_eq_left hi, ite_eq_left hei]
  simp only [walk, Walk.mem_support_append_iff, Walk.support_concat, List.mem_append,
    List.mem_cons, List.not_mem_nil, or_false] at hv
  rcases hv with (hv | hv) | hv
  · exact Or.inl hv
  · subst v
    exact Or.inr (Walk.start_mem_support _)
  · exact Or.inr hv

lemma support_overlap (E : D.Exchange) (i j : I) (hij : i ≠ j) (v : V)
    (hi : v ∈ E.front i ∪ E.tail (E.e i))
    (hj : v ∈ E.front j ∪ E.tail (E.e j)) : v = s i ∧ v = s j := by
  rcases hi with hi | hi <;> rcases hj with hj | hj
  · exact D.overlap i j hij v (E.front_subset i hi) (E.front_subset j hj)
  · by_cases he : i = E.e j
    · subst i
      exact (Set.disjoint_left.mp (E.front_tail_disjoint _) hi hj).elim
    · have h := D.overlap i (E.e j) he v (E.front_subset i hi) (E.tail_subset _ hj)
      exact (E.not_source_tail _ (h.2 ▸ hj)).elim
  · by_cases he : j = E.e i
    · subst j
      exact (Set.disjoint_left.mp (E.front_tail_disjoint _) hj hi).elim
    · have h := D.overlap j (E.e i) he v (E.front_subset j hj) (E.tail_subset _ hi)
      exact (E.not_source_tail _ (h.2 ▸ hi)).elim
  · have he : E.e i ≠ E.e j := fun h => hij (E.e.injective h)
    have h := D.overlap _ _ he v (E.tail_subset _ hi) (E.tail_subset _ hj)
    exact (E.not_source_tail _ (h.1 ▸ hi)).elim

noncomputable def routing (E : D.Exchange) : BagRouting G s B := by
  let paths : ∀ i, Σ v, G.Path (s i) v := fun i =>
    if hi : i ∈ E.active then ⟨(D.paths (E.e i)).1, (E.walk i hi).toPath⟩ else D.paths i
  refine ⟨paths, D.bag ∘ E.e, D.bag_injective.comp E.e.injective, ?_, ?_⟩
  · intro i
    by_cases hi : i ∈ E.active
    · have he : paths i = ⟨(D.paths (E.e i)).1, (E.walk i hi).toPath⟩ := dite_eq_left hi
      rw [he]
      exact D.endpoint_mem _
    · have he : paths i = D.paths i := dite_eq_right hi
      rw [he]
      simpa [E.fixed i hi] using D.endpoint_mem i
  · intro i j hij v hvi hvj
    apply E.support_overlap i j hij v
    · by_cases hi : i ∈ E.active
      · have he : paths i = ⟨(D.paths (E.e i)).1, (E.walk i hi).toPath⟩ := dite_eq_left hi
        rw [he] at hvi
        exact E.walk_support i hi ((E.walk i hi).support_toPath_subset_support hvi)
      · have he : paths i = D.paths i := dite_eq_right hi
        rw [he] at hvi
        exact Or.inl (by simpa [front, hi] using hvi)
    · by_cases hj : j ∈ E.active
      · have he : paths j = ⟨(D.paths (E.e j)).1, (E.walk j hj).toPath⟩ := dite_eq_left hj
        rw [he] at hvj
        exact E.walk_support j hj ((E.walk j hj).support_toPath_subset_support hvj)
      · have he : paths j = D.paths j := dite_eq_right hj
        rw [he] at hvj
        exact Or.inl (by simpa [front, hj] using hvj)

lemma length_inequality (E : D.Exchange) (i : I) :
    (E.routing.paths i).2.val.length + (if E.e i ∈ E.active then E.b (E.e i) else 0) ≤
      (D.paths (E.e i)).2.val.length + (if i ∈ E.active then E.a i + 1 else 0) := by
  by_cases hi : i ∈ E.active
  · have he : E.routing.paths i = ⟨(D.paths (E.e i)).1, (E.walk i hi).toPath⟩ := dite_eq_left hi
    rw [he, ite_eq_left hi, ite_eq_left ((E.stable i).mp hi)]
    have hlen : (E.walk i hi).toPath.val.length ≤ (E.walk i hi).length :=
      (E.walk i hi).length_bypass_le_length
    have ha := (E.bounds i hi).1
    have hb := E.bounds (E.e i) ((E.stable i).mp hi)
    have hia := E.bounds i hi
    have hleni : E.a i ≤ (D.paths i).2.val.length := by omega
    have hwlen : (E.walk i hi).length = E.a i + 1 +
        ((D.paths (E.e i)).2.val.length - E.b (E.e i)) := by
      simp only [walk, Walk.length_append, Walk.length_concat, Walk.take_length,
        Nat.min_eq_left hleni, Walk.drop_length]
    rw [hwlen] at hlen
    change (E.walk i hi).toPath.val.length + E.b (E.e i) ≤ _
    omega
  · have he : E.routing.paths i = D.paths i := dite_eq_right hi
    rw [he, E.fixed i hi]
    simp only [ite_eq_right hi, le_refl]

lemma shorter [Fintype I] (E : D.Exchange) (hE : E.active.Nonempty) :
    E.routing.totalLength < D.totalLength := by
  let a : I → ℕ := fun i => if i ∈ E.active then E.a i + 1 else 0
  let b : I → ℕ := fun i => if i ∈ E.active then E.b i else 0
  have hlt : (∑ i, a i) < ∑ i, b i := by
    apply Finset.sum_lt_sum
    · intro i _
      by_cases hi : i ∈ E.active
      · have := (E.bounds i hi).1
        simp only [a, b, ite_eq_left hi]
        omega
      · simp [a, b, hi]
    · obtain ⟨i, hi⟩ := hE
      refine ⟨i, Finset.mem_univ _, ?_⟩
      have := (E.bounds i hi).1
      simp only [a, b, ite_eq_left hi]
      omega
  have hs := Finset.sum_le_sum (s := Finset.univ) (fun i _ => E.length_inequality i)
  change (∑ i, ((E.routing.paths i).2.val.length + b (E.e i))) ≤
    ∑ i, ((D.paths (E.e i)).2.val.length + a i) at hs
  rw [Finset.sum_add_distrib, Finset.sum_add_distrib, Equiv.sum_comp E.e b,
    Equiv.sum_comp E.e (fun i => (D.paths i).2.val.length)] at hs
  change E.routing.totalLength + (∑ i, b i) ≤ D.totalLength + (∑ i, a i) at hs
  omega

end Exchange

lemma Minimal.no_exchange [Fintype I] [DecidableEq V] [DecidableEq I]
    {D : BagRouting G s B} (hD : D.Minimal) (E : D.Exchange) : ¬ E.active.Nonempty := by
  intro hE
  exact (not_lt_of_ge (hD E.routing)) (E.shorter hE)

lemma cycle_permutation [Fintype I] [DecidableEq I] (Q : Finset I) (hQ : Q.Nonempty)
    (f : I → I) (hf : ∀ i ∈ Q, f i ∈ Q) :
    ∃ T : Finset I, T ⊆ Q ∧ T.Nonempty ∧ ∃ e : Equiv.Perm I,
      (∀ i, i ∉ T → e i = i) ∧ (∀ i, i ∈ T ↔ e i ∈ T) ∧
      (∀ i ∈ T, e i = f i) := by
  classical
  let C := Q.powerset.filter fun T => T.Nonempty ∧ ∀ i ∈ T, f i ∈ T
  have hQC : Q ∈ C := Finset.mem_filter.mpr ⟨Finset.mem_powerset.mpr (by rfl), hQ, hf⟩
  obtain ⟨T, hTC, hmin⟩ := C.exists_min_image Finset.card ⟨Q, hQC⟩
  obtain ⟨hTQ, hn, hclosed⟩ := Finset.mem_filter.mp hTC
  have hsub : T.image f ⊆ T := by
    intro i hi
    obtain ⟨j, hj, rfl⟩ := Finset.mem_image.mp hi
    exact hclosed j hj
  have hIC : T.image f ∈ C := by
    refine Finset.mem_filter.mpr ⟨Finset.mem_powerset.mpr (hsub.trans (Finset.mem_powerset.mp hTQ)),
      hn.image f, ?_⟩
    intro i hi
    exact Finset.mem_image.mpr ⟨i, hsub hi, rfl⟩
  have heq : T.image f = T := Finset.eq_of_subset_of_card_le hsub (hmin _ hIC)
  let g : ↥T → ↥T := fun i => ⟨f i, hclosed i i.property⟩
  have hsurj : Function.Surjective g := by
    intro i
    have hi : i.val ∈ T.image f := heq.symm ▸ i.property
    obtain ⟨j, hj, he⟩ := Finset.mem_image.mp hi
    exact ⟨⟨j, hj⟩, Subtype.ext he⟩
  let e0 : Equiv.Perm ↥T := Equiv.ofBijective g (Finite.surjective_iff_bijective.mp hsurj)
  let e := Equiv.Perm.ofSubtype e0
  refine ⟨T, Finset.mem_powerset.mp hTQ, hn, e, ?_, ?_, ?_⟩
  · intro i hi
    exact Equiv.Perm.ofSubtype_apply_of_not_mem e0 hi
  · intro i
    exact (Equiv.Perm.ofSubtype_apply_mem_iff_mem e0 i).symm
  · intro i hi
    exact Equiv.Perm.ofSubtype_apply_of_mem e0 hi

lemma Minimal.low_degree [Fintype I] [DecidableEq V] [DecidableEq I]
    [DecidableRel G.Adj] {D : BagRouting G s B} (hD : D.Minimal)
    (S : Finset V) (hS : S ⊆ D.usedFinset) (hne : S.Nonempty) :
    ∃ v ∈ S, (S.filter (G.Adj v)).card ≤ 2 * Fintype.card I := by
  classical
  by_contra hno
  have hdeg : ∀ v ∈ S, 2 * Fintype.card I < (S.filter (G.Adj v)).card := by
    intro v hv
    exact Nat.lt_of_not_ge (fun hh => hno ⟨v, hv, hh⟩)
  let R : I → Finset ℕ := fun i => (Finset.range ((D.paths i).2.val.length + 1)).filter
    fun a => (D.paths i).2.val.getVert a ∈ S
  let Q : Finset I := Finset.univ.filter fun i => (R i).Nonempty
  let a : I → ℕ := fun i => if hi : (R i).Nonempty then (R i).min' hi else 0
  have hQiff : ∀ i, i ∈ Q ↔ (R i).Nonempty := by simp [Q]
  have hamin : ∀ i ∈ Q, a i ∈ R i := by
    intro i hi
    have hn := (hQiff i).mp hi
    dsimp [a]
    rw [dite_eq_left hn]
    exact Finset.min'_mem _ _
  have haS : ∀ i ∈ Q, (D.paths i).2.val.getVert (a i) ∈ S := by
    intro i hi
    exact (Finset.mem_filter.mp (hamin i hi)).2
  have hfind : ∀ v ∈ S, ∃ i ∈ Q, ∃ b, b ∈ R i ∧ (D.paths i).2.val.getVert b = v := by
    intro v hv
    obtain ⟨i, hi⟩ := (D.mem_usedFinset v).mp (hS hv)
    obtain ⟨b, hb, hblen⟩ := Walk.mem_support_iff_exists_getVert.mp hi
    have hbR : b ∈ R i := Finset.mem_filter.mpr ⟨Finset.mem_range.mpr (by omega), hb ▸ hv⟩
    exact ⟨i, (hQiff i).mpr ⟨b, hbR⟩, b, hbR, hb⟩
  have hQ : Q.Nonempty := by
    obtain ⟨v, hv⟩ := hne
    obtain ⟨i, hi, _⟩ := hfind v hv
    exact ⟨i, hi⟩
  let M := Finset.univ.biUnion fun i : I =>
    {(D.paths i).2.val.getVert (a i), (D.paths i).2.val.getVert (a i + 1)}
  have hM : M.card ≤ 2 * Fintype.card I := by
    calc
      _ ≤ ∑ i : I, ({(D.paths i).2.val.getVert (a i),
        (D.paths i).2.val.getVert (a i + 1)} : Finset V).card := Finset.card_biUnion_le
      _ ≤ ∑ i : I, 2 := Finset.sum_le_sum fun _ _ => Finset.card_le_two
      _ = _ := by simp [Nat.mul_comm]
  have hm0 : ∀ i, (D.paths i).2.val.getVert (a i) ∈ M := by
    intro i
    exact Finset.mem_biUnion.mpr ⟨i, Finset.mem_univ _, by simp⟩
  have hm1 : ∀ i, (D.paths i).2.val.getVert (a i + 1) ∈ M := by
    intro i
    exact Finset.mem_biUnion.mpr ⟨i, Finset.mem_univ _, by simp⟩
  have hout : ∀ i, ∃ j b, i ∈ Q → j ∈ Q ∧ a j + 2 ≤ b ∧
      b ≤ (D.paths j).2.val.length ∧
      G.Adj ((D.paths i).2.val.getVert (a i)) ((D.paths j).2.val.getVert b) := by
    intro i
    by_cases hi : i ∈ Q
    · have hlarge := hdeg _ (haS i hi)
      have hex : ∃ v ∈ S.filter (G.Adj ((D.paths i).2.val.getVert (a i))), v ∉ M := by
        by_contra hh
        have hsub : S.filter (G.Adj ((D.paths i).2.val.getVert (a i))) ⊆ M := by
          intro v hv
          by_contra hvm
          exact hh ⟨v, hv, hvm⟩
        have hle := (Finset.card_le_card hsub).trans hM
        omega
      obtain ⟨v, hv, hvm⟩ := hex
      obtain ⟨hvS, hvadj⟩ := Finset.mem_filter.mp hv
      obtain ⟨j, hj, b, hbR, hb⟩ := hfind v hvS
      have hbj : a j ≤ b := by
        have hn := (hQiff j).mp hj
        dsimp [a]
        rw [dite_eq_left hn]
        exact Finset.min'_le _ _ hbR
      have hb0 : b ≠ a j := by
        intro he
        apply hvm
        rw [he] at hb
        exact hb ▸ hm0 j
      have hb1 : b ≠ a j + 1 := by
        intro he
        apply hvm
        rw [he] at hb
        exact hb ▸ hm1 j
      have hblen : b ≤ (D.paths j).2.val.length := by
        have hh := (Finset.mem_filter.mp hbR).1
        simp only [Finset.mem_range] at hh
        omega
      refine ⟨j, b, fun _ => ⟨hj, by omega, hblen, ?_⟩⟩
      rwa [hb]
    · exact ⟨i, 0, fun hh => (hi hh).elim⟩
  choose f c hfc using hout
  obtain ⟨T, hTQ, hT, e, hfix, hstable, heq⟩ :=
    cycle_permutation Q hQ f (fun i hi => (hfc i hi).1)
  let E : D.Exchange := {
    active := T
    e := e
    fixed := hfix
    stable := hstable
    a := a
    b := fun i => c (e.symm i)
    bounds := by
      intro i hi
      have hpre : e.symm i ∈ T := (hstable _).mpr (by simpa using hi)
      have he : f (e.symm i) = i := (heq _ hpre).symm.trans (e.apply_symm_apply i)
      have hc := (hfc _ (hTQ hpre)).2
      rw [he] at hc
      exact ⟨hc.1, hc.2.1⟩
    edge := by
      intro i hi
      have hc := (hfc i (hTQ hi)).2.2.2
      rw [← heq i hi] at hc
      simpa only [Equiv.symm_apply_apply] using hc
  }
  exact hD.no_exchange E hT

end BagRouting

end LinearListHadwiger

end OAI