Prepare a schedule so that all courses are taught in the least time - algorithm

I encountered one interview question:
There are some professors, some courses, and some students.
Each professor can teach only a single course.
Each course has a fixed duration(Eg. 10 weeks).
For each professor, you are given time availability schedule(assume week wise).
Each student has a list of courses he wants to learn.
There can be only 1:1 classes, i.e., 1 professor can teach only a single student.
A student can attend only one course at a time.
A professor has to finish teaching a course in a one go.
Your aim is to prepare a schedule so that all courses are taught in the least time.
My Approach: I mentioned that this will be solved via graph theory.Like make a directed edge from teacher to course or teacher to student.But I was not able to solve it completely .
Is my approach correct or is it DP problem?
Pseudocode or Algorithm suggestions?

The problem you were asked is a schedulling problem, which is a dynamic programming problem. In particular, your problem is what is usally called FJm|brkdwn,pj=10|Cmax, wich can be traduced as follow:
There are m machines (the professors) that can process a part of a job (here, a job is the full teaching of a student) independently and in whatever order. Some machines may process the same part of a job (the same course)
machines are not continuously available
the duration of a part of a job (a course) is 10 weeks
you want to minimize the time completion of all jobs
There exist solvers that are well optimized for schedulling problems, but I am not sure if to model your problem as a scheduling problem and to process it through a shedulling problem solver is what was intended by your job interviewer.

This is similar to the m-coloring problem. Except here we are asked to return the minimum m. Unfortunately, it's an NP-hard problem.
For the given problem, consider a course as a vertex and edge b/w 2 vertexes if a common student exists or professor is the same.
Now first, find the upper bound of m (minimum colors required) using Welsh–Powell Algorithm and then we can do a binary search to find which is the smallest value of m for which we are able to color all the vertex (with no 2 adjacent vertexes with the same color) using Graph Coloring

Related

Representing Travelling Salesman as Linear Expression

I've seen online that one can write the travelling salesman problem as a linear expression and compute it using software such as CPLEX for java.
I have a 1000 towns and need to find a short distance. I plan on partitioning these 1000 towns into clusters of ~100 towns and performing some linear programming algorithm on these individual clusters.
The question I have is, how exactly do I represent this as a linear expression.
So I have 100 towns and I'm sure everyone's aware of how TSP works.
I literally have no clue how I can write linear constraints, objectives and variables which satisfy the TSP.
Could someone explain to me how this is done or send me a link which explains it clearly, because I've been researching a lot and can't seem to find anything.
EDIT:
A bit of extra information I found:
We label the cities with numbers 0 to n and define the matrix:
Would this yield the following matrix for 5 towns?
The constraints are:
i) Each city be arrived at from exactly one other city
ii) From each city there is a departure to exactly one other city
iii) The route isn't broken up into separate islands.
Again, this makes complete sense to me, but I'm still having trouble writing these constraints as a linear expression. Apparently it's a simple enough matrix.
Thanks for any help !
According to this Wikipedia article the travelling salesman problem can be modelled as an integer linear program, which I believe to be the key issue of the question. The idea is to have decision variables of permitted values in {0,1} which model selected edges in the graph. Suitable constraints must ensure that the selected edges cover every node, the selected edges form a collection of cycles and there is only one connected component (which in total means that there is exactly one cycle which contains every node). Note that the article also gives an explicit formulation and explains the interpretations of the constraints.
As the travelling salesman problem is NP-hard, it cannot be solved via (non-integral) linear programming unless P=NP.
The TSP problem is a rather complex integer programming problem due to its combinatorial nature.
There are several (exact and approximated) techniques to solve it. The model in Wikipedia is just one of them: it has constraints to ensure there is only one incoming and outgoing arc in each node and the constraints with u variables are for preventing sub-cycles in the solution. There is also several ways to write this sub-cycle preventing constraints, some of them can be found on section 4.1 of this article. The section presents some models for the TSP problem (all of them can be solved using CPLEX, Gurobi, MATLAB or other Integer/Linear Programming Software.
Hope I could be of any help. (=

Algorithm - Creating an exam schedule

I have a problem of creating an exam schedule based on three factors: rooms, courses, and days. There are a set number of rooms r, courses c, and days d where each day has three slots.
There is also a set of students and a mapping from students to courses so that there cannot be any conflicts.
I'm trying to find an algorithm for this and found that this fit the max-flow problem.
I'm having troubles making a flow network graph for this.
Thanks
The competition ITC2007 track 1 examination defined a very good, non-trivial exam scheduling problem with multiple real-world datasets. Because the problem is NP-complete, there's no polynomial algorithm known to man that solves it optimally.
For inspiration on which algorithms can handle this problem, take a look at this java, open source implementation of that competition with OptaPlanner:
Exam scheduling documentation including a description of all constraints, dataset sizes and domain model.
Exam scheduling source code

algorithm for solving resource allocation problems

Hi I am building a program wherein students are signing up for an exam which is conducted at several cities through out the country. While signing up students provide a list of three cities where they would like to give the exam in order of their preference. So a student may say his first preference for an exam centre is New York followed by Chicago followed by Boston.
Now keeping in mind that as the exam centres have limited capacity they cannot accomodate each students first choice .We would however try and provide as many students either their first or second choice of centres and as far as possible avoid students having to give the third choice centre to a student
Now any ideas of a sorting algorithm that would mke this process more efficent.The simple way to do this would be to first go through the list of first choice of students allot as many as possible then go through the list of second choices and allot. However this may lead to the students who are first in the list getting their first centre and the last students getting their third choice or worse none of their choices. Anything that could make this more efficient
Sounds like a variant of the classic stable marriages problem or the college admission problem. The Wikipedia lists a linear-time (in the number of preferences, O(n²) in the number of persons) algorithm for the former; the NRMP describes an efficient algorithm for the latter.
I suspect that if you randomly generate preferences of exam places for students (one Fisher–Yates shuffle per exam place) and then apply the stable marriages algorithm, you'll get a pretty fair and efficient solution.
This problem could be formulated as an instance of minimum cost flow. Let N be the number of students. Let each student be a source vertex with capacity 1. Let each exam center be a sink vertex with capacity, well, its capacity. Make an arc from each student to his first, second, and third choices. Set the cost of first choice arcs to 0; the cost of second choice arcs to 1; and the cost of third choice arcs to N + 1.
Find a minimum-cost flow that moves N units of flow. Assuming that your solver returns an integral solution (it should; flow LPs are totally unimodular), each student flows one unit to his assigned center. The costs minimize the number of third-choice assignments, breaking ties by the number of second-choice assignments.
There are a class of algorithms that address this allocating of limited resources called auctions. Basically in this case each student would get a certain amount of money (a number they can spend), then your software would make bids between those students. You might use a formula based on preferences.
An example would be for tutorial times. If you put down your preferences, then you would effectively bid more for those times and less for the times you don't want. So if you don't get your preferences you have more "money" to bid with for other tutorials.

Travelling Salesman with multiple salesmen?

I have a problem that has been effectively reduced to a Travelling Salesman Problem with multiple salesmen. I have a list of cities to visit from an initial location, and have to visit all cities with a limited number of salesmen.
I am trying to come up with a heuristic and was wondering if anyone could give a hand. For example, if I have 20 cities with 2 salesmen, the approach that I thought of taking is a 2 step approach. First, divide the 20 cities up randomly into 10 cities for 2 salesman each, and I'd find the tour for each as if it were independent for a few iterations. Afterwards, I'd like to either swap or assign a city to another salesman and find the tour. Effectively, it'd be a TSP and then minimum makespan problem. The problem with this is that it'd be too slow and good neighborhood generation of swapping or assigning a city is hard.
Can anyone give an advise on how I could improve the above?
EDIT:
The geo-location for each city are known, and the salesmen start and end at the same places. The goal is to minimize the max travelling time, making this sort of a minimum makespan problem. So for example, if salesman1 takes 10 hours and salesman2 takes 20 hours, the maximum travelling time would be 20 hours.
TSP is a difficult problem. Multi-TSP is probably much worse. I'm not sure you can find good solutions with ad-hoc methods like this. Have you tried meta-heuristic methods ? I'd try using the Cross Entropy method first : it shouldn't be too hard to use it for your problem. Otherwise look for Generic Algorithms, Ant Colony Optimization, Simulated Annealing ...
See "A Tutorial on the Cross-Entropy Method" from Boer et al. They explain how to use the CE method on the TSP. A simple adaptation for your problem might be to define a different matrix for each salesman.
You might want to assume that you only want to find the optimal partition of cities between the salesmen (and delegate the shortest tour for each salesman to a classic TSP implementation). In this case, in the Cross Entropy setting, you consider a probability for each city Xi to be in the tour of salesman A : P(Xi in A) = pi. And you work, on the space of p = (p1, ... pn). (I'm not sure it will work very well, because you will have to solve many TSP problems.)
When you start talking about multiple salesmen I start thinking about particle swarm optimization. I've found a lot of success with this using a gravitational search algorithm. Here's a (lengthy) paper I found covering the topic. http://eprints.utm.my/11060/1/AmirAtapourAbarghoueiMFSKSM2010.pdf
Why don't you convert your multiple TSP into the traditional TSP?
This is a well-known problem (transforming multiple salesman TSP into TSP) and you can find several articles on it.
For most transformations, you basically copy your depot (where the salesmen start and finish) into several depots (in your case 2), make the edge weights in a way to force a TSP to come back to the depot twice, and then remove the two depots and turn them into one.
Voila! got two min cost tours that cover the vertices exactly once.
I wouldn't start writing an algorythm for such complicated issue (unless that's my day job - to write optimization algorythms). Why don't you turn to a generic solution like http://www.optaplanner.org/ ? You have to define your problem and the program uses algorithms that top developers took years to create and optimize.
My first thought on reading the problem description would be to use a standard approach for the salesman problem (googling for an appropriate one as I've never actually had to write code for it); Then take the result and split it in half. Your algorithm then could be to decide where "half" is -- maybe it is half of the cities, or maybe it is based on distance, or maybe some combination. Or search the result for the largest distance between two cities and choose that as the separation between salesman #1's last city and salesman #2's first city. Of course it does not limit to two salesman, you would break into x pieces; But overall the idea is that your standard 1 salesman TSP solution should have already gotten the "nearby" cities next to each other in the travel graph, so you don't have to come up with a separate grouping algorithm...
Anyway, I'm sure there are better solutions, but this seems like a good first approach to me.
Have a look at this question (562904) - while not identical to yours there should be some good food for thought and references for further study.
As mentioned in the answer above the hierarchical clustering solution will work very well for your problem. Instead of continuing to dissolve clusters until you have a single path, however, stop when you have n, where n is the number of salesmen you have. You can probably improve it some by adding in some "fake" stops to improve the likelihood that your clusters end up evenly spaced from the initial destination if the initial clusters are too disparate. It's not optimal - but you're not going to get an optimal solution for a problem like this. I'd create an app that visualizes the problem and then test many variants of the solution to get a feel for whether your heuristic is optimal enough.
In any case I would not randomize the clusters, that would cause the majority of the clusters to be sub-optimal.
just by starting to read your question using genetic algorithm came to my head. just use two genetic algorithm in the same time one can solve how to assign cities to salesmen and the other can solve the TSP for each salesman you have.

Algorithm for computing timetable given restrictions

I'm considering a hypothetical problem, and looking for guidance on how to approach solving the problem, from an algorithmic point of view.
The Problem:
Consider a university. You have the following objects:
Teaching staff. Each staff member teaches one or more papers.
Students. Each student takes one or more papers.
Rooms. Rooms hold a certain number of students, and contain certain types of equipment.
Papers. Require a certain type of equipment, and a certain amount of time each week.
Given information about enrollments (i.e.- how many students are enrolled in each paper, and what staff are allocated to teach each paper), how can I compute a timetable that obeys the following restrictions:
Staff can only teach one thing at once.
Students can only attend one paper at once.
Rooms can only hold a certain number of students.
Papers that require a certain type of equipment can only be held in in a room that provides that type of equipment.
Hours of operation are Monday to Friday, 8-12 and 1-5.
Discussion:
In reality I'm not too concerned with the situation outlined above - it's the general class of problem that I'm curious about. At first glance It seems to me like a good fit for a genetic algorithm, but the fitness function for such an algorithm would be incredibly complex.
What's a good approach for trying to solve this kind of constraint-satisfying problem?
I guess there's probably no way to solve this perfectly, since students may well take a combination of papers that leads to impossible situations, especially as the number of students & papers grows.
Staying on genetic algorithms, I don't think the fitness function for this would be very complex, quite the opposite.
You basically just check your candidate solution (whatever the encoding) for each of the constraints (you only have 5) and assign a weight to them so that when a constraint is not satisfied the weight is added to a total score that could represent fitness.
In such a scenario you just minimize the fitness function (because best fitness possible is 0, meaning all the constraints are satisfied) and let the GA crunch the numbers.
The encoding will take a bit of figuring out, but once that's done it should be straightforward, unless I am missing something, of course :)
A very restricted version of this problem is NP-Complete.
Consider the case when exactly one student can take a paper.
Now for a given time slot (say the paper is taught all day), you can construct a 3-partite graph, with Rooms, Papers and Students, with an edge between a paper and a student if that student wants to take it. Also add edges between a paper and it's possible rooms.
We now see that the 3 Dimensional matching problem is an instance of your problem: you need to pick a non-overlapping (student, paper, room) combination for that particular timeslot.
You are probably better off with some heuristics for the general problem. Sorry, I can't help you there.
Hope that helps.

Resources