Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I advise you to look at <a href="http://www.topcoder.com/tc?module=Static&amp;d1=tutorials&amp;d2=graphsDataStrucs3" rel="noreferrer">TopCoder</a> tutorial that have very pragmatic apporach. You will need to find out how STL priority queue works and make sure you don't have any <code>negative edge weights</code> in your graph.</p> <p>Decent full implementation can be found <a href="http://zobayer.blogspot.com/2009/12/dijkstras-algorithm-in-c.html" rel="noreferrer">here</a>. You will have to add Path vector to it and implement <code>RecoverPath</code> method in order to get nodes on path from source to sink. To use this solution you will also need to convert your <code>adjacency matrix</code> to <code>adjacency list</code> in following way:</p> <pre><code>for (int i=0;i&lt;nNodes;i++) for (int j=0;j&lt;nNodes;j++){ if (costMatrix[i][j] != NO_EDGE_VALUE){ G[i].pb(make_pair(j,costMatrix[i],[j])); } } </code></pre> <p>EDIT: If your graph is dense I would advise you to use <a href="http://en.wikipedia.org/wiki/Bellman%E2%80%93Ford_algorithm" rel="noreferrer">Ford Bellman</a> algorithm is much simpler and shouldn't be much slower.</p> <p>EDIT2: To calculate path you should add in the header</p> <pre><code>int P[MAX]; /*array with links to parents*/ for(i=0; i&lt;=nodes; i++) P[i] = -1; /*magic unset value*/ // dijkstra while(!Q.empty()) { .... if(!F[v] &amp;&amp; D[u]+w &lt; D[v]) { D[v] = D[u] + w; /*By setting P[v] value we will remember what is the previous node on path from source */ P[v] = u; // &lt;-- added line Q.push(pii(v, D[v])); } ... } </code></pre> <p>Then you have to add RecoverPath method (it works only when path exists)</p> <pre><code>vector&lt;int&gt; RecoverPath(int src, int dest){ vector&lt;int&gt; path; int v = dest; while (v != src) { path.push_back(v); v = P[v]; } path.push_back(src); std::reverse(path.begin(),path.end()); return path; } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload