コード例 #1
0
    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)
        g.add_edge(vertex1=vd1, vertex2=vd3)
        g.add_edge(vertex1=vd2, vertex2=vd4)
        visitor = bfs.distance_recording_visitor(start_vertex=vd1)
        bfs.breadth_first_search(graph=g, vertex=vd1, visitor=visitor)

        self.assertEqual(visitor.distance_for, {
            vd1: 0,
            vd2: 1,
            vd3: 1,
            vd4: 2
        })
        visitor2 = bfs.distance_recording_visitor(start_vertex=vd2)
        bfs.breadth_first_search(graph=g, vertex=vd2, visitor=visitor2)

        self.assertEqual(visitor2.distance_for, {
            vd1: 1,
            vd2: 0,
            vd3: 2,
            vd4: 1
        })
コード例 #2
0
ファイル: topology.py プロジェクト: renesugar/cctbx_project
    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]
コード例 #3
0
ファイル: topology.py プロジェクト: renesugar/cctbx_project
    def distances_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.distance_recording_visitor(start_vertex=vertex)
        bfs.breadth_first_search(
            graph=self.graph,
            vertex=vertex,
            visitor=visitor,
        )

        atom_for = self.atom_for
        return dict((atom_for[v], visitor.distance_for.get(v, None))
                    for v in self.descriptors)
コード例 #4
0
ファイル: topology.py プロジェクト: cctbx/cctbx-playground
  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 ]
コード例 #5
0
 def test_simple_graph(self):
     node_1 = Node(1)
     node_2 = Node(2)
     node_3 = Node(3)
     node_1.add_adjacent_nodes(node_2)
     node_2.add_adjacent_nodes(node_3)
     breadth_first = breadth_first_search(node_1)
     self.assertEqual(breadth_first.get(0), [node_1])
     self.assertEqual(breadth_first.get(1), [node_2])
     self.assertEqual(breadth_first.get(2), [node_3])
コード例 #6
0
ファイル: topology.py プロジェクト: cctbx/cctbx-playground
  def distances_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.distance_recording_visitor( start_vertex = vertex )
    bfs.breadth_first_search(
      graph = self.graph,
      vertex = vertex,
      visitor = visitor,
      )

    atom_for = self.atom_for
    return dict(
      ( atom_for[ v ], visitor.distance_for.get( v, None ) )
      for v in self.descriptors
      )
コード例 #7
0
  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 )
    g.add_edge( vertex1 = vd1, vertex2 = vd3 )
    g.add_edge( vertex1 = vd2, vertex2 = vd4 )
    visitor = bfs.distance_recording_visitor( start_vertex = vd1 )
    bfs.breadth_first_search( graph = g, vertex = vd1, visitor = visitor )

    self.assertEqual(
      visitor.distance_for,
      { vd1: 0, vd2: 1, vd3: 1, vd4: 2 }
      )
    visitor2 = bfs.distance_recording_visitor( start_vertex = vd2 )
    bfs.breadth_first_search( graph = g, vertex = vd2, visitor = visitor2 )

    self.assertEqual(
      visitor2.distance_for,
      { vd1: 1, vd2: 0, vd3: 2, vd4: 1 }
      )
コード例 #8
0
 def test_large_graph_with_cycle(self):
     node_0 = Node(0)
     node_1 = Node(1)
     node_2 = Node(2)
     node_3 = Node(3)
     node_2.add_adjacent_nodes(node_0)
     node_2.add_adjacent_nodes(node_3)
     node_0.add_adjacent_nodes(node_1)
     node_0.add_adjacent_nodes(node_2)
     node_1.add_adjacent_nodes(node_2)
     node_3.add_adjacent_nodes(node_3)
     breadth_first = breadth_first_search(node_2)
     self.assertEqual(breadth_first.get(0), [node_2])
     self.assertEqual(breadth_first.get(1), [node_0, node_3])
     self.assertEqual(breadth_first.get(2), [node_1])
コード例 #9
0
  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 )
コード例 #10
0
 def test_empty_graph(self):
     graph = None
     self.assertEqual(breadth_first_search(graph), {})
コード例 #11
0
    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)