Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6838969
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T23:39:43+00:00 2026-05-26T23:39:43+00:00

I am studying queuing theory in which I am frequently presented with the following

  • 0

I am studying queuing theory in which I am frequently presented with the following situation.

Let x, y both be n-tuples of nonnegative integers (depicting lengths of the n queues). In addition, x and y each have distinguished queue called their “prime queue”. For example,

x = [3, 6, 1, 9, 5, 2] with x’ = 1

y = [6, 1, 5, 9, 5, 5] with y’ = 5

(In accordance with Python terminology I am counting the queues 0-5.)

How can I implement/construct the following permutation f on {0,1,…,5} efficiently?

  1. first set f(x’) = y’. So here f(1) = 5.
  2. then set f(i) = i for any i such that x[i] == y[i]. Clearly there is no need to consider the indices x’ and y’. So here f(3) = 3 (both length 9) and f(4) = 4 (both length 5).
  3. there are now equally sized sets of queues unpaired in x and in y. So here in x this is {0,2,5} and in y this is {0,1,2}.
  4. rank these from from 1 to s, where s is the common size of the sets, by length with 1 == lowest rank == shortest queue and s == highest rank == longest queue. So here, s = 3, and in x rank(0) = 1, rank(2) = 3 and rank(5) = 2, and in y rank(0) = 1, rank(1) = 3, rank(2) = 2. If there is a tie, give the queue with the larger index the higher rank.
  5. pair these s queues off by rank. So here f(0) = 0, f(2) = 1, f(5) = 2.

This should give the permutation [0, 5, 1, 3, 4, 2].

My solution consists of tracking the indices and loops over x and y multiple times, and is terribly inefficient. (Roughly looking at n >= 1,000,000 in my application.)

Any help would be most appreciated.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T23:39:44+00:00Added an answer on May 26, 2026 at 11:39 pm

    Since you must do the ranking, you can’t get linear and will need to sort. So it looks pretty straightforward. You do 1. in O(1) and 2. in O(n) by just going over the n-tuples. At the same time, you can construct the copy of x and y with only those that are left for 3. but do not include only the value, but instead use tuple of value and its index in the original.

    In your example, x-with-tuples-left would be [[3,0],[1,2],[2,5]] and y-with-tuples-left would be [[6,0],[1,1],[5,2]].

    Then just sort both x-with-tuples-left and y-with-tuples-left (it will be O(n.log n)), and read the permutation from the second element of the corresponding tuples.

    In your example, sorted x-with-… would be [[1,2],[2,5],[3,0]] and sorted y-with-… would be [[1,1],[5,2],[6,0]]. Now, you nicely see 5. from the second elements: f(2)=1, f(5)=2, f(0)=0.

    EDIT: Including O(n+L) in Javascript:

    function qperm (x, y, xprime, yprime) {
      var i;
      var n = x.length;
      var qperm = new Array(n);
      var countsx = [], countsy = []; // same as new Array()
      qperm[xprime] = yprime; // doing 1.
    
      for (i = 0; i < n; ++i) {
        if (x[i] == y[i] && i != xprime && i != yprime) { // doing 2.
          qperm[i] = i; }
        else { // preparing for 4. below
          if (i != xprime) {
            if (countsx[x[i]]) countsx[x[i]]++; else countsx[x[i]] = 1; }
          if (i != yprime) {
            if (countsy[y[i]]) countsy[y[i]]++; else countsy[y[i]] = 1; } }
    
      // finishing countsx and countsy
      var count, sum;
      for (i = 0, count = 0; i < countsx.length; ++i) {
        if (countsx[i]) {
          sum = count + countsx[i];
          countsx[i] = count;
          count = sum; }
      for (i = 0, count = 0; i < countsy.length; ++i) {
        if (countsy[i]) {
          sum = count + countsy[i];
          countsy[i] = count;
          count = sum; }
    
      var yranked = new Array(count);      
      for (i = 0; i < n; ++i) {
        if (i != yprime && (x[i] != y[i] || i == xprime)) { // doing 4. for y
          yranked[countsy[y[i]]] = y[i];
          countsy[y[i]]++; } }
    
      for (i = 0; i < n; ++i) {
        if (i != xprime && (x[i] != y[i] || i == yprime)) { // doing 4. for x and 5. at the same time
          // this was here but was not right: qperm[x[i]] = yranked[countsx[x[i]]];
          qperm[i] = yranked[countsx[x[i]]];
          // this was here but was not right: countsy[y[i]]++; } } }
          countsx[x[i]]++; } }
    
      return qperm; }
    

    Hopefully it’s correct 😉

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am studying an existing Perl program, which includes the following line of code:
While studying for a Functional Programming exam, I came across the following questions from
While studying for a Functional Programming exam, I came across the following question from
After studying TCP/UDP difference all week, I just can't decide which to use. I
While studying for the Zend PHP Exam I came across the following contradicting information:
While studying C# in ASP.net I have trouble understanding several classes. In which scenario
I was studying an open source code where I came across the following line
I'm studying sqlite features and I've discovered the SQLite Shared-Cache Mode which is disabled
I'm studying Turing machines for my course in formal languages ​​theory, the professor recommended
Studying JSR-299, I read section 5.1 of the Weld reference which explains how scopes

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.