0

I have made this program for shortest path in a bidirectional graph using dijkstra's algorithm. Can this algorithm be converted to a program to find the longest path in the graph.

#include<bits/stdc++.h>
using namespace std;

#define mp make_pair
#define ft first
#define sd second

vector< pair<long long int,long long int> > g[101000];
long long int n,m;
vector<long long int> dist;
vector <long long int> vis;

void dijkstra(long long int source,long long int l)
{
    dist.clear();
    long long int i,j,k;
    for(i=0;i<n;i++)
    {
        dist.push_back(INT_MAX);
        vis.push_back(0);
    }
    dist[source] = 0;
    set< pair<long long int,long long int> > s; // pair is dist, node_number
    set< pair<long long int,long long int> >::iterator it;

    for(i=0;i<n;i++)
        s.insert(mp(dist[i],i));

    while(!s.empty())
    {
        it = s.begin();
        pair<long long int,long long int> temp = *it;
        s.erase(temp); // remove minimum val node
        long long int cur = temp.sd;
        long long int val = temp.ft;

        if(val == INT_MAX)
            return;
        for(i=0;i<g[cur].size();i++)
        {
            long long int nb = g[cur][i].ft;
            if(!vis[nb] && dist[nb] > val + g[cur][i].sd)
            {
                s.erase(mp(dist[nb],nb)); // erase old val
                dist[nb] = val + g[cur][i].sd;
                s.insert(mp(dist[nb],nb));
            }
        }
    }
    for(int i=0;i<n;i++)
    {
        cout<<dist[i]<<" ";
    }
    cout<<endl;

}
int main()
{
    long long int i,j,k;

        dist.clear();
        vis.clear();

        long long int x,y,z;
        cin>>n>>m;
        for(i=0;i<m;i++)
            g[i].clear();

        for(i=0;i<m;i++)
        {
            cin>>x>>y>>z;
            x--;    y--;

            g[x].push_back(mp(y,z));
            g[y].push_back(mp(x,z));
        }
        dijkstra(0,n-1);

        return 0;
}

Basically i have is probabilities instead of path cost, so i want is the path with max probability. How can i go about achieving this.

Ami Tavory
  • 74,578
  • 11
  • 141
  • 185
Vandan Bhardwaj
  • 53
  • 3
  • 11
  • @BeyelerStudios IINM, this is problematic: negation followed by a constant addition finds a combination of cheapest + shortest path in the resulting graph, which is not necessarily the most expensive path in the original graph. In any case the question is about path probabilities, which don't exactly sum this way in any case. – Ami Tavory Oct 29 '16 at 08:56

1 Answers1

5

(Offtopic - using #include <bits/stdc++.h> is a nonportable hack.)

Basically i have is probabilities instead of path cost, so i want is the path with max probability. How can i go about achieving this.

Say that starting from s, you want to find the path of maximum probability to each v. If a path with probability Πipi is largest among such paths, then -∑ilog(pi) is smallest. Note that this corresponds to a graph with each edge weight being log(1/pi) which is positive, hence you can apply Dijkstra (there will be no negative-weight cycles).

Ami Tavory
  • 74,578
  • 11
  • 141
  • 185