|
CS 3343 Analysis of Algorithms Fall 2012 Recitation 11 A Few Answers In-Class Problems Week 11: Nov 06-Nov 08 |
| In Java Language | In C Language |
|---|---|
public void printGraph() {
// print the data in the graph,
// perhaps in the form below
}
public int edgeLength(int v1, int v2) {
// if (v1, v2) is an edge
// return edgeLen of this edge
// otherwise return -1
}
public int pathLength(int[] path) {
// calculate and return the length
// of the path specified by the input
// parameter. If the "path" includes
// a non-edge return -1
}
| void printgraph(struct node* graph[],
int count) {
// print the data in the graph,
// perhaps in the form below
}
int edgelength(struct graphnode* graph[],
int v1, int v2) {
// if (v1, v2) is an edge
// return edgelen of this edge
// otherwise return -1
}
int pathlength(struct node* graph[],
int path[], int plen) {
// calculate and return the length
// of the path specified by the input
// parameter. If the "path" includes
// a non-edge return -1
}
|
// three Hamiltonian paths
int[] Ham1 =
{47,45,42,44,46,43,41,40,39,37,33,38,
34,28,29,24,21,16,10,15,20,23,27,32,
26,36,35,31,30,25,22,18,17,11, 7,12,
8,13,19,14, 9, 5, 4, 0, 2, 1, 6, 3};
int[] Ham2 =
{47,45,42,44,46,43,41,40,38,39,37,32,
36,35,31,30,25,22,17,11,12,18,26,27,
33,34,29,28,23,19,13,14,20,24,21,15,
16,10, 6, 5, 9, 8, 7, 4, 1, 0, 2, 3};
int[] Ham3 =
{47,45,42,44,46,43,41,40,39,37,33,32,
36,35,31,30,25,26,27,28,23,20,19,18,
22,17,11,12,13,14,15, 9, 8, 7, 4, 5,
1, 0, 2, 3, 6,10,16,21,24,29,34,38};
System.out.println("Path length: " +
graph.pathLength(Ham2));
| // three Hamiltonian paths
int ham1[] =
{47,45,42,44,46,43,41,40,39,37,33,38,
34,28,29,24,21,16,10,15,20,23,27,32,
26,36,35,31,30,25,22,18,17,11, 7,12,
8,13,19,14, 9, 5, 4, 0, 2, 1, 6, 3};
int ham2[] =
{47,45,42,44,46,43,41,40,38,39,37,32,
36,35,31,30,25,22,17,11,12,18,26,27,
33,34,29,28,23,19,13,14,20,24,21,15,
16,10, 6, 5, 9, 8, 7, 4, 1, 0, 2, 3};
int ham3[] =
{47,45,42,44,46,43,41,40,39,37,33,32,
36,35,31,30,25,26,27,28,23,20,19,18,
22,17,11,12,13,14,15, 9, 8, 7, 4, 5,
1, 0, 2, 3, 6,10,16,21,24,29,34,38};
printf("Path length: %i\n",
pathlength(graph, ham3, 48));
|
(Start of definition of function:)
hamPaths(int u, int pathLen, int edges):
(On entering the color of u must be White.)
Set u's color to Black. (u is on the current path)
For each vertex v in u's adjacency list do the following:
If the color of v is White then
(You need to know the edge length from u to v.)
(Call hamPaths recursively using)
hamPaths(v, new path length, new # of edges);
(reflecting the addition of this edge to the path)
(In case the color of v is Black do nothing,
since the vertex is already on the current path)
(Finish up the call to hamPaths:)
Set color of u to White (no longer on current path)
(Return from hampaths.)
|
public void hamPathsStart() { // for "small.txt"
// set color of vertices 10,9 to Black
// push vertices 10,9
hamPaths(8, 133, 2);
}
|
public void hamPathsStart() { // for 48-capitals
// set color of vertices 47,46,45,44,43,42 to Black
// push vertices 47,45,42,44,46,43 in that order
hamPaths(41, 709, 6););
}
|