Sequence Reconstruction in C++


Suppose we have to check whether the original sequence org can be uniquely reconstructed from the sequences in seqs. The original sequence is a permutation of the integers from 1 to n, and n in range 1 ≤ n ≤ 10^4. Here the reconstruction means making a shortest common supersequence of the sequences in seqs. We have to check whether there is only one sequence that can be reconstructed from seqs and it is the original sequence.

So, if the input is like org = [1,2,3], seqs = [[1,2],[1,3]], then the output will be false, because [1,2,3] is not the only one sequence that can be reconstructed, because [1,3,2] is also a valid sequence that can be reconstructed.

To solve this, we will follow these steps −

  • Define a function ok(), this will take v1, v2,

  • if size of v1 is not equal to size of v2, then −

    • return false

  • for initialize i := 0, when i < size of v1, update (increase i by 1), do −

    • if v1[i] is not equal to v2[i], then −

      • return false

  • return true

  • From the main method do the following

  • n := size of original sequence

  • Define an array graph of size (n + 1)

  • Define one map indegree

  • idx := 0

  • for initialize i := 0, when i < size of seqs, update (increase i by 1), do −

    • if size of seqs[i] >= 1 and (seqs[i, 0] > n or seqs[i, 0] < 1), then −

      • return false

    • if size of seqs[i] >= 1 and not call count(seqs[i, 0]) of indegree, then −

      • indegree[seqs[i, 0]] := 0

    • for initialize j := 1, when j < size of seqs[i], update (increase j by 1), do −

      • u := seqs[i, j - 1]

      • v := seqs[i, j]

      • insert v at the end of graph[u]

      • (increase indegree[v] by 1)

      • if u > n or v > n or u < 1 or v < 1, then −

        • return false

  • Define one queue

  • for initialize i := 1, when i <= n, update (increase i by 1), do −

    • if i is in indegree and indegree[i] is same as 0, then −

      • insert i into q

  • while (not q is empty), do −

    • if size of q > 1, then −

      • return false

    • if idx is same as size of org, then −

      • return false

    • node := first element of q

    • delete element from q

    • if org[idx] is not equal to node, then −

      • return false

    • (increase idx by 1)

    • for initialize i := 0, when i < size of graph[node], update (increase i by 1), do −

      • v := graph[node, i]

      • (decrease indegree[v] by 1)

      • if indegree[v] is same as 0, then −

        • insert v into q

  • return true when idx is same as size of org

Example 

Let us see the following implementation to get better understanding −

 Live Demo

#include <bits/stdc++.h>
using namespace std;
class Solution {
public:
   bool ok(vector <int<& v1, vector <int<& v2){
      if (v1.size() != v2.size())
         return false;
      for (int i = 0; i < v1.size(); i++) {
         if (v1[i] != v2[i])
            return false;
      }
      return true;
   }
   bool sequenceReconstruction(vector<int<& org, vector<vector<int<>& seqs){
      int n = org.size();
      vector<int< graph[n + 1];
      unordered_map<int, int> indegree;
      int idx = 0;
      for (int i = 0; i < seqs.size(); i++) {
         if (seqs[i].size() >= 1 && (seqs[i][0] > n || seqs[i][0] < 1))
            return false;
         if (seqs[i].size() >= 1 && !indegree.count(seqs[i][0])) {
            indegree[seqs[i][0]] = 0;
         }
         for (int j = 1; j < seqs[i].size(); j++) {
            int u = seqs[i][j - 1];
            int v = seqs[i][j];
            graph[u].push_back(v);
            indegree[v]++;
            if (u > n || v > n || u < 1 || v < 1)
               return false;
         }
      }
      queue<int< q;
      for (int i = 1; i <= n; i++) {
         if (indegree.count(i) && indegree[i] == 0) {
            q.push(i);
         }
      }
      while (!q.empty()) {
         if (q.size() > 1) {
            return false;
         }
         if (idx == org.size()) {
            return false;
         }
         int node = q.front();
         q.pop();
         if (org[idx] != node) {
            return false;
         }
         idx++;
         for (int i = 0; i < graph[node].size(); i++) {
            int v = graph[node][i];
            indegree[v]--;
            if (indegree[v] == 0) {
               q.push(v);
            }
         }
      }
      return idx == org.size();
   }
};
main(){
   Solution ob;
   vector<int< v = {1,2,3};
   vector<vector<int<> v1 = {{1,2},{1,3}};
   cout << (ob.sequenceReconstruction(v, v1));
}

Input

{1,2,3}, {{1,2},{1,3}}

Output

0

Updated on: 19-Nov-2020

134 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements