1. [25]. Implement the class Tree. Often given a graph, we end up finding a tree. Tree is also a graph with the addition of the relationship "parent": for every node we need to know the parent. In our case, we can have a field of the class to point to the graph that corresponds to the tree or the original graph from which we calculated the tree. The root of the tree has no parent, and this we will denote by the constant NONE which will be equal to "-1". We want to have the following methods:
setParent(node-id, parent-node-id)
getParent(node-id) , return parent-node-id
findChildren(node-id), return
an array of children
print(), print the tree
Partial Marks [-10]: The printing of the tree could be a simple output of the child-parent pairs.
Full Marks : The printing of the tree should be in a breadth first way by generation (hop distance from the root) and in the form: parent -> child
Assume the tree:
root -- node1 -- node3
|
\-- node4
|
|-- node2 -- node5
The output should look like:
0: root
1: root -> node1
1: root -> node2
2: node1 -> node3
2: node1 -> node4
2: node2 -> node5
Tip: we can use a simple array or linked-list to keep track of the parent information as we have said in class.
2. [50]. Implement Dijkstra's algorithm for shortest paths. You will read a directed weighted graph with positive weights. The source/root node is kept in the field "Source" and of the class with a default value of the first node-id that you read from the input file.
The output is:
2.a. The COST of the shortest path between
the source and every node.
2.b. The shortest path tree (with the print() method
you have implemented in the previous question).
3. [25]. Implement Prim's algorithm for minimum spanning trees. Here you can assume that the graph is undirected.
The output is:
3.a. The total cost of the minimum spanning tree.
3.b. The minimum spanning tree (with your print()
method).
Tip: Note that the Prim and Dijkstra algorithms have very similar
structure and the main difference is the "criterion of selecting the next
node". Try to take advantage of that to avoid retyping the same code.
Some careful planning may be needed though.
The user should be able to choose which algorithm (tree printing,
Prim, Dijkstra) to run.
This could be done through the use of an integer variable (1,2,3) correspondingly.
2
// for Dijkstra
30
1 2 103
// node with id : 1 gets to be the root
2 3 110
2 1 150
.........
n1 n2 weight
Also, again your program should be in multiple files and
compiling with a makefile.
Tree Data:
1
5
1 2 1
2 1 1
2 3 1
3 2 1
2 4 1
4 2 1
3 5 1
5 3 1
Prim's
1
5
1 2 1
2 1 1
2 3 10
3 2 10
2 4 11
4 2 11
1 4
20
4 1
20
3 5 13
5 3 13