4

Maximum efficiency problem

The are N cities and there is a wanderer.

The time it takes for him to go from a town to another town is known - Txy (from town x to town y).

From any town he can go to any other town so it is a complete graph.

In each town there is a an amount of money Mx the wanderer wants to collect.

It isn't enough time to pass through all cities.

Having the total available time T and the starting point i, the problem is to find the best route so that the money he collects will be maximum.

Input numbers range:

  • N is between 400 and 600
  • Mx(M1, M2, ...) are between 100 and 500, x between 1 and N
  • Txy are between 80 and 200, x and y are between 1 and N
  • Txy is the optimal time distance so that Txy < Txz + Tzy, for any x, y and z between 1 and N.
  • T is between 3500 and 5000
trumpetlicks
  • 7,033
  • 2
  • 19
  • 33
Andrei Susanu
  • 99
  • 1
  • 4
  • Is it a question? An opened challenge? Where do you need help? – Déjà vu Feb 05 '13 at 13:48
  • It's not a challenge, it's a real problem I'm facing with. The problem is in another context, but I've wrote it like this to be easier to read / understand. I'm looking for the optimal solution. – Andrei Susanu Feb 05 '13 at 14:28
  • Where does he start ? in any city ? – Grigor Gevorgyan Feb 05 '13 at 14:29
  • The starting point is defined as one of the cities. – Andrei Susanu Feb 05 '13 at 14:31
  • Is the amount of money in each town known or random between the range 100 and 500? – Nuclearman Feb 05 '13 at 17:49
  • Im still trying to figure out how this is much different from the original travelling salesman problem. It is a fully connected graph with costs associated with each hop / destination. – trumpetlicks Feb 05 '13 at 18:07
  • @trumpetlicks: If T is large enough, the solution could be the shortest Hamiltonian path (assuming cycles aren't allowed, which may or may not be the case), as nothing says the traveler has to end where they started, not the shortest Hamiltonian tour, which is TSP. If the amount of money is random, then you need to find the most cities you can visit with a travel time of less than T. – Nuclearman Feb 05 '13 at 19:22

4 Answers4

2

Seems dynamic:

Denote a[ x ] - money to collect from city x.

Let dp[ x ][ t ] mean the maximal quantity of money he can collect spending time t and finishing in city x. Initialisation and updates as follows:

  1. for startpoint x0, dp[ x0 ][ 0 ] := a[ x0 ]. For other cities x dp[ x ][ 0 ] := -1 (invalid);
  2. for each time t from 1 to T:
    for each city x:
    for each city y s.t. edge[ y ][ x ] <= t:
    denote p := t - edge[ y ][ x ];
    if dp[ y ][ p ] >= 0 // it's possible to get to y in time p
    then dp[ x ][ t ] = max( dp[ x ][ t ], dp[ y ][ t - edge[ x ][ y ]] + a[ x ] )
  3. return maximum over all dp[ x ][ t ].

Total complexity is O( T*N^2 ).

Grigor Gevorgyan
  • 6,753
  • 4
  • 35
  • 64
  • It's wrong. It isn't checking for circles. It would apply route x-y-x-y-x-y-x-y if that is the most efficient. Circle check screws complexity big time. – Kobor42 Feb 05 '13 at 15:25
  • This solution works for the Traveling salesman problem too, hands down. But that is proven that it's NP-hard. That would mean you solved The P-NP problem? No you didn't. T can be any big number, even bigger than N! Your complexity estimation is proper in a way that doesn't mean anything. – Kobor42 Feb 05 '13 at 16:51
  • @Kobor42: In general you're right, but here we have a constraint on T: T is between 3500 and 5000, so it's ok to use it in complexity estimation. As for circles, I didn't find in the statement that it's forbitten to visit same city twice. – Grigor Gevorgyan Feb 05 '13 at 18:27
  • Then again: THE SOLUTION IS WRONG AS IT IS!!! WILL YOU FIX IT? – Kobor42 Feb 06 '13 at 08:29
  • What then again ? I refuted your arguments in my previous comment. If you still think something is wrong - tell it. Or go and write your own answer. Still better then writing capsed comments. – Grigor Gevorgyan Feb 06 '13 at 09:21
  • Look at the very first comment. It is mine. I will quote it here: "It's wrong. It isn't checking for circles. It would apply route x-y-x-y-x-y-x-y" – Kobor42 Feb 25 '13 at 13:35
1

I am thinking of a backtracking based solution. You should define an algorithm to find the optimum solution (the one that gets more money). You end the algorithm either when you have travelled to all the cities or when you have exceeded the time you have. To ignore non profitable candidates: you have to test if the money to earn, based by the minimum number of cities that are still remaining to visit, is at least as the current optimum solution; and also check the minimum time that takes from going from one city to all that still are remaining.

To know the minimum quantity of money you will earn you have to multiply the number of cities that are still to be visited per the minimum quantity of money there's in one city.

The same applies to the minimum time you need to visit all remaining cities.

Edit: I forgot to tell you the cost of this algorithm is O(a^n), where a is the number of the arists of the graph (that is N*(N-1)) and n the number of vertices (that is N). The cost can be better if you define good functions to know when your actual candidate can't become a solution and also when it can't be better than the current optimal solution (if you're lucky to find a solution at the beggining of the iterating process, this really helps reducing the time to operate).

0

The amount of money in each town is not known, so the best possible route is the shortest route from one town to the next.

Here is what I would do if I were to program this in Javascript using recursion:

http://jsfiddle.net/rd13/ShL9X/5/

c = ['london', 'manchester', 'liverpool']; //Cities

t = {
    'liverpool': {
        'manchester': 130,
        'london': 260
    },
    'london': {
        'manchester': 250,
        'liverpool': 260
    },
    'manchester': {
        'london': 250,
        'liverpool': 130
    }
} //Time between cities

cn = 'london'; //Current city

var shortestDistance = 500,
    allottedTime = 300,
    next_city,
    time = 0,
    totalTime = 0,
    path = [cn];

optimal = function (cn) {

    for (i in t[cn]) {
        //So as not to visit cities that we have already visited
        if(path.indexOf(i)===-1) {
            next_city = t[cn][i] < shortestDistance ? i : next_city;
            shortestDistance = t[cn][i];
            totalTime = Math.min(t[cn][i] , shortestDistance);
        }
    }
    time += totalTime;

    path.push(next_city);

    if(time > allottedTime){
        document.write("The most optimal route between cities is: " + path);
    } else {
        optimal(next_city);
    }

}

optimal(cn);

Sorry no help on the algorithm, this is from a programming perspective as I thought it was an interesting challenge. The above isn't optimal mind.

StuR
  • 12,042
  • 9
  • 45
  • 66
  • @Kobor42 Which problem? – StuR Feb 05 '13 at 16:17
  • Ok. After carefully reading: 1. The code is wrong. "shortestDistance = t[cn][i]; totalTime = Math.min(t[cn][i] , shortestDistance);" This will always be the last t[cn][i] value. 2. In the problem it is written, that every Txy is optimal. No shorter way through other cities exist. 3. The original problem contains the preconception that WE KNOW how much money we want to collect. So shortest way to any city would leave out the rich ones. – Kobor42 Feb 06 '13 at 08:31
  • @Kobor42 Fair enough points. Although my understanding of the question was that we only know the distances Txy, and T.. and not the amount of money in each city. – StuR Feb 06 '13 at 10:09
-1

This is a variant of a well known problem normally known as the travelling salesman problem. You can find a detailed description of this and similar problems as well as further links on wikipedia here : http://en.wikipedia.org/wiki/Travelling_salesman_problem

  • Not it isn't. Travelling salesman problem requires to visit all the cities and is NP-hard, while this problem does not and has a polynomial solution, see my answer – Grigor Gevorgyan Feb 05 '13 at 14:44
  • I said that the problem was a VARIANT not identical to the travelling salesmane problem. – signal11 Feb 05 '13 at 15:09
  • Well then I don't understand how P problem can be a variant of NP one. What does variant mean then ? – Grigor Gevorgyan Feb 05 '13 at 15:14
  • Variant means similar but different in some way. The problem described is like an orienteering score event. The orienteer gets points for visiting controls. Each control has a different number of points(which is known). There is a time limit with points lost for finishing over the time limit. There is not enough time to visit all the controls. The winner is the orienteer who collects the most points. This problem is described in more detail here: which also has a reference to the travelling salesman problem. – signal11 Feb 05 '13 at 15:45