Beispiel #1
0
  def connected_segment_from(self, atom):

    if atom not in self.descriptor_for:
      raise ValueError, "Unknown atom: %s" % atom

    from graph import breadth_first_search as bfs

    vertex = self.descriptor_for[ atom ]
    visitor = bfs.vertex_recording_visitor( start_vertex = vertex )
    bfs.breadth_first_search(
      graph = self.graph,
      vertex = vertex,
      visitor = visitor,
      )

    atom_for = self.atom_for

    return [ atom_for[ v ] for v in visitor.visited_vertices ]
Beispiel #2
0
    def connected_segment_from(self, atom):

        if atom not in self.descriptor_for:
            raise ValueError, "Unknown atom: %s" % atom

        from graph import breadth_first_search as bfs

        vertex = self.descriptor_for[atom]
        visitor = bfs.vertex_recording_visitor(start_vertex=vertex)
        bfs.breadth_first_search(
            graph=self.graph,
            vertex=vertex,
            visitor=visitor,
        )

        atom_for = self.atom_for

        return [atom_for[v] for v in visitor.visited_vertices]
  def manipulation(self, g):

    vd1 = g.add_vertex()
    vd2 = g.add_vertex()
    vd3 = g.add_vertex()
    vd4 = g.add_vertex()

    g.add_edge( vertex1 = vd1, vertex2 = vd2 )
    visitor = bfs.vertex_recording_visitor( start_vertex = vd1 )
    bfs.breadth_first_search( graph = g, vertex = vd1, visitor = visitor )
    self.assertEqual( visitor.visited_vertices, [ vd1, vd2 ] )

    visitor = bfs.vertex_recording_visitor( start_vertex = vd2 )
    bfs.breadth_first_search( graph = g, vertex = vd2, visitor = visitor )
    self.assertEqual( visitor.visited_vertices, [ vd2, vd1 ] )

    visitor = bfs.vertex_recording_visitor( start_vertex = vd3 )
    bfs.breadth_first_search( graph = g, vertex = vd3, visitor = visitor )
    self.assertEqual( visitor.visited_vertices, [ vd3 ] )

    g.add_edge( vertex1 = vd2, vertex2 = vd3 )
    g.add_edge( vertex1 = vd2, vertex2 = vd4 )
    visitor = bfs.vertex_recording_visitor( start_vertex = vd1 )
    bfs.breadth_first_search( graph = g, vertex = vd1, visitor = visitor )
    self.assertEqual( visitor.visited_vertices[:2], [ vd1, vd2 ] )
    self.assertEqual(
      set( visitor.visited_vertices ),
      set( [ vd1, vd2, vd3, vd4 ] ),
      )

    visitor = bfs.vertex_recording_visitor( start_vertex = vd2 )
    bfs.breadth_first_search( graph = g, vertex = vd2, visitor = visitor )
    self.assertEqual(
      set( visitor.visited_vertices ),
      set( [ vd1, vd2, vd3, vd4 ] ),
      )
    self.assertEqual( visitor.visited_vertices[0], vd2 )

    g.add_edge( vertex1 = vd1, vertex2 = vd3 )
    visitor = bfs.vertex_recording_visitor( start_vertex = vd1 )
    bfs.breadth_first_search( graph = g, vertex = vd1, visitor = visitor )
    self.assertEqual(
      set( visitor.visited_vertices ),
      set( [ vd1, vd2, vd3, vd4 ] ),
      )
    self.assertEqual( len( visitor.visited_vertices ), 4 )
    def manipulation(self, g):

        vd1 = g.add_vertex()
        vd2 = g.add_vertex()
        vd3 = g.add_vertex()
        vd4 = g.add_vertex()

        g.add_edge(vertex1=vd1, vertex2=vd2)
        visitor = bfs.vertex_recording_visitor(start_vertex=vd1)
        bfs.breadth_first_search(graph=g, vertex=vd1, visitor=visitor)
        self.assertEqual(visitor.visited_vertices, [vd1, vd2])

        visitor = bfs.vertex_recording_visitor(start_vertex=vd2)
        bfs.breadth_first_search(graph=g, vertex=vd2, visitor=visitor)
        self.assertEqual(visitor.visited_vertices, [vd2, vd1])

        visitor = bfs.vertex_recording_visitor(start_vertex=vd3)
        bfs.breadth_first_search(graph=g, vertex=vd3, visitor=visitor)
        self.assertEqual(visitor.visited_vertices, [vd3])

        g.add_edge(vertex1=vd2, vertex2=vd3)
        g.add_edge(vertex1=vd2, vertex2=vd4)
        visitor = bfs.vertex_recording_visitor(start_vertex=vd1)
        bfs.breadth_first_search(graph=g, vertex=vd1, visitor=visitor)
        self.assertEqual(visitor.visited_vertices[:2], [vd1, vd2])
        self.assertEqual(
            set(visitor.visited_vertices),
            set([vd1, vd2, vd3, vd4]),
        )

        visitor = bfs.vertex_recording_visitor(start_vertex=vd2)
        bfs.breadth_first_search(graph=g, vertex=vd2, visitor=visitor)
        self.assertEqual(
            set(visitor.visited_vertices),
            set([vd1, vd2, vd3, vd4]),
        )
        self.assertEqual(visitor.visited_vertices[0], vd2)

        g.add_edge(vertex1=vd1, vertex2=vd3)
        visitor = bfs.vertex_recording_visitor(start_vertex=vd1)
        bfs.breadth_first_search(graph=g, vertex=vd1, visitor=visitor)
        self.assertEqual(
            set(visitor.visited_vertices),
            set([vd1, vd2, vd3, vd4]),
        )
        self.assertEqual(len(visitor.visited_vertices), 4)