X-Git-Url: https://git.njae.me.uk/?a=blobdiff_plain;f=lib%2Fgraph.njae%2Fgraph.rb;h=77b96da5fa7ea86d7b174cb7ddcedd1ebd24416e;hb=2f72fc8e85b5c9c8bdcf622dc3b512a3dee5060c;hp=b1de59b324be4297ceed4ef6c1a8dd2fdfe890df;hpb=353a5d54ad4014af126c76617d9b02029a3ee98c;p=graph.njae.git diff --git a/lib/graph.njae/graph.rb b/lib/graph.njae/graph.rb index b1de59b..77b96da 100644 --- a/lib/graph.njae/graph.rb +++ b/lib/graph.njae/graph.rb @@ -24,18 +24,42 @@ module GraphNjae self end + # Connects two vertices, creating and storing a new edge + # Also adds the vertices, unless they're already in the graph + def connect(vertex1, vertex2) + self.vertices << vertex1 unless self.vertices.include? vertex1 + self.vertices << vertex2 unless self.vertices.include? vertex2 + edge = Edge.new + self.edges << edge + edge << vertex1 << vertex2 + end + # Form a product graph of this graph and the other. - # Return the new graph. + # Return the product graph. def product(other) product_graph = Graph.new self.vertices.each do |v1| other.vertices.each do |v2| - product_vertex = Vertex.new - product_vertex.left_node = v1 - product_vertex.right_node = v2 - product_graph << product_vertex + product_graph << Vertex.new({:g1_vertex => v1, :g2_vertex => v2}) + end + end + self.edges.each do |e1| + e1_vertices = e1.vertices + other.edges.each do |e2| + e2_vertices = e2.vertices + source = product_graph.vertices.find {|v| v.g1_vertex == e1_vertices[0] and v.g2_vertex == e2_vertices[0]} + destination = product_graph.vertices.find {|v| v.g1_vertex == e1_vertices[1] and v.g2_vertex == e2_vertices[1]} + product_graph.connect source, destination + source = product_graph.vertices.find {|v| v.g1_vertex == e1_vertices[0] and v.g2_vertex == e2_vertices[1]} + destination = product_graph.vertices.find {|v| v.g1_vertex == e1_vertices[1] and v.g2_vertex == e2_vertices[0]} + product_graph.connect source, destination end end + product_graph + end + + # Performs similarity flooding on a graph + def similarity_flood(&normalization) end end # class