2011-06-11 81 views
6

下面是我从Wikipedia article中的伪代码中写出的Dijkstra算法的实现。对于具有约40 000个节点和80 000个边的图,运行需要3或4分钟。这就像正确的数量级?如果没有,我的实施有什么问题?Dijkstra算法实现的性能

struct DijkstraVertex { 
    int index; 
    vector<int> adj; 
    vector<double> weights; 
    double dist; 
    int prev; 
    bool opt; 
    DijkstraVertex(int vertexIndex, vector<int> adjacentVertices, vector<double> edgeWeights) { 
    index = vertexIndex; 
    adj = adjacentVertices; 
    weights = edgeWeights; 
    dist = numeric_limits<double>::infinity(); 
    prev = -1; // "undefined" node 
    opt = false; // unoptimized node 
    } 
}; 

void dijsktra(vector<DijkstraVertex*> graph, int source, vector<double> &dist, vector<int> &prev) { 
    vector<DijkstraVertex*> Q(G); // set of unoptimized nodes 
    G[source]->dist = 0; 
    while (!Q.empty()) { 
    sort(Q.begin(), Q.end(), dijkstraDistComp); // sort nodes in Q by dist from source 
    DijkstraVertex* u = Q.front(); // u = node in Q with lowest dist 
    u->opt = true; 
    Q.erase(Q.begin()); 
    if (u->dist == numeric_limits<double>::infinity()) { 
     break; // all remaining vertices are inaccessible from the source 
    } 
    for (int i = 0; i < (signed)u->adj.size(); i++) { // for each neighbour of u not in Q 
    DijkstraVertex* v = G[u->adj[i]]; 
    if (!v->opt) { 
     double alt = u->dist + u->weights[i]; 
     if (alt < v->dist) { 
     v->dist = alt; 
     v->prev = u->index; 
     } 
    } 
    } 
    } 
    for (int i = 0; i < (signed)G.size(); i++) { 
    assert(G[i] != NULL); 
    dist.push_back(G[i]->dist); // transfer data to dist for output 
    prev.push_back(G[i]->prev); // transfer data to prev for output 
    } 
} 

回答

5

有几件事情可以改善这一点:

  • 实现优先级队列与排序和擦除增加了一个因素| E |到运行时 - 使用STL的heap functions将日志(N)插入和删除到队列中。
  • 请勿将所有节点同时放入队列中,而只能将您发现路径(可能是也可能不是最佳,因为您可以通过队列中的节点找到间接路径)发现路径中的所有节点。
  • 为每个节点创建对象会创建不必要的内存碎片。如果你关心排挤最后的5-10%,你可以考虑一个解决方案来直接将数量矩阵和其他信息表示为数组。
+0

感谢您的答复。我得到的印象是,我目前的实现不是非常糟糕,并且根据您的建议,对于有40 000个节点的问题,我预计执行时间为1到3分钟。执行时间接近30秒或1秒是不合理的。这是真的? – zoo 2011-06-12 00:37:06

1

使用priority_queue。

我Dijkstra算法实现:

struct edge 
{ 
    int v,w; 
    edge(int _w,int _v):w(_w),v(_v){} 
}; 
vector<vector<edge> > g; 
enum color {white,gray,black}; 
vector<int> dijkstra(int s) 
{ 
    int n=g.size(); 
    vector<int> d(n,-1); 
    vector<color> c(n,white); 
    d[s]=0; 
    c[s]=gray; 
    priority_queue<pair<int,int>,vector<pair<int,int> >,greater<pair<int,int> > > q; // declare priority_queue 
    q.push(make_pair(d[s],s)); //push starting vertex 
    while(!q.empty()) 
    { 
     int u=q.top().second;q.pop(); //pop vertex from queue 
     if(c[u]==black)continue; 
     c[u]=black; 
     for(int i=0;i<g[u].size();i++) 
     { 
      int v=g[u][i].v,w=g[u][i].w; 
      if(c[v]==white) //new vertex found 
      { 
       d[v]=d[u]+w; 
       c[v]=gray; 
       q.push(make_pair(d[v],v)); //add vertex to queue 
      } 
      else if(c[v]==gray && d[v]>d[u]+w) //shorter path to gray vertex found 
      { 
       d[v]=d[u]+w; 
       q.push(make_pair(d[v],v)); //push this vertex to queue 
      } 
     } 
    } 
    return d; 
} 
+0

我知道这篇文章有点旧了。但是我没有得到你想通过g [u] .size()实现的目标。你是否试图扫描g的邻接列表。 – user1354510 2012-05-31 04:39:30

+0

g是相邻列表 – frp 2012-05-31 18:45:11

+0

g [u] .size()是与顶点u相连的顶点数。 – frp 2012-05-31 18:46:38