Python实现数据结构之图实现(5)

图的实现方法

源码

  • 邻接矩阵实现方法
    # -*- coding:utf-8 -*-
    
    class Graph():
        '''实现图'''
        def __init__(self):
            self.start = []
            self.wight = [None]
            self.count_vertex = 0
    
        def add_vertex(self,key):
            '''添加顶点'''
            self.start.append(key)
            self.count_vertex += 1
            if self.count_vertex > 1:
                temp_list = [None]*(2*self.count_vertex-1)
                for i in temp_list:
                    self.wight.append(i)
    
        def add_edge(self,key,nbr,wight=None):
            '''添加边'''
            if key not in self.start:
                self.add_vertex(key)
            if nbr not in self.start:
                self.add_vertex(nbr)
            for i in self.start:
                if key == i:
                    index_01 = self.start.index(i) + 1
                if nbr == i:
                    index_02 = self.start.index(i) + 1
            self.wight[index_01*index_02-1] = wight
    
        def get_vertex_num(self):
            '''返回所有顶点数量'''
            return self.count_vertex
        
        def get_vertex(self,i):
            '''查找顶点及相应边'''
            for n in self.start:
                new_val = [i]
                if i == n:
                    if self.start.index(n) != 0:
                        for index,val in enumerate(self.wight):
                            if (index+1) % (self.start.index(n)+1) == 0:
                                if val != None:
                                    temp = self.start[(index+1) // (self.start.index(n)+1)-1]
                                    new_val.append([temp,val]) 
                        return new_val
                    elif self.start.index(n) == 0:
                        new_list = self.wight[:self.count_vertex]
                        for i in new_list:
                            if i != None:
                                new_val.append([self.start[new_list.index(i)],i])
                        return new_val
    
    
    if __name__ == "__main__":
        graph = Graph()
        graph.add_vertex('a')
        graph.add_vertex('b')
        graph.add_edge('a','b',10)
        graph.add_edge('a','c',12)
        graph.add_edge('b','c',15)
        graph.add_edge('b','d',15)
        print(graph.get_vertex_num())
        print(graph.get_vertex('a'))
    
  • 邻接列表实现方法
    # -*- coding:utf-8 -*-
    
    class Vertex():
        '''顶点类,包含顶点信息及连接边'''
        def __init__(self,key):
            self.id = key
            self.connect = {}
    
        def add_neignbor(self,nbr,wight=0):
            # nbr是顶点对象所连接的另外节点,也就是顶点对象的key值
            # wight表示的权重,也就是两点之间的距离
            self.connect[nbr] = wight
    
        def get_connects(self):
            # 返回顶点所连接的其他点
            return [[i.id, v] for i, v in self.connect.items()]
    
    
    class Graph():
        '''实现图'''
        def __init__(self):
            self.vertlist = {}
            self.count_vertex = 0
    
        def add_vertex(self,key):
            '''在列表中添加节点'''
            self.count_vertex += 1
            self.vertlist[key] = Vertex(key)
    
        def get_vertex(self,i):
            '''查找顶点'''
            return self.vertlist[i].get_connects() if i in self.vertlist else None
        
        def add_edge(self,key,nbr,wight=0):
            '''添加边'''
            if key not in self.vertlist:
                self.add_vertex(key)
            if nbr not in self.vertlist:
                self.add_vertex(nbr)
            self.vertlist[key].add_neignbor(self.vertlist[nbr],wight)
        
        def get_vertex_num(self):
            '''返回所有顶点数量'''
            return self.count_vertex
    
    
    if __name__ == "__main__":
        graph = Graph()
        graph.add_vertex('a')
        graph.add_vertex('b')
        graph.add_edge('a','b',10)
        graph.add_edge('b','a',10)
        graph.add_edge('a','c',12)
        graph.add_edge('b','c',15)
        print(graph.get_vertex_num())
        print(graph.get_vertex('a'))
    
  • 0
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值