Saturday, June 18, 2022
HomeWordPress DevelopmentTravelling Salesman Downside (TSP) utilizing Decreased Matrix Methodology

Travelling Salesman Downside (TSP) utilizing Decreased Matrix Methodology


  

#embrace <bits/stdc++.h>

utilizing namespace std;

  

#outline N 4

#outline INF INT_MAX

  

struct Node {

      

    

    

    

    vector<pair<int, int> > path;

  

    

    int reducedMatrix[N][N];

  

    

    int price;

  

    

    int vertex;

    

    

    int degree;

};

  

Node* newNode(int parentMatrix[N][N],

              vector<pair<int, int> > const& path,

              int degree, int i, int j)

{

    Node* node = new Node;

      

    

    node->path = path;

  

    

    if (degree != 0) {

          

        

        node->path.push_back(make_pair(i, j));

    }

  

    

    memcpy(node->reducedMatrix, parentMatrix,

           sizeof node->reducedMatrix);

  

    

    

    for (int ok = 0; degree != 0 && ok < N; ok++) {

          

        

        node->reducedMatrix[i][k] = INF;

          

        

        node->reducedMatrix[k][j] = INF;

    }

  

    

    

    node->reducedMatrix[j][0] = INF;

  

    

    node->degree = degree;

  

    

    node->vertex = j;

  

    

    return node;

}

  

int rowReduction(int reducedMatrix[N][N], 

                 int row[N])

{

    

    fill_n(row, N, INF);

  

    

    for (int i = 0; i < N; i++) {

        for (int j = 0; j < N; j++) {

            if (reducedMatrix[i][j] < row[i]) {

                row[i] = reducedMatrix[i][j];

            }

        }

    }

  

    

    

    for (int i = 0; i < N; i++) {

        for (int j = 0; j < N; j++) {

            if (reducedMatrix[i][j] != INF

                && row[i] != INF) {

                reducedMatrix[i][j] -= row[i];

            }

        }

    }

    return 0;

}

  

int columnReduction(int reducedMatrix[N][N], 

                    int col[N])

{

    

    fill_n(col, N, INF);

  

    

    for (int i = 0; i < N; i++) {

        for (int j = 0; j < N; j++) {

            if (reducedMatrix[i][j] < col[j]) {

                col[j] = reducedMatrix[i][j];

            }

        }

    }

    

    

    for (int i = 0; i < N; i++) {

        for (int j = 0; j < N; j++) {

            if (reducedMatrix[i][j] != INF

                && col[j] != INF) {

                reducedMatrix[i][j] -= col[j];

            }

        }

    }

    return 0;

}

  

int calculateCost(int reducedMatrix[N][N])

{

    

    int price = 0;

  

    

    int row[N];

    rowReduction(reducedMatrix, row);

  

    

    int col[N];

    columnReduction(reducedMatrix, col);

  

    

    

    for (int i = 0; i < N; i++) {

        price += (row[i] != INT_MAX) ? row[i] : 0,

            price += (col[i] != INT_MAX) ? col[i] : 0;

    }

    return price;

}

  

void TSPPAthPrint(vector<pair<int, int> > const& listing)

{

    for (int i = 0; i < listing.dimension(); i++) {

        cout << listing[i].first + 1 << " -> "

             << listing[i].second + 1 << "n";

    }

}

  

struct Min_Heap {

    bool operator()(const Node* lhs, const Node* rhs) const

    {

        return lhs->price > rhs->price;

    }

};

  

int clear up(int CostGraphMatrix[N][N])

{

    

    

    priority_queue<Node*, vector<Node*>, Min_Heap> pq;

    vector<pair<int, int> > v;

  

    

    

    Node* root = newNode(CostGraphMatrix, v, 0, -1, 0);

  

    

    

    root->price = calculateCost(root->reducedMatrix);

  

    

    pq.push(root);

  

    

    

    

    whereas (!pq.empty()) {

          

        

        

        Node* min = pq.prime();

  

        

        

        pq.pop();

  

        

        int i = min->vertex;

          

        

        if (min->degree == N - 1) {

              

            

            min->path.push_back(make_pair(i, 0));

              

            

            TSPPAthPrint(min->path);

              

            

            return min->price;

        }

  

        

        

        for (int j = 0; j < N; j++) {

            if (min->reducedMatrix[i][j] != INF) {

                  

                

                

                Node* youngster

                    = newNode(min->reducedMatrix, min->path,

                              min->degree + 1, i, j);

  

                child->price

                    = min->price + min->reducedMatrix[i][j]

                      + calculateCost(child->reducedMatrix);

                  

                

                pq.push(youngster);

            }

        }

        

        

        

        

        delete min;

    }

    return 0;

}

  

int principal()

{

    int CostGraphMatrix[N][N] = { { INF, 10, 15, 20 },

                                  { 10, INF, 35, 25 },

                                  { 15, 35, INF, 30 },

                                  { 20, 25, 30, INF } };

  

    

    cout << "Whole price is " << clear up(CostGraphMatrix);

    return 0;

}

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisment -
Google search engine

Most Popular

Recent Comments