0

0

具有权重大于等于1的边的最小乘积路径

王林

王林

发布时间:2023-08-30 11:37:06

|

1655人浏览过

|

来源于tutorialspoint

转载

具有权重大于等于1的边的最小乘积路径

为了发现具有权重大于或等于1的最小边的路径,我们可以使用稍作修改的Dijkstra算法。首先,我们将源节点的权重设为1,将其他节点的权重设为无穷大。在算法执行过程中,我们不再更新距离,而是更新权重的乘积。这样可以确保选择具有最小权重的路径。通过在每一步选择权重最小的节点,我们迭代地发现最短路径,直到达到目标节点。最后,沿着这条路径的权重乘积将是最小的,满足给定的条件。

使用的方法

  • 修改后的Dijkstra算法,带有加权乘积

  • 修改的Bellman-Ford算法,带有加权乘积

加权乘积的改进Dijkstra算法

在修改后的Dijkstra算法中,我们首先将源节点的权重设置为无穷大,将所有其他节点的权重也设置为无穷大。在执行计算的过程中,我们不是用所有权重来更新距离,而是用到目前为止遇到的权重的乘积来更新它们。在每一步中,我们选择具有最小权重的节点,并以相同的方式更新其相邻节点的权重。这个过程一直持续到达目标节点。最终,沿着这条路径的权重乘积将表示最小可能值,满足权重大于或等于1的条件。

算法

  • 将所有中心权重初始化为无限大,除了源中心,其权重设置为0。

  • 创建一个清除集合来跟踪已删除的节点。

  • 当存在未访问的节点时,

    • 选择未访问节点中权重最小的中心。

    • 将所选中心标记为已访问。

    • 对于每个未访问的相邻枢纽:

    • 计算当前中心节点的权重和与之相连的边的权重。

    • 如果计算出的项小于相邻中心的权重,则用计算出的乘积替换其权重。

  • 重复步骤3,直到目标中心消失或所有中心都被访问。

    Audo Studio
    Audo Studio

    AI音频清洗工具(噪音消除、声音平衡、音量调节)

    下载
  • 目标中心的重量反映了从源头到目标点沿途最小的物品重量。

示例

#include 
using namespace std;

// Function to return the smallest product of edges
double modifiedDijkstra(int source, int destination, vector > > graph)
{
    // If the source is equal to the destination
    if (source == destination)
        return 0;

    // Initialize the priority queue
    set> pq;
    pq.insert({1, source});

    // Visited array
    bool visited[graph.size()] = {0};

    // While the priority queue is not empty
    while (!pq.empty())
    {
        // Current node
        int current = pq.begin()->second;

        // Current product of weights
        double product = pq.begin()->first;

        // Pop the top-most element
        pq.erase(pq.begin());

        // If already visited, continue
        if (visited[current])
            continue;

        // Mark the node as visited
        visited[current] = true;

        // If it is a destination node
        if (current == destination)
            return product;

        // Traverse the neighbors of the current node
        for (auto neighbor : graph[current])
        {
            int neighborNode = neighbor.first;
            double weight = neighbor.second;

            // Calculate the product of weights
            double newProduct = product * weight;

            // Insert the new product and neighbor into the priority queue
            pq.insert({newProduct, neighborNode});
        }
    }

    // If no path exists
    return -1;
}

// Function to add an edge to the graph
void addEdge(vector>>& graph, int u, int v, double weight)
{
    graph[u].push_back({v, weight});
}

// Function to print the graph
void printGraph(const vector>>& graph)
{
    for (int i = 1; i < graph.size(); i++)
    {
        cout << "Node " << i << ": ";
        for (auto neighbor : graph[i])
        {
            cout << "(" << neighbor.first << ", " << neighbor.second << ") ";
        }
        cout << endl;
    }
}

// Driver code
int main()
{
    int numNodes = 3;

    // Graph as adjacency list
    vector>> graph(numNodes + 1);

    // Input edges
    addEdge(graph, 1, 3, 9);
    addEdge(graph, 2, 3, 1);
    addEdge(graph, 1, 2, 5);

    // Source and destination
    int source = 1, destination = 3;

    // Modified Dijkstra
    double smallestProduct = modifiedDijkstra(source, destination, graph);

    // Print the result
    cout << "Smallest product of weights: " << smallestProduct << endl;

    // Print the graph
    cout << "Graph: " << endl;
    printGraph(graph);

    return 0;
}

输出

Smallest product of weights: 5
Graph: 
Node 1: (3, 9) (2, 5) 
Node 2: (3, 1) 
Node 3: 

修改后的带加权乘积的Bellman-Ford算法

在带有加权对象的调整后的Bellman-Ford算法中,我们通过将源中心的负载设置为1,将所有其他中心的负载设置为无穷大来开始。在每个循环中,通过比较当前节点的权重和连接它们到目标中心的边的负载来解开所有边。如果计算得到的权重比目标中心的负载小,我们将其权重增加计算得到的权重。重复这个过程V-1次,其中V是中心的总数,以确保考虑到所有可能的路径。目标中心的权重表示从源到目标的路径上最小的权重。

算法

  • 除了源中心之外,所有其他中心的权重应为无穷大。

  • 重复执行上述步骤 V−1 次,其中 V 是节点的总数:

    • 对于图表中的每条边,计算当前中心的项目权重以及与它们相连的边的权重。

    • 如果计算出的物品小于目标中心的重量,则用计算出的乘积升级其重量。

  • 经过V−1个周期,所有中心节点的权重将被确定。

  • 在计算过程中,如果图表中存在负权重循环,将会区分出一个额外的循环。如果在此过程中有任何权重被修正,这表明存在一个负权重循环的存在。

  • 目标中心的重量反映了从源头到目标点沿途最小的物品重量。

  • 贪婪着色算法基于可用颜色和邻居顶点使用的颜色,以贪婪的方式为顶点分配颜色。虽然它可能不总是使用最少数量的颜色来完成图表着色,但它提供了一种快速高效的顶点着色方法。

示例

#include 
#include 
#include 

struct Edge {
    int source, destination;
    double weight;
};

// Function to find the smallest product of weights using the modified Bellman-Ford algorithm
double findSmallestProduct(int numNodes, int source, int destination, std::vector& edges) {
    std::vector weights(numNodes, std::numeric_limits::infinity());
    weights[source] = 1;

    for (int i = 1; i < numNodes; i++) {
        for (const auto& edge : edges) {
            double newWeight = weights[edge.source] * edge.weight;
            if (newWeight < weights[edge.destination]) {
                weights[edge.destination] = newWeight;
            }
        }
    }

    for (const auto& edge : edges) {
        double newWeight = weights[edge.source] * edge.weight;
        if (newWeight < weights[edge.destination]) {
            return -1.0; // Negative-weight cycle detected
        }
    }

    return weights[destination];
}

int main() {
    int numNodes = 4;

    std::vector edges = {
        {0, 1, 2.0},
        {1, 2, 0.5},
        {2, 3, 1.5},
        {0, 3, 1.2},
        {1, 3, 0.8}
    };

    int source = 0, destination = 3;

    double smallestProduct = findSmallestProduct(numNodes, source, destination, edges);

    if (smallestProduct < std::numeric_limits::infinity()) {
        std::cout << "The smallest product of weights along the path from node " << source
                  << " to node " << destination << " is: " << smallestProduct << std::endl;
    } else {
        std::cout << "A negative-weight cycle is detected. No valid path exists." << std::endl;
    }

    return 0;
}

输出

The smallest product of weights along the path from node 0 to node 3 is: 1.2

结论

本文阐明了如何找到具有权重大于或等于1的最小边的路径。它介绍了两个算法,即改进的Dijkstra算法和改进的Bellman-Ford算法,用于解决这个问题。改进的Dijkstra算法在每一步选择具有最小权重的节点,而改进的Bellman-Ford算法迭代地解开边以更新权重。文章提供了这两个算法在C语言中的实现,并用测试输入说明了它们的使用。输出结果是从源节点到目标节点的路径上的最小权重。

相关专题

更多
C++ 高级模板编程与元编程
C++ 高级模板编程与元编程

本专题深入讲解 C++ 中的高级模板编程与元编程技术,涵盖模板特化、SFINAE、模板递归、类型萃取、编译时常量与计算、C++17 的折叠表达式与变长模板参数等。通过多个实际示例,帮助开发者掌握 如何利用 C++ 模板机制编写高效、可扩展的通用代码,并提升代码的灵活性与性能。

10

2026.01.23

php远程文件教程合集
php远程文件教程合集

本专题整合了php远程文件相关教程,阅读专题下面的文章了解更多详细内容。

29

2026.01.22

PHP后端开发相关内容汇总
PHP后端开发相关内容汇总

本专题整合了PHP后端开发相关内容,阅读专题下面的文章了解更多详细内容。

21

2026.01.22

php会话教程合集
php会话教程合集

本专题整合了php会话教程相关合集,阅读专题下面的文章了解更多详细内容。

21

2026.01.22

宝塔PHP8.4相关教程汇总
宝塔PHP8.4相关教程汇总

本专题整合了宝塔PHP8.4相关教程,阅读专题下面的文章了解更多详细内容。

13

2026.01.22

PHP特殊符号教程合集
PHP特殊符号教程合集

本专题整合了PHP特殊符号相关处理方法,阅读专题下面的文章了解更多详细内容。

11

2026.01.22

PHP探针相关教程合集
PHP探针相关教程合集

本专题整合了PHP探针相关教程,阅读专题下面的文章了解更多详细内容。

8

2026.01.22

菜鸟裹裹入口以及教程汇总
菜鸟裹裹入口以及教程汇总

本专题整合了菜鸟裹裹入口地址及教程分享,阅读专题下面的文章了解更多详细内容。

55

2026.01.22

Golang 性能分析与pprof调优实战
Golang 性能分析与pprof调优实战

本专题系统讲解 Golang 应用的性能分析与调优方法,重点覆盖 pprof 的使用方式,包括 CPU、内存、阻塞与 goroutine 分析,火焰图解读,常见性能瓶颈定位思路,以及在真实项目中进行针对性优化的实践技巧。通过案例讲解,帮助开发者掌握 用数据驱动的方式持续提升 Go 程序性能与稳定性。

9

2026.01.22

热门下载

更多
网站特效
/
网站源码
/
网站素材
/
前端模板

精品课程

更多
相关推荐
/
热门推荐
/
最新课程
Laravel---API接口
Laravel---API接口

共7课时 | 0.6万人学习

php-src源码分析探索
php-src源码分析探索

共6课时 | 0.5万人学习

关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送

Copyright 2014-2026 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号