This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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) |