本文整理汇总了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)
示例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}})
示例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"])
示例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}})
示例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
示例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
示例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
示例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
示例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
示例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
示例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}})
示例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
示例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])
示例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())
示例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