Maktub_Wiki Maktub_Wiki
主站 (opens new window)
  • 服务搭建
  • 网络服务
  • 开源框架
  • 操作系统
  • iOS/MacOS
  • 算法导论(Python)
  • Leetcode
  • 线性代数
主站 (opens new window)
  • 服务搭建
  • 网络服务
  • 开源框架
  • 操作系统
  • iOS/MacOS
  • 算法导论(Python)
  • Leetcode
  • 线性代数
  • 第2章 算法基础

    • 2.1 插入排序
    • 2.2 算法分析
    • 2.3 设计算法
  • 第6章 堆排序

    • 6.2 维护堆的性质
    • 6.3 建堆
    • 6.4 堆排序算法
    • 6.5 优先队列
  • 第7章 快速排序

    • 7.1 快速排序的描述
  • 第8章 线性时间排序

    • 8.2 计数排序
    • 8.4 桶排序
  • 第12章 二叉树搜索

    • 12.1 什么是二叉树搜索
    • 12.3 插入和删除
  • 第13章 红黑树

    • 13.1 红黑树的性质
    • 13.2 旋转
  • 第15章 动态规划

    • 15.4 最长公共子序列
  • 第23章 最小生成树

    • 23.2 Kruskal算法和Prim算法
  • 第24章 单源最短路径

    • 24.1 Bellman-Ford算法
    • 24.3 Dijkstra算法
  • 第25章 所有结点对的最短路径问题

    • 25.2 Floyd-Warshall算法
    • 25.3 用于稀疏图的Johnson算法
      • 说明
        • 代码
  • 第31章 数论算法

    • 31.2 最大公约数
    • 31.6 元素的幂
  • 第32章 字符串匹配

    • 32.4 Knuth-Morris-Pratt算法
  • 算法导论
  • 第25章 所有结点对的最短路径问题
Maktub_小明
2024-09-04
目录

25.3 用于稀疏图的Johnson算法

# 说明

# 代码

import heapq

def bellman_ford(graph, start):
    distance = {vertex: float('inf') for vertex in graph}
    distance[start] = 0

    for _ in range(len(graph) - 1):
        for u in graph:
            for v, weight in graph[u].items():
                if distance[u] + weight < distance[v]:
                    distance[v] = distance[u] + weight

    return distance

def dijkstra(graph, start):
    distance = {vertex: float('inf') for vertex in graph}
    distance[start] = 0
    priority_queue = [(0, start)]

    while priority_queue:
        current_distance, current_vertex = heapq.heappop(priority_queue)

        if current_distance > distance[current_vertex]:
            continue

        for neighbor, weight in graph[current_vertex].items():
            distance_via_current = current_distance + weight
            if distance_via_current < distance[neighbor]:
                distance[neighbor] = distance_via_current
                heapq.heappush(priority_queue, (distance[neighbor], neighbor))

    return distance

def johnson(graph):
    # Step 1: Add a new source vertex
    new_source = 'new_source'
    graph[new_source] = {v: 0 for v in graph}

    # Step 2: Run Bellman-Ford from the new source
    potential = bellman_ford(graph, new_source)

    # Step 3: Reweight the edges
    reweighted_graph = {}
    for u in graph:
        reweighted_graph[u] = {}
        for v, weight in graph[u].items():
            reweighted_graph[u][v] = weight + potential[u] - potential[v]

    # Step 4: Run Dijkstra for each vertex
    all_pairs_shortest_paths = {}
    for vertex in graph:
        all_pairs_shortest_paths[vertex] = dijkstra(reweighted_graph, vertex)

    return all_pairs_shortest_paths

# 示例图
graph = {
    'A': {'B': 1, 'C': 4},
    'B': {'C': 2, 'D': 5},
    'C': {'D': 1},
    'D': {}
}

shortest_paths = johnson(graph)
print(shortest_paths)
上次更新: 2025/03/22, 03:52:10
25.2 Floyd-Warshall算法
31.2 最大公约数

← 25.2 Floyd-Warshall算法 31.2 最大公约数→

Theme by Vdoing | Copyright © 2021-2025 Maktub_小明 | MIT License
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式