当前位置: 首页>>代码示例>>Python>>正文


Python Graph.vertices方法代码示例

本文整理汇总了Python中Graph.Graph.vertices方法的典型用法代码示例。如果您正苦于以下问题:Python Graph.vertices方法的具体用法?Python Graph.vertices怎么用?Python Graph.vertices使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在Graph.Graph的用法示例。


在下文中一共展示了Graph.vertices方法的8个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: GraphTest

# 需要导入模块: from Graph import Graph [as 别名]
# 或者: from Graph.Graph import vertices [as 别名]
class GraphTest(unittest.TestCase):

    def setUp(self):
        self.v = Vertex('v')
        self.w = Vertex('w')
        self.x = Vertex('x')
        self.y = Vertex('y')
        self.z = Vertex('z')
        self.e = Edge(self.v, self.w)
        self.e2 = Edge(self.v, self.x)
        self.g = Graph([self.v, self.w ,self.x, self.y],[self.e, self.e2])

    def test_get_edge_not_exist(self):
        edge = self.g.get_edge(self.v, self.y)
        self.assertIsNone(edge)

    def test_vertices_is_list(self):
        ''' Verifica que el retorno de la funcion vertices sea una lista '''
        vs = self.g.vertices()
        self.assertEqual( list, type( vs ) )

    def test_vertices_exist(self):
        ''' Todos los vertices incluidos en la lista existen en el grafo '''
        vs = ['v','w','x','y' ]
        self.assertEqual( sorted(vs), self.g.vertices() )

    def test_vertices_not_exist(self):
        ''' El vertice z no existe en el grafo generado en setup() '''
        vs =  ['v','w','x','y','z' ]
        self.assertNotEqual( sorted(vs), self.g.vertices() )

    def test_get_edge_exist(self):
        edge = self.g.get_edge(self.v, self.w)
        self.assertEqual(edge, self.e)

    def test_remove_edge_is_removed( self ):
        '''
        La prueba checa si el Edge esta por ahi.

        Depende de la funcion de get_edge
        '''
        edge = self.g.remove_edge( self.e )
        self.assertIsNone( self.g.get_edge( self.v, self.w ) )

    def test_vertex_repr(self):
        self.assertEqual( repr(self.v), "Vertex('v')" )

    def test_edge_one_vertex(self):
        self.assertRaises( ValueError, Edge, self.v )

    def test_edge_repr(self):
        self.assertEqual( repr(self.e),
                    "Edge(Vertex('v'), Vertex('w'))" )
开发者ID:mimex,项目名称:Grafo-TDD,代码行数:55,代码来源:test_Graph.py

示例2: test_vertices

# 需要导入模块: from Graph import Graph [as 别名]
# 或者: from Graph.Graph import vertices [as 别名]
  def test_vertices(self):
    g = Graph()
    v = Vertex('v')
    w = Vertex('w')
    x = Vertex('x')

    # Vertices when graph is empty
    self.verify_list_equal_unordered(g.vertices(), [])

    # Graph has one vertex
    g.add_vertex(v)
    self.verify_list_equal_unordered(g.vertices(), [v])

    # Graph has multiple vertex
    g.add_vertex(w)
    g.add_vertex(x)
    self.verify_list_equal_unordered(g.vertices(), [v, w, x])
开发者ID:shubhamgupta30,项目名称:think_bayes,代码行数:19,代码来源:Graph_test.py

示例3: test_graph

# 需要导入模块: from Graph import Graph [as 别名]
# 或者: from Graph.Graph import vertices [as 别名]
 def test_graph(self):
     v = Vertex('v')
     w = Vertex('w')
     self.assertEqual(repr(v), "Vertex('v')")
     
     e = Edge(v, w)
     self.assertEqual(repr(e), "Edge(Vertex('v'), Vertex('w'))")
     
     g = Graph([v, w], [e])
     self.assertEqual(repr(g), "{Vertex('w'): {Vertex('v'): Edge(Vertex('v'), Vertex('w'))}, Vertex('v'): {Vertex('w'): Edge(Vertex('v'), Vertex('w'))}}")
     
     e2 = g.get_edge(v, w)
     self.assertEqual(e, e2)
     
     e3 = g.get_edge(v, v)
     self.assertEqual(e3, None)
     
     vs = [Vertex(c) for c in 'abcd']
     g = Graph(vs)
     g.add_regular_edges(3)
     
     for v in g.vertices():
         es = g.out_edges(v)
         self.assertEqual(len(es), 3)
         
         vs = g.out_vertices(v)
         self.assertEqual(len(vs), 3)
         
     g.remove_edge(Edge(Vertex('a'), Vertex('c')))
     
     vs = g.vertices()
     self.assertEqual(len(vs), 4)
     
     es = g.edges()
     self.assertEqual(len(es), 5)
     
     g.add_all_edges()
     es = g.edges()
     self.assertEqual(len(es), 6)
     
     g2 = eval(repr(g))
     self.assertEqual(g, g2)
开发者ID:paulmouzas,项目名称:think-complexity,代码行数:44,代码来源:GraphTest.py

示例4: test_list_vertices

# 需要导入模块: from Graph import Graph [as 别名]
# 或者: from Graph.Graph import vertices [as 别名]
    def test_list_vertices(self):

        v = Vertex(1)
        v2 = Vertex(2)
        v3 = Vertex(3)

        g = Graph(vs=[v, v2, v3], es=[])

        vertices = g.vertices()

        self.assertTrue(v in vertices)
        self.assertTrue(v2 in vertices)
        self.assertTrue(v3 in vertices)
开发者ID:tsoporan,项目名称:think_complex,代码行数:15,代码来源:tests.py

示例5: edge_product_graph

# 需要导入模块: from Graph import Graph [as 别名]
# 或者: from Graph.Graph import vertices [as 别名]
 def edge_product_graph(self, g1, g2):
     result = Graph()
     for e1 in g1.edges():
         for e2 in g2.edges():
             if g1.get_edge_attribute(e1) == None:
                 continue
             if g2.get_edge_attribute(e2) == None:
                 continue
             if g1.get_edge_attribute(e1) != g2.get_edge_attribute(e2):
                 continue
             if g1.get_node_attribute(e1[0]) != g2.get_node_attribute(e2[0]):
                 continue
             if g1.get_node_attribute(e1[1]) != g2.get_node_attribute(e2[1]):
                 continue
             # the two edges match
             # print("edge matches:" + str(e1) + str(e2))
             # print(g1.get_edge_attribute(e1),g2.get_edge_attribute(e2))
             result.add_vertex((e1, e2))
     # add edges in r (between edge pairs that are compatible)
     # print(len(result.vertices()))
     product_nodes = result.vertices()
     for i in range(len(product_nodes)):
         for j in range(i+1, len(product_nodes)):
             ee1 = product_nodes[i]
             ee2 = product_nodes[j]
             if ee1 == ee2:
                 continue
             middle_node1 = None
             middle_node2 = None
             for v1 in ee1[0]:
                 for v2 in ee2[0]:
                     if v1 == v2:
                         middle_node1 = v1
             for v1 in ee1[1]:
                 for v2 in ee2[1]:
                     if v1 == v2:
                         middle_node2 = v1
             result.add_edge((ee1,ee2))
             if middle_node1 == None or middle_node2 == None:
                 # print("d_edge setting : ", ee1, ee2)
                 result.set_edge_attribute({(ee1,ee2) : "d-edge"})
             elif g1.get_node_attribute(middle_node1) == g2.get_node_attribute(middle_node2):
                 result.set_edge_attribute({(ee1,ee2) : "c-edge"})
             else:
                 # print("d_edge setting : ", ee1, ee2)
                 result.set_edge_attribute({(ee1,ee2) : "d-edge"})
     # print(" resulting edge product graph: ")
     # for e in result.edges():
     #     print(str(e))
     #     print(result.get_edge_attribute(e))
     return result
开发者ID:,项目名称:,代码行数:53,代码来源:

示例6: Graph

# 需要导入模块: from Graph import Graph [as 别名]
# 或者: from Graph.Graph import vertices [as 别名]
from DepthFirstPaths import selectDepthFirstPaths as selectDepthFirstPaths

from BreadthFirstPaths import BreadthFirstPaths as BreadthFirstPaths

# create simple graph

G = Graph()
G.addEdge("A", "B")
G.addEdge("A", "C")
G.addEdge("B", "D")
G.addEdge("D", "A")
G.addEdge("C", "E")

G.toString()

print "vertices:", G.vertices()
print "vertices adjacent to B:", G.neighborsOf("B")
print

# recursive depth first paths

dfp = selectDepthFirstPaths(recursive=True)(G, "A", verbose=True);
print "depth first search path to D:", dfp.pathTo("D")
print

# stack-based depth first paths 

dfp = selectDepthFirstPaths(recursive=False)(G, "A", verbose=True);
print "depth first search path to D:", dfp.pathTo("D")
print
开发者ID:wocjan,项目名称:basic_graph_algs_Python,代码行数:32,代码来源:test.py

示例7: Graph

# 需要导入模块: from Graph import Graph [as 别名]
# 或者: from Graph.Graph import vertices [as 别名]
'''
from Graph import Graph

g = { "a" : ["d"],
        "b" : ["c"],
        "c" : ["b", "c", "d", "e"],
        "d" : ["a", "c"],
        "e" : ["c"],
        "f" : []
    }


graph = Graph(g)

print("Vertices of graph:")
print(graph.vertices())

print("Edges of graph:")
print(graph.edges())

print("Add vertex:")
graph.add_vertex("z")

print("Vertices of graph:")
print(graph.vertices())
 
print("Add an edge:")
graph.add_edge({"a","z"})
    
print("Vertices of graph:")
print(graph.vertices())
开发者ID:Lieto,项目名称:Python,代码行数:33,代码来源:graph_demo.py

示例8: Graph

# 需要导入模块: from Graph import Graph [as 别名]
# 或者: from Graph.Graph import vertices [as 别名]
 print e
 
 g = Graph([v, w], [e])
 pprint(g, width=1)
 
 e1 = g.get_edge(v, v)
 e2 = g.get_edge(w, v)
 print e1, e2
 
 e1 = Edge(v, v)
 e2 = Edge(w, w)
 g = Graph([v, w], [e, e1, e2])
 g.remove_edge(Edge(v, v))
 pprint(g, width=1)
 
 print g.vertices()
 
 print g.edges()
 
 u = Vertex('u')
 g = Graph([u, v, w])
 pprint(g, width=1)
 g.add_all_edges()
 pprint(g, width=1)
 
 print g.out_edges(v)
 print g.out_vertices(v)
 
 # test creation of Graph of degree d
 alphabet = 'abcdefghijklmnopqrstuvwxyz'
 vs = []
开发者ID:nikos-daniilidis,项目名称:python-complexity,代码行数:33,代码来源:GraphTest.py


注:本文中的Graph.Graph.vertices方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。