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


Python Graph.add_edge方法代码示例

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


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

示例1: test_get_edge

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

# 需要导入模块: from Graph import Graph [as 别名]
# 或者: from Graph.Graph import add_edge [as 别名]
 def test_add_edge(self):
     graph1 = Graph("Graphec")
     graph1.add_edge("A", "B")
     self.assertEqual(graph1.Nodes["A"].name, "A")
     self.assertEqual(graph1.Nodes["B"].name, "B")
     self.assertEqual(graph1.Edges[0].froms, graph1.Nodes["A"])
     self.assertEqual(graph1.Edges[0].to, graph1.Nodes["B"])
开发者ID:Hristiyan-Andreev,项目名称:HackBuglaria,代码行数:9,代码来源:testGraph.py

示例4: test_add_all_edges

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

示例5: randomTree

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

# 需要导入模块: from Graph import Graph [as 别名]
# 或者: from Graph.Graph import add_edge [as 别名]
def erdos_renyi(n, p=1/2):
    """
    constructs erdos-renyi random graph on n vertices with probability p
    (default p=1/2)
    """
    G = Graph(V=range(n), E=[], normalize=False)
    for i in G.V:
        for j in G.V:
            if i < j and random() < p:
                G.add_edge((i, j))
    return G
开发者ID:samuelBB,项目名称:MRCN,代码行数:13,代码来源:GraphTheoreticTools.py

示例7: GraphGen

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

示例8: build_graph

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

示例9: edge_product_graph

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

示例10: genGraph

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

示例11: test_remove_vertex

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

示例12: string_graph

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

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

    # Edges when graph is empty
    self.verify_list_equal_unordered(g.edges(), [])

    # Edges when only vertices present
    g.add_vertex(v)
    g.add_vertex(w)
    self.verify_list_equal_unordered(g.edges(), [])

    # Edges when edges present
    g.add_edge(e1)
    g.add_edge(e2)
    g.add_edge(e3)
    self.verify_list_equal_unordered(g.edges(), [e1, e2, e3])
开发者ID:shubhamgupta30,项目名称:think_bayes,代码行数:24,代码来源:Graph_test.py

示例14: test_add_edge

# 需要导入模块: from Graph import Graph [as 别名]
# 或者: from Graph.Graph import add_edge [as 别名]
    def test_add_edge(self):
        v1 = Vertex('1')
        v2 = Vertex('2')
        e1 = Edge(v1, v2)

        v3 = Vertex('3')
        v4 = Vertex('4')
        e2 = Edge(v3, v4)

        e3 = Edge(v1, v4)

        g = Graph(vs=[v1,v2,v3,v4], es=[e1,e2])

        # Ensure that each vertex has the correct edge
        self.assertTrue(e1 in g[v1].values())
        self.assertTrue(e1 in g[v2].values())
        self.assertTrue(e2 in g[v3].values())
        self.assertTrue(e2 in g[v4].values())

        g.add_edge(e3)

        self.assertTrue(e3 in g[v1].values())
        self.assertTrue(e3 in g[v4].values())
开发者ID:tsoporan,项目名称:think_complex,代码行数:25,代码来源:tests.py

示例15: topology_to_network

# 需要导入模块: from Graph import Graph [as 别名]
# 或者: from Graph.Graph import add_edge [as 别名]
def topology_to_network(nodes, edges):
    """(nodes, edges) -> Graph
    Given a simple topology description, generate a graph network
    of the computers and cable types created by the network
    
     network_nodes is a list of computer addresses
     network cables is a list of tuples of the form
      (cabletype, computer1, computer2)"""
    
    network = Graph()
    nodeLookup = {}
    for name in nodes:
        node = Node(name)
        nodeLookup[name] = node
        network.add_node(node)
        
    for cabletype, computer1, computer2 in edges:
        node1 = nodeLookup[computer1]
        node2 = nodeLookup[computer2]
        edge = Edge(cabletype)
        network.add_edge(edge, node1, node2)

    return network
开发者ID:MasaruFukuie,项目名称:Frog2,代码行数:25,代码来源:topology.py


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