本文整理汇总了Python中Graph.addVertex方法的典型用法代码示例。如果您正苦于以下问题:Python Graph.addVertex方法的具体用法?Python Graph.addVertex怎么用?Python Graph.addVertex使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Graph
的用法示例。
在下文中一共展示了Graph.addVertex方法的7个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: main
# 需要导入模块: import Graph [as 别名]
# 或者: from Graph import addVertex [as 别名]
def main():
g = Graph()
for i in range(6):
g.addVertex(i)
g.addEdge(0,1,5)
g.addEdge(0,5,2)
g.addEdge(1,2,4)
g.addEdge(2,3,9)
g.addEdge(3,4,7)
g.addEdge(3,5,3)
g.addEdge(4,0,1)
print "result:"
connected = BFS(g,0,3)
print connected
示例2: main
# 需要导入模块: import Graph [as 别名]
# 或者: from Graph import addVertex [as 别名]
def main():
v1 = Vertex('A')
v2 = Vertex('B')
v3 = Vertex('C')
e1 = Edge(v1,v3)
e = Edge(v1,v2)
print e
g = Graph()
g.addVertex(v1)
g.addVertex(v2)
g.addVertex(v3)
print g.addEdge(e)
print g.addEdge(e1)
print g
print "Will try to delete edge"
print g.delEdge(v1,v2)
print g
v4 = Vertex('A')
g.delVertex(v4)
print g
示例3: loadGraph
# 需要导入模块: import Graph [as 别名]
# 或者: from Graph import addVertex [as 别名]
def loadGraph():
graph = Graph()
try:
aux = open(sys.argv[2])
for line in aux:
line = line.replace(':',',').replace('\n','').split(',')
v1 = Vertex(line[0])
graph.addVertex(v1)
for n in range(1,len(line)):
if not line[n]:
continue
v2 = Vertex(line[n])
if (not graph.isVertex(v2)):
graph.addVertex(v2)
graph.addEdge(v1,v2)
return graph
except KeyError:
print "An error has occurred"
except IndexError:
usage()
except IOError:
print "Verify parameters, input file does not exist"
return False
示例4: main
# 需要导入模块: import Graph [as 别名]
# 或者: from Graph import addVertex [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'
示例5: main
# 需要导入模块: import Graph [as 别名]
# 或者: from Graph import addVertex [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'
示例6: Stack
# 需要导入模块: import Graph [as 别名]
# 或者: from Graph import addVertex [as 别名]
#Creates a stack and tests a stack of 10 integers. Implements LIFO 0 goes in first up to 9 then nine comes out first
sizeofstack = 10
i = 0;
stack = Stack()
while i < sizeofstack:
stack.push(i)
i += 1
print "testing stack"
while stack.checkSize() != 0:
print stack.pop()
print " "
#Graph
#creates a Graph intially with 10 unconnected nodes then randomly connects the nodes
#then randomly searches for 5 nodes
graph = Graph()
i = 0
while i < 10:
graph.addVertex(i)
i += 1
i = 0
while i < 20:
graph.addEdge(randint(0,9), randint(0,9))
i += 1
i = 0
while i < 5:
graph.findVertex(randint(0,10))
i += 1
示例7: main
# 需要导入模块: import Graph [as 别名]
# 或者: from Graph import addVertex [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'):
#.........这里部分代码省略.........