Skip to content

Instantly share code, notes, and snippets.

@MyklClason
MyklClason / graph.rb
Last active December 20, 2023 05:00
Basic graph utility functions class (Ruby)
# This is a very simple hash based graph utility class.
# By Mykl Clason
# https://gist.github.com/MyklClason/fe603a6005a1dedc6ec65fda7ff47f68
class Graph
def self.undirected_graph_from_edges(edges)
# Edges are (a,b) pairs with a => b and b => a relationships.
edges.each_with_object(Hash.new { |h, k| h[k] = Set.new }) { |(k, v), h| h[k] << v }
end
def self.directed_graph_from_edges(edges)