lib/yargraph.rb
# If #edges_in_all_hamiltonian_cycles is too slow, the method
# here is faster, but is not guaranteed to find every edge that is
# part of Hamiltonian cycles. This method proceeds under the assumption
# that the graph has at least 1 Hamiltonian cycle, but may stumble
# across evidence to the contrary.
#
# Returns an instance of EdgeSearchResult where #edges_in_all are those
# edges that are in all hamiltonian cycles, and #edges_in_none are those
# edges that are not in any hamiltonian cycles. While
If #edges_in_all_hamiltonian_cycles is too slow, the method
here is faster, but is not guaranteed to find every edge that is
part of Hamiltonian cycles. This method proceeds under the assumption
that the graph has at least 1 Hamiltonian cycle, but may stumble
across evidence to the contrary.

Returns an instance of EdgeSearchResult where #edges_in_all are those
edges that are in all hamiltonian cycles, and #edges_in_none are those
edges that are not in any hamiltonian cycles. While
If #edges_in_all_hamiltonian_cycles is too slow the method here is faster but is not guaranteed to find every edge that is part of Hamiltonian cycles . This method proceeds under the assumption that the graph has at least 1 Hamiltonian cycle but may stumble across evidence to the contrary . Returns an instance of EdgeSearchResult where #edges_in_all are those edges that are in all hamiltonian cycles and #edges_in_none are those edges that are not in any hamiltonian cycles . While
def some_edges_in_all_hamiltonian_cycles
      stack = DS::Stack.new
      result = EdgeSearchResult.new
      g = copy
      g.vertices.to_a.reverse.each do |v|
        stack.push v
      end
      while v = stack.pop
        all_neighbours = g.neighbours(v)
        ham_neighbours = result.hamiltonian_neighbours(v)
        if all_neighbours.length < 2
          result.contains_hamcycle = false
        elsif all_neighbours.length == 2
          all_neighbours.each do |n|
            unless result.edges_in_all.edge?(v,n)
              result.edges_in_all.add_edge(v,n)
              stack.push n 
            end
            if result.edges_in_all[v].include?(n) and result.edges_in_none[v].include?(n)
              result.contains_hamcycle = false
            end
          end
        elsif ham_neighbours.length >= 2
          all_neighbours.each do |n|
            next if ham_neighbours.include?(n)
            result.edges_in_none.add_edge(v,n)
            g.delete_edge(v,n)
            stack.push n 
          end
        else
        end
      end
      return result
    end
def some_edges_in_all_hamiltonian_cycles stack = DS :: Stack . new result = EdgeSearchResult . new g = copy g . vertices . to_a . reverse . each do | v | stack . push v end while v = stack . pop all_neighbours = g . neighbours ( v ) ham_neighbours = result . hamiltonian_neighbours ( v ) if all_neighbours . length < 2 result . contains_hamcycle = false elsif all_neighbours . length == 2 all_neighbours . each do | n | unless result . edges_in_all . edge? ( v , n ) result . edges_in_all . add_edge ( v , n ) stack . push n end if result . edges_in_all [ v ] . include? ( n ) and result . edges_in_none [ v ] . include? ( n ) result . contains_hamcycle = false end end elsif ham_neighbours . length >= 2 all_neighbours . each do | n | next if ham_neighbours . include? ( n ) result . edges_in_none . add_edge ( v , n ) g . delete_edge ( v , n ) stack . push n end else end end return result end
If #edges_in_all_hamiltonian_cycles is too slow, the method
here is faster, but is not guaranteed to find every edge that is
part of Hamiltonian cycles.
If #edges_in_all_hamiltonian_cycles is too slow the method here is faster but is not guaranteed to find every edge that is part of Hamiltonian cycles .
# As we are deleting edges, make a deep copy to start with # Fill up the stack, in reverse to ease testing #        p v #        p all_neighbours #        p ham_neighbours # If a vertex contains 1 or 0 total neighbours, then the graph cannot contain # any hamcycles (in contrast, degree 1 doesn't preclude hampaths). # If a vertex has degree 2 then both edges must be a part of the hamcycle #now need to re-evalute the neighbour, as its neighbourhood is changed # if an edge be and must not be in all hamcycles, then the graph is not Hamiltonian. # Are there any concrete examples of this? Possibly. # There cannot be any further hamcycle edges from this vertex, so the rest of the edges # cannot be a part of _any_ hamcycle #reconsider the neighbour # Anything else that can be done cheaply? # Maybe edges that create non-Hamiltonian cycles when only considering edges # that are in all Hamiltonian cycles -> these cannot be in any hamcycle #p stack #p result