Advertisement
Guest User

Untitled

a guest
Apr 27th, 2017
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. template<class T>
  2. vector<Vertex<T> *> Graph<T>::calculateFordFulkerson(T source) {
  3.     Graph<T> gr = this->clone();
  4.     Graph<T> gf = this->clone();
  5.     gf.resetEdgeFlow();
  6.  
  7.     Vertex<T>* src = gr.getVertex(source);
  8.     if (src==NULL) throw -1;
  9.  
  10.     while(true)
  11.     {
  12.         vector<Vertex<T>*> visited;
  13.         visited.push_back(src);
  14.  
  15.         float res = this->calculateFordFulkerson(src, NULL, 99999, &gf, &gr, visited);
  16.         if (res < 0)
  17.         {
  18.             //*** All paths were searched. Terminating!
  19.             break;
  20.         }
  21.     }
  22.  
  23.     return gf.vertexSet;
  24. }
  25.  
  26. template<class T>
  27. float Graph<T>::calculateFordFulkerson(Vertex<T>* current, Vertex<T>* parent, float min, Graph<T>* gf, Graph<T>* gr, vector<Vertex<T> *> visited) {
  28.  
  29.     vector<Edge<T> > edges = current->adj;
  30.  
  31.     for (unsigned int i = 0; i < edges.size(); i++)
  32.     {
  33.         Edge<T> edg = edges[i];
  34.  
  35.         //*** Searching edge to vertex edg.dest->info
  36.  
  37.         Vertex<T>* dest = edges[i].dest;
  38.         Vertex<T>* destInGraph = this->getVertex(edges[i].dest->info);
  39.  
  40.         bool beenVisited = false;
  41.         for (unsigned int j = 0; j < visited.size(); j++)
  42.         {
  43.             if (dest->info == visited[j]->info)
  44.             {
  45.                 //*** Detected that dest->info was already visited. Will now continue with the next one!
  46.                 beenVisited = true;
  47.                 break;
  48.             }
  49.         }
  50.  
  51.         if (beenVisited)
  52.             continue;
  53.  
  54.         if (edg.flow < min)
  55.         {
  56.             min = edg.flow;
  57.             //*** Minimum flow updated to: min
  58.         }
  59.  
  60.         visited.push_back(dest);
  61.  
  62.         if (destInGraph->adj.size() == 0)
  63.         {
  64.             //*** Reached a destination: dest->info
  65.  
  66.             //Upon the return, update the edges with the min value
  67.             float newValue = edg.flow - min;
  68.             if (newValue == 0)
  69.             {
  70.                 //*** Deleting edge from current->info to dest->info since the val is 0!
  71.                 gr->removeEdge(current->info, dest->info);
  72.             }
  73.             else
  74.                 current->updateEdgeFlow(i, edg.flow - min);
  75.  
  76.  
  77.             //Check to see if this edge already exists
  78.             bool exists = false;
  79.             unsigned int k = 0;
  80.             for (k = 0; k < dest->adj.size(); k++)
  81.             {
  82.                 if (dest->adj[k].dest->info == current->info)
  83.                 {
  84.                     exists = true;
  85.                     break;
  86.                 }
  87.             }
  88.  
  89.             if (!exists)
  90.             {
  91.                 gr->addEdge(dest->info, current->info, edg.weight, min);
  92.                 //*** Edge from dest->info to current->info does not exist in Gr... Adding with min
  93.             }
  94.             else
  95.             {
  96.                 dest->updateEdgeFlow(k, dest->adj[k].flow + min);
  97.                 //*** Edge from dest->info to current->info already exists in Gr... Updating to: dest->adj[k].flow + min
  98.             }
  99.  
  100.             //Search for the correct edge on Gf since both graphs may be unsynchronized
  101.             Vertex<T>* updatedVertex = gf->getVertex(current->info);
  102.             for (k = 0; k < current->adj.size(); k++)
  103.             {
  104.                 if (current->adj[k].dest->info == dest->info)
  105.                     break;
  106.             }
  107.             float oldValue = updatedVertex->adj[k].flow;
  108.  
  109.             updatedVertex->updateEdgeFlow(k, oldValue + min);
  110.             Edge<T> updatedEdge = updatedVertex->adj[k];
  111.  
  112.             //*** Setting k new edge flow value to the final graph from updatedVertex->info to updatedEdge.dest->info with k flow value of updatedEdge.flow
  113.  
  114.             //****** Returning minimum value of : min
  115.             return min;
  116.         }
  117.         else
  118.         {
  119.             //*** Will enter recursion with new current: dest->info
  120.             float newmin = this->calculateFordFulkerson(dest, current, min, gf, gr, visited);
  121.  
  122.             //*** Returned from recursion where new current was: dest->info
  123.             //*** New minimum value is: newmin
  124.  
  125.             if (newmin < 0)
  126.             {
  127.                 //*** There are no more paths to the destination. Exiting with new minimum: newmin
  128.                 continue;
  129.             }
  130.  
  131.             //Upon the return, update the edges with the min value
  132.             float newValue = edg.flow - newmin;
  133.             if (newValue == 0)
  134.             {
  135.                 //*** Deleting edge from current->info to dest->info since the val is 0!
  136.                 gr->removeEdge(current->info, dest->info);
  137.             }
  138.             else
  139.                 current->updateEdgeFlow(i, edg.flow - newmin);
  140.  
  141.             //Check to see if this edge already exists
  142.             bool exists = false;
  143.             unsigned int k = 0;
  144.             for (k = 0; k < dest->adj.size(); k++)
  145.             {
  146.                 if (dest->adj[k].dest->info == current->info)
  147.                 {
  148.                     exists = true;
  149.                     break;
  150.                 }
  151.             }
  152.  
  153.             if (!exists)
  154.             {
  155.                 gr->addEdge(dest->info, current->info, edg.weight, newmin);
  156.                 //*** Edge from dest->info to current->info does not exist in Gr... Adding with newmin
  157.             }
  158.             else
  159.             {
  160.                 float newValue = dest->adj[k].flow + newmin;
  161.                 dest->updateEdgeFlow(k, newValue);
  162.                 //*** Edge from dest->info to current->info already exists in Gr... Updating to: newValue
  163.             }
  164.  
  165.             //Search for the correct edge on Gf since both graphs may be unsynchronized
  166.             Vertex<T>* updatedVertex = gf->getVertex(current->info);
  167.             for (k = 0; k < updatedVertex->adj.size(); k++)
  168.             {
  169.                 if (updatedVertex->adj[k].dest->info == dest->info)
  170.                     break;
  171.             }
  172.             if (k < updatedVertex->adj.size())
  173.             {
  174.                 float oldValue = updatedVertex->adj[k].flow;
  175.  
  176.                 updatedVertex->updateEdgeFlow(k, oldValue + newmin);
  177.                 Edge<T> updatedEdge = updatedVertex->adj[k];
  178.  
  179.                 //*** Setting k new edge flow value to the final graph from updatedVertex->info to updatedEdge.dest->info with k flow value of updatedEdge.flow
  180.             }
  181.             else {
  182.                 //*** Didn't find the edge on Gf. Probably this was a REVERSE EDGE created in Gr. Find corresponding in Gf...
  183.                 Vertex<T>* reverseVertex = gf->getVertex(dest->info);
  184.                 for (k = 0; k < reverseVertex->adj.size(); k++)
  185.                 {
  186.                     if (reverseVertex->adj[k].dest->info == current->info)
  187.                         break;
  188.                 }
  189.                 if (k < reverseVertex->adj.size())
  190.                 {
  191.                     float oldValue = reverseVertex->adj[k].flow;
  192.  
  193.                     reverseVertex->updateEdgeFlow(k, oldValue - newmin);
  194.                     Edge<T> reverseEdge = reverseVertex->adj[k];
  195.  
  196.                     //*** ADJUSTING edge flow value to the final graph from reverseVertex->info to reverseEdge.dest->info with k flow value of reverseEdge.flow
  197.                 }
  198.  
  199.             }
  200.  
  201.             //*** Returning new minimum value of newmin after recursion!
  202.             return newmin;
  203.         }
  204.  
  205.     }
  206.  
  207.     return -1;
  208. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement