当前位置: 首页>>代码示例>>Python>>正文


Python Graph.add_vertex方法代码示例

本文整理汇总了Python中Graph.add_vertex方法的典型用法代码示例。如果您正苦于以下问题:Python Graph.add_vertex方法的具体用法?Python Graph.add_vertex怎么用?Python Graph.add_vertex使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在Graph的用法示例。


在下文中一共展示了Graph.add_vertex方法的7个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: json

# 需要导入模块: import Graph [as 别名]
# 或者: from Graph import add_vertex [as 别名]
 def json(self, f):
     data = json.load(open(f))
     directed = data["direct"]
     g = Graph()
     if directed == 1:
         g = DGraph()
     for v in data["vertices"]:
         g.add_vertex(v)
     for e in data["edges"]:
         g.connect(e[2], e[0], e[1]) 
     return g
开发者ID:Pernath,项目名称:lenguajes20161_ReyVenado,代码行数:13,代码来源:GraphReader.py

示例2: csv

# 需要导入模块: import Graph [as 别名]
# 或者: from Graph import add_vertex [as 别名]
 def csv(self, f):
     reader = csv.reader(open(f), delimiter=",")
     directed = next(reader)[0]
     directed = int(directed[7:])        
     g = Graph()
     if directed == 1:
         g = DGraph()
     for u,v,w in reader:
         g.add_vertex(u)
         g.add_vertex(v[2:3])
         g.connect(w,u,v[2:3])
     return g
开发者ID:Pernath,项目名称:lenguajes20161_ReyVenado,代码行数:14,代码来源:GraphReader.py

示例3: xml

# 需要导入模块: import Graph [as 别名]
# 或者: from Graph import add_vertex [as 别名]
 def xml(self, f):
     tree = ET.parse(f)
     root = tree.getroot()
     directed = int(root.attrib.get('direct'))
     g = Graph()
     if directed == 1:
         g = DGraph()
     for vertex in root.findall('vertex'):
         g.add_vertex(vertex.get('label'))        
     for edge in root.findall('edge'):
         g.connect(edge.get('weight'), edge.get('source'), edge.get('target'))
     return g
开发者ID:Pernath,项目名称:lenguajes20161_ReyVenado,代码行数:14,代码来源:GraphReader.py

示例4: Graph

# 需要导入模块: import Graph [as 别名]
# 或者: from Graph import add_vertex [as 别名]
            v = G.get_vertex(to)
            if v.get_distance() > (u.get_distance() + u.get_weight(v)):
                v.set_previous(u)
                v.set_distance(u.get_distance() + u.get_weight(v))

    #loop over all nodes and print their distances
    for i in G.get_vertices():
        u = G.get_vertex(i)
        print "Node", u.get_vertex_ID(), "with distance", u.get_distance()

# Test Code
#create an empty graph
G = Graph()
    
#add vertices to the graph
for i in ["a", "b", "c", "d", "e"]:
    G.add_vertex(i)

#add edges to the graph - need one for each edge to make them undirected
#since the edges are unweighted, make all cost 1
G.add_edge("a", "c", 6)
G.add_edge("a", "d", 3)
G.add_edge("b", "a", 3)
G.add_edge("c", "d", 2)
G.add_edge("d", "c", 1)
G.add_edge("d", "b", 1)
G.add_edge("e", "b", 4)
G.add_edge("e", "d", 2)

bellman_ford(G, "a")
开发者ID:ArjunReddyD,项目名称:algorithm-design-techniques,代码行数:32,代码来源:bellman_ford.py

示例5: Vertex

# 需要导入模块: import Graph [as 别名]
# 或者: from Graph import add_vertex [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()

开发者ID:ANB2,项目名称:ThinkComplexity,代码行数:31,代码来源:Graph5.py

示例6: print

# 需要导入模块: import Graph [as 别名]
# 或者: from Graph import add_vertex [as 别名]
        for w in v.get_connections():
            if w.get_distance() is None:
                w.set_previous(v)
                w.set_distance(1 + v.get_distance())
                q.enqueue(w)

    #loop over all nodes and print their distances
    for v in g:
        print("Node", v.get_vertex_ID(), "with distance", v.get_distance())

#create an empty graph
g = Graph()
    
#add vertices to the graph
for i in ["a", "b", "c", "d", "e"]:
    g.add_vertex(i)

#add edges to the graph - need one for each edge to make them undirected
#since the edges are unweighted, make all cost 1
g.add_edge("a", "b", 1)
g.add_edge("a", "d", 1)
g.add_edge("b", "d", 1)
g.add_edge("b", "e", 1)
g.add_edge("c", "a", 1)
g.add_edge("c", "f", 1)
g.add_edge("d", "f", 1)
g.add_edge("d", "g", 1)
g.add_edge("e", "g", 1)
g.add_edge("g", "f", 1)
unweighted_shortest_path(g, "a") 
开发者ID:ArjunReddyD,项目名称:algorithm-design-techniques,代码行数:32,代码来源:unweighted_shortest_path.py

示例7: ReadMap

# 需要导入模块: import Graph [as 别名]
# 或者: from Graph import add_vertex [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
开发者ID:agarwali,项目名称:CampusTourGuide,代码行数:64,代码来源:read_graph.py


注:本文中的Graph.add_vertex方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。