本文整理汇总了Python中Graph.setEdge方法的典型用法代码示例。如果您正苦于以下问题:Python Graph.setEdge方法的具体用法?Python Graph.setEdge怎么用?Python Graph.setEdge使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Graph
的用法示例。
在下文中一共展示了Graph.setEdge方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: main
# 需要导入模块: import Graph [as 别名]
# 或者: from Graph import setEdge [as 别名]
def main():
# make a new Graph called fredo
fredo = Graph()
# add 5 vertices
fredo.addVertex('A')
fredo.addVertex('B')
fredo.addVertex('C')
fredo.addVertex('D')
fredo.addVertex('E')
#show graph with no edges
fredo.display()
#check edge and vertex count
if fredo.countEdges() !=0:
print 'Edge count incorrect'
else:
print 'Edge count correct'
if fredo.countVertices() != 5:
print 'Vertex count incorrect'
else:
print 'Vertex count correct'
#Set edges to values
fredo.setEdge('A', 'A', 1.0)
fredo.setEdge('A', 'B', 2.0)
fredo.setEdge('A', 'C', 3.0)
fredo.setEdge('A', 'D', 4.0)
fredo.setEdge('B', 'A', 5.0)
fredo.setEdge('B', 'B', 6.0)
fredo.setEdge('B', 'C', 7.0)
fredo.setEdge('B', 'D', 8.0)
fredo.setEdge('C', 'A', 9.0)
fredo.setEdge('C', 'B', 10.0)
fredo.setEdge('C', 'C', 11.0)
fredo.setEdge('C', 'D', 12.0)
fredo.setEdge('D', 'A', 13.0)
fredo.setEdge('D', 'B', 14.0)
fredo.setEdge('D', 'C', 15.0)
fredo.setEdge('D', 'D', 16.0)
fredo.setEdge('E', 'B', 17.0)
#Show new graph
fredo.display()
#Check new edge count
if fredo.countEdges() !=13:
print 'Edge count 2 incorrect'
else:
print 'Edge count 2 correct'
#Add duplicate vertex
fredo.addVertex('B')
#Check if vertex count remains same
if fredo.countVertices() != 5:
print 'Vertex count 2 incorrect'
else:
print 'Vertex count 2 correct'
#Remove zero edge from graph
fredo.removeEdge('D','D')
#Ensure edge count remains same
if fredo.countEdges() !=13:
print 'Edge count 3 incorrect'
else:
print 'Edge count 3 correct'
#Remove vertex
fredo.removeVertex('B')
#Display graph
fredo.display()
#Check edges and Vertices
if fredo.countEdges() !=6:
print 'Edge count 4 incorrect'
else:
print 'Edge count 4 correct'
if fredo.countVertices() !=4:
print 'Vertex count 4 incorrect'
else:
print 'Vertex count 4 correct'
#Test graph clear (THIS IS A BUG)
fredo.clear()
#Test to see if no vertices or edges remain
if fredo.countEdges() !=0:
print 'Edge count 5 incorrect'
else:
print 'Edge count 5 correct'
if fredo.countVertices() !=0:
print 'Vertex count 5 incorrect'
else:
print 'Vertex count 5 correct'
示例2: main
# 需要导入模块: import Graph [as 别名]
# 或者: from Graph import setEdge [as 别名]
def main():
# make a new Graph called fredo
fredo = Graph()
fredo.addVertex('A')
fredo.addVertex('B')
fredo.addVertex('C')
fredo.setEdge('A', 'A', 1.0)
fredo.setEdge('A', 'B', 2.0)
fredo.setEdge('A', 'C', 3.0)
fredo.setEdge('B', 'A', 5.0)
fredo.setEdge('B', 'B', 6.0)
fredo.setEdge('B', 'C', 7.0)
fredo.setEdge('C', 'A', 9.0)
fredo.setEdge('C', 'B', 10.0)
fredo.setEdge('C', 'C', 11.0)
fredo.display()
if fredo.countVertices() != 3:
print 'Bad graph! Vertices are wrong', g.countVertices()
else:
print 'Vertices are correct'
fredo.addVertex('D')
fredo.addVertex('E')
fredo.addVertex('E')
#duplicating a vertex doesn't add another vertex to the count
fredo.display()
if fredo.countVertices() != 5:
print 'Bad graph! Vertices are wrong', g.countVertices()
else:
print 'Vertices are correct'
if fredo.countEdges() != 6:
print 'Bad graph! Edges are wrong', fredo.countEdges()
else:
print 'Edges are still correct'
fredo.setEdge('D', 'A', 0.0)
fredo.setEdge('D', 'A', 9.0)
#fredo.setEdge('D', 'A', 0.0)
#if you set it to zero, it doesn't
#count it as a edge
fredo.setEdge('D', 'B', 2.0)
fredo.setEdge('E', 'C', 4.0)
#this edge below doesn't add an edge, and if you take it out, it
#doesn't change anything as well
fredo.setEdge('E', 'E', 4.0)
fredo.display()
if fredo.countEdges() != 9:
print 'Bad graph! Edges are wrong', fredo.countEdges()
else:
print 'Edges are still correct'
示例3: main
# 需要导入模块: import Graph [as 别名]
# 或者: from Graph import setEdge [as 别名]
def main():
print 'Testing all the things'
# make a new variable of type Graph (so it gets made like the Graph.py class)
g = Graph()
# add 4 Vertices to the Graph, with the names Abilene, Boston, Chicago, Denver
g.addVertex('Abilene')
g.addVertex('Boston')
g.addVertex('Chicago')
g.addVertex('Denver')
# Now we have a Graph with 4 Vertices and 16 empty Edges.
# Let's try to set all the Edges. (We don't actually set
# Edges from a Vertex to itself, so these self Edges should
# never actually show up when we display that Graph.)
g.setEdge('Abilene', 'Abilene', 1.0)
g.setEdge('Abilene', 'Boston', 2.0)
g.setEdge('Abilene', 'Chicago', 3.0)
g.setEdge('Abilene', 'Denver', 4.0)
g.setEdge('Boston', 'Abilene', 5.0)
g.setEdge('Boston', 'Boston', 6.0)
g.setEdge('Boston', 'Chicago', 7.0)
g.setEdge('Boston', 'Denver', 8.0)
g.setEdge('Chicago', 'Abilene', 9.0)
g.setEdge('Chicago', 'Boston', 10.0)
g.setEdge('Chicago', 'Chicago', 11.0)
g.setEdge('Chicago', 'Denver', 12.0)
g.setEdge('Denver', 'Abilene', 13.0)
g.setEdge('Denver', 'Boston', 14.0)
g.setEdge('Denver', 'Chicago', 15.0)
g.setEdge('Denver', 'Denver', 16.0)
# print the Vertex names and the Edge costs in the array
g.display()
# check if our vertexCount is correct
if g.countVertices() != 4:
print 'Bad graph! Vertices are wrong', g.countVertices()
else:
print 'Test 1 ok'
# check if our edgeCount is correct
if g.countEdges() != 12:
print 'Bad graph! Edges are wrong', g.countEdges()
else:
print 'Test 2 ok'
# make a copy of g, called h
h = Graph()
h.copyDeeply(g)
# take out a Vertex
g.removeVertex('Chicago')
print 'Here is graph g after Chicago is removed'
g.display()
# make a copy of the 3-Vertex Graph g, called i
i = Graph()
i.copyDeeply(g)
# check if our vertexCount is correct after Chicago comes out
if g.countVertices() != 3:
print 'Bad graph! Vertices are wrong', g.countVertices()
else:
print 'Test 3 ok'
# check if our edgeCount is correct after Chicago's edges come out
if g.countEdges() != 6:
print 'Bad graph! Edges are wrong', g.countEdges()
else:
print 'Test 4 ok'
# check if our edgeCount is correct after Abilene->Boston edge comes out
g.setEdge('Abilene','Boston', 0.0)
if g.countEdges() != 5:
print 'Bad graph! Edges are wrong', g.countEdges()
else:
print 'Test 5 ok'
g.removeEdge('Boston','Denver')
# check if our edgeCount is correct after Boston->Denver edge comes out
if g.countEdges() != 4:
print 'Bad graph! Edges are wrong', g.countEdges()
else:
print 'Test 6 ok'
g.removeEdge('Boston','Boston')
# check if our edgeCount is correct after empty Boston->Boston edge comes out
if g.countEdges() != 4:
print 'Bad graph! Edges are wrong', g.countEdges()
else:
print 'Test 7 ok'
# check if we know that the Boston->Denver edge is out
if not g.containsEdge('Boston', 'Denver'):
print 'Bad graph! Edges are wrong, Boston->Denver should not be in Graph'
else:
print 'Test 8 ok'
# check if Boston is there
if not g.containsVertex('Boston'):
#.........这里部分代码省略.........