无环有向图的最短路径和最长路径(权重任意)

EdgeWeightedDigraph.h

#pragma once
#include <memory>
#include <fstream>
#include <stdexcept>
template <typename T>
class QSP;
template<typename T>
class EdgeWeightedDigraph
{
private:
    class AdjacentcyList
    {
    public:
        class DiEdge
        {
        public:
            std::shared_ptr<DiEdge> next;
            int v;
            int w;
            T weight;
        public:
            DiEdge(const int& s,const int& e,const T& t):v(s),w(e),weight(t),next(nullptr)
            {

            }
            DiEdge() = default;
            int from()
            {
                return v;
            }
            int to()
            {
                return w;
            }
            T Weight()
            {
                return weight;
            }
        };
        std::shared_ptr<DiEdge> head;
        class Iterator
        {
        private:
            std::shared_ptr<DiEdge> it;
        public:
            Iterator(std::shared_ptr<DiEdge> i) :it(i)
            {

            }
            bool operator == (const Iterator& rhs)const
            {
                return it == rhs.it;
            }
            bool operator != (const Iterator& rhs)const
            {
                return it != rhs.it;
            }
            Iterator operator ++()
            {
                it = it->next;
                return *this;
            }
            DiEdge operator *()const
            {
                if (it == nullptr)
                    throw std::out_of_range("* nullptr error");
                return *it;
            }
        };
        Iterator begin()const
        {
            return Iterator(head);
        }
        Iterator end()const
        {
            return Iterator(nullptr);
        }
        AdjacentcyList():head(nullptr)
        {

        }
/*****************************************
函数名称:   addDiEdge
******************************************/
        void addDiEdge(const int& s,const int& e,const T& t)
        {
            if (head == nullptr)
            {
                head = std::make_shared<DiEdge>(DiEdge(s, e, t));
                return;
            }
            std::shared_ptr<DiEdge> curr = head;
            while (curr->next != nullptr)
                curr = curr->next;
            curr->next = std::make_shared<DiEdge>(DiEdge(s, e, t));
            return;
        }
    };
private:
    std::unique_ptr<AdjacentcyList[]> adj;
    int nV;
    int nE;
public:
    EdgeWeightedDigraph(const std::string& file):nE(0)
    {
        std::ifstream in(file);
        in >> nV;
        adj = std::move(std::unique_ptr<AdjacentcyList[]>(new AdjacentcyList[nV]));
        while (!in.eof())
        {
            int pre, curr;
            T weight;
            in >> pre >> curr >> weight;
            adj[pre].addDiEdge(pre, curr, weight);
            ++nE;
        }
    }
    friend class QSP<T>;
};

QSP.h

#pragma once
#include "priority_queue.h"
#include "EdgeWeightedDigraph.h"
#include <stack>

template <typename T>
class QSP
{
    using Ed = typename EdgeWeightedDigraph<T>::AdjacentcyList::DiEdge;
private:
    std::stack<int> resufdfs;
    bool cycle;
    std::unique_ptr<bool[]> marked;
    std::unique_ptr<bool[]> stackOn;
    std::unique_ptr<Ed*[]> edge;
    std::unique_ptr<T[]> disTo;
    int s;
    int e;
private:
    void iscycle(EdgeWeightedDigraph<T>* ewd,const int& i)
    {
        stackOn[i] = true;
        marked[i] = true;
        for (auto& ed : ewd->adj[i])
        {
            int w = ed.to();
            if (stackOn[w])
                cycle = true;
            if (cycle)
                return;
            if (!marked[w])
            iscycle(ewd, w);
        }
        stackOn[i] = false;
    }
    void iscycle(EdgeWeightedDigraph<T>* ewd)
    {
        for (int i = 0; i < ewd->nV; ++i)
            if (!marked[i]) iscycle(ewd, i);
    }
    void getTop(EdgeWeightedDigraph<T>* ewd,const int& i)
    {
        marked[i] = true;
        for (auto &ed : ewd->adj[i])
        {
            int w = ed.to();
            if (!marked[w])
                getTop(ewd, w);
        }
        resufdfs.push(i);
    }
    void getTop(EdgeWeightedDigraph<T>* ewd)
    {
        for (int i = 0; i < ewd->nV; ++i)
            marked[i] = false;
        for (int i = 0; i < ewd->nV; ++i)
         if (!marked[i]) getTop(ewd, i);
    }
    void relax(EdgeWeightedDigraph<T>* ewd,const int& i)
    {
        for (auto& ed : ewd->adj[i])
        {
            int w = ed.to();
            //求最长路径时将 > 改为 <
            if (disTo[w] > disTo[i] + ed.weight)
            {
                disTo[w] = disTo[i] + ed.weight;
                edge[w] = new Ed(i, w, ed.weight);
            }
        }
    }
public:
    QSP(EdgeWeightedDigraph<T>* ewd,const int& s,const int& e)
        :marked(new bool[ewd->nV]),stackOn(new bool[ewd->nV]),cycle(false),s(s),e(e),disTo(new T[ewd->nV]),edge(new Ed*[ewd->nV])
    {
        for (int i = 0; i < ewd->nV; ++i)
        {
            marked[i] = false;
            stackOn[i] = false;
        }
        iscycle(ewd);
        if (cycle)
            throw std::logic_error("can't handle cycle graph");
        getTop(ewd);
        for (int i = 0; i < ewd->nV; ++i)
        {
            edge[i] = nullptr;
            //求最长路径时将 max() 改为 min()
            disTo[i] = std::numeric_limits<T>::max();
        }
        disTo[s] = T{};
        while (!resufdfs.empty())
        {
            int v = resufdfs.top();
            resufdfs.pop();
            relax(ewd, v);
        }
    }
    T SPW()
    {
        return disTo[e];
    }
    void path()
    {
        stack<Ed> sk;
        for (auto i = edge[e]; i != nullptr; i = edge[i->from()])
        {
            sk.push(*i);
            if (i->from() == s)
                break;
        }
        while (!sk.empty())
        {
            cout << sk.top().from() << ends << sk.top().to() << ends << sk.top().weight << endl;
            sk.pop();
        }
    }
};

测试文件: Text.txt

8
5 4 0.35
4 7 0.37
5 7 0.28
5 1 0.32
4 0 0.38
0 2 0.26
3 7 0.39
1 3 0.29
7 2 0.34
6 2 0.40
3 6 0.52
6 0 0.58
6 4 0.93

main.cpp

#include <iostream>
#include "EdgeWeightedDigraph.h"
#include "QSP.h"
using namespace std;

int main()
{
    EdgeWeightedDigraph<double> ewd("Text.txt");
    try
    {
        QSP<double> qsp(&ewd, 5,6);
        qsp.path();
        cout << qsp.SPW();
    }
    catch (const exception& e)
    {
        cout << e.what();
    }
    system("pause");
    return 0;
}

运行:

这里写图片描述

求最长路径,只需改动注释指出部分。。。。

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
对于有向无环图的最短路径问题,可以使用拓扑排序和动态规划的方法来解决。以下是一个使用 C# 编写的示例代码: ```csharp using System; using System.Collections.Generic; public class Graph { private int V; // 图中顶点的数量 private List<List<Node>> adj; // 邻接表,用于存储图的边 public Graph(int v) { V = v; adj = new List<List<Node>>(); for (int i = 0; i < V; i++) { adj.Add(new List<Node>()); } } public void AddEdge(int u, int v, int weight) { Node node = new Node(u, v, weight); adj[u].Add(node); } public void ShortestPath(int source) { int[] dist = new int[V]; for (int i = 0; i < V; i++) { dist[i] = int.MaxValue; } dist[source] = 0; // 拓扑排序 Stack<int> stack = new Stack<int>(); bool[] visited = new bool[V]; for (int i = 0; i < V; i++) { if (!visited[i]) { TopologicalSortUtil(i, visited, stack); } } // 计算最短路径 while (stack.Count > 0) { int u = stack.Pop(); if (dist[u] != int.MaxValue) { foreach (var node in adj[u]) { if (dist[u] + node.Weight < dist[node.V]) { dist[node.V] = dist[u] + node.Weight; } } } } // 输出最短路径 Console.WriteLine("顶点\t距离"); for (int i = 0; i < V; i++) { Console.WriteLine(i + "\t" + dist[i]); } } private void TopologicalSortUtil(int v, bool[] visited, Stack<int> stack) { visited[v] = true; foreach (var node in adj[v]) { if (!visited[node.V]) { TopologicalSortUtil(node.V, visited, stack); } } stack.Push(v); } private class Node { public int U { get; set; } // 边的起始顶点 public int V { get; set; } // 边的结束顶点 public int Weight { get; set; } // 边的权重 public Node(int u, int v, int weight) { U = u; V = v; Weight = weight; } } } public class Program { public static void Main(string[] args) { int V = 6; // 图中顶点的数量 Graph graph = new Graph(V); graph.AddEdge(0, 1, 2); graph.AddEdge(0, 4, 1); graph.AddEdge(1, 2, 3); graph.AddEdge(4, 2, 2); graph.AddEdge(4, 5, 4); graph.AddEdge(2, 3, 6); graph.AddEdge(5, 3, 1); int source = 0; graph.ShortestPath(source); } } ``` 在这个示例代码中,我们首先创建了一个有向无环图对象,然后使用`AddEdge`方法添加图的边。然后,我们调用`ShortestPath`方法来计算从给定源顶点到其他顶点的最短路径。最后,我们输出了所有顶点的最短路径。 请注意,这里的示例代码使用了拓扑排序和动态规划的思想来解决有向无环图的最短路径问题。这种方法适用于没有负权边的图。如果图中存在负权边,则需要使用其他算法,如 Bellman-Ford 算法或 Dijkstra 算法的变种。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值