Laboratory Exercises, C++ Programming
Laboratory Exercises, C++ Programming
Laboratory Exercises, C++ Programming
Create successful ePaper yourself
Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.
Tools for Practical <strong>C++</strong> Development 21<br />
/* insert an arc from the vertex ‘from’ to the vertex ‘to’ */<br />
void add_arc(const std::string& from, const std::string& to);<br />
/* return the vertex names in reverse topological order */<br />
std::stack top_sort() throw(cyclic);<br />
/* print the vertex list (for debugging) */<br />
void debugPrint() const;<br />
private:<br />
void insert(Vertex* v); // insert the vertex pointer v in vptrs, if<br />
// it’s not already there<br />
Vertex* find(const std::string& name) const; // return a pointer to the<br />
// vertex named name, 0 if not found<br />
void dfs_visit(Vertex* v, std::stack& result) throw(cyclic);<br />
// visit v in the traversal algorithm<br />
};<br />
std::vector vptrs; // vector of pointers to vertices<br />
VertexList(const VertexList&); // forbid copying<br />
VertexList& operator=(const VertexList&);<br />
Finally, the class Graph is merely a typedef for a VertexList:<br />
Notes:<br />
typedef VertexList Graph;<br />
• add vertex in VertexList should create a new Vertex object and add it to the vertex list.<br />
It should do nothing if a vertex with that name already is present in the list.<br />
• add arc should insert to in from’s adjacency list. It should start with inserting from and<br />
to in the vertex list — in that way arcs and vertices may be added in arbitrary order.<br />
• find is used in add vertex and in insert.<br />
• The VertexList destructor is quite difficult to write correctly, since you have to be careful<br />
so no Vertex object is deleted more than once. It is easiest to start by removing pointers<br />
from the adjacency lists so the graph isn’t circular, before deleting the objects. — You may<br />
treat the destructor as optional, if you wish.<br />
• Advice: draw your own picture of a graph structure, with all objects, vptrs and pointers.<br />
A7. The classes are in the files vertex.h, vertex.cc, vertexlist.h, vertexlist.cc, graph.h. Implement<br />
the classes, except top sort and dfs visit, and test using the program graph test.cc.<br />
Comment out the lines in the function testGraph that check the top sort algorithm.<br />
4.3 Topological Sort<br />
A topological ordering of the vertices of a graph is essentially obtained by a depth-first search<br />
from all vertices. (There are other algorithms for topological sorting. If you prefer another<br />
algorithm you are free to use that one instead.) The topological ordering is not necessarily unique.<br />
For instance, the vertices of the graph in the preceding section may be ordered like this:<br />
A C B D E or<br />
C A D B E or . . .<br />
The algorithm is described below, in pseudo-code. The algorithm produces the vertices in reverse<br />
topological order, which is why we use a stack for the result.