Given a permutation P = p1, p2, …., pn of first n natural numbers (1 ≤ n ≤ 10). One can swap any two consecutive elements pi and pi + 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.
Input: P = “213”, P' = “321”
213 <-> 231 <-> 321
Input: P = “1234”, P' = “4123”
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, visitedstart = visitedfinish = true.
- Let srcu be the root of vertex u in BFS progression. So, srcstart = start and srcfinish = finish.
- Let Du be the shortest distance from vertex u to it’s tree’s root. So Dstart = Dfinish = 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 (visitedv = false) into queue’s back, then let Dv = Du + 1, srcv = srcu and visitedv = true. Especially, if v was visited and srcv != srcu then we can immediately return Du + Dv + 1.
Below is the implementation of the above approach:
- Meet in the middle
- Shortest path from source to destination such that edge weights along path are alternatively increasing and decreasing
- Shortest path on a Square
- Multistage Graph (Shortest Path)
- Shortest path in a Binary Maze
- Dijkstra’s shortest path algorithm using set in STL
- Some interesting shortest path questions | Set 1
- Shortest path in an unweighted graph
- Shortest Path Faster Algorithm
- Dijkstra's shortest path with minimum edges
- Dijkstra's Shortest Path Algorithm using priority_queue of STL
- 0-1 BFS (Shortest Path in a Binary Weight Graph)
- Shortest Path in Directed Acyclic Graph
- Multi Source Shortest Path in Unweighted Graph
- Shortest path with exactly k edges in a directed and weighted graph | Set 2
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.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.
Improved By : sanjeev2552