1

I want to implement Tabu Search in Python to solve a problem related to a Graph (finding trees, coloring problem and that kind of stuff). I have written basic codes in Python and that's the first time I am writing something like that. I know how the algorithm works (in theory) and I am having some trouble to implement it.

What's the best way to generate the random solution so I can start the algorithm? I want to generate a random tree from an initial graph:

graph = { "a" : ["c"],
      "b" : ["c", "e"],
      "c" : ["a", "b", "d", "e"],
      "d" : ["c"],
      "e" : ["c", "b"],
      "f" : []
    }

I'm using these functions to see the edges and isolated nodes:

def generate_edges(graph):
    edges = []
    for node in graph:
        for neighbour in graph[node]:
            edges.append((node, neighbour))

    return edges

print(generate_edges(graph))


def find_isolated_nodes(graph):
    """ returns a list of isolated nodes. """
    isolated = []
    for node in graph:
        if not graph[node]:
            isolated += node
    return isolated

print(find_isolated_nodes(graph))

And this one to find the paths:

def find_all_paths(self, start_vertex, end_vertex, path=[]):
""" find all paths from start_vertex to
    end_vertex in graph """
graph = self.__graph_dict
path = path + [start_vertex]
if start_vertex == end_vertex:
    return [path]
if start_vertex not in graph:
    return []
paths = []
for vertex in graph[start_vertex]:
    if vertex not in path:
        extended_paths = self.find_all_paths(vertex,
                                             end_vertex,
                                             path)
        for p in extended_paths:
            paths.append(p)
return paths

What I need is to find a tree as a random solution so I can change it until is the biggest possible one in the graph I have.

What could be the best way to do that? Thank you.

Rory Daulton
  • 21,934
  • 6
  • 42
  • 50
Jo Olive
  • 57
  • 6
  • Hi Jo Olive, welcome to StackOverflow! :) Unfortunately, this isn't the best site for asking for recommendations. This site is used for specific questions with specific answers. It looks like you do have a specific question hidden within there though: "How can I generate a random solution?" Assuming that that's your specific question, we'll need more information. What have you tried so far and why didn't it work? What exactly is inside the `.txt` file you have? Adding that information to your question will help. – Pro Q Feb 25 '19 at 22:40
  • Also, don't worry if your question gets a few downvotes at first, hopefully people who vote your question down will also leave a comment explaining what you can do to make your question better. (This also serves as a reminder for people leaving downvotes to *leave a comment* unless I've already covered the reasons that a downvote was left.) – Pro Q Feb 25 '19 at 22:42
  • 1
    Thank you. Inside the file I have the information related to the initial graph: p edge 125 736 e 5 1 e 6 2 e 8 4 e 9 4 e 9 6 e 11 2 e 13 5 e 14 7 e 14 9 e 14 13 e 15 8 e 16 10 e 16 12 e 17 2 e 18 12 e 19 5 e 19 8 e 19 11 e 21 7 e 21 8 e 22 17 e 23 13 e 24 21 e 25 3 e 25 10 e 27 2 e 27 6 e 28 9 e 28 17 e 28 19 e 29 1 (...) I'll edit my question, you are right, thank you! – Jo Olive Mar 13 '19 at 13:08
  • Also, relooking at your question (and also relying on information I learned recently) would [this](https://stackoverflow.com/a/8279596/5049813) solve your problem? (I did a quick search for turning graphs into trees) – Pro Q Mar 13 '19 at 13:13
  • Thank you @ProQ, I'll take a look. – Jo Olive Mar 13 '19 at 14:11
  • @RoryDaulton I've edited it once again. Thanks – Jo Olive Mar 13 '19 at 14:18

0 Answers0