Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

How to do this with an adjacency matrix? Write a C++ program called ts.cpp that implements the topological sorting algorithm based on the DFS algorithm

How to do this with an adjacency matrix?

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 determine if the input graph is a DAG (= directed acyclic graph) or not. If the graph is not a DAG, your program has to stop without further processing. However, if its a DAG, your program should 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 we grade your programming assignment, we will use the g++ compiler on the cloud9. So, you must use the cloud9 for the homework. Additionally, you must include five items such as Title, Abstract, Class ID, Name, and Date at the beginning of the program as head comments.

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

3

2

0 1

1 2

The first line (= 3 in the example) indicates that there are three vertices in the graph. The second line (= 2 in the example) presents the number of edges in the graph. The remaining two lines are the edge information in the graph. For the homework, you should 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 theres no blank space at the end of each line. If your program does not read the file properly, your program will get no credit. And also, note that you used this format at the homework 2.

This is a sample run of the program on the cloud9. Your program should be compiled and executed exactly like this.

$ g++ -o ts ts.cpp

$ ./ts

Enter a filename: C:\\tmp\\t1.txt

This is a DAG.

Start node(s): 0

Popping-off order: 2 1 0

Topological sort: 0 -> 1 -> 2

In the program, your program has to follow our convention (= ascending order) as you learned in the class.

This is another sample input file called t2.txt.

4

4

0 1

1 2

2 3

3 1

t2.txt describes a directed graph like below:

This is a sample run on the cloud9:

$ g++ -o ts ts.cpp

$ ./ts

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

This is not a DAG.

This is the last sample input file called t3.txt.

5

5

2 3

3 4

1 2

0 2

2 4

t3.txt describes a directed graph like below:

This is a sample run on the cloud9:

$ g++ -o ts ts.cpp

$ ./ts

Enter a filename: C:\\tmp\\t3.txt

This is a DAG.

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). Thus, your program starts from the node 0 between the two possible starting nodes 0 and 1.

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Professional Visual Basic 6 Databases

Authors: Charles Williams

1st Edition

1861002025, 978-1861002020

More Books

Students also viewed these Databases questions