Lab 8: Word Snakes
Instructor: Dr. Todd Ebert
Suggested Due Date: April 22nd
Weighted Hamilton Paths
A Hamilton path for a graph G = (V, E) is a path that visits every vertex v ∈ V exactly once.
Moreover, if G’s edges are weighted, then we say that the path is a weighted path. In addition,
the weight of a path is the sum of the weights of all its edges. Finally, a path P is said to be the
heaviest iff no path weighs more than P .
Write a program that reads a weighted directed graph G from a text file, and finds its heaviest
Hamilton path (assuming it has one), or indicates that G has no Hamilton path. The text file will
consist of a single list of integers of the form a1 , b1 , w1 , a2 , b2 , w2 , . . . , am , bm , wm , where, e.g., (a1 , b1 )
represents the first directed edge, and w1 is its weight. Assume that each vertex of G is listed in one
or more of the edges (i.e. there are no isolated vertices).
Your program should find the heaviest Hamilton path by performing an exhaustive depth-first search,
starting at each of the different vertices. The following code should help get you started.
class PathFinder
{
Graph graph
double max_weight
GraphNode[] current_path
GraphNode[] solution
//Class method
void heaviest_ham_path(GraphNode node, double total_weight, int current_depth)
}
1
void heaviest_ham_path(GraphNode node, double total_weight, int current_depth)
{
GraphNode child
if(current_depth == graph.order() - 1) //a Hamilton Path has been found
{
record_solution(total_weight)
return
}
for each edge e in node.outgoing()
{
child = e.child()
if(child.is_visited()) //child has already been visited
continue
child.set_visited()
current_path[current_depth] = child
heaviest_ham_path(child, total_weight + e.weight(), current_depth+1)
child.set_not_visited()
}
}
Word Snakes
Let W be a set of n words. A word snake for W is permutation w1 , . . . , wn of the words such that,
for all i = 1, . . . , n − 1, a suffix of wi is a prefix of wi+1 . In other words, the end of current word
overlaps with the beginning of the next word in the permutation. Moreover, the length of the overlap
is the number of characters in the overlap. For example, the word broom has an overlap with the
word omnibus via the suffix/prefix om. In this case the length of the overlap is 2. Of course, there
could be several different word snakes for W. But we want to find the snake having the highest score.
The score of a word snake is given as L21 + · · · + L2n−1 , where Li is the length of the overlap between
wi and wi+1 , for all i = 1, . . . , n − 1.
Write a program that reads a text file that contains a set of words (one per line) W, and finds the
highest scoring word snake for W. Solve the problem using your algorithm for finding the heaviest
Hamilton path of a graph.
2
Summary of Program Options
1. Read in a weighted directed graph from a text file and display its vertices and list of weighted
edges.
2. Read in a weighted directed graph from a text file and display its heaviest Hamilton path, the
weights of each of its edges, and its total weight.
3. Read in a set of words W from a text file and order the words to form a word snake. Display
the word snake so that one word is printed per line, follwed by the overlap score produced by
that word’s suffix. For example,
broom 4
omibus 9
business
Include the total score for the word snake.
3
© Copyright 2026 Paperzz