C++ program called ts.cpp Solution

Write a C++ program called ts.cpp that implements the topological sorting algorithm based on the DFS algorithm. Your program should read an input file name and display the starting node(s), popping-off order, and topologically sorted list. In the problem, you can assume that the number of nodes in the input file is less than 100. When you write the program, don’t forget to include “Title”, “Abstract”, “ID (A four-digit number)”, “Name”, and “Date”.

Input file format: This is a sample input file called t1.txt.



The first line (= 3 in the example) indicates that there are three vertices in the graph. For the homework, we can assume that the first vertex starts from the number 0. Thus, t1.txt describes a directed graph like below:



One blank space is used to delimiter the data. Note that there’s no blank space at the end of each line. If your program does not read the file properly, your program will get no credit.

The following presents a sample run of the program. Your program should be compiled and executed exactly like this.

Enter filename: C:\\tmp\\t2.txt

Start node(s): 0 1

Popping-off order: 4 3 2 0 1

Topological sort: 1 - 0 - 2 - 3 - 4

In the program, your program has to follow our convention (= ascending order).

This is another sample input file called t2.txt.



This is another sample run:

Enter filename: C:\\tmp\\t2.txt

Start node(s): 0 1

Popping-off order: 4 3 2 0 1

Topological sort: 1 - 0 - 2 - 3 - 4

Again, your program must follow our convention (= ascending order). So, your program starts from the node 0 between the two possible starting nodes 0 and 1. And also, you should follow our convention in the DFS algorithm.
Powered by