I was asked to code a program without using any data structure libraries.
Inputs are:
The number of primary servers in the system.
The number of secondary servers in the system.
A set of service requests each consisting of an arrival time and two service times.
This set is terminated by a dummy record with arrival time and service times all equal to 0. (Note: the arrival times are sorted in ascending order).
I'm quite new to java so I would like to get advice what's the best way of doing this or resources which would help me understand the concept better.
I know we do need to create 2 Queues, one for the primary and secondary server to store the data while they are waiting to be served.
I probably have to create counters to increment and decrement for the time. Hopefully my thought process is right.
But I'm unsure how do we go about creating multiple Queues and what data structure i would use for the servers.
The way to approach this problem is to draw a diagram of the real-world situation. Customers come in and line up. There are X servers, each of which can handle one customer at a time. Also model the role of the secondary servers, whatever it is.
Then start handling transactions: server engages customer, passes order to secondary server queue and waits. Gets response from secondary server, finishes with customer, etc. Describe every place where information is exchanged between customers, servers, and secondary servers.
If you do that, you have a very good understanding of the problem you're trying to solve, and a real-world solution. Then you just have to model that in code. Your best bet is to first write a basic outline in pseudocode that describes the data structures and algorithms you're going to use. Once you have that, you can simulate its operation by (again) resorting to pencil and paper.
When you're convinced that you have the algorithm right, then you sit down to write code. And writing the code is pretty much a straightforward translation of your pseudocode.
I'm programming an app that will display the result of a team. I'd like notify the user when there's a goal. To do this, I created a service where I'll take the score and put it in a String every 30 seconds and I'll create a notification if it changes. My question is: What's the best way to repeat this every 30 seconds ?
Thank's and sorry for my poor english
Well if you want to do a call every x seconds that would require an ajax setTimeout (not setInterval). If you are going to do that it would be a good idea to send back some data to the db each round trip to make sure your query is not massive, and searching the entire db table.
For example you might send back the most recent timestamp on each round trip. And then have the backend query check how many rows are greater then that timestamp and display the count to the user. So if there is one row, show the user 1 new row. And when they click it query for the content. This should be a low impact query for a high impact activity (pooling would be a better option). Goodluck.
PS: If you want to get fancy and really learn some stuff tonight I would do some research on asynchronous servlets, rather then just pooling as another poster advised. That would lead you down the rabbit hole to some really sweet stuff.
You can do something like :
check if anything changed (yes - send new info, no - do nothing)
sleep 30 sec (Thread.sleep(30000))
repeat
I'm trying to solve a problem but unfortunately my solution is not really the best for this task.
Task:
At a party there are N guests ( 0 < N < 30000 ). All guests tell when they get to the party and when they leave (for example [10;12]). The task is to take photos of as many people as possible at the party. On a photo there can only be 2 people (a pair) and each person can only be on exactly one photo. Of course, a photo can only be taken when the two persons are at the party at the same time. This is the case when their attendance intervals overlap.
My idea: I wrote a program which from the intervals creates a graph of connections. From the graph I search for the person who has the least number of connections. From the connected persons I also select the person who has the least connections. Then these two are chosen as a pair on a photo. Both are removed from the graph. The algorithm runs until no connections are left.
This approach works however there is a 10 secs limit for the program to calculate. With 1000 entries it runs in 2 secs, but even with 4000 it takes a lot of time. Furthermore, when I tried it with 25000 data, the program stops with an out of memory error, so I cannot even store the connections properly.
I think a new approach is needed here, but I couldn't find an other way to make this work.
Can anyone help me to figure out the proper algorithm for this task?
Thank you very much!
Sample Data:
10
1 100
2 92
3 83
4 74
5 65
6 55
7 44
8 33
9 22
10 11
The first line is the number of guests the further data is the intervals of people at the party.
No need to create graph here, this problem can be solved well on intervals structure. Sort people by ascending order of their leaving time(ending point of interval). Then iterate over them in that sorted order: if current person is not intersecting with anyone, then he should be removed. If he is intersecting with more than one person, take as a pair one of them who has earliest leaving time. During iteration you should compare each person only with next ones.
Proving this approach is not so difficult, so I hope you can prove it yourself.
Regarding running time, simple solution will be O(N^2), however I think that it can be reduced to O(N * logN). Anyway, O(N^2) will fit in 10 seconds on a normal PC.
Seems like a classical maximum matching problem to me.
You build a graph, where people who can possibly be pictured together (their time intervals intersect) are connected with an edge, and then find maximum matching, for example, with Edmond's Blossom algorithm.
I wouldn't say, that it's quite easy to implement. However, you can get quite a good approximation of this with Kuhn's algorithm for maximum matching in bipartite graphs. This one is really easy to implement, but won't give you the exact solution.
I have some really simple idea:
Assume, party will take Xh, make X sets for each hour, add to them appropriate people. Of course, people who will be there longer than hour will be in few sets. Now if there are 2 sets "together" with even number of ppl, you just could take n/2 photos for each sets. If there are 2 sets of odd number of people you are looking for someone who will be on each of that 2 sets, and move him to one of them (so you got 2 even number sets of people who will be on the same time on the party).
Remember to remove all used ppl (consider some class - Man with lists of all his/her hours).
My idea probably should be expand to more advanced "moving people" algorithm, through more than one neighboring set.
I think the following can do:
First, read all the guests' data and sort them into an array by leaving time ascending. Then, take first element of the array and iterate through next elements until the very first time-match found (next guest's entry time is less than this guest's leave time), if found, remove both from array as a pair, and report it elsewhere. If not, remove the guest as it can't be paired at all. Repeat until the array is empty.
The worst case of this is also N^2, as a party can be like [1,2],[3,4],... where no guests could be paired with each other, and the algorithm will search through all 30000 guests in vain all the time. So I don't think this is the optimal algorithm, but it should give an exact answer.
You say you already have a graph structure representation. I assume your vertices represent the guest and the interval of their staying at the party and the edges represent overlap of the respective intervals. What you then have to solve is the graph theoretical maximum matching problem, which has been solved before.
However, as indicated in my comments above, I think you can exploit the properties of the problem, especially the transitivity-like "if A leaves before B leaves and B leaves before C arrives, then A and C will not meet, either" like this:
Wait until the next yet unphotographed guest is about to leave, then take a photo of this one with the one who leaves next among those present.
You might succeed in thinking about the earliest time a photo can be taken: It is the time when the second person arrives at the party.
So as a photographer, goto the party being the first person and wait. Whenever a person arrives, take a photo with him/her and all other persons at the party. As a person appears only once, you will not have any duplicates.
While taking a photo (i.e. iterating over the list of guests), remove those guests who actually left the party.
I am looking for help in designing a scheduling algorithm for a medical review board:
Everyday hundreds of customers are scheduled starting 14 days later to specialized doctors.
Each patient may need to visit more than one doctor, in extreme cases could be up to 5 visits.
There are a fixed number of rooms, some of them with specialized equipment. For some of the meetings only specific rooms can be used.
Each doctor has a specific schedule, but usually between 14:00 and 19:00.
The main requirement is to try to have each patient come only once.
Many contraints including second visit with same doctor, avoid conflicts of interest (patient and doctor know each other) among others. Hospital/residents problem not suitable, mainly because of constraints. We are trying a solution using a prioritizing scheme and then trying to reschedule exceptions.
At this moment we are trying to define the algorithm, this is part of a whole system to manage the medical review board.
The sytem is based on Java with dojo for FE and EJB for BE.
This is a question that may be closed because it is too localized. It won't be much help to someone else. But it's a fun problem so I thought I'd throw out some ideas.
You are going to need to find matches for the most complex cases first.
Look for "best-fit" solutions. Don't take time on an empty day if you can fill up another day.
You're going to have to figure out a way to iterate through the matching so you try a wide range of possibilities. Some way to pull back, make a different choice, and then continue without getting into an infinite loop.
You might do the fitting up to (let's say) 80% and then swap around people. Swap a 3 hour appointment with a 2 and a 1 or something. The goal is to leave the schedule with the most "flexibility".
You're going to need to determine your swapping rules. What makes a schedule better?
Here's a bunch of SO questions for you to read:
Worker Scheduling Algorithm
Best Fit Scheduling Algorithm
Is there a scheduling algorithm that optimizes for "maker's schedules"?
Hope some of this helps.
I have a database full of two different types of users (Mentors and Mentees), whereby I want the second group (Mentees) to be able to "search" for people in the first group (Mentors) who match their profile. Mentors and Mentees can both go in and change items in their profile at any point in time.
Currently, I am using Apache Mahout for the user matching (recommender.mostSimilarIDs()). The problem I'm running into is that I have to reload the user data every single time anyone searches. By itself, this doesn't take that long, but when Mahout processes the data it seems to take a very long time (14 minutes for 3000 Mentors and 3000 Mentees). After processing, matching takes mere seconds. I also get the same INFO message over and over again while it's processing ("Processed 2248 users"), while looking at the code shows that the message should only be outputted every 10000 users.
I'm using the GenericUserBasedRecommender and the GenericDataModel, along with the NearestNUserNeighborhood, AveragingPreferenceInferrer and PearsonCorrelationSimilarity. I load mentors from the database, add the mentee to the list of POJOs and convert them to a FastByIDMap to give to the DataModel.
Is there a better way to be doing this? The product owner needs the data to be current for every search.
(I'm the author.)
You shouldn't need to ask it to reload the data every time, why's that?
14 minutes sounds way, way too long to load such a small amount of data too, something's wrong. You might follow up with more info at user#mahout.apache.org.
You are seeing log messages from a DataModel, which you can disable in your logging system of choice. It prints one final count. This is nothing to worry about.
I would advise you against using a PreferenceInferrer unless you absolutely know you want it. Do you actually have ratings here? I might suggest LogLikelihoodSimilarity if not.