Advertisement
Guest User

'Fake Code'

a guest
Dec 3rd, 2013
1,293
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.19 KB | None | 0 0
  1.  
  2.  
  3. #include <iostream>
  4. #include <stack>    //For tracing paths at the end
  5. #define NAN 999;    //To mark nodes that don't directly connect
  6.  
  7. using namespace std;
  8.  
  9. int current, mindex;
  10. int distanceArray[7][7]; //2D array to hold the distances from each point to all others
  11. int d[6]; //Single distance array from source to points
  12. int p[6];
  13. int copyD[6]; //Single array to keep the predecessors
  14. int order[6];
  15.  
  16.  
  17. void tracePath(int x)
  18. {
  19.     //Stack to hold predecessors
  20.     stack<int> path;
  21.  
  22.     cout<<d[x]<<": ";
  23.     bool done = false;
  24.     int ps = x;
  25.     while(!done)
  26.     {
  27.         path.push(ps);
  28.         ps=p[ps];
  29.  
  30.         if(ps==1)
  31.         {
  32.             path.push(ps);
  33.             done = true;
  34.         }
  35.     }
  36.     while(!path.empty())
  37.     {
  38.         int j= path.top();
  39.         path.pop();
  40.         if(path.empty())
  41.             cout<<j;
  42.         else
  43.             cout<<j<<"->";
  44.     }
  45.     cout<<endl;
  46.  
  47. }
  48.  
  49.  
  50. void output()
  51. {
  52.  
  53.     //Create array with the shortest paths in order
  54.     //array stores the reference to end node so we can get predecessor
  55.     //Set order array to distance array
  56.  
  57. /*  cout<<"Starting array: "<<endl;
  58.     for (int i = 1; i<7; i++)
  59.     {
  60.         copyD[i] = d[i];
  61.         cout<<copyD[i]<<" ";
  62.     }
  63.     cout<<endl; */
  64.  
  65.     int theMin = 1;
  66.  
  67.     for(int c=1; c<7; c++)
  68.     {
  69.         for(int v=1; v<7; v++)
  70.         {
  71.             if (copyD[theMin] > copyD[v])
  72.             {
  73.                 theMin=v;
  74.                 //cout<<"New min is: "<<v<<endl;
  75.             }
  76.            
  77.         }//End inside for
  78.  
  79.         //Change the value of min to not catch it in next loops
  80.         copyD[theMin]=999;
  81.         //Store index of the min in our order array
  82.         order[c]=theMin;
  83.  
  84.         /*
  85.         cout<<"New array: ";
  86.         for (int i = 1; i<7; i++)
  87.         {
  88.             cout<<copyD[i]<<" ";
  89.         }
  90.         cout<<endl;
  91.  
  92.         */
  93.     /*  cout<<"Order array: ";
  94.         for (int i = 1; i<7; i++)
  95.         {
  96.             cout<<order[i]<<" ";
  97.         }
  98.         cout<<endl; */
  99.  
  100.         //Now we have order array of shortest paths
  101.         //Take each path, put it's predecessors in stack to trace
  102.         //and print distance followed by path
  103.  
  104.        
  105.  
  106.     } //End outside for
  107.  
  108.     for (int i=1; i<7; i++)
  109.             tracePath(order[i]);
  110.  
  111.    
  112.    
  113.    
  114. }
  115.  
  116.  
  117.  
  118. void printArray()
  119. {
  120.  
  121.  
  122. cout<<"Printing 2D array"<<endl;
  123.  
  124. for(int i=1; i < 7; i++)
  125. {
  126.     for (int j=1; j < 7; j++)
  127.     {   if(distanceArray[i][j]==-99)
  128.             cout<<" - ";
  129.         else
  130.             cout<<distanceArray[i][j]<<" ";
  131.         if(j==6)
  132.             cout<<endl;
  133.     }
  134. }
  135.  
  136. cout<<"Printing Distances:"<<endl;
  137.  
  138. for(int i=1; i<7; i++)
  139.     cout<<"Distance from 1 to "<<i<<" is "<<d[i]<<endl;
  140.  
  141. cout<<"Printing predecessors: "<<endl;
  142.    
  143. for(int i=1; i<7; i++)
  144.     cout<<"Predecessor of "<<i<<" is "<<p[i]<<endl;
  145.  
  146.  
  147. }
  148.  
  149. void Initialize()
  150. {
  151.         /*
  152.     Distances
  153. 1->2 = 10
  154. 1->4 = 30
  155. 1->5 = 100
  156. 2->3 = 50
  157. 3->5 = 10
  158. 3->6 = 5
  159. 4->6 = 15
  160. 4->3 = 20
  161. 5->4 = 60
  162. */
  163.  
  164. //Hardcoding the test case into distanceArray
  165. //First make them all infinity
  166. //When they are visited, they are marked with a different number
  167.  
  168. for(int i=1; i < 7; i++)
  169. {
  170.     d[i]=NAN;   //All distances start high
  171.     p[i]=0;     //No predecessors at first
  172.  
  173.     for (int j=0; j < 7; j++)
  174.             distanceArray[i][j]=NAN;
  175. }
  176.  
  177.     d[1]=0; //Source to source
  178.     p[1]=1;
  179.  
  180. //Now overwrite nodes that connect with the weights of their edges
  181. distanceArray[1][2]=10;
  182. distanceArray[1][4]=30;
  183. distanceArray[1][5]=100;
  184. distanceArray[2][3]=50;
  185. distanceArray[3][5]=10;
  186. distanceArray[3][6]=5;
  187. distanceArray[4][6]=15;
  188. distanceArray[4][3]=20;
  189. distanceArray[5][4]=60;
  190.  
  191. }
  192.  
  193. void dijkstra() {
  194.    
  195.     //Create Map
  196.     Initialize();
  197.  
  198.     for(int i=0; i<5; i++)
  199.     {
  200.        
  201.         current=1;
  202.        
  203.         while(current!=6)
  204.         {
  205.             //Iterate through and update distances/predecessors
  206.             //For loopt go through columns, while current iterates rows
  207.             for(int j=1; j<7; j++)
  208.             {
  209.                 //Check if distance from current to this node is less than
  210.                 //distance already stored in d[j] + weight of edge
  211.  
  212.                 if(distanceArray[current][j]+d[current]<d[j])
  213.                 {
  214.                     //cout<<"Previous distance to "<<j<<" was "<<d[j]<<" from "<<p[j]<<endl;
  215.                     //cout<<"New smaller distance is "<<distanceArray[current][j]+d[current]<<" from "<<current<<endl;
  216.                     //Update distance
  217.                     d[j] = distanceArray[current][j]+d[current];
  218.                     //Update p
  219.                     p[j] = current;
  220.                 }    
  221.             }
  222.             //Go to next row in distanceArray[][]
  223.             current++;
  224.         } //End while
  225.        
  226.        
  227.     } //End for
  228.     //printArray();
  229.  
  230. }
  231.  
  232.  
  233.  
  234. int main(int argc, char *argv[]) {
  235.    
  236.     dijkstra();
  237.     output();
  238.     return 0;
  239. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement