Class: EmailGraph::UndirectedGraph

Inherits:
Object
  • Object
show all
Defined in:
lib/email_graph/undirected_graph.rb

Instance Method Summary collapse

Constructor Details

#initializeUndirectedGraph



5
6
7
# File 'lib/email_graph/undirected_graph.rb', line 5

def initialize
  @store = {}
end

Instance Method Details

#add_edge(e) ⇒ Object

Adds an edge and associated vertices if they don’t already exist and returns the edge



36
37
38
39
# File 'lib/email_graph/undirected_graph.rb', line 36

def add_edge(e)
  v, w = *e.vertices
  edge(v, w) || e.vertices.each{ |v| add_vertex(v); @store[v].add(e)}
end

#add_vertex(v) ⇒ Object

Adds a vertex if it doesn’t already exist and returns it



30
31
32
# File 'lib/email_graph/undirected_graph.rb', line 30

def add_vertex(v)
  @store[v] ||= Set.new
end

#edge(v, w) ⇒ Object

A specific edge from v to w



20
21
22
# File 'lib/email_graph/undirected_graph.rb', line 20

def edge(v, w)
  (@store[v] || []).find{ |e| e.vertices.include?(w) }
end

#edgesObject

All edges



15
16
17
# File 'lib/email_graph/undirected_graph.rb', line 15

def edges
  @store.values.to_set.flatten
end

#edges_with(v) ⇒ Object

Edges involving a vertex v



25
26
27
# File 'lib/email_graph/undirected_graph.rb', line 25

def edges_with(v)
  @store[v]
end

#verticesObject

All vertices



10
11
12
# File 'lib/email_graph/undirected_graph.rb', line 10

def vertices
  @store.keys
end