bellman-ford算法_python+实例
the python code:
the code base the vertex(nodes) set and edge set seperately
import math import logging as l l.basicConfig(level=l.INFO) class Edge(): def __init__(self, start, end, weight): self.start = start self.end = end self.weight = weight class Node(): def __init__(self, sign): # self.number = number self.sign = sign # for initial nodes(vertex) of the graph self.distance = math.inf # set the node's precursor: self.precursor = None def initalize_source_node(self): self.distance = 0 return self class G(): s_node=None def __init__(self, edges, nodes): self.edges = edges self.nodes = nodes # def generate_nodes(self): # # get the nodes number(you can custom the number regularity,there use the default simple number system) # self.nodes = [Node(chr(sign)) for sign in range(ord('A'), ord('E')+1)] def log_print_nodes(self): for node in self.nodes: l.debug(f'{node.sign,node.distance}') def weight(self, u, v): for edge in self.edges: if edge.start == u and edge.end == v: return edge.weight return math.inf def relax(self, edge): u=edge.start v=edge.end l.debug(f'self.weight(u, v):{self.weight(u, v)}') new_distance= u.distance+self.weight(u, v) #debug l.debug(f'{edge.start.sign,edge.end.sign}') l.debug(f'new_distance:{new_distance}') if v.distance >new_distance: v.distance=new_distance v.precursor=u # def initialize_single_source(G, source_node): # # for node in G.nodes: # # node.distance=0 # # node.precursor=None # source_node.distance = 0 def bellman_ford(self, s): G.s_node=s.initalize_source_node() l.info(f'G.s_node:{G.s_node.sign}') for i in range(len(self.nodes)-1): for edge in self.edges: self.relax(edge) l.debug(f'{edge.end.distance}') #debug self.log_print_nodes() return self def print_ford_result(self): # self.bellman_ford(s) if not self.is_exist_shortest(): print("there is a nagetive circle.") else: for node in self.nodes: # print()''' ''' print(f'to node:{node.sign},the distance is:{node.distance}') def is_exist_shortest(self): for edge in self.edges: if edge.end.distance>edge.start.distance+edge.weight: return False return True def print_precursor(self,node): if node.sign==G.s_node.sign: print(G.s_node.sign,end=" ") # return else: if node.precursor==None: print(G.s_node.sign,"->",node.sign,"(the node is not accessible)",end=" ") else: self.print_precursor(node.precursor) print(node.sign,end=" ") def print_path(self): for node in self.nodes: # print(node.sign) self.print_precursor(node) print() def generate_nodes(): # get the nodes number(you can custom the number regularity,there use the default simple number system) nodes = [Node(chr(sign)) for sign in range(ord('A'), ord('E')+1)] return nodes #debug:print nodes: def print_nodes(): for node in nodes: print(node.sign,node.distance) # print_nodes() def get_node_instance(sign): for node in nodes: if node.sign==sign: return node #throw exception return None # get the edges parameters to instantiate the edge nodes ,put the edges to the list edges; def generate_edges(): while(True): line = input("input node:") if line == "0": break edge_param = line.split(",") start, end, weight = edge_param[0], edge_param[1], int(edge_param[2]) start_node=get_node_instance(start) end_node=get_node_instance(end) # print(end_node.sign) edges.append(Edge(start_node,end_node , weight)) return edges '''debug the edges is right: ''' def print_edges(): for edge in edges: # print(edge.start.sign,edge.end.sign,edge.weight) l.info((edge.start.sign,edge.end.sign,edge.weight)) # print_edges() nodes=[] nodes=generate_nodes() edges = [] edges=generate_edges() G=G(edges,nodes) # G.print_nodes() source_node=input("input the source node you want:(from 'A'~'E')\n") G.bellman_ford(get_node_instance(source_node)) G.print_ford_result() G.print_path() ''' test data: A,B,-1 A,C,4 B,C,3 D,C,5 D,B,1 B,D,2 B,E,2 E,D,-3 0 '''
the result:
source A:
source D:
overview
understand the process:
a long example:
Correctness:
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 分享4款.NET开源、免费、实用的商城系统
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
2022-02-25 EM@初等数学公式@高中数学公式大全