Matrix_code

graph/math - System of difference Constraints

Jun 27th, 2016 (edited)
142
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.63 KB | None | 0 0
  1. /*
  2.  
  3.         System of difference Constraints
  4. * Given Some inequality on some variable (Xi , Xj , …. ) in form Xj – Xi <= W
  5. * We need to determine whether we can assign values to variables so that all the given inequality is satisfiable or not ? If satisfiable, then output A solution.
  6.  
  7. Solution:
  8. Building Constraints graph:
  9. 1. For each variable we create a vertex.
  10. 2. For each in-equality, Xj – Xi <= W , We give an edge (Vi,Vj) with cost W
  11. 3. Create a source vertex (S) and give an edge (S,Vi) with cost = 0
  12.  
  13. * If the graph contains any negative cycle, then there is no solution. Otherwise, We have a solution for each variable and
  14. for each variable (Xi):
  15.     Xi = Shortest Path distance of Vi from the Source vertex in Constraint Graph.
  16. Shortest Path can be calculated from Bellman-Ford algorithm.
  17. Other Results from The constraint Graph:
  18. # Bellman-Ford maximizes x1 + x2 + …. + xn subject to the constraints xj – xi ≤ wij and xi ≤ 0
  19. # Bellman-Ford also minimizes maximum{xi} – minimum {xi}
  20.  
  21. Example Problem:
  22. x1 - x2 <= 0
  23. x1 - x5 <= -1
  24. x2 - x5 <= 1
  25. x3 - x1 <= 5
  26. x4 - x1 <= 4
  27. x4 - x3 <= -1
  28. x5 - x3 <= -3
  29. x5 - x4 <= -3
  30.  
  31. Using BellMen-Ford Algorithm:
  32. x = {5,  3, 0,  1,  4}
  33.  
  34. */
  35.  
  36. #include <bits/stdc++.h>
  37. using namespace std;
  38. int n = 5;  // Number of Variables.
  39.  
  40. vector<pair<int,int > >G[505];
  41. int d[505];
  42. bool inq[505];
  43. int Times[505];
  44. bool spfa()
  45. {
  46.     queue<int>q;
  47.     for(int i = 1;i <= n; i ++ ) {
  48.         d[i]= 0;
  49.         q.push(i);
  50.         inq[i] = 1;
  51.         Times[i] = 1;
  52.     }
  53.    
  54.     while(!q.empty()){
  55.         int u = q.front(); q.pop();
  56.         inq[u] = 0;
  57.         for(int i = 0; i < G[u].size(); i ++ ) {
  58.             int v = G[u][i].first, w = G[u][i].second;
  59.             if(d[v] > d[u] + w) {
  60.                 d[v] = d[u]  + w;
  61.                 if(inq[v] == 0) {
  62.                     inq[v] = 1;
  63.                     q.push(v);
  64.                     Times[v] ++;
  65.                    
  66.                     if(Times[v] > n+2 ) return 0;
  67.                 }
  68.                
  69.             }
  70.         }
  71.     }
  72.     return 1;
  73. }
  74. int main()
  75. {
  76.     G[2].push_back(make_pair(1,0)); // x1-x2<=0
  77.     G[5].push_back(make_pair(1,-1));//..
  78.     G[5].push_back(make_pair(2,1));
  79.     G[1].push_back(make_pair(3,5));
  80.     G[1].push_back(make_pair(4,4));
  81.     G[3].push_back(make_pair(4,-1));
  82.     G[3].push_back(make_pair(5,-3));
  83.     G[4].push_back(make_pair(5,-3));
  84.     bool flag = spfa();
  85.     if(flag) {
  86.         for(int i = 1; i<= n;i ++) {
  87.             printf("X%d = %d\n",i,d[i]);
  88.         }
  89.     }
  90.     else printf("No Solution\n");
  91.     /* Expected Output
  92.     X1 = -5
  93.     X2 = -3
  94.     X3 = 0
  95.     X4 = -1
  96.     X5 = -4
  97.     */
  98. }
Add Comment
Please, Sign In to add comment