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 =

**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-ordinates` `class` `pairs {` `public` `:` ` ` `int` `x;` ` ` `int` `y;` `};` ` ` `// Function to calculate the ` `// manhattan distance between ` `// pair of points` `int` `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 point` `int` `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 intialize the input` `// and find the minimum distance ` `// by visiting every cordinate` `void` `findMinDistance()` `{` ` ` `int` `noOfcustomer = 1;` ` ` `vector<pairs> cordinate;` ` ` `vector<` `int` `> nodes;` ` ` `// filling the coordinates into vector` ` ` `pairs office, home, customer;` ` ` `office.x = 0;` ` ` `office.y = 0;` ` ` `cordinate.push_back(office);` ` ` `home.x = 5;` ` ` `home.y = 5;` ` ` `cordinate.push_back(home);` ` ` `customer.x = 1;` ` ` `customer.y = 1;` ` ` `cordinate.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(` ` ` `cordinate[i], cordinate[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 Code` `int` `main()` `{` ` ` `// Function Call` ` ` `findMinDistance();` ` ` `return` `0;` `}` |

**Output:**

10

**Performance Analysis:**

**Time Complexity:***O(N! * N)***Auxiliary Space:***O(N*^{2})

*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.*