-2

In the longest word chain problem -

Make a longest word chain from given word set so that the subsequent word is starting from last character of preceding word.

I am confused with the algorithm which matches with the following problem, tried Levenshtein distance but it is not what is required in the problem. Any help will be appreciated to suggest the algorithm or any links to provide more information.

linux c
  • 19
  • 1
  • 1
  • Does being a chain require that it not repeat the same word? ​ ​ –  Jan 28 '16 at 03:13

1 Answers1

1

This problem maps to the longest path problem, from Graph Theory: Each word is a node in a graph, and two nodes have a directed edge between them if the second word begins with the last character of the first word. This problem is NP-hard. This means that you can only solve it optimally if the number of words is small. If the number of words is big, then different heuristics can be tried, yielding sub-optimal, but perhaps good enough results.

If the number of words is less than 20, and there are probably only few edges for each edge, then is should be possible to enumerate all possible acyclic paths. Then you just need to choose the longest one.

def generate_all_paths(graph):
  paths = []
  for node in graph.nodes:
    paths.append(generate_all_paths_beginning_at_node(n,graph))
  return paths

def generate_all_paths_beginning_at_node(start_node,graph):
  paths = []
  reduced_graph = extract_node_from_graph(graph,start_node)
  for next_node in graph.get_nodes_that_have_an_edge_from(start_node)
    sub_paths = generate_all_paths_beginning_at_node(next_node,reduced_graph)
    paths.append([[start_node] + p for p in sub_paths]) # Add start_node to all paths generated from the reduced_graph.
  if paths==[]: # No edge coming out of start_node
    paths = [[start_node]] # The only solution is a path of length 0 (one node).
return paths
Adi Levin
  • 5,165
  • 1
  • 17
  • 26
  • yes we need optimal solution and considering the quantity of words it will be less than 20 – linux c Jan 26 '16 at 17:56
  • Thanks and can you let me know the prupose for start_node, i think it restrict the count of words ? – linux c Jan 27 '16 at 01:58
  • The function generate_all_paths_beginning_at_node() finds all the acyclic paths in a graph beginning with a given node, this is the start_node. In order for general_all_paths() to cover all possible paths, we loop over all nodes in the graph, and send each one of them to generate_all_paths_beginning_at_node() as a start node. – Adi Levin Jan 27 '16 at 06:16
  • longest-path-in-a-graph-of-maximum-degree-2 also "maps to the longest path problem, from Graph Theory: Each" node in the input is a node in the output, and two nodes in the output "have a directed edge between them if" and only if they have a directed edge between them in the input. ​ (continued ...) ​ ​ ​ –  Jan 28 '16 at 03:12
  • (... continued) ​ In other words, giving a mapping from the OP's problem to to the longest path problem only shows that the OP's problem is in [F](https://complexityzoo.uwaterloo.ca/Complexity_Zoo:F#fp)P^[||](https://complexityzoo.uwaterloo.ca/Complexity_Zoo:P#pparnp)F[NP](https://complexityzoo.uwaterloo.ca/Complexity_Zoo:F#fnp), rather than [any hardness of the OP's problem](http://stackoverflow.com/a/33636224/380772). ​ ​ ​ ​ –  Jan 28 '16 at 03:12