Skip to content

Instantly share code, notes, and snippets.

@aniketstiwari
aniketstiwari / graph.rb
Created May 9, 2021 16:55 — forked from MyklClason/graph.rb
Basic graph utility functions class (Ruby)
# This is a very simple hash based graph utility class.
# By Mykl Clason
class Graph
def self.undirected_graph_from_edges(edges)
# Edges are (a,b) pairs with a => b and b => a relationships.
graph = {}
edges.each do |source, adjacency|
graph[source] = Set.new unless graph[source].present?
graph[adjacency] = Set.new unless graph[adjacency].present?
graph[source].add(adjacency)