Question
How to do this with an adjacency matrix? Please update my code Write a C++ program called ts.cpp that implements the topological sorting algorithm based
How to do this with an adjacency matrix? Please update my code
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.
My code start here
#include
#include
#include
#include
using namespace std;
void dfs(int adj_mat[][100], int count, int currVert, int nodes, int visited[]) {
// cout << currVert << " ";
stack
numbers.push(currVert);
// visited[currVert] = 1;
count++;
visited[currVert] = count;
for (int adjVert = 0; adjVert < nodes; adjVert++) {
if (visited[adjVert] == 0 && adj_mat[currVert][adjVert] == 1) {
dfs(adj_mat, count, adjVert, nodes, visited);
}
}
while (!numbers.empty()) {
cout << numbers.top();
numbers.pop();
}
}
int main() {
ifstream inFile;
string filename;
cout << "Enter a filename: ";
cin >> filename;
inFile.open(filename.c_str());
int nodes, edges, vert1, vert2;
inFile >> nodes >> edges;
inFile >> vert1 >> vert2;
int adj_mat[100][100];
int visited[nodes];
vector
vector
vector
vector
for (int vert = 0; vert < nodes; vert++) {
visited[vert] = 0;
}
for (int row = 0; row < nodes; row++) {
for (int col = 0; col < nodes; col++) {
adj_mat[row][col] = 0;
}
}
adj_mat[vert1][vert2] = 1;
numbers1.push_back(vert1);
numbers2.push_back(vert2);
for (int i = 0; i < edges-1; i++) {
int start, dest;
inFile >> start >> dest;
numbers1.push_back(start);
numbers2.push_back(dest);
adj_mat[start][dest] = 1;
}
for (int i = 0; i < numbers1.size(); i++) {
if (numbers1[i] == 0 || numbers1[i] == 1) {
startVert.push_back(numbers1[i]);
destVert.push_back(numbers2[i]);
}
}
bool check;
for (int i = 0; i < destVert.size()-1; i++) {
if (destVert[i] != destVert[i + 1]) {
check = false;
break;
} else {
check = true;
}
}
cout << endl;
cout << "Start node(s): ";
if (startVert.size() == 2) {
if (check) {
if (adj_mat[startVert[0]][destVert[0]] == adj_mat[startVert[1]][destVert[0]]) {
cout << "There are two starting vertices!" << endl;
if (startVert[0] == 1) {
startVert[0] = 0;
startVert[1] = 1;
}
for (int i = 0; i < startVert.size(); i++) {
cout << startVert[i] << " ";
}
cout << endl;
}
} else {
cout << startVert[0] << endl;
}
} else {
for (int i = 0; i < startVert.size(); i++) {
cout << startVert[i] << " ";
}
cout << endl;
}
dfs(adj_mat, 0, 0, startVert[0], visited);
}
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started