Question

Write a C++ program called ts.cpp that implements the topological sorting algorithm based on the DFS...

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 it’s 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 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. 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.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Implemented c++ source code:-
======================
#include<iostream>
#include <fstream>
#include <list>
#include <stack>
using namespace std;
class DAG_Graph
{
public:
int Vertices;
list<int> *adj;
public:
DAG_Graph(int Vertices)
{
this->Vertices=Vertices;
adj = new list<int>[Vertices];
}   
void addEdge(int Vertices, int width)
{
adj[Vertices].push_back(width);
}
void Topo_Sort()
{
stack<int> Stack;
bool *visited = new bool[Vertices]; // Mark all the vertices as not visited
for(int i=0;i<Vertices;i++)
visited[i] = false;
for(int i=0;i<Vertices;i++)
if(visited[i]==false)
SortUtil(i, visited, Stack);
while(Stack.empty() == false)
{
cout<< Stack.top()<<"-> ";
Stack.pop();
}
}
void SortUtil(int v, bool visited[], stack<int> &Stack)
{
visited[v]=true;
list<int>::iterator i;
for (i = adj[v].begin(); i != adj[v].end(); ++i)
if (!visited[*i])
SortUtil(*i, visited, Stack);
Stack.push(v);
}
};
int main()
{
char filename[]="Topo.txt";
cout<<"\n-----------------------------------------";
cout<<"\n**** Implementation of Topological sort ***";
cout<<"\n-----------------------------------------";
cout<<"\nPlease enter the Filename ";
cin>>filename;
ifstream infile;
infile.open(filename);
int length,width,vertices;
infile>>length;
infile>>length;
DAG_Graph graph(length);
for(int i=2;i<length;i++)
{
infile>>vertices;
infile>>width;
graph.addEdge(vertices,width);
}
cout<<"\nThe Order is: "<<endl;
graph.Topo_Sort();
infile.close();
return 0;
}

input file(Topo.txt):-
===============
5
5
2 3
3 4
1 2
0 2
2 4

Sample Output:-
============
-----------------------------------------
**** Implementation of Topological sort ***
-----------------------------------------
Please enter the Filename Topo.txt
The Order is:
1-> 2-> 3-> 4-> 0->
--------------------------------
Process exited after 4.888 seconds with return value 0
Press any key to continue . . .





C:\UsersiVENKEWDesktop\Chegg-TasksTopo-Sort.exe Implementation of Topological sort C3 Please enter the Filename Topo.txt The Order is: Process exited after 4.888 seconds with return value e Press any key to continue .. .

Add a comment
Know the answer?
Add Answer to:
Write a C++ program called ts.cpp that implements the topological sorting algorithm based on the DFS...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Write a Java program called Histogram.java that displays a list of distinct characters in an input...

    Write a Java program called Histogram.java that displays a list of distinct characters in an input tile and the occurrence of each eharacte. Your iogram should 1ead an input file name from a use. After that, your program should read characters in the file and display a list of distinct characters and their occurTeces. Finally, your program should draw a veril bafo the occuences For the assignment, your program has.to display the result exactly as the sample run. For instance,...

  • C++ (1) Write a program to prompt the user for an input and output file name....

    C++ (1) Write a program to prompt the user for an input and output file name. The program should check for errors in opening the files, and print the name of any file which has an error, and exit if an error occurs. For example, (user input shown in caps in first line, and in second case, trying to write to a folder which you may not have write authority in) Enter input filename: DOESNOTEXIST.T Error opening input file: DOESNOTEXIST.T...

  • In c programming . Part A: Writing into a Sequential File Write a C program called...

    In c programming . Part A: Writing into a Sequential File Write a C program called "Lab5A.c" to prompt the user and store 5 student records into a file called "stdInfo.txt". This "stdInfo.txt" file will also be used in the second part of this laboratory exercise The format of the file would look like this sample (excluding the first line) ID FIRSTNAME LASTNAME GPA YEAR 10 jack drell 64.5 2018 20 mina alam 92.3 2016 40 abed alie 54.0 2017...

  • Shortest Path (DAG) Students, In this coding assignment, you will be asked to write Java code...

    Shortest Path (DAG) Students, In this coding assignment, you will be asked to write Java code to read a text file (input.txt) of a graph represented as an adjacency list. Example: A, 7, B, 9, C B, 1, C C, 8, E, 2, F D, 1, E, 3, G E, 3, G F, 3, D, -3, H H, 6, G K, 3, H, -1, F The graph represented by the adjacency list above looks like this: The graph is a...

  • Please provide C language code no c++ ,txt file also needed with comment Finish task 5...

    Please provide C language code no c++ ,txt file also needed with comment Finish task 5 Task5: Breadth First Search (15 pts) · Write a program to read the graph information from the file and traverse the graph using BFS algorithm as introduced in lecture. The input for each algorithm is an undirected unweighted connected graph stored in a local file using an adjacency list. Following is the example of the input file (graph.txt) and the graph First line is...

  • In C++ Design a format for storing graphs in files. This will store your graph into a file with t...

    in C++ Design a format for storing graphs in files. This will store your graph into a file with the following requirements: The first line will contain the number of Vertices. The second line will have a ‘U’ or a ‘D’ to tell the system if it is Undirected or Directed. The rest of the lines will be here for each of the edges. Each edge will contain three pieces of information: An integer containing the first Vertex An integer...

  • in c prog only please!! Name this program one.c - This program takes two command line...

    in c prog only please!! Name this program one.c - This program takes two command line arguments: 1. an input filename 2. a threshold Your program will create two files: 1. even.txt - contains all integers from the input file that are even and greater than the threshold 2. odd. txt - contains all integers from the input file that are odd and greater than the threshold • The input file will exist and only contain a set of integers....

  • Write a complete C++ program that defines the following class: Class Salesperson { public: Salesperson( );...

    Write a complete C++ program that defines the following class: Class Salesperson { public: Salesperson( ); // constructor ~Salesperson( ); // destructor Salesperson(double q1, double q2, double q3, double q4); // constructor void getsales( ); // Function to get 4 sales figures from user void setsales( int quarter, double sales); // Function to set 1 of 4 quarterly sales // figure. This function will be called // from function getsales ( ) every time a // user enters a sales...

  • Please help with program this. Thank you so much in advance! Create a Java program which...

    Please help with program this. Thank you so much in advance! Create a Java program which implements a simple stack machine. The machine has 6 instructions Push operand Puts a value on the stack. The operand is either a floating point literal or one of 10 memory locations designated MO M9 Pop operand Pops the value on the top of the stack and moves it to the memory location MO-M9 Add Pops the top two values off the stack, performs...

  • plz use python to answer it, thanks in advance 3. Tree and back arcs in a DFS 40 Marks For a given set of digraphs, write a program that performs DFS on each digraph starting at node 0 and prints...

    plz use python to answer it, thanks in advance 3. Tree and back arcs in a DFS 40 Marks For a given set of digraphs, write a program that performs DFS on each digraph starting at node 0 and prints out the total number of tree arcs and back arcs resulting from the traversal. Use our standard convention that when there is a choice of white or grey nodes, the one with the lowest index should be chosen. Input format:...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT