Minimum Manhattan distance covered by visiting every coordinates from a source to a final vertex
Given an array arr[] of co-ordinate points and a source and final co-ordinate point, the task is to find the minimum manhattan distance covered from the source to the final vertex such that every point of the array is visited exactly once.
Manhattan Distance =
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more, please refer Complete Interview Preparation Course.
In case you wish to attend live classes with experts, please refer DSA Live Classes for Working Professionals and Competitive Programming Live for Students.
Examples:
Input: source = (0, 0), final = (100, 100)
arr[] = {(70, 40), (30, 10), (10, 5), (90, 70), (50, 20)}
Output: 200
Input: source = (0, 0), final = (5, 5)
arr[] = {(1, 1)}
Output: 10
Approach: The idea is to use permutation and combination to generate every possible permutation movements to the co-ordinates and then compute the total manhattan distance covered by moving from the first co-ordinate of the array to the final co-ordinate and If the final distance covered is less than the minimum distance covered till now. Then update the minimum distance covered.
Below is the implementation of the above approach:
C++
// C++ implementation to find the// minimum manhattan distance// covered by visiting N co-ordinates#include <bits/stdc++.h>using namespace std;// Class of co-ordinatesclass pairs {public: int x; int y;};// Function to calculate the// manhattan distance between// pair of pointsint calculate_distance(pairs a, pairs b){ return abs(a.x - b.x) + abs(a.y - b.y);}// Function to find the minimum// distance covered for visiting// every co-ordinate pointint findMinDistanceUtil(vector<int> nodes, int noOfcustomer, int** matrix){ int mindistance = INT_MAX; // Loop to compute the distance // for every possible permutation do { int distance = 0; int prev = 1; // Computing every total manhattan // distance covered for the every // co-ordinate points for (int i = 0; i < noOfcustomer; i++) { distance = distance + matrix[prev][nodes[i]]; prev = nodes[i]; } // Adding the final distance distance = distance + matrix[prev][0]; // if distance is less than // minimum value than update it if (distance < mindistance) mindistance = distance; }while ( next_permutation( nodes.begin(), nodes.end() )); return mindistance;}// Function to initialize the input// and find the minimum distance// by visiting every coordinatevoid findMinDistance(){ int noOfcustomer = 1; vector<pairs> coordinate; vector<int> nodes; // filling the coordinates into vector pairs office, home, customer; office.x = 0; office.y = 0; coordinate.push_back(office); home.x = 5; home.y = 5; coordinate.push_back(home); customer.x = 1; customer.y = 1; coordinate.push_back(customer); // make a 2d matrix which stores // distance between two point int** matrix = new int*[noOfcustomer + 2]; // Loop to compute the distance between // every pair of points in the co-ordinate for (int i = 0; i < noOfcustomer + 2; i++) { matrix[i] = new int[noOfcustomer + 2]; for (int j = 0; j < noOfcustomer + 2; j++) { matrix[i][j] = calculate_distance( coordinate[i], coordinate[j]); } // Condition to not move the // index of the source or // the final vertex if (i != 0 && i != 1) nodes.push_back(i); } cout << findMinDistanceUtil( nodes, noOfcustomer, matrix);}// Driver Codeint main(){ // Function Call findMinDistance(); return 0;} |
10
Performance Analysis:
- Time Complexity: O(N! * N)
- Auxiliary Space: O(N2)

