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


Python Graph.add_vertex方法代码示例

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


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

示例1: test_get_edge

# 需要导入模块: from Graph import Graph [as 别名]
# 或者: from Graph.Graph import add_vertex [as 别名]
  def test_get_edge(self):
    g = Graph()
    v = Vertex('v')
    w = Vertex('w')
    x = Vertex('x')
    e1 = Edge(v,w)
    e2 = Edge(w,x)
    e3 = Edge(v,x)

    # Query for edge from an empty graph
    self.assertEqual(g.get_edge(v, w), None)

    # Query for a non existent edge with one vertex present
    g.clear()
    g.add_vertex(v)
    self.assertEqual(g.get_edge(v, w), None)

    # Query for a non existent edge with both vertices present
    g.clear()
    g.add_vertex(v)
    g.add_vertex(w)
    self.assertEqual(g.get_edge(v, w), None)

    # Query for an existent edge
    g.clear()
    g.add_edge(e1)
    g.add_edge(e2)
    self.assertEqual(g.get_edge(v,w), e1)
开发者ID:shubhamgupta30,项目名称:think_bayes,代码行数:30,代码来源:Graph_test.py

示例2: test_remove_edge

# 需要导入模块: from Graph import Graph [as 别名]
# 或者: from Graph.Graph import add_vertex [as 别名]
  def test_remove_edge(self):
    g = Graph()
    v = Vertex('v')
    w = Vertex('w')
    x = Vertex('x')
    e1 = Edge(v,w)
    e2 = Edge(w,x)
    e3 = Edge(v,x)

    # Remove edge from an empty graph
    g.remove_edge(e1)
    self.assertEqual(g, {})

    # Remove a non existent edge with one vertex present
    g.clear()
    g.add_vertex(v)
    g.remove_edge(e1)
    self.assertEqual(g, {v:{}})

    # Remove a non existent edge with both vertices present
    g.clear()
    g.add_vertex(v)
    g.add_vertex(w)
    g.remove_edge(e1)
    self.assertEqual(g, {v:{}, w:{}})

    # Remove an existent edge
    g.clear()
    g.add_edge(e1)
    g.add_edge(e2)
    g.remove_edge(e1)
    self.assertEqual(g, {v:{}, w:{x:e2}, x:{w:e2}})
开发者ID:shubhamgupta30,项目名称:think_bayes,代码行数:34,代码来源:Graph_test.py

示例3: test_remove_vertex

# 需要导入模块: from Graph import Graph [as 别名]
# 或者: from Graph.Graph import add_vertex [as 别名]
  def test_remove_vertex(self):
    g = Graph()
    v = Vertex('v')
    w = Vertex('w')
    x = Vertex('x')
    e1 = Edge(v,w)
    e2 = Edge(w,x)
    e3 = Edge(v,x)

    # Test removal of only vertex
    g.add_vertex(v)
    g.remove_vertex(v)
    self.assertEqual(g, {})

    # Test removal of vertex with no edges incident on it
    g.clear()
    g.add_vertex(v)
    g.add_edge(e2)
    g.remove_vertex(v)
    self.assertEqual(g, {w:{x:e2}, x:{w:e2}})

    # Test removal of vertex with edges incident on it.
    g.clear()
    g.add_edge(e1)
    g.add_edge(e2)
    g.add_edge(e3)
    g.remove_vertex(v)
    self.assertEqual(g, {w:{x:e2}, x:{w:e2}})
开发者ID:shubhamgupta30,项目名称:think_bayes,代码行数:30,代码来源:Graph_test.py

示例4: test_add_vertex

# 需要导入模块: from Graph import Graph [as 别名]
# 或者: from Graph.Graph import add_vertex [as 别名]
  def test_add_vertex(self):
    g = Graph()
    # Tests that a label is used only once.
    g.add_vertex(Vertex('label1'))
    g.add_vertex(Vertex('label2'))
    g.add_vertex(Vertex('label2'))

    self.assertEqual(g, {Vertex('label1'):{}, Vertex('label2'):{}})
开发者ID:shubhamgupta30,项目名称:think_bayes,代码行数:10,代码来源:Graph_test.py

示例5: randomTree

# 需要导入模块: from Graph import Graph [as 别名]
# 或者: from Graph.Graph import add_vertex [as 别名]
def randomTree(n):
    """
    constructs a random tree by sequentially adding new edges to a random
    vertex already part of the tree
    """
    G = Graph(V=[0], E=[])
    for i in range(1, n):
        G.add_edge((choice(G.V), i))
        G.add_vertex(i)
    return G
开发者ID:samuelBB,项目名称:MRCN,代码行数:12,代码来源:GraphTheoreticTools.py

示例6: test_cycle_with_isolated_points

# 需要导入模块: from Graph import Graph [as 别名]
# 或者: from Graph.Graph import add_vertex [as 别名]
 def test_cycle_with_isolated_points(self):
     graph = Graph()
     graph.add_directed_link(1, 2)
     graph.add_vertex(6)
     graph.add_vertex(7)
     graph.add_directed_link(2, 3)
     graph.add_vertex(1)
     graph.add_directed_link(6, 4)
     graph.add_directed_link(3, 1)
     graph.add_directed_link(3, 4)
     self.assertEqual(graph.topological_sort(), None)
开发者ID:vks4git,项目名称:a3200-2015-algs,代码行数:13,代码来源:tests.py

示例7: create_graph

# 需要导入模块: from Graph import Graph [as 别名]
# 或者: from Graph.Graph import add_vertex [as 别名]
    def create_graph(self):
        """visualize graph from contributions in self

        @rtype: Graph
        @return: graph visualization
        """
        graph = Graph()
        for contrib in self:
            graph.add_vertex(contrib.donor, "donor")
            graph.add_vertex(contrib.recipient, "recipient")
            graph.add_directed_edge(contrib.donor, contrib.recipient, contrib.amount)
        graph.visualize_graph()
开发者ID:RLuckom,项目名称:python-graph-visualizer,代码行数:14,代码来源:ContributionList.py

示例8: test_add_vertex

# 需要导入模块: from Graph import Graph [as 别名]
# 或者: from Graph.Graph import add_vertex [as 别名]
    def test_add_vertex(self):
        v1 = Vertex('1')
        v2 = Vertex('2')
        v3 = Vertex('3')

        g = Graph(vs=[v1, v2])
        self.assertTrue(v1 in g)
        self.assertTrue(v2 in g)

        g.add_vertex(v3)

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

示例9: GraphGen

# 需要导入模块: from Graph import Graph [as 别名]
# 或者: from Graph.Graph import add_vertex [as 别名]
class GraphGen(object):
    def __init__(self, vertices, density, is_weighted):
        if density <= 0.0:
            Exception("Density too low")
        elif vertices < 2:
            Exception("Too few vertices")
        elif density > 1.0:
            density = 1.0

        self.vertices = vertices
        self.density = density
        self.is_weighted = is_weighted
        self.graph = Graph()
        self.gen_graph()

    def gen_graph(self):
        rand = Random()
        margin = self.density * sys.maxint

        coherent = False
        while not coherent:
            self.graph = Graph()
            for v in xrange(0, self.vertices):
                v = "v" + v.__str__()
                self.graph.add_vertex(v)
                for u in self.graph.get_vertices():
                    if margin >= rand.randint(0, sys.maxint) and v != u:
                        self.graph.add_edge(v, u, rand.randint(1, 999))
            if len(Dijkstra(self.graph, "v0")[0]) == self.vertices:
                coherent = True


    def get_graph(self):
        return self.graph

    def __str__(self):
        return self.graph.__str__()

    def string_graph(self):
        string_graph = Graph()
        string_graph.add_vertex("v0")
        string_graph.add_vertex("v1")
        string_graph.add_vertex("v2")
        string_graph.add_vertex("v3")
        string_graph.add_edge("v0", "v1", 410)
        string_graph.add_edge("v0", "v2", 81)
        string_graph.add_edge("v0", "v3", 321)
        string_graph.add_edge("v1", "v2", 337)
        string_graph.add_edge("v1", "v3", 125)
        string_graph.add_edge("v2", "v3", 733)

        return string_graph
开发者ID:bathemean,项目名称:pythonbach,代码行数:54,代码来源:GraphGen.py

示例10: build_graph

# 需要导入模块: from Graph import Graph [as 别名]
# 或者: from Graph.Graph import add_vertex [as 别名]
def build_graph(data):
    g = Graph()
    dict = {}
    for v in data["nodes"]:
        g.add_vertex(v.id)
        g.set_node_attribute({v.id: v})
        dict[id] = v
    for e in data["edges"]:
        source = e["source"]
        target = e["target"]
        g.add_edge((source,target))
        g.set_edge_attribute({(source,target):e})
    return g
开发者ID:,项目名称:,代码行数:15,代码来源:

示例11: edge_product_graph

# 需要导入模块: from Graph import Graph [as 别名]
# 或者: from Graph.Graph import add_vertex [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,代码来源:

示例12: string_graph

# 需要导入模块: from Graph import Graph [as 别名]
# 或者: from Graph.Graph import add_vertex [as 别名]
    def string_graph(self):
        string_graph = Graph()
        string_graph.add_vertex("v0")
        string_graph.add_vertex("v1")
        string_graph.add_vertex("v2")
        string_graph.add_vertex("v3")
        string_graph.add_edge("v0", "v1", 410)
        string_graph.add_edge("v0", "v2", 81)
        string_graph.add_edge("v0", "v3", 321)
        string_graph.add_edge("v1", "v2", 337)
        string_graph.add_edge("v1", "v3", 125)
        string_graph.add_edge("v2", "v3", 733)

        return string_graph
开发者ID:bathemean,项目名称:pythonbach,代码行数:16,代码来源:GraphGen.py

示例13: test_vertices

# 需要导入模块: from Graph import Graph [as 别名]
# 或者: from Graph.Graph import add_vertex [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

示例14: genGraph

# 需要导入模块: from Graph import Graph [as 别名]
# 或者: from Graph.Graph import add_vertex [as 别名]
def genGraph(rootNode):
    g = {rootNode: set(rootNode.children)}
    nodeQueue = Queue.Queue()
    curNode = rootNode
    graph = Graph(g)

    for n in rootNode.children:
        nodeQueue.put(n)
        graph.add_vertex(n);
        #print( str(n.idx)  + "is a child of " + str(rootNode.idx))


    while (not nodeQueue.empty()):
        curNode = nodeQueue.get()
        for d in curNode.children:
            nodeQueue.put(d); 
            graph.add_vertex(d);
            graph.add_edge({curNode, d})
            #print( str(d.idx)  + "is a child of " + str(curNode.idx))
            
    return graph
开发者ID:freddylukai,项目名称:Hospitalist,代码行数:23,代码来源:graphGen.py

示例15: test_add_all_edges

# 需要导入模块: from Graph import Graph [as 别名]
# 或者: from Graph.Graph import add_vertex [as 别名]
  def test_add_all_edges(self):
    g = Graph()
    v = Vertex('v')
    w = Vertex('w')
    x = Vertex('x')
    e1 = Edge(v,w)
    e2 = Edge(w,x)
    e3 = Edge(v,x)

    # Test on a empty graph
    g.add_all_edges()
    self.assertEqual(g, {})

    # Test on a non-edgeless graph
    g.add_edge(e1)
    g.add_vertex(x)
    g.add_all_edges()
    self.assertEqual(g, {v:{w:e1}, w:{v:e1}, x:{}})

    # Test on a edgeless graph
    g.clear()
    g.add_vertex(v)
    g.add_vertex(w)
    g.add_vertex(x)
    g.add_all_edges()
    self.assertEqual(g, {v:{w:e1, x:e3}, w:{v:e1, x:e2}, x:{w:e2, v:e3}})
开发者ID:shubhamgupta30,项目名称:think_bayes,代码行数:28,代码来源:Graph_test.py


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