本文整理汇总了Python中Graph.vertices方法的典型用法代码示例。如果您正苦于以下问题:Python Graph.vertices方法的具体用法?Python Graph.vertices怎么用?Python Graph.vertices使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Graph
的用法示例。
在下文中一共展示了Graph.vertices方法的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: readJSON
# 需要导入模块: import Graph [as 别名]
# 或者: from Graph import vertices [as 别名]
def readJSON(archivo):
graph = Graph()
with open(os.getcwd()+"/ejemplos/"+archivo,encoding='utf-8') as data_file:
data = json.loads(data_file.read())
if data['direct']==1:
graph.set_dirigida(True)
for vertice in data['vertices']:
graph.add_vertice(Vertex(vertice,0,[]))
for arista in data['edges']:
graph.add_arista(Edges(arista[0],arista[1],arista[2]))
for vertice in graph.vertices():
if vertice.etiqueta == arista[0]:
vertice.add_vecino(arista[1])
for vertex in graph.vertices():
if vertex.etiqueta == arista[1]:
vertex.add_vecino(arista[0])
return graph
示例2: readCSV
# 需要导入模块: import Graph [as 别名]
# 或者: from Graph import vertices [as 别名]
def readCSV(archivo):
graph = Graph()
reader = csv.reader(open(os.getcwd()+"/ejemplos/"+archivo, 'r'))
tipo= next(reader, None)
if tipo ==['direct=1']:
graph.set_dirigida(True)
for row in enumerate(reader):
if row[1][0] not in graph.get_etiquetas():
graph.add_vertice(Vertex(row[1][0], 1, [row[1][1][2::3]]))
else:
for vertice in graph.vertices():
if vertice.etiqueta == row[1][0]:
vertice.add_vecino(row[1][1][2::3])
if row[1][1][2::3] not in graph.get_etiquetas():
graph.add_vertice(Vertex(row[1][1][2::3], 1, [row[1][0]]))
else:
for vertice in graph.vertices():
if vertice.etiqueta == row[1][1][2::3]:
vertice.add_vecino(row[1][0])
graph.add_arista(Edges(row[1][0],row[1][1][2::3],row[1][2][1::]))
return graph
示例3: readXML
# 需要导入模块: import Graph [as 别名]
# 或者: from Graph import vertices [as 别名]
def readXML(archivo):
graph=Graph()
doc = minidom.parse(os.getcwd()+"/ejemplos/"+archivo)
vertexs = doc.getElementsByTagName("vertex")
edges = doc.getElementsByTagName("edge")
grafo = doc.getElementsByTagName("graph")
lis=[]
for g in grafo:
lis.append(g.getAttribute("direct"))
if lis[0] == '1':
graph.set_dirigida(True)
for vertex in vertexs:
graph.add_vertice(Vertex(vertex.getAttribute("label"),0,[]))
for edge in edges:
graph.add_arista(Edges(edge.getAttribute("source"),edge.getAttribute("target"),edge.getAttribute("weight")))
for vertice in graph.vertices():
if vertice.etiqueta == edge.getAttribute("source"):
vertice.add_vecino(edge.getAttribute("target"))
for vertex in graph.vertices():
if vertex.etiqueta == edge.getAttribute("target"):
vertex.add_vecino(edge.getAttribute("source"))
return graph
示例4: Vertex
# 需要导入模块: import Graph [as 别名]
# 或者: from Graph import vertices [as 别名]
"""
from Graph import *
from Dist import Dist
# make the initial graph
v1 = Vertex()
v2 = Vertex()
e = Edge(v1, v2)
g = Graph([v1, v2], [e])
# add new vertices and edges
for i in range(2000):
v1 = Vertex()
v2 = g.random_vertex()
e = Edge(v1, v2)
g.add_vertex(v1)
g.add_edge(e)
# plot the histogram of degrees
d = Dist()
vs = g.vertices()
for v in vs:
d.count(v.degree)
d.plot_ccdf(loglog)
show()
示例5: Edge
# 需要导入模块: import Graph [as 别名]
# 或者: from Graph import vertices [as 别名]
e5 = Edge(v,q)
#Test some methods
#get_edge(e)
g.get_edge(v,w)
print "got e1"
g.get_edge(v,q)
print "\n"
print g
print "\n"
#remove_edge(e)
# g1.remove_edge(e1)
# g1.remove_edge(e2)
# g1.remove_edge(e3)
# print "The graph with removed edge\n"
# print g1
print "Print all the vertices in the graph"
print g.vertices()
print "Print all the edges in the graph"
print g.edges()
示例6: ReadMap
# 需要导入模块: import Graph [as 别名]
# 或者: from Graph import vertices [as 别名]
class ReadMap(object):
"""This class reads a database file with .map extension
Attributes:
header: a list containing the header section of the file
filename: The name of the database
graph: a graph object generated after reading the file, initially empty
names: a list that only consists of the names of the buildings in the map
"""
def __init__(self):
self.header = []
self.filename = ""
self.graph = Graph()
self.names = []
def read_file(self, filename):
"""Reads an ASCII file with .map extension to crate the map of campus
Reads an An ASCII file where each line will be an edge.
The first column will contain a vertex, second column a vertex, and third column
will contain distance of the edge. All the column separated by spaces.
Update self.graph and self.names by extracting the edges from the file.
"""
f = open(filename, "r")
for i in range(5):
self.header.append(f.readline()) # reads and stores the header section of the file
line = f.readline()
while line != "EOF":
sp_line = line.split() # split the first line by spaces
v1 = Vertex(sp_line[0]) # create the first vertex obj from the first column
v2 = Vertex(sp_line[1]) # create second vertex obj from secomd column
newEdge = Edge(v1, v2) # create and edge between those two edges
if v1 not in self.graph.vertices(): # if v1 is not already added
self.graph.add_vertex(v1) # add that to the graph
# the following block is trick used to extrach the names of the buildings
# all the vertex labels other than name of buildings have been stored as numbers
# so every vertex label is first tried to convert into number
# if the label is not a number, it raises the exception, and we know
# that it is the name of the buiding
try:
a = int(v1.label)
except:
self.names.append(v1.label)
if v2 not in self.graph.vertices():
self.graph.add_vertex(v2)
try:
a = int(v2.label)
except:
self.names.append(v2.label)
self.graph.add_edge(newEdge) # add the edge to the graph
newEdge.set_distance(int(sp_line[2])) # set the distance of the edge
line = f.readline()
self.filename = filename # updates the filename