首页 > 文章列表 > 在一个有向加权图中,求解恰好包含k条边的最短路径

在一个有向加权图中,求解恰好包含k条边的最短路径

最短路径 关键词: 有向图 加权图
247 2023-09-07

在协调加权图表中,找到具有精确 k 个边的最简短路径的问题包括确定在精确导航 k 个边时权重最小的路径。这将通过采用动态编程策略来实现,例如采用 3D 框架来存储所有可想到的方式中的最小权重。计算在顶点和边上重复,在每一步都调整最小权重。通过考虑具有精确 k 个边的所有可能的方式,计算可以区分图表中具有 k 个边的最有限的方式。

使用的方法

  • 朴素递归方法

  • 带边缘约束的 Dijkstra 算法

朴素递归方法

朴素递归方法可能是解决问题的一种重要而明确的策略,包括将复杂的问题分解为更小的子问题并递归地解决它们。在这种方法中,作品多次调用自身来探究子问题,直到达到基本情况。尽管如此,由于重复计算和覆盖子问题,对于更大的问题发生可能会造成浪费。它需要诸如记忆或能量编程之类的优化方法。容易受骗的递归方法很容易获得和实现,但可能会承受指数时间复杂度。它通常用于解决小规模问题或作为更优化计算的起点。

算法

  • 表征工作最短路径(图、u、v、k),该路径以图表、源顶点 u、目标顶点 v 和边数 k 作为输入。

  • 检查基本情况:

  • a。如果 k 和 u 与 v 收支平衡,则返回(因为在这种情况下不允许有边)。

  • 第二个。如果 k 为 1 并且图表中 u 和 v 之间存在边,则返回其权重。

  • c.如果 k 小于或等于 0,则返回无边界(因为不允许负边缘或零边缘)。

  • 初始化一个无限的变量res来存储最短路径距离。

  • 图表应按如下方式迭代所有顶点:

  • a。如果 u 和 i 没有上升到 u 或 v,则从 u 到 i 存在一条边:

  • 递归调用shortestPath,其中i为现代源顶点,v为目标顶点,k−1为剩余边数。

  • 如果返回的结果不是无限的,则将res升级为res和当前边的权重与递归结果的最小值。

  • 返回 res 的值,作为精确分离 k 个边的最有限方式。

示例

#include <iostream>
#include <climits>

#define V 4
#define INF INT_MAX

int shortestPathWithKEdges(int graph[][V], int source, int destination, int k) {
    // Base cases
    if (k == 0 && source == destination)
        return 0;
    if (k == 1 && graph[source][destination] != INF)
        return graph[source][destination];
    if (k <= 0)
        return INF;

    // Initialize result
    int shortestPathDistance = INF;

    // Explore all adjacent vertices of the source vertex
    for (int i = 0; i < V; i++) {
        if (graph[source][i] != INF && source != i && destination != i) {
            int recursiveDistance = shortestPathWithKEdges(graph, i, destination, k - 1);
            if (recursiveDistance != INF)
                shortestPathDistance = std::min(shortestPathDistance, graph[source][i] + recursiveDistance);
        }
    }

    return shortestPathDistance;
}

int main() {
    int graph[V][V] = {
        {0, 10, 3, 2},
        {INF, 0, INF, 7},
        {INF, INF, 0, 6},
        {INF, INF, INF, 0}
    };
    int source = 0, destination = 3, k = 2;
    std::cout << "Weight of the shortest path is " << shortestPathWithKEdges(graph, source, destination, k) << std::endl;
    return 0;
}

输出

Weight of the shortest path is 9

带边缘约束的 Dijkstra 算法

带边限制的 Dijkstra 算法是一种图表遍历计算,用于识别图表上源顶点与所有其他顶点之间的最短路径。它考虑了图表边缘的限制或约束,例如最极端或最不极端的边缘权重。该计算保留所需的顶点线并迭代地选择移除最少的顶点。此时,如果找到更短的路径,它会通过增加相邻顶点的间距来放松它们。此准备工作将持续进行,直到访问完所有顶点为止。具有边缘命令的 Dijkstra 算法保证所选择的方式满足所需的边缘限制,同时找到最有限的方式

算法

  • 使用以下参数制作 Dijkstra 的作品

  • Graph:带有顶点和边的输入图表

    来源:最有限路径的起始顶点

    约束:边缘的限制或障碍

    初始化一组已消失的顶点和一条用于存储顶点及其距离的需求线。

  • 创建一个删除簇,并将除源顶点之外的所有顶点的删除设置为可终止性,源顶点设置为 0。

  • 将源顶点以其距离排列到所需的行中。

  • 虽然需求管线不可清除,但请执行以下操作:

  • 将消除次数最少的顶点从所需队列中出列。

  • 如果现在不再访问该顶点,

  • 将其标记为已访问。

  • 对于现代顶点的每个相邻顶点:

  • 应用边缘障碍来确定是否可以考虑边缘。

  • 考虑边缘权重和约束,计算从供给顶点到相邻顶点的未使用距离。

  • 如果当前的分隔符比现代的分隔符短,请改进分隔符数组。

  • 将相邻顶点以其未使用的距离排队到所需的行中。

  • 到达所有顶点后,单独的簇将包含从供应顶点到满足边缘约束的每个顶点的最大短距离。

  • 返回单独的簇作为结果。

示例

#include <iostream>
#include <vector>
#include <limits>

struct Edge {
    int destination;
    int weight;
};

void dijkstra(const std::vector<std::vector<Edge>>& graph, int source, std::vector<int>& distance) {
    int numVertices = graph.size();
    std::vector<bool> visited(numVertices, false);
    distance.resize(numVertices, std::numeric_limits<int>::max());
    distance[source] = 0;

    for (int i = 0; i < numVertices - 1; ++i) {
        int minDistance = std::numeric_limits<int>::max();
        int minVertex = -1;

        for (int v = 0; v < numVertices; ++v) {
            if (!visited[v] && distance[v] < minDistance) {
                minDistance = distance[v];
                minVertex = v;
            }
        }

        if (minVertex == -1)
            break;

        visited[minVertex] = true;

        for (const auto& edge : graph[minVertex]) {
            int destination = edge.destination;
            int weight = edge.weight;

            if (!visited[destination] && distance[minVertex] != std::numeric_limits<int>::max() &&
                distance[minVertex] + weight < distance[destination]) {
                distance[destination] = distance[minVertex] + weight;
            }
        }
    }
}

int main() {
    int numVertices = 4;
    int source = 0;
    std::vector<std::vector<Edge>> graph(numVertices);

    // Add edges to the graph (destination, weight)
    graph[0] = {{1, 10}, {2, 3}};
    graph[1] = {{2, 1}, {3, 7}};
    graph[2] = {{3, 6}};

    std::vector<int> distance;
    dijkstra(graph, source, distance);

    // Print the shortest distances from the source vertex
    std::cout << "Shortest distances from vertex " << source << ":n";
    for (int i = 0; i < numVertices; ++i) {
        std::cout << "Vertex " << i << ": " << distance[i] << 'n';
    }

    return 0;
}

输出

Shortest distances from vertex 0:
Vertex 0: 0
Vertex 1: 10
Vertex 2: 3
Vertex 3: 9

结论

本文概述了两个重要的计算,以帮助理解协调和加权图表中的大多数问题。它阐明了易受骗的递归方法和带有边缘限制的 Dijkstra 计算。轻信递归方法包括递归地研究具有精确 k 个边的所有可能的方式,以发现最有限的方式。 Dijkstra 的边命令式计算采用了所需的线和面积规则,成功地找出了图表中从供给顶点到所有不同顶点的最大受限方式。本文包含了计算的具体说明,并给出了测试代码来说明其用法.