0

I am not sure whether this is an appropriate question to post on this platform.

The problem statement is present here: https://www.interviewbit.com/problems/permutation-swaps/

Other than that, here's a short summary of this graph-based question. Problem statement with an example:
let N = 4
given permutation A for numbers 1 to 4,
A = [1, 3, 2, 4]
and given,
B = [1, 4, 2, 3]
other than that we are given pairs of positions,
C = [[2, 4]]
We have to find out, whether we can convert permutation A to permutation B if only numbers at pairs of positions(good pairs) mentioned in C can be swapped.
Here, in the above example for pair (2, 4) in C, the number at position 2 in A is = 3 and the number at position 4 is 4. if we, swap the numbers at both these positions, we get [1, 4, 2, 3] which is equal to B. Hence, it is possible.

Olivier
  • 13,283
  • 1
  • 8
  • 24
  • 2
    Hi! Welcome to StackOverflow. I understand the problem statement, but what is your question about it? – Stef Sep 09 '21 at 14:57
  • The question is tagged with [breadth-first-search] which is the right algorism to use. What is your question about it ? What did you try ? – c0der Sep 11 '21 at 03:54
  • Please for future searches, display the problem (keep the link if you wish eventhough I won't recommend it), and show what you tried so far. – Gar Sep 15 '21 at 11:53
  • Please provide enough code so others can better understand or reproduce the problem. – Community Sep 17 '21 at 07:11

2 Answers2

0

Your really need to ask an actual question!

Here is a simple algorithm to strt your thinking about what you want.

Add A to L, a list of reachable permutations
Loop P over permutations in L
   Loop S over C
      Apply C to P, giving R
          If R == B
              **DONE**  TRUE
          If R not in L
              Add R to L 
**DONE** FALSE
ravenspoint
  • 19,093
  • 6
  • 57
  • 103
0

Algorithm: Create a graph whose nodes are the positions 1..n, and with an edge between two positions if and only if you can swap them.

For every position where you have an unwanted number, if the number you want can't be reached through the graph, then no permutation can work and you should return 0.

If every position has its wanted number reachable, then return 1.

But why is it sufficient to have every wanted number reachable?

To start, construct a maximal spanning forest in the graph according to the following pseudo-code:

foreach node in 1..10:
    if node not in any tree:
        construct maximal tree by breadth-first search
        if tree has > 1 element:
            add tree to list of trees in our forest

And now we construct our permutation as follows:

while the forest is non-empty:
   pick a tree in the forest:
       find a leaf in the tree:
           With a chain of swaps, get the right value into that node
           remove the leaf from the tree
           if the tree has only 1 element left, remove the tree from the forest

I will omit the proof that this always produces a permutation that gets to the desired end result. But it does.

btilly
  • 43,296
  • 3
  • 59
  • 88