Generate lexicographically smallest Permutation of 1 to N where elements follow given relation
Given an integer N and an array arr[] of M pairs of type (Ai, Bi), the task is to generate the lexicographically smallest possible permutation of 1 to N such that every Ai, occurs before every Bi.
Examples:
Input: N = 4, arr[] = { {2, 1}, {3, 4}, {2, 4} }
Output: 2 1 3 4
Explanation: The following five permutations P satisfy the condition:
(2, 1, 3, 4), (2, 3, 1, 4), (2, 3, 4, 1), (3, 2, 1, 4), (3, 2, 4, 1).
The lexicographically smallest among them is (2, 1, 3, 4).Input: N = 2, arr[] = { {2, 1} }
Output: 2 1
Approach: The problem can be solved using the concept of Graph based on the following idea:
Consider a N vertex graph where there is an directed edge from A to B if A occurs before B in the permutation.
- So the target is to find a ordering such that any vertex occurs in the permutation after all its predecessors (the vertices which have edges directed to this one) have occurred in the permutation.
- While doing so, move from the least value to the maximum possible value for getting the lexicographically smallest permutation.
This can be done with the help of topological sorting.
- The topological sort will start from the vertices having indegree 0 and in sequence of smaller to greater value.
- After the topological sorting is over, if any vertex is still not included in the permutation, then no such permutation is possible.
Follow the below steps to solve the problem:
- Initialize one vector (say ans) to store the resulting permutation.
- Visit the M pairs and create directed edges from the first value of arr[i] to the second value of arr[i]. Also, increase the indegree of the second value of arr[i] by 1.
- Now find all the elements with indegree 0, push them in the resultant permutation in increasing order and start the topological sort.
- Add any vertex to the permutation when its indegree becomes 0.
- Maintain the lexicographic ordering while doing so, i.e. move from the lower value to the higher value.
- Take the help of min heap to perform the topological sorting:
- Initially keep the vertices with indegree 0 in the heap.
- Then pop the least value vertex, append it to the ans, remove all its edges and decrease the indegree by 1 of all the vertices connected to it.
- If the indegree of any vertex becomes 0 in this process, push that into the heap.
- Return ans as the final required permutation.
Below is the implementation of the above approach.
C++
// C++ program to implement above approach #include <bits/stdc++.h>using namespace std; // Function to obtain the permutationvector<int> Calculate(int n, int m, vector<pair<int, int> > arr){ // Prepare an empty array ans vector<int> ans; vector<int> indegree(n); vector<vector<int> > out(n); // Build the directed graph for (int i = 0; i < m; i++) { int a = arr[i].first; int b = arr[i].second; a--; b--; // Calculate indegree of every element indegree[b] = indegree[b] + 1; // Store connection of each node out[a].push_back(b); } // Declaring the min heap priority_queue<int, vector<int>, greater<int> > heap; for (int i = 0; i < n; ++i) { if (indegree[i] == 0) { // Push elements in priority queue // if indegree is 0 heap.push(i); } } // Run topological sort while (!heap.empty()) { // Choose vertex with degree 0, // denoted by i int i = heap.top(); heap.pop(); // Push i+1 to the tail of ans ans.push_back(i + 1); // Remove i and edges going out from i for (int j : out[i]) { indegree[j] -= 1; // In the process if any node's indegree // becomes 0 then push the element // to the priority queue if (indegree[j] == 0) { heap.push(j); } } } // If size of ans is not n then // output would is not possible if (ans.size() != n) { ans.clear(); ans.push_back(-1); } return ans;} // Driver codeint main(){ int N = 4, M = 3; vector<pair<int, int> > arr = { { 2, 1 }, { 3, 4 }, { 2, 4 } }; // Function call vector<int> res = Calculate(N, M, arr); for (int x : res) cout << x << " "; return 0;} |
2 1 3 4
Time Complexity: O(N)
Auxiliary Space: O(N)


