Скоро Codeforces Round 1133 (Div. 1, Div. 2)! Регистрация здесь. ×

Блог пользователя BanazadehAria

Автор BanazadehAria, история, 7 лет назад, По-английски

Hi, What is the best way to get the shortest path from vertex a to b in a weighted undirected graph?

I have done this but can we do better than O(|V|)?

map<int,list<pair<int,int> > > adjList;

void addEdge(int u,int v,int weight){
    adjList[u].pb(mp(v,weight));
    adjList[v].pb(mp(u,weight));
}

void FindShortestPath(int u,int v){
    int dp[n];
    dp[u]=0;
    map<int,bool> visited;
    queue<int> q;q.push(u);visited[u]=true;
    while(!q.empty()){
        int now = q.front();q.pop();
        for(auto neight : adjList[now]){
            dp[neight.first] = min(dp[neight.first],dp[now]+neight.second);
            if(!visited[neight.first]){
                q.push(neight.first);visited[neight.first]=true;
            }
        }
    }cout << dp[v];
}
  • Проголосовать: нравится
  • -15
  • Проголосовать: не нравится

»
7 лет назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

Auto comment: topic has been updated by BanazadehAria (previous revision, new revision, compare).

»
7 лет назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

As far as I know, the best way to get the shortest path in a graph with positive weight is Dijkstra, and the complexity is $$$O(n\log n)$$$

»
7 лет назад, скрыть # |
← Rev. 2  
Проголосовать: нравится 0 Проголосовать: не нравится

can we do better than O(|V|)?

What are you talking about? Lol. Take any deterministic path finding/shortest path algo. We can always make it traverse the entire graph for ANY given graph. I am lazy to provide a proof but you can easily prove it by induction. Anyway, without going into further details, just consider the case where we have a line graph. Now set your source and destination to be the ends of the graph. Your algo has no choice but to traverse all vertices of the graph.

»
7 лет назад, скрыть # |
 
Проголосовать: нравится +6 Проголосовать: не нравится

Your algorithm is wrong.

(1, 2) w = 4
(1, 3) w = 1
(3, 2) w = 1
(2, 4) w = 1

the shortest path from 1 to 4 las length 3 but your algorithm will say its 5...

  • »
    »
    7 лет назад, скрыть # ^ |
     
    Проголосовать: нравится 0 Проголосовать: не нравится

    Oh can you say what is wrong in my algorithm?

    • »
      »
      »
      7 лет назад, скрыть # ^ |
       
      Проголосовать: нравится +1 Проголосовать: не нравится

      You are using a queue instead of heap so you are not taking into considerations the weights. What you do, would work in unweighted graphs, because what you are really doing is BFS.

      You set node u as "visited" the first time you visit it. But maybe in the future you will find a shortest path that pass through u and leads to a better solution for an other node v. So to fix your solution, you should include node "u" to the queue every time you find a shortest path to u. But this will lead to O(n^2), so this is why we use Dijkstra's algorithm with heap.

      • »
        »
        »
        »
        7 лет назад, скрыть # ^ |
         
        Проголосовать: нравится -8 Проголосовать: не нравится

        But maybe in the future you will find a shortest path that pass through u and leads to a better solution for the other node v Hi I didn't understand why is that?

        If we can go to a better place from u at later times then at first we can go to because we are visiting all of the neighbors of u.

»
7 лет назад, скрыть # |
 
Проголосовать: нравится +13 Проголосовать: не нравится

Username checks out