0

Having a couple of cities and their locations I want to create a data structure that would represent a graph like this. This graph represent all possible paths that can be taken in order to visit every city only once:

graph

My question is, since this is probably a very common problem, is there an algorithm or already made data structure to represent this? The programming language is not important (although I would prefer java).

Z.Szymon
  • 337
  • 1
  • 13
  • 1
    Your graph is a rooted tree (root is the upper vertex where the path starts). You can use any way to store a tree. – Andreikkaa Mar 27 '19 at 12:35
  • 1
    That is sort of broad. There are several possible data structures to represent a graph. You diagram suggests you are interested in a tree structure in particular, this can be encoded pretty easily, see [Java tree data-structure?](https://stackoverflow.com/q/3522454). If you want a more comprehensive implementation you can look at some existing library like [Guava](https://github.com/google/guava). – jdehesa Mar 27 '19 at 12:39

3 Answers3

1

This tree is bad. There are redundant data in it. For instance connection between nodes 2 and 4 occurs three times in the tree. You want a "structure" that automatically gives the solution to your problem, so that it's easier for you, but that's not how problem solving works. Input data is one set of data, output data is another set of data, and they could appear similar, but they can also be quite different.

One simple matrix with one triangle empty and the other containing data should have all the information you need. Coordinates of the matrix are nodes, cells are distances. This is your input data.

What you do with this matrix in your code is a different matter. Maybe you want to write all possible paths. Then write them. Use input data and your code to produce output data.

Dialecticus
  • 16,400
  • 7
  • 43
  • 103
1

What you are looking for is actually a generator of all permutations. If you keep one city fixed as the first one (London, in your diagram), then you need to generate all permutations of the list of all your remaining nodes (Warsaw, Łódź, Berlin).

Often such an algorithm is done recursively by looping over all elements, taking it out and doing this recursively for the remaining elements. Often libraries are use to achieve this, e. g. itertools.permutations in Python.

Each permutation generated this way should then be put in the resulting graph you originally wanted. For this you can use any graph-representation you would like, e. g. a nested dictionary structure:

{ a: { b: { c: d,
            d: c },
       c: { b: d,
            d, b },
       d: { b: c,
            c: b } } }
Alfe
  • 56,346
  • 20
  • 107
  • 159
1

Your problem seems very close to the traveling salesman problem, a classic among the classics.
As you did intuite, the graph that will represent all the possible solutions is indeed a tree (the paths from the root to any of its leaf should represent a solution).
From there, you can ask yourself several questions:

  • Is the first city that I'll visit an important piece of information, or is it only the order that matters ? For instance, is London-Warsaw-Berlin-Lidz equivalent to Warsaw-Berlin-Lidz-London ?
    Usually, we consider these solutions as being equivalent to solve a TSP, but it might not be the case for you.

  • Did you see the link between a potential solution to the TSP and a permutation ? Actually, what you're looking for is a way (and the data structure that goes with it) to generate all the permutations of a given set(your set of cities).

With these two points in mind, we can think about a way to generate such a tree. A good strategy to work with trees is to think recursively.
We have a partial solution, meaning the k first cities. Then, the next possible city can be any among the n-k cities remaining. That gives the following pseudo-code.

get_all_permutations(TreeNode node, Set<int>not_visited){
    for (city in not_visited){
        new_set = copy(not_visited);
        new_set.remove(city);
        new_node = new TreeNode();
        new_node.city = city;
        node.add_child(new_node);
        get_all_permutations(new_node, new_set);
    }
}

This will build the tree recursively.
Depending on your answer to the first point I mentioned (about the importance of the first city), you might want to assign a city to the root node, or not.

Some good points to look in, if you want to go further with this kind of problem/thinking, are enumeration algorithms, and recursive algorithms. They're generally a good option when your goal is to enumerate all the elements of a set. But they're also generally an inefficient way to solve problems (for example, in the case of the TSP, solving using this algorithm results in a very inefficient approach. There are some much much better ones).

m.raynal
  • 2,983
  • 2
  • 21
  • 34