Sunday, June 26, 2022
HomeSoftware DevelopmentTravelling Salesman Downside (TSP) utilizing Lowered Matrix Technique

Travelling Salesman Downside (TSP) utilizing Lowered Matrix Technique


  

#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 value;

  

    

    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 okay = 0; degree != 0 && okay < N; okay++) {

          

        

        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 value = 0;

  

    

    int row[N];

    rowReduction(reducedMatrix, row);

  

    

    int col[N];

    columnReduction(reducedMatrix, col);

  

    

    

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

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

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

    }

    return value;

}

  

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

{

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

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

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

    }

}

  

struct Min_Heap {

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

    {

        return lhs->value > rhs->value;

    }

};

  

int resolve(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->value = calculateCost(root->reducedMatrix);

  

    

    pq.push(root);

  

    

    

    

    whereas (!pq.empty()) {

          

        

        

        Node* min = pq.high();

  

        

        

        pq.pop();

  

        

        int i = min->vertex;

          

        

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

              

            

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

              

            

            TSPPAthPrint(min->path);

              

            

            return min->value;

        }

  

        

        

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

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

                  

                

                

                Node* little one

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

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

  

                child->value

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

                      + calculateCost(child->reducedMatrix);

                  

                

                pq.push(little one);

            }

        }

        

        

        

        

        delete min;

    }

    return 0;

}

  

int essential()

{

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

                                  { 10, INF, 35, 25 },

                                  { 15, 35, INF, 30 },

                                  { 20, 25, 30, INF } };

  

    

    cout << "Whole value is " << resolve(CostGraphMatrix);

    return 0;

}

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments