Zulip Chat Archive

Stream: Is there code for X?

Topic: Hamiltonian paths define a bijection with Fin n


Snir Broshi (Sep 09 2025 at 20:47):

Hello, is there a way to convert a Hamiltonian path to a bijection with Fin n?
Something like the following:

def SimpleGraph.Walk.IsHamiltonian.equivFin {α : Type*} [DecidableEq α] (G : SimpleGraph α) {a b : α} (p : G.Walk a b) : α  Fin p.support.length := sorry

and maybe even more specifically that p.getVert is that bijection, and an instance of Fintype.
Thanks!

Jakub Nowak (Sep 10 2025 at 10:14):

I was searching for something like that for docs#SimpleGraph.Walk.IsCycle and couldn't find anything.

Vlad Tsyrklevich (Sep 10 2025 at 12:59):

apply? followed by two exact?s proves the given statement (+ Hamiltonian hypothesis)

import Mathlib
def SimpleGraph.Walk.IsHamiltonian.equivFin {α : Type*} [DecidableEq α] (G : SimpleGraph α) {a b : α} (p : G.Walk a b) (h : p.IsHamiltonian) : α  Fin p.support.length := by
  refine (List.Nodup.getEquivOfForallMemList p.support ?_ ?_).symm
  · exact List.nodup_iff_count_eq_one.mpr fun a_1 a => h a_1
  · exact fun x => mem_support h x

Snir Broshi (Sep 10 2025 at 13:00):

Wow, thanks! Do you think Mathlib will accept something like this?

Vlad Tsyrklevich (Sep 10 2025 at 13:07):

There is already https://leanprover-community.github.io/mathlib4_docs/Mathlib/Combinatorics/SimpleGraph/Hamiltonian.html#SimpleGraph.Walk.IsHamiltonian.length_eq for the case of Fintype \a, does that work?

Jakub Nowak (Sep 10 2025 at 13:09):

As a side note, I would suggest making (p : G.Walk a b) argument implicit. It can be deduced from (h : p.IsHamiltonian).

Vlad Tsyrklevich (Sep 10 2025 at 13:22):

This instance seems fairly nice to have:

instance SimpleGraph.Walk.IsHamiltonian.finite {V : Type*} [DecidableEq V] {G : SimpleGraph V} {u v : V} {p : G.Walk u v} (h : p.IsHamiltonian) : Finite V := by
  refine finite_iff_exists_equiv_fin.mpr p.support.length, ⟨?_⟩⟩
  refine (List.Nodup.getEquivOfForallMemList p.support ?_ ?_).symm
  · exact List.nodup_iff_count_eq_one.mpr fun a_1 a => h a_1
  · exact fun x => mem_support h x

I'll clean it up and submit it

Jakub Nowak (Sep 10 2025 at 13:40):

What's the use case for SimpleGraph.Walk.IsHamiltonian.finite?

Jakub Nowak (Sep 10 2025 at 13:43):

Ah, it's an instance. Yeah, it does sound useful.

Jakub Nowak (Sep 10 2025 at 13:46):

I think, you could also do docs#Fintype ?

Jakub Nowak (Sep 10 2025 at 13:47):

Or even only docs#Fintype, as docs#Finite follows from docs#Fintype.

Lawrence Wu (llllvvuu) (Sep 10 2025 at 13:59):

I’m not sure if that instance will ever be synthesized, since it takes a proof (AFAIK instances only get synthesized from other instances). I wouldn’t add a Fintype instance for sure, because it’s data, you will get diamonds. You only want to add such an instance if it’s canonical.

Vlad Tsyrklevich (Sep 10 2025 at 14:04):

Is the proper procedure in cases like this to leave it as a def then? It seems useful to be able to able to get a Finite instance, and though I doubt there's many people working with SimpleGraph constructively, Fintype as well I suppose

Robin Arnez (Sep 10 2025 at 14:10):

I don't think it's possible to make this into an instance (even if IsHamiltonian would be a class) since instance search always goes backwards and there no way to determine h, p, u, v or G from Finite V

Robin Arnez (Sep 10 2025 at 14:10):

You can keep this as a theorem and then use have := h.finite at the use site though

Jakub Nowak (Sep 10 2025 at 14:12):

If it's def, and not instance, than wouldn't adding Fintype be fine?

Vlad Tsyrklevich (Sep 10 2025 at 14:20):

#29502

Jakub Nowak (Sep 10 2025 at 14:42):

The name's a bit ambiguous in my opinion. Maybe IsHamiltonian.finiteVerts or IsHamiltonian.finiteVertex? Not sure which one.

Jakub Nowak (Sep 10 2025 at 14:46):

Plural makes more sense grammatically to me. But e.g. SimpleGraph.instFiniteConnectedComponent uses singular.

Snir Broshi (Oct 22 2025 at 17:01):

FWIW there's also #30149 #30206 #30224


Last updated: Dec 20 2025 at 21:32 UTC