5 | module Data.Graph.Indexed.Ring.Relevant.Candidates
7 | import Data.Graph.Indexed.Ring.Relevant.ShortestPath
8 | import Data.Graph.Indexed.Ring.Relevant.Types
14 | origin : ISubgraph o k e n -> NCycle o -> NCycle k
15 | origin g = {path $= map (Subgraph.origin g)}
17 | isolate : {o : _} -> ISubgraph (S o) k e n -> Maybe (NCycle k)
18 | isolate {o = n@(S (S _))} g =
19 | Just $
NC (map (origin g) $
nodes g ++ [FZ]) (S n) 1
22 | notLast : Fin k -> SnocList (Fin k) -> Bool
23 | notLast x (_ :< y :< _) = x /= y
26 | revOnto : SnocList a -> SnocList a -> List a
27 | revOnto sx [<] = sx <>> []
28 | revOnto sx (sy:<y) = revOnto (sx :< y) sy
30 | appMaybe : SnocList a -> Maybe a -> SnocList a
31 | appMaybe sc = maybe sc (sc:<)
33 | gte3 : Nat -> Maybe (Subset Nat (LTE 3))
35 | case tryLTE {n} 3 of
36 | Just0 prf => Just (Element n prf)
39 | parameters {o : Nat}
40 | (g : ISubgraph o k e Nat)
44 | connector : SnocList (Fin o) -> SnocList (Fin o) -> Fin o -> Bool
45 | connector sx sy x = smaller root rdeg g x && notLast x sx && notLast x sy
53 | cycleSystem : List (NCycle o)
54 | cycleSystem = go [<] (shortestPaths g root)
59 | odd : (p1,p2 : Path o) -> Maybe (NCycle o)
61 | flip map (gte3 (pred $
p1.length + p2.length)) $
\(Element s _) =>
62 | NC (revOnto p1.path p2.path) s (p1.combos * p2.combos)
65 | even : (p1,p2 : Path o) -> Fin o -> Maybe (NCycle o)
67 | if connector p1.path p2.path x
69 | flip map (gte3 (p1.length + p2.length)) $
\(Element s _) =>
70 | NC (revOnto (p1.path :< x) p2.path) s (p1.combos * p2.combos)
73 | addCs : SnocList (NCycle o) -> Path o -> List (Path o) -> SnocList (NCycle o)
75 | addCs sc p@(P len1 p1 _ f1 l1 _) (q@(P len2 p2 _ f2 l2 _)::qs) =
76 | let True := len1 == len2 | False => sc
77 | False := f1 == f2 | True => addCs sc p qs
78 | False := adjacent g l1 l2 | True => addCs (appMaybe sc $
odd p q) p qs
79 | ns := keys $
intersect (neighboursAsAL g l1) (neighboursAsAL g l2)
80 | in addCs (sc <>< mapMaybe (even p q) ns) p qs
85 | go : SnocList (NCycle o) -> List (Path o) -> List (NCycle o)
86 | go sxs [] = sxs <>> []
87 | go sxs (p :: ps) = go (addCs sxs p ps) ps
89 | findCandidates : Subgraph k e Nat -> Candidates k e
90 | findCandidates (G 0 g) = Empty
91 | findCandidates sg@(G (S k) g) =
92 | case filter ((2 <) . deg g) (nodes g) of
93 | [] => maybe Empty (Isolate sg) (isolate g)
94 | ns => System (S k) g (ns >>= \n => cycleSystem g n (deg g n))
99 | componentCandidates : Subgraph k e n -> Candidates k e
100 | componentCandidates = findCandidates . toDegs
106 | candidates : {k : _} -> IGraph k e n -> List (Candidates k e)
107 | candidates = map componentCandidates . biconnectedComponents