// Analyse structurelle (cubique / ponts / planarite / apex / isomorphisme)
class Analyse
{
public static bool EstCubique(Graphe g) =>
g.N() > 0 && g.Adj.All(vois => vois.Count == 3);
// Ponts par parcours profond lowlink (Tarjan), iteratif.
public static List<(int, int)> Ponts(Graphe g)
{
int n = g.N();
var visites = new bool[n];
var resultat = new List<(int, int)>();
for (int depart = 0; depart < n; depart++)
{
if (visites[depart]) continue;
visites[depart] = true;
var prof = new int[n]; Array.Fill(prof, int.MaxValue);
var bas = new int[n]; Array.Fill(bas, int.MaxValue);
int horloge = 0;
prof[depart] = 0; bas[depart] = 0;
var pile = new Stack<(int u, int i, int pere)>();
pile.Push((depart, 0, int.MaxValue));
while (pile.Count > 0)
{
var (u, i, pere) = pile.Peek();
if (i < g.Adj[u].Count)
{
pile.Pop();
pile.Push((u, i + 1, pere));
int w = g.Adj[u][i];
if (w == pere) continue;
if (visites[w])
{
bas[u] = Math.Min(bas[u], prof[w]);
}
else
{
horloge++;
prof[w] = horloge; bas[w] = horloge;
visites[w] = true;
pile.Push((w, 0, u));
}
}
else
{
pile.Pop();
if (pile.Count > 0)
{
var (parent, _, _) = pile.Peek();
bas[parent] = Math.Min(bas[parent], bas[u]);
if (bas[u] > prof[parent]) resultat.Add((parent, u));
}
}
}
}
return resultat;
}
public static bool SansPont(Graphe g) => Ponts(g).Count == 0;
public static List<List<int>> ComposantesConnexes(Graphe g)
{
int n = g.N();
var vus = new bool[n];
var comps = new List<List<int>>();
for (int depart = 0; depart < n; depart++)
{
if (vus[depart]) continue;
vus[depart] = true;
var pile = new Stack<int>(); pile.Push(depart);
var comp = new List<int>();
while (pile.Count > 0)
{
int u = pile.Pop();
comp.Add(u);
foreach (int w in g.Adj[u])
if (!vus[w]) { vus[w] = true; pile.Push(w); }
}
comp.Sort();
comps.Add(comp);
}
return comps;
}
public static bool EstPlanaire(Graphe g)
{
int n = g.N();
if (n <= 2) return true;
if (g.M() > 3 * n - 6) return false;
return EtatLr.Nouveau(g).Executer();
}
public static bool EstApex(Graphe g)
{
for (int v = 0; v < g.N(); v++)
if (EstPlanaire(g.Supprime(v))) return true;
return false;
}
public static Description Decrire(string nom, Graphe g) => new Description
{
Nom = nom, N = g.N(), M = g.M(),
Cubique = EstCubique(g),
SansPontB = SansPont(g),
Planaire = EstPlanaire(g),
Apex = EstApex(g),
};
// Isomorphisme par backtracking : bijection sommets preservant l'adjacence.
public static bool EstIsomorphe(Graphe g1, Graphe g2)
{
int n = g1.N();
if (n != g2.N() || g1.M() != g2.M()) return false;
var correspondance = new int[n];
var utilise = new bool[n];
bool Adjacents(int a, int b, Graphe g) => g.Adj[a].Contains(b);
bool Essaie(int i)
{
if (i == n) return true;
for (int c = 0; c < n; c++)
{
if (utilise[c] || g1.Adj[i].Count != g2.Adj[c].Count) continue;
bool coherent = true;
for (int j = 0; j < i; j++)
if (Adjacents(i, j, g1) != Adjacents(c, correspondance[j], g2))
{ coherent = false; break; }
if (!coherent) continue;
utilise[c] = true;
correspondance[i] = c;
if (Essaie(i + 1)) return true;
utilise[c] = false;
}
return false;
}
return Essaie(0);
}
}
// Constructeurs de graphes
class Graphes
{
static List<string> Noms(int k) => Enumerable.Range(0, k).Select(i => i.ToString()).ToList();
public static Graphe Complet(int k)
{
var g = new Graphe(Noms(k));
for (int i = 0; i < k; i++)
for (int j = i + 1; j < k; j++) g.Ajoute(i, j);
return g;
}
public static Graphe PrismeTriangulaire()
{
var g = new Graphe(Noms(6));
for (int i = 0; i < 3; i++)
{
g.Ajoute(i, (i + 1) % 3);
g.Ajoute(i + 3, (i + 1) % 3 + 3);
g.Ajoute(i, i + 3);
}
return g;
}
public static Graphe Petersen()
{
var g = new Graphe(Noms(10));
for (int i = 0; i < 5; i++)
{
g.Ajoute(i, (i + 1) % 5);
g.Ajoute(i + 5, (i + 2) % 5 + 5);
g.Ajoute(i, i + 5);
}
return g;
}
// Deux copies de K4 moins une arete, recollees par un sommet de chaque cote,
// relies par un unique pont. Cubique, avec exactement un pont.
public static Graphe CubiqueAPont()
{
var noms = new List<string>();
foreach (string tag in new[] { "a", "b" })
for (int i = 0; i < 4; i++) noms.Add(tag + i);
noms.Add("wa"); noms.Add("wb");
var g = new Graphe(noms);
foreach (int tag in new[] { 0, 4 })
for (int i = 0; i < 4; i++)
for (int j = i + 1; j < 4; j++)
if ((i, j) != (2, 3)) g.Ajoute(tag + i, tag + j);
int wa = 8, wb = 9;
g.Ajoute(wa, 2); g.Ajoute(wa, 3);
g.Ajoute(wb, 6); g.Ajoute(wb, 7);
g.Ajoute(wa, wb);
return g;
}
// Echelle de Mobius M_n : cycle de n sommets + barreaux i -- (i + n/2).
public static Graphe EchelleMobius(int n)
{
var g = new Graphe(Noms(n));
for (int i = 0; i < n; i++) g.Ajoute(i, (i + 1) % n);
for (int i = 0; i < n / 2; i++) g.Ajoute(i, (i + n / 2) % n);
return g;
}
public static Graphe CompletBiparti(int p, int q)
{
var g = new Graphe(Noms(p + q));
for (int i = 0; i < p; i++)
for (int j = p; j < p + q; j++) g.Ajoute(i, j);
return g;
}
public static Graphe Hypercube3()
{
var noms = Enumerable.Range(0, 8).Select(i => i.ToString("000")).ToList();
var g = new Graphe(noms);
for (int u = 0; u < 8; u++)
for (int b = 0; b < 3; b++) g.Ajoute(u, u ^ (1 << b));
return g;
}
// Graphe de Heawood : 7 points, 7 droites de l'incidence de Fano ;
// la droite i porte les points i, i+1 et i+3 (mod 7).
public static Graphe Heawood()
{
var noms = new List<string>();
for (int i = 0; i < 7; i++) noms.Add("p" + i);
for (int i = 0; i < 7; i++) noms.Add("d" + i);
var g = new Graphe(noms);
for (int i = 0; i < 7; i++)
{
g.Ajoute(i, 7 + i);
g.Ajoute(i, 7 + (i + 1) % 7);
g.Ajoute(i, 7 + (i + 3) % 7);
}
return g;
}
// Modele d'appariement (pairing model), portage du generateur
// networkx.random_regular_graph.
public static Graphe RegulierAleatoire(int d, int n, Random rng)
{
bool Contient(List<(int, int)> aretes, int s1, int s2) =>
aretes.Any(ab => ab.Item1 == s1 && ab.Item2 == s2);
while (true)
{
var aretes = new List<(int, int)>();
var stubs = new List<int>();
for (int sommet = 0; sommet < n; sommet++)
for (int k = 0; k < d; k++) stubs.Add(sommet);
bool abandon = false;
int garde = 0;
while (stubs.Count > 0)
{
// Garde anti-boucle : un appariement qui ne converge plus
// (stubs residuels ne pouvant former que des aretes paralleles
// ou des boucles) est abandonne et relance, au lieu de tourner
// indefiniment sur les memes stubs.
if (++garde > 1000) { abandon = true; break; }
for (int i = stubs.Count - 1; i > 0; i--)
{
int j = rng.Next(i + 1);
(stubs[i], stubs[j]) = (stubs[j], stubs[i]);
}
var kept = new List<int>();
for (int i = 0; i + 1 < stubs.Count; i += 2)
{
int s1 = stubs[i], s2 = stubs[i + 1];
if (s1 > s2) (s1, s2) = (s2, s1);
if (s1 != s2 && !Contient(aretes, s1, s2))
{
aretes.Add((s1, s2));
}
else
{
kept.Add(s1); kept.Add(s2);
}
}
stubs = kept;
}
if (abandon) continue;
var g = new Graphe(Noms(n));
foreach (var (a, b) in aretes) g.Ajoute(a, b);
if (g.Adj.All(v => v.Count == d)) return g;
}
}
}
// Solveur Z3 de la k-coloration d'aretes
class Resolveur
{
public static (List<int> Coloration, string Statut) ColorationAretesZ3(Graphe g, int k, double timeLimitS)
{
var aretes = g.Aretes();
int na = aretes.Count;
var ctx = new Context();
var xv = new BoolExpr[na, k];
for (int e = 0; e < na; e++)
for (int c = 0; c < k; c++)
xv[e, c] = ctx.MkBoolConst($"x{e}_{c}");
var s = ctx.MkSolver();
// Z3 4.13.0 : le timeout se fixe sur le Solver via Set("timeout", ms) ;
// 0 ms force une interruption immediate (statut UNKNOWN).
s.Set("timeout", timeLimitS > 0 ? (uint)(timeLimitS * 1000) : 1u);
for (int e = 0; e < na; e++)
{
var xs = new List<BoolExpr>();
for (int c = 0; c < k; c++) xs.Add(xv[e, c]);
s.Assert(ctx.MkAtMost(xs, 1u)); // au plus une couleur par arete
s.Assert(ctx.MkAtLeast(xs, 1u)); // + au moins une = exactement une
}
for (int vv = 0; vv < g.N(); vv++)
for (int c = 0; c < k; c++)
{
var list = new List<BoolExpr>();
for (int e = 0; e < na; e++)
if (aretes[e].Item1 == vv || aretes[e].Item2 == vv) list.Add(xv[e, c]);
if (list.Count > 0) s.Assert(ctx.MkAtMost(list, 1u));
}
var r = s.Check();
if (r == Status.SATISFIABLE)
{
var m = s.Model;
var trueLit = ctx.MkTrue();
var coloration = new List<int>(Enumerable.Repeat(0, na));
for (int e = 0; e < na; e++)
for (int c = 0; c < k; c++)
if (m.Evaluate(xv[e, c], true).Equals(trueLit)) coloration[e] = c;
return (coloration, "SAT");
}
if (r == Status.UNSATISFIABLE) return (null, "UNSAT");
return (null, "UNKNOWN");
}
public static string VerdictTexte(List<int> coloration, string statut)
{
if (coloration != null) return "3-colorable";
if (statut == "UNSAT") return "INFEASIBLE (chi' >= 4)";
return $"INDETERMINE ({statut})";
}
}
// Affichage textuel
class Affichage
{
public static string V(bool b) => b ? "True" : "False";
public static void TableTemoins(List<Description> descs)
{
Console.WriteLine(
"{0,-22}{1,3}{2,4} {3,-8}{4,-10}{5,-9}{6}",
"graphe", "n", "m", "cubique", "sans_pont", "planaire", "apex");
foreach (var d in descs)
Console.WriteLine(
"{0,-22}{1,3}{2,4} {3,-8}{4,-10}{5,-9}{6}",
d.Nom, d.N, d.M, V(d.Cubique), V(d.SansPontB), V(d.Planaire), V(d.Apex));
}
public static void AfficheAdjacences(Graphe g, string titre, (int, int)? marque)
{
Console.WriteLine(titre);
for (int u = 0; u < g.N(); u++)
{
var voisins = g.Adj[u].Select(w =>
{
bool estMarque = false;
if (marque.HasValue)
{
int ma = marque.Value.Item1, mb = marque.Value.Item2;
estMarque = (ma == u && mb == w) || (ma == w && mb == u);
}
return estMarque ? $"[{g.Noms[w]}]" : g.Noms[w];
}).ToList();
Console.WriteLine($" {g.Noms[u],4} : {string.Join(" ", voisins)}");
}
}
public static void AfficheColoration(Graphe g, string titre, int k, List<int> coloration)
{
var aretes = g.Aretes();
Console.WriteLine(titre);
for (int c = 0; c < k; c++)
{
var aretesC = aretes
.Select((ab, i) => (ab, i))
.Where(t => coloration[t.i] == c)
.Select(t => $"{g.Noms[t.ab.Item1]}-{g.Noms[t.ab.Item2]}")
.ToList();
Console.WriteLine($" couleur {c} : {string.Join(", ", aretesC)}");
}
}
}