Given a permutation **P = p _{1}, p_{2}, …., p_{n}** of first

**n**natural numbers

**(1 ≤ n ≤ 10)**. One can swap any two consecutive elements

**p**and

_{i}**p**

_{i + 1}**(1 ≤ i < n)**. The task is to find the minimum number of swaps to change

**P**to another permutation

**P' = p'**.

_{1}, p’_{2}, …., p’_{n}**Examples:**

Input:P = “213”, P' = “321”

Output:2

213 <-> 231 <-> 321

Input:P = “1234”, P' = “4123”

Output:3

**Approach:** This problem can be solved using Dijkstra’s Shortest Path Algorithm. Seem like there is nothing related to a graph in the statement. But assume one permutation is one vertex, then every swap of a permutation’s elements is an edge which connects this vertex with another vertex. So finding the minimum number of swaps now becomes a simple BFS/shortest path problem.

Now let’s analyze time complexity. We have **n!** vertices, each vertex has **n – 1** adjacent vertices. We also have to store vertices visited state by map because their representations are hard to be stored by normal arrays. So total time complexity is **O(N log(N!) * N!)**. Meet In The Middle technique can be used to make the solution faster.

Meet In The Middle solution is similar to Dijkstra’s solution with some modifications.

- Let
**P**be the start vertex and**P'**be the finish Vertex. - Let both start and finish be roots. We start BFS from both the roots, start and finish at the same time but using only one queue.
- Push start and finish into queue’s back,
**visited**._{start}= visited_{finish}= true - Let
**src**be the root of vertex u in BFS progression. So,_{u}**src**._{start}= start and src_{finish}= finish - Let
**D**be the shortest distance from vertex_{u}**u**to it’s tree’s root. So**D**._{start}= D_{finish}= 0 - While queue is not empty, pop queue’s front which is vertex
**u**then push all vertices**v**which are adjacent with**u**and haven’t been visited yet**(visited**into queue’s back, then let_{v}= false)**D**,_{v}= D_{u}+ 1**src**and_{v}= src_{u}**visited**. Especially, if_{v}= true**v**was visited and**src**then we can immediately return_{v}!= src_{u}**D**._{u}+ D_{v}+ 1

Below is the implementation of the above approach:

## C++

`// C++ implementation of the approach ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Function to find minimum number of ` `// swaps to make another permutation ` `int` `ShortestPath(` `int` `n, string start, string finish) ` `{ ` ` ` `unordered_map<string, ` `bool` `> visited; ` ` ` `unordered_map<string, ` `int` `> D; ` ` ` `unordered_map<string, string> src; ` ` ` ` ` `visited[start] = visited[finish] = ` `true` `; ` ` ` `D[start] = D[finish] = 0; ` ` ` `src[start] = start; ` ` ` `src[finish] = finish; ` ` ` ` ` `queue<string> q; ` ` ` `q.push(start); ` ` ` `q.push(finish); ` ` ` ` ` `while` `(!q.empty()) { ` ` ` ` ` `// Take top vertex of the queue ` ` ` `string u = q.front(); ` ` ` `q.pop(); ` ` ` ` ` `// Generate n - 1 of it's permutations ` ` ` `for` `(` `int` `i = 1; i < n; i++) { ` ` ` ` ` `// Generate next permutation ` ` ` `string v = u; ` ` ` `swap(v[i], v[i - 1]); ` ` ` ` ` `// If v is not visited ` ` ` `if` `(!visited[v]) { ` ` ` ` ` `// Set it visited ` ` ` `visited[v] = ` `true` `; ` ` ` ` ` `// Make root of u and root of v equal ` ` ` `src[v] = src[u]; ` ` ` ` ` `// Increment it's distance by 1 ` ` ` `D[v] = D[u] + 1; ` ` ` ` ` `// Push this vertex into queue ` ` ` `q.push(v); ` ` ` `} ` ` ` ` ` `// If it is already visited ` ` ` `// and roots are different ` ` ` `// then answer is found ` ` ` `else` `if` `(src[u] != src[v]) ` ` ` `return` `D[u] + D[v] + 1; ` ` ` `} ` ` ` `} ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` `string p1 = ` `"1234"` `, p2 = ` `"4123"` `; ` ` ` `int` `n = p1.length(); ` ` ` `cout << ShortestPath(n, p1, p2); ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Python3

`# Python3 implementation of the approach ` `from` `collections ` `import` `deque, defaultdict ` ` ` `# Function to find minimum number of ` `# swaps to make another permutation ` `def` `shortestPath(n: ` `int` `, start: ` `str` `, finish: ` `str` `) ` `-` `> ` `int` `: ` ` ` `visited, D, src ` `=` `defaultdict(` `lambda` `: ` `False` `), defaultdict( ` ` ` `lambda` `: ` `0` `), defaultdict(` `lambda` `: '') ` ` ` `visited[start] ` `=` `visited[finish] ` `=` `True` ` ` `D[start] ` `=` `D[finish] ` `=` `0` ` ` `src[start], src[finish] ` `=` `start, finish ` ` ` ` ` `q ` `=` `deque() ` ` ` `q.append(start) ` ` ` `q.append(finish) ` ` ` ` ` `while` `q: ` ` ` ` ` `# Take top vertex of the queue ` ` ` `u ` `=` `q[` `0` `] ` ` ` `q.popleft() ` ` ` ` ` `# Generate n - 1 of it's permutations ` ` ` `for` `i ` `in` `range` `(n): ` ` ` `v ` `=` `list` `(u) ` ` ` `v[i], v[i ` `-` `1` `] ` `=` `v[i ` `-` `1` `], v[i] ` ` ` ` ` `v ` `=` `''.join(v) ` ` ` ` ` `if` `not` `visited[v]: ` ` ` ` ` `# Set it visited ` ` ` `visited[v] ` `=` `True` ` ` ` ` `# Make root of u and root of v equal ` ` ` `src[v] ` `=` `src[u] ` ` ` ` ` `# Increment it's distance by 1 ` ` ` `D[v] ` `=` `D[u] ` `+` `1` ` ` ` ` `# Push this vertex into queue ` ` ` `q.append(v) ` ` ` ` ` `# If it is already visited ` ` ` `# and roots are different ` ` ` `# then answer is found ` ` ` `elif` `u ` `in` `src ` `and` `src[u] !` `=` `src[v]: ` ` ` `return` `D[u] ` `+` `D[v] ` `+` `1` ` ` `# Driver Code ` `if` `__name__ ` `=` `=` `"__main__"` `: ` ` ` ` ` `p1 ` `=` `"1234"` ` ` `p2 ` `=` `"4123"` ` ` `n ` `=` `len` `(p1) ` ` ` `print` `(shortestPath(n, p1, p2)) ` ` ` `# This code is contributed by ` `# sanjeev2552 ` |

*chevron_right*

*filter_none*

## C#

`// C# implementation of ` `// the above approach ` `using` `System; ` `using` `System.Collections; ` `using` `System.Text; ` `using` `System.Collections.Generic; ` ` ` `class` `GFG{ ` ` ` `// Function to find minimum number of ` `// swaps to make another permutation ` `static` `int` `ShortestPath(` `int` `n, ` `string` `start, ` ` ` `string` `finish) ` `{ ` ` ` `Dictionary<` `string` `, ` ` ` `bool` `> visited = ` `new` `Dictionary<` `string` `, ` ` ` `bool` `>(); ` ` ` `Dictionary<` `string` `, ` ` ` `int` `> D = ` `new` `Dictionary<` `string` `, ` ` ` `int` `>(); ` ` ` `Dictionary<` `string` `, ` ` ` `string` `> src = ` `new` `Dictionary<` `string` `, ` ` ` `string` `>(); ` ` ` ` ` `visited[start] = ` `true` `; ` ` ` `visited[finish] = ` `true` `; ` ` ` ` ` `D[start] = 0; ` ` ` `D[finish] = 0; ` ` ` ` ` `src[start] = start; ` ` ` `src[finish] = finish; ` ` ` ` ` `Queue q = ` `new` `Queue(); ` ` ` `q.Enqueue(start); ` ` ` `q.Enqueue(finish); ` ` ` ` ` `while` `(q.Count != 0) ` ` ` `{ ` ` ` ` ` `// Take top vertex of the queue ` ` ` `string` `u = (` `string` `)q.Peek(); ` ` ` `q.Dequeue(); ` ` ` ` ` `// Generate n - 1 of it's permutations ` ` ` `for` `(` `int` `i = 1; i < n; i++) ` ` ` `{ ` ` ` ` ` `// Generate next permutation ` ` ` `StringBuilder tmp = ` `new` `StringBuilder(u); ` ` ` `char` `t = tmp[i]; ` ` ` `tmp[i] = tmp[i - 1]; ` ` ` `tmp[i - 1] = t; ` ` ` ` ` `string` `v = tmp.ToString(); ` ` ` ` ` `// If v is not visited ` ` ` `if` `(!visited.GetValueOrDefault(v, ` `false` `)) ` ` ` `{ ` ` ` ` ` `// Set it visited ` ` ` `visited[v] = ` `true` `; ` ` ` ` ` `// Make root of u and root of v equal ` ` ` `src[v] = src[u]; ` ` ` ` ` `// Increment it's distance by 1 ` ` ` `D[v] = D[u] + 1; ` ` ` ` ` `// Push this vertex into queue ` ` ` `q.Enqueue(v); ` ` ` `} ` ` ` ` ` `// If it is already visited ` ` ` `// and roots are different ` ` ` `// then answer is found ` ` ` `else` `if` `(src[u] != src[v]) ` ` ` `return` `D[u] + D[v] + 1; ` ` ` `} ` ` ` `} ` ` ` `return` `0; ` `} ` ` ` `// Driver Code ` `public` `static` `void` `Main(` `string` `[] args) ` `{ ` ` ` `string` `p1 = ` `"1234"` `, p2 = ` `"4123"` `; ` ` ` `int` `n = p1.Length; ` ` ` ` ` `Console.Write(ShortestPath(n, p1, p2)); ` `} ` `} ` ` ` `// This code is contributed by rutvik_56 ` |

*chevron_right*

*filter_none*

**Output:**

3

## Recommended Posts:

- Meet in the middle
- Shortest path from source to destination such that edge weights along path are alternatively increasing and decreasing
- Dijkstra’s shortest path algorithm using set in STL
- Dijkstra's Shortest Path Algorithm using priority_queue of STL
- Finding shortest path between any two nodes using Floyd Warshall Algorithm
- Building an undirected graph and finding shortest path using Dictionaries in Python
- Time taken by two persons to meet on a circular track
- Dijkstra's shortest path algorithm | Greedy Algo-7
- Shortest Path in Directed Acyclic Graph
- Some interesting shortest path questions | Set 1
- Shortest path with exactly k edges in a directed and weighted graph
- Shortest Path in a weighted Graph where weight of an edge is 1 or 2
- Printing Paths in Dijkstra's Shortest Path Algorithm
- Shortest path in a Binary Maze
- Print shortest path to print a string on screen
- 0-1 BFS (Shortest Path in a Binary Weight Graph)
- Multistage Graph (Shortest Path)
- Shortest Path Faster Algorithm
- Shortest path to reach one prime to other by changing single digit at a time
- Shortest path in an unweighted graph

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.