Finding Attributes using Relational Algebra - relational-algebra

I have a couple of questions that I'm unsure of. Here goes...
The schema:
Instructor(InsID, I_Name,DeptName,Salary)
Student(SID,StName,DeptName, Tot_Credit)
Course(Course_ID,Course_Title,DeptName, credits)
a. Find the course titles for all 3-credit courses offered by the Science dept.
b. Find the student IDs for all students who studied with the Instructor Johnson
I tried to answer them, but I'll need someone to verify/help me find the correct answer.
a. (Pi)Course_Title(Course|X|((sigma)(credits=3 and DeptName="Science")(Course)))
b. (Pi)SID((sigma)(I_Name="Johnson")(Instructor))
I'm not sure if those are correct, but any help would be appreciated.

a is correct but can be shorter. You have 3 ops, only two are needed.
b is incorrect (but I'm not going to spoonfeed anything more).

Related

Algorithm to process a circular group

Noob here, sorry if this is a too silly question. Only I am looking to the name of the algorithm. because I am pretty sure someone solved this problem before, but I cannot found anything on google, mostly because of a lack of vocabulary.
Basically, what I am looking, is the best algorithm to solve the following situation:
I have a group of elements, let say Companies. I need to process all of them, one by one, but the criteria are that the next one will be the least attended. For example, if my universe where 3 companies:
Oracle
Apple
Google
The first time, whichever of those will fulfill the criteria, so let's say we choose Oracle. We process Oracle, so in the next round it will be or Apple or Google, but clearly not Oracle. Let's choose now Apple. The next round is clearly Google. When I finished the first round, I need to attend them again, this time, I do not need to choose at random, because of the 3 companies, Oracle was processed the most time ago.
I am sure there is a well known algorithm for this
As #Henry mentioned in the comments, the answer to my question is "Round Robin"

NSGA-II with flight ticket reservation

I am totally confused with this optimization problem. Since I am a beginner for this evolutionary algorithms, if I asked a basic question, please forgive me. I checked many NSGA II examples. They took flight ticket reservation as a standard problem to explain two-objective optimization problem. I clearly understand the concept that for example, if we want to choose low cost and a few hours of traveling time, it is really hard to find best from multiple combinations as bellow. For example, if we have the following combinations,
How can I generate the initial population--> Chromosome. Since ticket types are Pareto front, how can I represent individuals (Chromosomes)?

Algorithm for preference based grouping

I am looking to figure out a way to sort people into classes by preference.
For example, say there are 100 students that are each going to be assigned one of five classes:
Science - 40 seats
Math - 15 seats
History - 15 seats
Computers - 20 seats
Writing - 10 seats
Each student has three preferred classes that are ordered by preference. What is the best way to approach dividing up the students so that as many people get their first and second choice classes as possible, while at the same time making sure that no class has too many students for the room.
I've thought about approaching it by the following method:
Group all students by their first choice class
See which classes have too many students and which have too few
Check to see if any students in the overbooked classes have second choice classes which are underbooked
Move those students accordingly
Repeat 2-4 with 3rd choice classes
While I feel like this is a reasonable implementation, I am wondering if there are any other algorithms that solve this problem in a better way. I have tried searching all over, but I cannot find anything that would solve this kind of problem.
From your description, this sounds very much like one of the variations of the Stable Marriage Problem
Check the Wiki link and you will see a description of the Gale-Shapley Algorithm, which is a good solution.

Need help to figure out which algorithm to use

I need an algorithm to solve a problem with the following conditions:
There is a set of "n" people and another set of "m" workshops, there are more people than workshops. Each person has chosen a subset of size "j" of the total workshops and has assigned values to each depending on how much they would like to assist that particular workshop. Now, every workshop only has a limited amount of vacancies.
Given these conditions the problem would be:
What is the best way to assign people to workshops, so that each person participates in the workshop which she considers most valuable (given the problem constraints, that is, if a person canĀ“t participate in their first choice, then the algorithm should choose the second, third, fourth, and so on).
I think the problem is related to combinatorial optimization but I don't know much about algorithms. If anyone can tell me the name of one from which to start investigating, I'd be very grateful.
Thanks! And please excuse my english.
This is a matching problem with one-sided preferences (in the sense that people have preferences for the workshops, but not the other way around).
Here is an excellent paper that discusses this problem in more detail: https://mattmccutchen.net/lumc/index.html
An optimal solution to this problem isn't particularly clear. There are many different optimal (Pareto efficient) criterions. Unfortunately, the problem is NP-hard for many of them.
However, there are criterion with polynomial time algorithms. There is a nice list of these in the "Related work" section of the paper I linked.

Possible NP-complete problem?

I'd just like someone to verify whether the following problem is NP-complete or if there is actually a better/easier solution to it than simple brute-force combination checking.
We have a sort-of resource allocation problem in our software, and I'll explain it with an example.
Let's say we need 4 people to be at work during the day-shift. This number, and the fact that it is a "day-shift" is recorded in our database.
However, we don't require just anyone to fill those spots, there's some requirements that needs to be filled in order to fit the bill.
Of those 4, let's say 2 of them has to be a nurse, and 1 of them has to be doctors.
One of the doctors also has to work as part of a particular team.
So we have this set of information:
Day-shift: 4
1 doctor
1 doctor, need to work in team A
1 nurse
The above is not the problem. The problem comes when we start picking people to work the day-shift and trying to figure out if the people we've picked so far can actually fill the criteria.
For instance, let's say we pick James, John, Ursula and Mary to work, where James and Ursula are doctors, John and Mary are nurses.
Ursula also works in team A.
Now, depending on the order we try to fit the bill, we might end up deducing that we have the right people, or not, unless we start trying different combinations.
For instance, if go down the list and pick Ursula first, we could match her with the "1 doctor" criteria. Then we get to James, and we notice that since he doesn't work in team A, the other criteria about "1 doctor, need to work in team A", can't be filled with him. Since the other two people are nurses, they won't fit that criteria either.
So we backtrack and try James first, and he too can fit the first criteria, and then Ursula can fit the criteria that needs that team.
So the problem looks to us as we need to try different combinations until we've either tried them all, in which case we have some criteria that aren't filled yet, even if the total number of heads working is the same as the total number of heads needed, or we've found a combination that works.
Is this the only solution, can anyone think of a better one?
Edit: Some clarification.
Comments to this question mentions that with this few people, we should go with brute-force, and I agree, that's probably what we could do, and we might even do that, in the same lane that some sort optimizations look at the size of the data and picks different sort algorithms with less initial overhead if the data size is small.
The problem though is that this is part of a roster planning system, in which you might have quite a few number of people involved, both as "We need X people on the day shift" as well as "We have this pool of Y people that will be doing it", as well as potential for a large "We have this list of Z criteria for those X people that will have to somehow match up with these Y people", and then you add to the fact that we will have a number of days to do the same calculation for, in real-time, as the leader adjusts the roster, and then the need for a speedy solution has come up.
Basically, the leader will see a live sum information on-screen that says how many people are still missing, both on the day-shift as a whole, as well as how many people is fitting the various criteria, and how many people we actually ned in addition to the ones we have. This display will have to update semi-live while the leader adjusts the roster with "What if James takes the day-shift instead of Ursula, and Ursula takes the night-shift".
But huge thanks to the people that has answered this so far, the constraint satisfaction problem sounds like the way we need to go, but we'll definitely look hard at all the links and algorithm names here.
This is why I love StackOverflow :)
What you have there is a constraint satisfaction problem; their relationship to NP is interesting, because they're typically NP but often not NP-complete, i.e. they're tractable to polynomial-time solutions.
As ebo noted in comments, your situation sounds like it can be formulated as an exact cover problem, which you can apply Knuth's Algorithm X to. If you take this tack, please let us know how it works out for you.
It does look like you have a constraint satisfaction problem.
In your case I would particularly look at constraint propagation techniques first -- you may be able to reduce the problem to a manageable size that way.
What happens if no one fits the criteria?
What you are describing is the 'Roommate Problem' it is lightly described in this thesis.
Bear with me, I'm searching for better links.
EDIT
Here's another fairly dense thesis.
As for me I would most likely trying to find reduction to bipartite graph matching problem. Also to prove that problem is NP usually is much more complicated than staying you cannot find polynomial solution.
I am not sure your problem is NP, it does not smell that way, but what I would do if I was you would be to order the requirements for the positions such that you try to fill the most specific first since fewer people will be available fill these positions, so you are less likely to have to backtrack a lot. There is no reason why you should not combine this with algorithm X, an algorithm of pure Knuth-ness.
I'll leave the theory to others, since my mathematical savvy is not so great, but you may find a tool like Cassowary/Cassowary.net or NSolver useful to represent your problem declaratively as a constraint satisfaction problem and then solve the constraints.
In such tools, the simplex method combined with constraint propagation is frequently employed to deterministically reduce the solution space and then find an optimal solution given a cost function. For larger solution spaces (which don't seem to apply in the size of problem you specify), occasionally genetic algorithms are employed.
If I remember correctly, NSolver also includes in sample code a simplification of an actual Nurse-rostering problem that Dr. Chun worked on in Hong Kong. And there's a paper on the work he did.
It sounds to me like you have a couple of separable problems that would be a lot easier to solve:
-- select one doctor from team A
-- select another doctor from any team
-- select two nurses
So you have three independent problems.
A clarification though, do you have to have two doctors (one from the specified team) and two nurses, or one doctor from the specified team, two nurses, and one other that can be either doctor or nurse?
Some Questions:
Is the goal to satisfy the constraints exactly, or only approximately (but as much as possible)?
Can a person be a member of several teams?
What are all possible constraints? (For example, could we need a doctor which is a member of several teams?)
If you want to satisfy the constraints exactly, then I would order the constraints decreasingly by strictness, that is, the ones which are most hardest to achieve (e.g. doctor AND team A in your example above) should be checked first!
If you want to satisfy the constraints approximately, then its a different story... you would have to specify some kind of weighting/importance-function which determines what we rather would have, when we can't match exactly, and have several possibilities to choose from.
If you have several or many constraints, take a look at Drools Planner (open source, java).
Brute force, branch and bound and similar techniques take to long. Deterministic algorithms such as fill the largest shifts first are very suboptimal. Meta-heuristics are a very good way to deal with this.
Take a specific look at the real-world nurse rostering example of Drools Planner. It's easy to add many constraints, such as "young nurses don't want to work the Saturday night" or "some nurses don't want to work to many days in a row".

Resources