news 2026/7/7 19:00:30

图算法:从基础到应用

作者头像

张小明

前端开发工程师

1.2k 24
文章封面图
图算法:从基础到应用

一、引言
图是一种强大的数据结构,用于表示对象之间的关系。从社交网络到路线规划,从网页连接到生物信息学,图算法在计算机科学的各个领域都有着广泛的应用。本文将介绍图的基本概念、常见算法及其实现。

二、图的表示方法

  1. 邻接矩阵
classGraphMatrix:def__init__(self,vertices):self.V=vertices self.graph=[[0]*verticesfor_inrange(vertices)]defadd_edge(self,u,v,weight=1,directed=False):self.graph[u][v]=weightifnotdirected:self.graph[v][u]=weightdefprint_graph(self):foriinrange(self.V):print(f"顶点{i}:{self.graph[i]}")
  1. 邻接表(更节省空间)
fromcollectionsimportdefaultdictclassGraphAdjList:def__init__(self,directed=False):self.graph=defaultdict(list)self.directed=directed self.vertices=set()defadd_edge(self,u,v,weight=1):self.graph[u].append((v,weight))self.vertices.add(u)self.vertices.add(v)ifnotself.directed:self.graph[v].append((u,weight))defprint_graph(self):forvertexinself.graph:print(f"{vertex}:{self.graph[vertex]}")

三、基础图遍历算法

  1. 深度优先搜索 (DFS)
defdfs_recursive(graph,start,visited=None):"""递归实现DFS"""ifvisitedisNone:visited=set()visited.add(start)print(start,end=" ")forneighbor,_ingraph.get(start,[]):ifneighbornotinvisited:dfs_recursive(graph,neighbor,visited)returnvisiteddefdfs_iterative(graph,start):"""迭代实现DFS"""visited=set()stack=[start]whilestack:vertex=stack.pop()ifvertexnotinvisited:print(vertex,end=" ")visited.add(vertex)# 将邻居逆序加入栈中,以保持与递归相同的遍历顺序forneighbor,_inreversed(graph.get(vertex,[])):ifneighbornotinvisited:stack.append(neighbor)returnvisited
  1. 广度优先搜索 (BFS)
fromcollectionsimportdequedefbfs(graph,start):"""BFS实现"""visited=set([start])queue=deque([start])whilequeue:vertex=queue.popleft()print(vertex,end=" ")forneighbor,_ingraph.get(vertex,[]):ifneighbornotinvisited:visited.add(neighbor)queue.append(neighbor)returnvisited

四、最短路径算法

  1. Dijkstra算法(单源最短路径,权重非负)
importheapqdefdijkstra(graph,start):"""Dijkstra最短路径算法"""# 初始化距离字典distances={vertex:float('inf')forvertexingraph}distances[start]=0previous={vertex:Noneforvertexingraph}# 优先队列priority_queue=[(0,start)]whilepriority_queue:current_distance,current_vertex=heapq.heappop(priority_queue)# 如果当前距离大于已知最短距离,跳过ifcurrent_distance>distances[current_vertex]:continue# 更新邻居节点的距离forneighbor,weightingraph.get(current_vertex,[]):distance=current_distance+weightifdistance<distances[neighbor]:distances[neighbor]=distance previous[neighbor]=current_vertex heapq.heappush(priority_queue,(distance,neighbor))returndistances,previousdefget_path(previous,target):"""从previous字典重构路径"""path=[]current=targetwhilecurrentisnotNone:path.append(current)current=previous[current]returnpath[::-1]
  1. Floyd-Warshall算法(所有节点对最短路径)
deffloyd_warshall(graph_matrix,vertices):"""Floyd-Warshall算法"""# 初始化距离矩阵dist=[[float('inf')]*verticesfor_inrange(vertices)]foriinrange(vertices):dist[i][i]=0forj,weightinenumerate(graph_matrix[i]):ifweight!=0:dist[i][j]=weight# 动态规划forkinrange(vertices):foriinrange(vertices):forjinrange(vertices):ifdist[i][j]>dist[i][k]+dist[k][j]:dist[i][j]=dist[i][k]+dist[k][j]returndist

五、最小生成树算法

  1. Prim算法
defprim_mst(graph,start):"""Prim最小生成树算法"""mst=[]visited=set([start])edges=[]# 初始化起始点的边forneighbor,weightingraph.get(start,[]):heapq.heappush(edges,(weight,start,neighbor))whileedgesandlen(visited)<len(graph):weight,u,v=heapq.heappop(edges)ifvnotinvisited:visited.add(v)mst.append((u,v,weight))# 添加新顶点的边forneighbor,wingraph.get(v,[]):ifneighbornotinvisited:heapq.heappush(edges,(w,v,neighbor))returnmst
  1. Kruskal算法
classDisjointSet:"""并查集实现,用于Kruskal算法"""def__init__(self,vertices):self.parent={v:vforvinvertices}self.rank={v:0forvinvertices}deffind(self,v):ifself.parent[v]!=v:self.parent[v]=self.find(self.parent[v])returnself.parent[v]defunion(self,v1,v2):root1=self.find(v1)root2=self.find(v2)ifroot1!=root2:ifself.rank[root1]>self.rank[root2]:self.parent[root2]=root1elifself.rank[root1]<self.rank[root2]:self.parent[root1]=root2else:self.parent[root2]=root1 self.rank[root1]+=1returnTruereturnFalsedefkruskal_mst(graph):"""Kruskal最小生成树算法"""edges=[]vertices=set()# 收集所有边foruingraph:vertices.add(u)forv,weightingraph.get(u,[]):vertices.add(v)edges.append((weight,u,v))# 按权重排序edges.sort()# 初始化并查集ds=DisjointSet(vertices)mst=[]forweight,u,vinedges:ifds.union(u,v):mst.append((u,v,weight))iflen(mst)==len(vertices)-1:breakreturnmst

六、拓扑排序(用于有向无环图)

deftopological_sort_kahn(graph):"""Kahn算法实现拓扑排序"""# 计算入度in_degree={vertex:0forvertexingraph}foruingraph:forv,_ingraph[u]:in_degree[v]=in_degree.get(v,0)+1# 初始化队列queue=deque([vforvingraphifin_degree.get(v,0)==0])topo_order=[]whilequeue:u=queue.popleft()topo_order.append(u)forv,_ingraph.get(u,[]):in_degree[v]-=1ifin_degree[v]==0:queue.append(v)iflen(topo_order)==len(graph):returntopo_orderelse:# 图中存在环returnNonedeftopological_sort_dfs(graph):"""DFS实现拓扑排序"""visited=set()temp=set()# 临时标记,用于检测环stack=[]defdfs(vertex):ifvertexintemp:# 检测到环raiseValueError("图中有环,无法进行拓扑排序")ifvertexinvisited:returntemp.add(vertex)forneighbor,_ingraph.get(vertex,[]):dfs(neighbor)temp.remove(vertex)visited.add(vertex)stack.append(vertex)forvertexingraph:ifvertexnotinvisited:dfs(vertex)returnstack[::-1]

七、应用示例:社交网络分析

classSocialNetworkAnalyzer:def__init__(self):self.graph=GraphAdjList(directed=False)defadd_friendship(self,person1,person2,strength=1):self.graph.add_edge(person1,person2,strength)deffind_degrees_of_separation(self,start,target):"""使用BFS查找两个人之间的分离度数"""ifstart==target:return0visited=set([start])queue=deque([(start,0)])# (person, distance)whilequeue:current_person,distance=queue.popleft()forneighbor,_inself.graph.graph.get(current_person,[]):ifneighbor==target:returndistance+1ifneighbornotinvisited:visited.add(neighbor)queue.append((neighbor,distance+1))return-1# 没有路径deffind_influential_people(self,top_n=5):"""使用度中心性查找最有影响力的人"""centrality={}forpersoninself.graph.vertices:centrality[person]=len(self.graph.graph.get(person,[]))# 按中心性排序sorted_centrality=sorted(centrality.items(),key=lambdax:x[1],reverse=True)returnsorted_centrality[:top_n]deffind_communities(self):"""使用DFS查找连通分量(社区)"""visited=set()communities=[]forpersoninself.graph.vertices:ifpersonnotinvisited:community=dfs_recursive(self.graph.graph,person,set())visited.update(community)communities.append(community)returncommunities# 使用示例if__name__=="__main__":# 创建图graph=GraphAdjList()graph.add_edge(0,1,4)graph.add_edge(0,7,8)graph.add_edge(1,2,8)graph.add_edge(1,7,11)graph.add_edge(2,3,7)graph.add_edge(2,8,2)graph.add_edge(2,5,4)graph.add_edge(3,4,9)graph.add_edge(3,5,14)graph.add_edge(4,5,10)graph.add_edge(5,6,2)graph.add_edge(6,7,1)graph.add_edge(6,8,6)graph.add_edge(7,8,7)print("图结构:")graph.print_graph()print("\nDFS遍历 (从节点0开始):")dfs_recursive(graph.graph,0)print("\n\nBFS遍历 (从节点0开始):")bfs(graph.graph,0)print("\n\nDijkstra最短路径 (从节点0开始):")distances,previous=dijkstra(graph.graph,0)forvertexindistances:print(f"到节点{vertex}的最短距离:{distances[vertex]}")print(f"路径:{get_path(previous,vertex)}")print("\nPrim最小生成树:")mst_prim=prim_mst(graph.graph,0)print(f"最小生成树边:{mst_prim}")total_weight=sum(weightfor_,_,weightinmst_prim)print(f"总权重:{total_weight}")# 社交网络示例print("\n--- 社交网络分析示例 ---")social_net=SocialNetworkAnalyzer()social_net.add_friendship("Alice","Bob")social_net.add_friendship("Alice","Charlie")social_net.add_friendship("Bob","David")social_net.add_friendship("Charlie","David")social_net.add_friendship("David","Eve")social_net.add_friendship("Frank","Grace")print(f"Alice和Eve之间的分离度数:{social_net.find_degrees_of_separation('Alice','Eve')}")print(f"最有影响力的人:{social_net.find_influential_people(3)}")print(f"发现的社区:{social_net.find_communities()}")

八、总结与扩展
本文介绍了图的基本表示方法和几种核心算法。图算法的应用远不止于此,还包括:

网络流算法:如Ford-Fulkerson算法解决最大流问题

图匹配算法:如匈牙利算法解决二分图匹配

强连通分量:Kosaraju或Tarjan算法

图着色问题:用于调度和寄存器分配

旅行商问题:启发式算法如遗传算法、模拟退火

图算法的选择取决于具体问题:

对于无权图最短路径,使用BFS

对于有权图(非负权)最短路径,使用Dijkstra

对于负权图最短路径,使用Bellman-Ford

对于所有节点对最短路径,使用Floyd-Warshall

对于最小生成树,Prim适合稠密图,Kruskal适合稀疏图

理解这些基础算法是解决更复杂图问题的基础,也是许多实际应用的核心。

版权声明: 本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:809451989@qq.com进行投诉反馈,一经查实,立即删除!
网站建设 2026/7/7 12:17:30

百度搜索不到?这里提供Qwen3-32B官方镜像直达链接

Qwen3-32B 官方镜像直达与技术深度解析 在当前大模型技术飞速演进的背景下&#xff0c;越来越多的企业和开发者开始将目光从闭源API转向可自主掌控的开源大语言模型。尽管GPT系列等商业模型表现强劲&#xff0c;但其高昂的调用成本、数据隐私风险以及部署灵活性不足等问题&…

作者头像 李华
网站建设 2026/7/7 12:40:53

关于字节Trae和阿里的通义灵码/Qoder哪个更好用

关于字节Trae和阿里的通义灵码/Qoder哪个更好用&#xff0c;这主要取决于你的核心需求。如果追求从零开始快速构建完整项目&#xff0c;适合选择字节的Trae&#xff1b;如果更看重融入现有IDE并深度理解、优化现有项目代码&#xff0c;那么阿里的通义灵码通常是更好的选择。 下…

作者头像 李华
网站建设 2026/7/7 8:22:53

AutoGPT能否自动注册账号?验证码识别限制说明

AutoGPT能否自动注册账号&#xff1f;验证码识别限制说明 在智能体技术迅猛发展的今天&#xff0c;我们越来越频繁地设想这样一个场景&#xff1a;只需对AI说一句“帮我注册一个GitHub账号”&#xff0c;它就能自主打开浏览器、填写表单、处理验证&#xff0c;最后把登录凭证交…

作者头像 李华
网站建设 2026/7/7 7:29:52

瑞派宠物医生:廿载专注一心为喵,精研猫科疾病管理

在深圳瑞派派特堡大成宠物医院&#xff0c;有一位被众多宠主信赖的“老朋友”。他从医二十余年&#xff0c;专注肾脏、泌尿内科及猫科领域&#xff0c;用专业与温情守护着一个又一个毛孩子的健康。他&#xff0c;就是特聘专科院长——邹盛银。初心萌动&#xff1a;一面锦旗&…

作者头像 李华
网站建设 2026/7/7 8:12:34

xdg-open command in Linux with Examples

xdg-open command in the Linux system is used to open a file or URL in the user’s preferred application. The URL will be opened in the user’s preferred web browser if a URL is provided. The file will be opened in the preferred application for files of tha…

作者头像 李华
网站建设 2026/7/7 18:31:13

当课程论文不再是“期末突击的煎熬”:一位大学生如何用AI科研助手把每周作业变成学术能力的“微训练场”?

在高校课程体系中&#xff0c;课程论文常常被学生视为“负担”——不是因为它难&#xff0c;而是因为它“模糊”&#xff1a;题目开放却无指导、字数要求明确但标准不明、查重要求严格却不知从何规范。更尴尬的是&#xff0c;它既不像考试那样有标准答案&#xff0c;又不像实验…

作者头像 李华