Advertisement
sleepy_coder

Bellman Ford queue based implementation c++

Nov 17th, 2018
132
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 9.20 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3.  
  4.  
  5. #define NULL_VALUE -999999
  6. #define SUCCESS_VALUE 999999
  7. ///DONE SUCCESFULLY
  8.  
  9. class ListNode//determines that it is a pure singly linked list
  10. {
  11. public:
  12.     int item;
  13.     ListNode * next;
  14. };
  15.  
  16.  
  17. class LinkedListWithTail
  18. {
  19.  
  20.     ListNode * list;
  21.     ListNode * tail;
  22.     int length;
  23.  
  24. public:
  25.     LinkedListWithTail()
  26.     {
  27.         list = NULL;  //initially set to NULL
  28.         tail = NULL;
  29.         length = 0;
  30.     }
  31.  
  32.     int getLength()
  33.     {
  34.         return length;
  35.     }
  36.  
  37.     ///add required codes to set up tail pointer
  38.     int insertItem(int item)  //insert at the beginning
  39.     {///task: 6
  40.         ListNode * newNode ;
  41.         newNode = new ListNode() ;
  42.         newNode->item = item ;
  43.         if(list == 0){//initially empty list
  44.             newNode->next = list;
  45.             list = tail = newNode;
  46.         }
  47.         else{//non empty list
  48.             newNode->next = list;
  49.             list = newNode;
  50.             ///no need to change the tail
  51.         }
  52.         length++;
  53.         return SUCCESS_VALUE;
  54.     }
  55.  
  56.     //add required codes to set up tail pointer
  57.     int deleteItem(int item)///task  - 06(connect the tail pointer properly)
  58.     {
  59.         ListNode *temp, *prev ;
  60.         temp = list ; //start at the beginning
  61.         while (temp != 0)
  62.         {
  63.             if (temp->item == item) break ;
  64.             prev = temp;
  65.             temp = temp->next ; //move to next node
  66.         }
  67.         if (temp == 0) return NULL_VALUE ; //item not found to delete
  68.         if (temp == list) //delete the first node
  69.         {
  70.             list = list->next ;
  71.             delete temp ;
  72.             length--;
  73.         }
  74.         else//found in middle or last position
  75.         {
  76.             prev->next = temp->next ;
  77.             delete temp;
  78.             length--;
  79.         }
  80.         ///here list maybe occurs as NULL
  81.         tail = list;
  82.         while(tail != NULL && tail->next != 0){//tail will consist of at least one node
  83.             tail = tail->next;
  84.         }
  85.         return SUCCESS_VALUE ;
  86.     }
  87.  
  88.     //add required codes to set up tail pointer
  89.     ListNode * searchItem(int item)
  90.     {
  91.         ListNode * temp ;
  92.         temp = list ; //start at the beginning
  93.         while (temp != 0)
  94.         {
  95.             if (temp->item == item) return temp ;
  96.             temp = temp->next ; //move to next node
  97.         }
  98.         return 0 ; //0 means invalid pointer in C, also called NULL value in C
  99.     }
  100.  
  101.     void printList()
  102.     {
  103.         ListNode * temp;
  104.         temp = list;
  105.         while(temp!=0)
  106.         {
  107.             printf("%d->", temp->item);
  108.             temp = temp->next;
  109.         }
  110.         printf("\n");
  111.         printf("Length: %d\n",getLength());
  112.     }
  113.  
  114.     //------------write code for the functions below-----------
  115.     int insertLast(int item)///task - 07 (making efficient using tail
  116.     {
  117.         //write your codes here
  118.         //effective code here .....bro
  119.         ListNode * lastNode = new ListNode();
  120.         lastNode->item = item;
  121.         lastNode->next = NULL;
  122.  
  123.         if(tail == 0)//also head == 0 so..
  124.         {
  125.             list = tail = lastNode;
  126.             length++;
  127.             return SUCCESS_VALUE;
  128.         }
  129.         else{
  130.             tail->next = lastNode;
  131.             tail = tail->next;
  132.             length++;
  133.             return SUCCESS_VALUE;
  134.         }
  135.     }
  136.  
  137.     ListNode * getItemAt(int pos)///task - 08
  138.     {
  139.          //write your codes here
  140.                  //write your codes here
  141.          if(pos > getLength() || pos < 1)return 0;
  142.  
  143.          ListNode* temp = list;
  144.          for(int i = 1;i<pos;i++){//need to iterate just one less then position
  145.             temp = temp->next;
  146.          }
  147.          return temp;
  148.  
  149.     }
  150.  
  151.     int deleteLast()///task - 09
  152.     {
  153.         //write your codes here
  154.         if(list == 0 && tail == 0)return NULL_VALUE;
  155.         else if(list == tail){//only one node
  156.             length--;
  157.             free(list);
  158.             list = tail = 0;
  159.         }
  160.         else{//at least two node
  161.             ListNode * temp = list;
  162.             while(temp -> next != tail){//traversing to beyond last node
  163.                 temp = temp->next;
  164.             }
  165.             temp->next = NULL;
  166.             free(tail);
  167.  
  168.             length--;
  169.  
  170.             tail = temp;
  171.         }
  172.         return SUCCESS_VALUE;
  173.     }
  174.  
  175.     //destructor
  176.     ~LinkedListWithTail()//this is for no reason bwt needed here
  177.     {
  178.         //write your codes here
  179.         ListNode* destructor = list;
  180.  
  181.         while(list != NULL){
  182.             --length;
  183.             list = list->next;
  184.             free(destructor);
  185.             destructor = list;
  186.         }
  187.         list = tail = 0;
  188.         //printf("\nList has been destructed\n");
  189.     }
  190. };
  191.  
  192.  
  193.  
  194.  
  195.  
  196.  
  197.  
  198. class Queue
  199. {
  200.     LinkedListWithTail ll;
  201. public:
  202.     Queue(){}
  203.  
  204.     void push(int item)
  205.     {
  206.         //write your codes here
  207.         ll.insertLast(item);
  208.     }
  209.  
  210.     int pop()
  211.     {
  212.        //write your codes here
  213.        if(ll.getLength() > 0){
  214.             int value = (ll.getItemAt(1))->item;
  215.             ll.deleteItem(value);
  216.             return value;
  217.         }
  218.         else{
  219.             puts("Queue UnderFlow");
  220.             return NULL_VALUE;
  221.         }
  222.     }
  223.  
  224.     int front(){
  225.         //write your codes here
  226.        if(ll.getLength() > 0){
  227.             int value = (ll.getItemAt(1))->item;
  228.             ///ll.deleteItem(value);
  229.             return value;
  230.         }
  231.         else{
  232.             puts("Queue UnderFlow");
  233.             return NULL_VALUE;
  234.         }
  235.     }
  236.    
  237.     bool empty(){
  238.         return ll.getLength() == 0;
  239.     }
  240. };
  241.  
  242.  
  243.  
  244.  
  245.  
  246.  
  247.  
  248.  
  249. //___________________________________________Adjacency List Part____________________________________
  250. int indx_1, indx_2;
  251. int lastIndx_1[1000], lastIndx_2[1000];
  252.  
  253. struct AdjList{
  254.     int from, to, weight, prevIndx;
  255. }adjList_1[1000], adjList_2[1000];
  256.  
  257. void addEdge_1(int& u, int& v, int& w){
  258.     if(indx_1==0){
  259.         memset(lastIndx_1, -1, sizeof(lastIndx_1));
  260.     }
  261.     adjList_1[indx_1].from = u, adjList_1[indx_1].to = v, adjList_1[indx_1].weight = w, adjList_1[indx_1].prevIndx = lastIndx_1[u];
  262.     lastIndx_1[u] = indx_1++;
  263. }
  264.  
  265. void addEdge_2(int& u, int& v, int& w){
  266.     if(indx_2==0){
  267.         memset(lastIndx_2, -1, sizeof(lastIndx_2));
  268.     }
  269.     adjList_2[indx_2].from = u, adjList_2[indx_2].to = v, adjList_2[indx_2].weight = w, adjList_2[indx_2].prevIndx = lastIndx_2[u];
  270.     lastIndx_2[u] = indx_2++;
  271. }
  272. //___________________________________________________________________________________________________
  273.  
  274.  
  275. #define INF (1<<30)
  276.  
  277. int nodes, edges;
  278. int source, destination;
  279. int cycle = -33;
  280. int dist[1000];
  281. int pred[1000];
  282. bool inQueue[1000];
  283. int pushCounter[1000];
  284. bool isCycleNeg = false;
  285.  
  286.  
  287. int bellmanFord(int source, int destination){
  288.  
  289.     Queue queue1;
  290.     queue1.push(source);
  291.     inQueue[source] = true;//1
  292.     dist[source] = 0;//2
  293.     pushCounter[source]++;//3
  294.     pred[source] = -1;
  295.  
  296.     while(!queue1.empty() && !isCycleNeg){
  297.         int u = queue1.front();
  298.         queue1.pop();
  299.         inQueue[u] = false;
  300.  
  301.         for(int e = lastIndx_1[u] ; e != -1 ; e = adjList_1[e].prevIndx){
  302.             int update = dist[u] + adjList_1[e].weight;
  303.             int v = adjList_1[e].to;
  304.  
  305.             if(dist[v] > update) {
  306.  
  307.                 dist[v] = dist[u] + adjList_1[e].weight;
  308.                 pred[v] = u;
  309.  
  310.                 if (!inQueue[v]) {
  311.                     queue1.push(v);
  312.                     inQueue[v] = true;
  313.                     if(++pushCounter[v] > nodes-1){
  314.                         isCycleNeg = true;
  315.                         ///return cycle;
  316.                     }
  317.                 }
  318.  
  319.             }
  320.         }
  321.     }
  322.  
  323.     return dist[destination];
  324. }
  325.  
  326.  
  327. void printPaht(int source, int destination){
  328.     if(source == destination){
  329.         cout<<destination<<" ";
  330.     }
  331.     else{
  332.         printPaht(source, pred[destination]);
  333.         cout<<destination<<" ";
  334.     }
  335.  
  336. }
  337.  
  338.  
  339. void input(){
  340.     cout<<"INPUT: "<<endl;
  341.     cin>>nodes>>edges;
  342.     for(int i = 1; i <= edges; i++){
  343.         int u, v, w;
  344.         cin>>u>>v>>w;
  345.         addEdge_1(u, v, w);
  346.     }
  347.     //write your codes here...
  348.  
  349.     cin>>source>>destination;
  350. }
  351.  
  352. void output(){
  353.     cout<<"OUTPUT: "<<endl;
  354.     if(isCycleNeg){
  355.         cout<<"Negative Cycle Detected"<<endl;
  356.         /*//choose graph vertex started with index 1
  357.         for(int i = 1; i <= nodes; i++){
  358.             cout<<"{ node: "<<i<<", push counter: "<<pushCounter[i]<<", dist: "<<dist[i]<<" }"<<endl;
  359.         }*/
  360.     }
  361.     else{
  362.         //choose graph vertex started with index 1
  363.         for(int i = 1; i <= nodes; i++){
  364.             cout<<"{ node: "<<i<<", push counter: "<<pushCounter[i]<<", dist: "<<dist[i]<<" }"<<endl;
  365.         }
  366.         printPaht(source, destination);
  367.     }
  368. }
  369.  
  370.  
  371.  
  372.  
  373.  
  374. int main(){
  375.     //freopen("input.txt", "r", stdin);
  376.     //freopen("output.txt", "w", stdout);
  377.     //writes your code here.............
  378.  
  379.     input();
  380.  
  381.     isCycleNeg = false;
  382.  
  383.     for(int i = 0 ; i <= nodes ; i++){
  384.         dist[i] = INF;
  385.         inQueue[i] = false;
  386.         pushCounter[i] = 0;
  387.         pred[i] = -1;
  388.     }
  389.  
  390.     int res = bellmanFord(source, destination);
  391.  
  392.     output();
  393.  
  394.     return 0;
  395. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement