2.16_vertex&graph_顶点类和图类 (更新)

顶点类
class Vertex(object):
    """顶点类"""

    def __init__(self, key, distance=0, predecessor=0, color=None):
        """
        :param key: 顶点的 key
        :param distance: 从起始顶点到此顶点路径长度
        :param predecessor: 可反向追溯到起点
        :param color: 标识此顶点的颜色 (white:未发现, gray:已发现, black:已完成搜索)
        """

        self.id = key
        self.connected_to = {}
        self.distance = distance
        self.predecessor = predecessor
        self.color = color

    def add_neighbor(self, nbr, weight=0):
        """
        :param nbr: 目标顶点对象的 key
        :param weight: 边的权重
        :return:
        """

        self.connected_to[nbr] = weight

    def __str__(self):
        return str(self.id) + ' connected to: ' + str([x.id for x in self.connected_to])

    def get_connections(self):
        
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值