Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I create a schedule for n-pastors visiting n-churches?

I want to make a schedule for many pastors. The conditions are:

  1. Every month, each pastor must must go to another church,
  2. The pastor must not go to same church where he came
  3. In 1 year he must go to 12 different churches
  4. There is 13 churches and 13 pastors and every church accepts only 1 pastor every month

I can't use random(1 to 12) because there is a chance the pastor could go to the same church (8,3% chance he goes to the same church).

I want to make the chance small (around 3% or less) that he goes to same church.

like image 618
ferdinand Avatar asked Dec 13 '22 05:12

ferdinand


1 Answers

Your conditions don't require that the next church for a given pastor be randomly selected. Couldn't you just iterate through the church list?

That is, assign each pastor a number, 0-12. Assign each church a number, 0-12. The first month:

Month 0:
pastor-0 --> church-0
pastor-1 --> church-1
pastor-2 --> church-2
...
pastor-n --> church-n

The next month, just increment one of the counters (with wrap-around)

Month 1:
pastor-0 --> church-1
pastor-1 --> church-2
pastor-2 --> church-3
...
pastor-n --> church-0

Then repeat for the remaining months:

Month 3:
pastor-0 --> church-2
pastor-1 --> church-3
pastor-2 --> church-4
...
pastor-(n-1) --> church-0
pastor-n --> church-1

There's a very simple loop to all this (O(n)). If it's confusing to you, I suggest trying the loop on paper with say n=3.

If the randomness is a requirement then please update your question.

EDIT BY PAX

I'm deleting my answer and up-voting this one since it's O(n) and the expansion of mine to cater for the edits would have been at least O(n^2).

You can still have randomness by making the pastor-0 through pastor-N values indexes into an array of pastors that has been randomly sorted so that makes this solution at least as good as mine.

END EDIT BY PAX

like image 87
Michael Haren Avatar answered Mar 08 '23 17:03

Michael Haren