本文整理汇总了Python中Graph.Graph.addVertex方法的典型用法代码示例。如果您正苦于以下问题:Python Graph.addVertex方法的具体用法?Python Graph.addVertex怎么用?Python Graph.addVertex使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Graph.Graph
的用法示例。
在下文中一共展示了Graph.addVertex方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: main
# 需要导入模块: from Graph import Graph [as 别名]
# 或者: from Graph.Graph import addVertex [as 别名]
def main():
rows = read_file()
orders = {}
for row in rows:
if row['Order__c'] not in orders:
orders[row['Order__c']] = []
orders[row['Order__c']].append({'Id': row['Id'], 'Shipper':row['Revenova_Shipper__c'],'Consignee':row['Revenova_Consignee__c'],'Carrier':row['Carrier__c']})
for name in orders:
try:
graph = Graph()
for load in orders[name]:
shipper = str(load['Shipper'])
consignee = str(load['Consignee'])
graph.addVertex(shipper,{'ship':load['Id']})
graph.addVertex(consignee,{'cons':load['Id']})
graph.addEdge(shipper, consignee, str(load['Carrier']))
custResults[name] = graph.flattenCustomerLoads()
carrResults[name] = graph.flattenCarrierLoads()
except RuntimeError:
errors.append(name)
print 'Recursion Error'
print 'Successes: '+str(len(orders))
print 'Errors: '+str(len(errors))
custLoads = [{'Order':n, 'Shipper':i['Shipper'], 'Consignee':i['Consignee']} for n in custResults for i in custResults[n]]
write_file(custLoads,'cust-loads.csv')
carrLoads = [{'Order':n, 'Shipper':i['Shipper'], 'Consignee':i['Consignee'], 'Carrier':i['Carrier']} for n in carrResults for i in carrResults[n]]
write_file(carrLoads,'carr-loads.csv')
示例2: getRTTGraph
# 需要导入模块: from Graph import Graph [as 别名]
# 或者: from Graph.Graph import addVertex [as 别名]
def getRTTGraph(self):
graph = Graph(self.conf.getNumNodes());
for i, node1 in enumerate(self.nodes):
for j, node2 in enumerate(self.nodes):
rtt = getNorm(getVet(node1, node2))
graph.addVertex(i, j, rtt)
return graph
示例3: prepG
# 需要导入模块: from Graph import Graph [as 别名]
# 或者: from Graph.Graph import addVertex [as 别名]
def prepG( type = 'class' ) :
if type == 'dict':
G = {1: [2,3,4,5],
2: [1,3,4,5],
3: [1,2,4],
4: [1,2,3,5,6,7],
5: [2,4],
6: [4],
7: [4, 5]
}
if type == 'class':
G = Graph()
for i in range(1,8):
G.addVertex(i)
G.addEdge(1, 2, 1)
G.addEdge(1, 3, 1)
G.addEdge(2, 3, 1)
for i in range(1,4) + range(5,8):
G.addEdge(4, i, 1)
G.addEdge(5, 2, 1)
G.addEdge(7, 5, 1)
if type == 'matrix':
G = [[0,1,1,1,0,0,0],
[1,0,1,1,1,0,0],
[1,1,0,1,0,0,0],
[1,1,1,0,1,1,1],
[0,1,0,1,0,0,0],
[0,0,0,4,0,0,0],
[0,0,0,1,1,0,0]]
return G
示例4: buildgraph
# 需要导入模块: from Graph import Graph [as 别名]
# 或者: from Graph.Graph import addVertex [as 别名]
def buildgraph(rows):
g = Graph(len(rows))
for node in range(len(rows)):
arr = rows[node].strip().split(" ")
rtts = [float(x) for x in arr if len(x) > 0]
for neighbor in range(len(rtts)):
g.addVertex(node,neighbor,rtts[neighbor])
return g
示例5: testFindTransitiveClosure
# 需要导入模块: from Graph import Graph [as 别名]
# 或者: from Graph.Graph import addVertex [as 别名]
def testFindTransitiveClosure(self):
newGraph = Graph()
newGraph.addVertex(1)
newGraph.addVertex(2)
newGraph.connect(vertexid1=1, vertexid2=2, cost=None)
testTransitiveClosureSet = newGraph.transitiveClosure(vertexid=1)
self.assertTrue(1 in testTransitiveClosureSet and
2 in testTransitiveClosureSet,
'should be inside the transitive closure')
del newGraph
示例6: visibility_graph
# 需要导入模块: from Graph import Graph [as 别名]
# 或者: from Graph.Graph import addVertex [as 别名]
def visibility_graph(S, start, goal):
V = getAllVertices(S)
V.append(start)
V.append(goal)
E = []
for v in V:
W = visible_vertices(v, S, start, goal)
for w in W:
E.append((v, w))
Gvis = Graph()
for v in V:
Gvis.addVertex(v)
for e in E:
Gvis.addEdge(e[0], e[1])
cost = GeoUtils.euclidean_distance(e[0],e[1])
Gvis.addCost((e[0], e[1]), cost)
return Gvis
示例7: FortranDependencies
# 需要导入模块: from Graph import Graph [as 别名]
# 或者: from Graph.Graph import addVertex [as 别名]
class FortranDependencies ():
def __init__ (self, files):
from Graph import Graph
self.files = files
# The graph models the dependencies and is returned by this function
self.g = Graph()
self.__compute__()
def getDependencyGraph (self):
return self.g
def __compute__ (self):
from re import IGNORECASE, compile
# Mapping from a module name to its file name
moduleNameToFileName = {}
module_line_regex = compile("^\s*module\s+(\S+)\s*$", IGNORECASE)
program_line_regex = compile("^\s*program\s+(\S+)\s*$", IGNORECASE)
use_line_regex = compile("^\s*use", IGNORECASE)
for fileName in self.files:
self.g.addVertex(fileName)
f = open(fileName)
for line in f:
if module_line_regex.search(line) or program_line_regex.search(line):
lexemes = line.split()
moduleName = lexemes[len(lexemes) - 1]
moduleNameToFileName[moduleName] = fileName
print("Module '%s' in file '%s'" % (moduleName, fileName))
f.close()
for fileName in self.files:
f = open(fileName)
for line in f:
if use_line_regex.search(line):
lexemes = line.split()
moduleName = lexemes[len(lexemes) - 1]
if moduleName in moduleNameToFileName:
print("Adding edge %s to %s" % (moduleNameToFileName[moduleName], fileName))
self.g.addEdge(moduleNameToFileName[moduleName], fileName)
f.close()
示例8: GraphTest
# 需要导入模块: from Graph import Graph [as 别名]
# 或者: from Graph.Graph import addVertex [as 别名]
class GraphTest(unittest.TestCase):
def setUp(self):
self.graph = Graph()
self.graph.addVertex(vertexid='v1')
self.graph.addVertex(vertexid='v2')
self.graph.addVertex(vertexid='v3')
self.graph.addVertex(vertexid='v4')
self.graph.addVertex(vertexid='v5')
self.graph.connect(vertexid1='v1', vertexid2='v2', cost=1)
self.graph.connect(vertexid1='v2', vertexid2='v3', cost=23)
self.graph.connect(vertexid1='v3', vertexid2='v4', cost=2)
self.graph.connect(vertexid1='v4', vertexid2='v5', cost=93)
# for node in self.graph.graph:
# print('sucessor: {}\n'.format(self.graph.graph.get(node).sucessor),
# 'predecessor: {}\n'.format(self.graph.graph.get(node).predecessor))
def tearDown(self):
del self.graph
def testShortestPathDijkstra(self):
dijkstra = Dijkstra()
S1 = 'v1'
end = 'v4'
D, R1 = dijkstra.dijkstra(self.graph, S1, end)
print(D, R1)
Path = []
while True:
Path.append(end)
if end == S1:
break
end = R1[end]
Path.reverse()
print(Path)
示例9: testIfItIsTree
# 需要导入模块: from Graph import Graph [as 别名]
# 或者: from Graph.Graph import addVertex [as 别名]
def testIfItIsTree(self):
self.assertFalse(self.graph.isTree(), 'should not be a tree')
# (1) (2) (3)
# \ | /
# \ | /
# (4)
# |
# (5)
newGraph = Graph()
newGraph.addVertex(1)
newGraph.addVertex(2)
newGraph.addVertex(3)
newGraph.addVertex(4)
newGraph.addVertex(5)
newGraph.connect(vertexid1=1, vertexid2=4, cost=None)
newGraph.connect(vertexid1=2, vertexid2=4, cost=None)
newGraph.connect(vertexid1=3, vertexid2=4, cost=None)
newGraph.connect(vertexid1=5, vertexid2=4, cost=None)
self.assertTrue(newGraph.isTree(), 'should be a tulip right, its a flower sort of...')
del newGraph
示例10: addEdge
# 需要导入模块: from Graph import Graph [as 别名]
# 或者: from Graph.Graph import addVertex [as 别名]
course_info.setdefault('STATISTICS110', []).append(4)
course_info.setdefault('STATISTICS110', []).append(110)
course_info.setdefault('ECONOMICS111', []).append(6)
course_info.setdefault('ECONOMICS111', []).append(111)
course_info.setdefault('DBMS112', []).append(5)
course_info.setdefault('DBMS112', []).append(112)
for key in course_info:
c= 1
for value in course_info[key]:
if c == 1:
g.addVertex(key,value)
c = c+1
else:
break
print "####################################################################################################"
print g.vertList
print g.numVertices
print "\n \n"
def addEdge():
g.addEdge('ALGORITHMS103','COMPUTER FUNDAMENTALS104',2)
g.addEdge('ADVANCE STATISTICS105','STATISTICS110',4)
g.addEdge('NETWORKING CONCEPTS106','COMPUTER FUNDAMENTALS104', 2)
g.addEdge('NETWORK SECURITY108', 'CRYPTOGRAPHY107', 6)
示例11: len
# 需要导入模块: from Graph import Graph [as 别名]
# 或者: from Graph.Graph import addVertex [as 别名]
# if not all nodes were covered, the graph must have a cycle
# raise a GraphTopographicalException
if len(topologicalList) != len(nodes):
print "Error"
# Printing the topological order
while len(topologicalList):
node = topologicalList.pop(0)
print node.getId()
if __name__ == "__main__":
G = Graph()
G.addVertex('a')
G.addVertex('b')
G.addVertex('c')
G.addVertex('d')
G.addVertex('e')
G.addEdge('a', 'b', 1)
G.addEdge('a', 'c', 1)
G.addEdge('b', 'd', 1)
G.addEdge('b', 'e', 1)
G.addEdge('c', 'd', 1)
G.addEdge('c', 'e', 1)
G.addEdge('d', 'e', 1)
G.addEdge('e', 'a', 1)
topologicalSort(G)
开发者ID:dxmahata,项目名称:elements-of-programming-interviews-python-solutions,代码行数:31,代码来源:TopologicalSort.py
示例12: rpathTo
# 需要导入模块: from Graph import Graph [as 别名]
# 或者: from Graph.Graph import addVertex [as 别名]
x.reverse()
return x
# Recursive version of find Path
# doesn't work on big graphs
# Recursion in python limited
# to 990
def rpathTo(self,vertex):
if self.__edgeTo[self.__graph.getIndex(vertex)] == None and not self.__visited[self.__graph.getIndex(vertex)]:
return [None]
elif self.__edgeTo[self.__graph.getIndex(vertex)] == None and self.__visited[self.__graph.getIndex(vertex)]:
return [vertex]
#if no path then we cannot append to None
x = self.rpathTo(self.__edgeTo[self.__graph.getIndex(vertex)])
x.append(vertex)
return x
g = Graph([1,2,3,4,5,7])
g.addEdge(1,2)
g.addEdge(3,2)
g.addEdge(4,3)
g.addEdge(7,5)
g.addEdge(4,5)
g.addEdge(4,7)
p = Path(g,1)
g.addVertex(6)
g.addEdge(6,7)
p.Update()
print p.hasPathTo(6)
print p.pathTo(6)
示例13: Graph
# 需要导入模块: from Graph import Graph [as 别名]
# 或者: from Graph.Graph import addVertex [as 别名]
return dfsTraversal
if __name__ == "__main__":
g = Graph()
#add random vertices to the graph
for i in range(6):
g.addVertex(i)
#print all the newly added vertices
for vertex in g:
print vertex
#add edges between the vertices
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)
开发者ID:dxmahata,项目名称:elements-of-programming-interviews-python-solutions,代码行数:33,代码来源:GraphTraversals.py
示例14: addVertex
# 需要导入模块: from Graph import Graph [as 别名]
# 或者: from Graph.Graph import addVertex [as 别名]
def addVertex(vertex):
Graph.addVertex(vertex)
queues.add(Heap())
示例15: Graph
# 需要导入模块: from Graph import Graph [as 别名]
# 或者: from Graph.Graph import addVertex [as 别名]
if node.getColor() == currNode.getColor():
return False
if node.getColor() == "white":
if currNode.getColor() == "red":
node.setColor("blue")
if currNode.getColor() == "blue":
node.setColor("red")
q.put(node)
return True
if __name__ == "__main__":
G1 = Graph()
G1.addVertex('a')
G1.addVertex('b')
G1.addVertex('c')
G1.addVertex('d')
G1.addVertex('e')
G1.addVertex('f')
G1.addEdge("a","b",1)
G1.addEdge("a","f",1)
G1.addEdge("f","e",1)
G1.addEdge("e","d",1)
G1.addEdge("d","c",1)
G1.addEdge("c","b",1)
print is_bipartite(G1,G1.getVertex('a'))
开发者ID:dxmahata,项目名称:elements-of-programming-interviews-python-solutions,代码行数:32,代码来源:DetectingBipartiteGraphUsingBFS.py