Completely stumped with this permutation algorithm...
** EDIT 4: I figured it out! It almost feels like a cop out after all of the different things I was trying, but heres what I came up with. I havent written it yet, but i'll just explain the idea:
I know that my vector is BigVector.size() in "width". I can put a certain number of littlevectors in there. How many littlevectors? Well that is a simple combinatorics problem:
int product = 1;
for(int n=0;n<bigvector.size();n++)
{
product *= bigvector[n].size();
}
Now, I have the "x" of my vector as bigvector.size(), which is 5.
I have the "y" of my vector as product, which is calculated at runtime.
I have successfully allocated space for every single combination possible. There wont be repitition either because A, B, C, D, and E stay in their own respective "columns".
Next, the only thing I have to do is put each item in their own columns and start from 0 if I reach the "end" of that littlevector:
vector< vector<Course> > All_Combos (bigvector.size)
for(int x=0;x<bigvector.size();x++)
{
int y=0;
while(y<product)
{
All_Combos[x].push_back(bigvector[x][y]);
y++;
if(y=bigvector[x].size){ y=0}; //exceeding boundry means start over.
}
}
And thats all there is to it!
From this point, all I need to do is test each course in each list for compatibility. I can do this by comparing bigvector[0,0] to bigvector[0,1:n], then [0,1] to [0,2..n], so on so forth till i compare [0,n-1] to [0,n]. Then I compare [1,0] so on so forth till i have compared every element in each row with members of its own row.
If at any time I detect a conflict, I can use vector.erase(), decrement my counter by 1, and continue. this way I never accidentally skip a row.
I hope anyone who reads through this doesnt get more confused, and that this helps someone. Yeah, its not the recursive tree solution and its actually pretty simple if you look at the data structure being created, but it works and its actually more efficient than if I were to try it recursively.
Course.cpp Class Implementation
EDIT 3: A generalized problem: I have a vector of X vectors which each contain a different number of elements inside. I need to find all combinations, without repitition, to pick 1 element from each vector.
Only the first and second links are entirely relevant to my problem, but I put the implementation up so you could see what a couple of other functions are doing or why something's return type is unexpected.
TL;DR: I had a vector of "Courses". I turned it into a vector of vector of Courses for easier permutating. Now I cant really figure out how to permutate through it in a sequential manner. Every solution I come up with requires me to dynamically create new variables, and that is a no-no in c++ to the best of my knowledge. (although curiously enough I think its entirely doable in matlab, python, bash and other high level languages).
I'm on IRC, AIM, and google talk. PM if you'd like to chat there instead of through the comments.
I can also submit the rest of the project (its a class schedule generator) but this function itself has nothing relevant.
In the mean time, i'll be writing a member function: bool Conflicts_With(Course);
Incase you guys manage to somehow break into valhalla and steal the secret pseudocode implementation from the highest branches of ragnarok, the above function is how i'll be checking for conflicts.
EDIT: I think I got the function done....now i'm back to being confused. Addition to course.cpp/course.h:
EDIT 2: FYI: bool Days[6] is what I was concerned with here. If it stores '1', that means theres an event on that day. Start time and End time are both simple integers in 24h format (for easier comparison). All this algorithm is doing is checking if the two "courses" in question have an event on the same day. For every day that they do have an event in common, it makes sure that one event starts after the other ends or that other event ends before the first event ends. If returnval stays 0 through the completion of the loop, everything is peachy. otherwise, it will return a 1, meaning things DO conflict.
bool Course::Conflicts_With(Course other)
{
bool returnval;
for(int a=0;a<DaysInWeek;a++)
{
if(this->Days[a] && other.Days[a])
{
if(this->Start_Time > other.End_Time || this->End_Time < other.Start_Time)
returnval = 0;
else
returnval = 1;
}
}
return (returnval);
}
Posted Dec 2, 2011, 3:13 AM UTC · edited