优化dijkstra实现

问题描述 投票:2回答:2

问题编辑,现在我只想知道是否可以使用队列来改进算法。

我发现此混合成本最大流量算法的实现,该算法使用dijkstra:http://www.stanford.edu/~liszt90/acm/notebook.html#file2

将其粘贴在此处,以防它在互联网空白中丢失:

// Implementation of min cost max flow algorithm using adjacency
// matrix (Edmonds and Karp 1972).  This implementation keeps track of
// forward and reverse edges separately (so you can set cap[i][j] !=
// cap[j][i]).  For a regular max flow, set all edge costs to 0.
//
// Running time, O(|V|^2) cost per augmentation
//     max flow:           O(|V|^3) augmentations
//     min cost max flow:  O(|V|^4 * MAX_EDGE_COST) augmentations
//     
// INPUT: 
//     - graph, constructed using AddEdge()
//     - source
//     - sink
//
// OUTPUT:
//     - (maximum flow value, minimum cost value)
//     - To obtain the actual flow, look at positive values only.

#include <cmath>
#include <vector>
#include <iostream>

using namespace std;

typedef vector<int> VI;
typedef vector<VI> VVI;
typedef long long L;
typedef vector<L> VL;
typedef vector<VL> VVL;
typedef pair<int, int> PII;
typedef vector<PII> VPII;

const L INF = numeric_limits<L>::max() / 4;

struct MinCostMaxFlow {
  int N;
  VVL cap, flow, cost;
  VI found;
  VL dist, pi, width;
  VPII dad;

  MinCostMaxFlow(int N) : 
    N(N), cap(N, VL(N)), flow(N, VL(N)), cost(N, VL(N)), 
    found(N), dist(N), pi(N), width(N), dad(N) {}

  void AddEdge(int from, int to, L cap, L cost) {
    this->cap[from][to] = cap;
    this->cost[from][to] = cost;
  }

  void Relax(int s, int k, L cap, L cost, int dir) {
    L val = dist[s] + pi[s] - pi[k] + cost;
    if (cap && val < dist[k]) {
      dist[k] = val;
      dad[k] = make_pair(s, dir);
      width[k] = min(cap, width[s]);
    }
  }

  L Dijkstra(int s, int t) {
    fill(found.begin(), found.end(), false);
    fill(dist.begin(), dist.end(), INF);
    fill(width.begin(), width.end(), 0);
    dist[s] = 0;
    width[s] = INF;

    while (s != -1) {
      int best = -1;
      found[s] = true;
      for (int k = 0; k < N; k++) {
        if (found[k]) continue;
        Relax(s, k, cap[s][k] - flow[s][k], cost[s][k], 1);
        Relax(s, k, flow[k][s], -cost[k][s], -1);
        if (best == -1 || dist[k] < dist[best]) best = k;
      }
      s = best;
    }

    for (int k = 0; k < N; k++)
      pi[k] = min(pi[k] + dist[k], INF);
    return width[t];
  }

  pair<L, L> GetMaxFlow(int s, int t) {
    L totflow = 0, totcost = 0;
    while (L amt = Dijkstra(s, t)) {
      totflow += amt;
      for (int x = t; x != s; x = dad[x].first) {
        if (dad[x].second == 1) {
          flow[dad[x].first][x] += amt;
          totcost += amt * cost[dad[x].first][x];
        } else {
          flow[x][dad[x].first] -= amt;
          totcost -= amt * cost[x][dad[x].first];
        }
      }
    }
    return make_pair(totflow, totcost);
  }
};

我的问题是,是否可以通过使用Dijkstra()中的优先级队列来改善它。我尝试过,但无法正常工作。实际上,我怀疑在Dijkstra中它应该在相邻节点而不是所有节点上循环...

非常感谢。

c++ algorithm matching priority-queue dijkstra
2个回答
1
投票

当然,可以使用minheap改进Dijkstra的算法。将顶点放入最短路径树并处理(即标记)所有相邻顶点后,我们的下一步是选择树中尚未标记的具有最小标记的顶点。这就是minheap想到的地方。而不是顺序扫描所有顶点,我们从堆中提取min元素并对其进行重组,这需要O(logn)时间与O(n)的时间。请注意,堆将仅保留那些尚未在最短路径树中的顶点。但是,如果我们更新顶点的标签,我们应该能够以某种方式修改堆中的顶点。


0
投票

我不确定使用优先级队列来实现Dijkstra的算法实际上会改善运行时间,因为使用优先级队列会减少找到与源的距离最小的顶点所需的时间(O(log V)在朴素的实现中具有优先级队列与O(V)的情况下),这也增加了处理新边缘所需的时间(在朴素的实现中具有优先级队列与O(V)的O(log V)) 。

因此,对于简单的实现,运行时间为O(V ^ 2 + E)。

但是,对于优先级队列实现,运行时间为O(V log V + E log V)。

对于非常密集的图,E可以是O(V ^ 2),这意味着朴素的实现将具有运行时间O(V ^ 2 + V ^ 2)= O(V ^ 2),而优先级队列实现将具有运行时间O(V log V + V ^ 2 log V)= O(V ^ 2 log V)。因此,如您所见,在密集图的情况下,优先级队列实现实际上在最坏情况下的运行时间更糟。

鉴于编写上述实现的人们将边缘存储为邻接矩阵而不是使用邻接列表,因此,看起来像编写此代码的人们期望图是O(V ^ 2)的密集图。边缘,因此在这里优先使用朴素的实现而不是优先级队列实现是有意义的。

有关Dijkstra算法的运行时间的更多信息,请阅读this Wikipedia page

© www.soinside.com 2019 - 2024. All rights reserved.