1. overview
In this project, you will implement topological sorting in a directed acyclic graph. In addition, your implementation will:
represent the graph as an adjacency matrix
always choose the vertex with the lowest ID when given a choice (e.g., when iterating through the neighbors of a vertex and when selecting a vertex of in-degree 0 to start)
throw an exception if it encounters a cycle
I have provided you with a driver and a preliminary header file for the project. The provided header is just to get you started–you are allowed to change it however you like; however, your solution must be compatible with the provided driver file. You are strongly encouraged to add more data members and member functions to the class defined in the header.
2 Submitted code
You should submit a zip archive containing two files: digraph.h and digraph.cpp, which define and implement a DigraphMatrix class. This class must have a constructor that accepts a string (or const string&), and it must have a function topologicalSort that accepts no parameters and returns a vector of integers. The constructor must be able to construct the graph based on the name of an input file to read. (See section 3 for a description of the graph file format.) Moreover, it must represent the corresponding graph using an adjacency matrix.
The topologicalSort function should return the vertices of the vertices of the graph, in topo- logically sorted order if the directed graph does not contain a cycle. If the directed graph does contain a cycle, though, topologicalSort should throw an exception. The type of exception it throws (and its what function) are not important, but it must be an std::exception object or one that inherits from std::exception.
3 Input file
The input graph is described in edge list format. The first line of the file has the number of vertices and edges of the graph, separated by a space (n and m, respectively).
The following m lines have two nonnegative integers each. These integers, u and v, will be in the range 0 to n − 1, and they represent the endpoints of an edge in the graph. Specifically, they represent an edge from u to v.
4 Example 1
Consider running the topological sorting algorithm described in class on the graph below:
We start by finding a node with no incoming edges. In this graph, only vertex 6 has no incoming edges, so we start our DFS at vertex 6 and mark it as discovered. The neighbors of 6 are 0, 1, and 2. Since we should consider neighbors in sorted order, we recurse on the 0 and mark it as discovered. The only neighbor of 0 is 1, so we recurse on 1 and mark it as discovered. The first neighbor of 1 is 2, so we recurse on 2 and mark discovered. The neighbors of 2 are 5 and 7, so we recurse on 5 and mark it discovered. The only neighbor of 5 is 7, so we recurse on 7 and mark it discovered.
Vertex 7 has no outgoing neighbors, so we mark it explored and add it to our output array (7). When we return to 5, it has no other neighbors, so we mark it explored and add it to the array (7, 5). Returning to 2, we see that 7 has already been explored, so we mark 2 explored and add it to the array (7, 5, 2). Returning to 1, the other neighbor of 1 is 4, which we have not visited yet, so we recurse on 4 and mark it discovered. The only undiscovered neighbor of 4 is 3, so we recurse on 3 and mark it discovered. The neighbors of 3 are 2 and 7, both of which are explored, so we mark 3 as explored and add it to the array (7, 5, 2, 3). Returning back to 4, we’ve explored both of its neighbors, so we mark 4 explored and add it to the array (7, 5, 2, 3, 4). When we return to 1, we see that the other neighbors are 2 and 7, both of which are explored, so we mark 1 explored and add it to the array (7, 5, 2, 3, 4, 1). We return to 0, but its only neighbor was 1, so we mark it
explored and add to the array (7, 5, 2, 3, 4, 1, 0). Finally, we return to 6, and all of its neighbors (indeed, every other vertex of the graph) has been marked explored, so it too is marked explored and added to the array (7, 5, 2, 3, 4, 1, 0, 6). Reversing this array yields the final answer (6, 0, 1, 4, 3, 2, 5, 7). (Due to the structure of the graph, this is the only correct answer.)
5 Example 2
Consider running the topological sorting algorithm described in class on the graph below:
This graph has two vertices with in-degree zero, 3 and 5. Since we need to select the vertex with lowest ID, we start with 3. Vertex 3 has two neighbors, 6 and 7, so after marking it discovered, we recurse on 6 and mark 6 discovered. The neighbors of 6 are 0 and 2, so we recurse on 0 and mark it discovered. The only neighbor of 0 is 1, so we recurse on 1 and mark it discovered. The neighbors of 1 are 4 and 7, so we recurse on 4 and mark it discovered. Vertex 4 has no outgoing neighbors, so we mark it explored, add it to the list (4), and return to 1. Vertex 1 continues to its neighbor 7, which we mark as discovered. Vertex 7 has no outgoing neighbors, so we mark it explored and add it to the list (4, 1). When we return to 1, we have explored all outgoing neighbors, so we mark 1 explored and add it to the list (4, 7, 1). We then return to 0, which has no other neighbors, so we mark 0 explored and add it to the list (4, 7, 1, 0). We return from 6 and continue to the next neighbor, which is 2, and mark it discovered. Vertex 2 has no neighbors, so we mark it explored, add it to the list (4, 7, 1, 0, 2), and return to 6. Vertex 6 has no other neighbors after 2, so we
mark it explored and add it to the list (4, 7, 1, 0, 2, 6). Finally, we return to 3, mark it explored,
and add it to the list (4, 7, 1, 0, 2, 6, 3). Since we have not yet discovered vertex 5, we explore it,
and as 4 has already been explored, we mark 5 explored and add it to the list (4, 7, 1, 0, 2, 6, 3,
5). Reversing this list yield the final answer (5, 3, 6, 2, 0, 1, 7, 4).
6 Example 3
We will trace the topological sorting algorithm on the graph below:
This graph has 4 vertices with in-degree zero: 0, 2, 3, and 4. We start at 0 and mark it discovered. The only neighbor of 0 is 1, so we mark it discovered. Vertex 1 has no outgoing neighbors, so we mark it explored, add it to the list (1), and return to 0. Since 0 has no other neighbors, we mark it explored and add it to the list (1, 0). We then start again at 2. After marking 2 as discovered, we see that its only neighbor, 1, has been marked explored, so we mark 2 explored and add it to the list (1, 0, 2). We continue on to vertex 3. We mark 3 as discovered, and since it has no neighbors, we mark it explored and add it to the list (1, 0, 2, 3). When we continue to 4, we mark it as discovered and continue on to 5. We mark vertex 5 discovered and continue to vertex 6, which we mark as discovered and continue to vertex 7. At vertex 7, we see that vertex 5 is a neighbor, which creates a cycle (5, 6, 7, 5). At this point, your code should throw an exception.
Note that the graphs formed by removing vertex 4 or reversing the edge (4, 5) would also have cycles. You may wish to trace the topological sorting algorithm on one of these graphs, as well.
7 Grading
Your code will be evaluated based on whether it produces the correct output for some number of test cases. You may use any development environment that you like when developing the code; however, it will be compiled and run using g++ in a Linux environment. Code that does not compile will not receive substantial credit, so be sure that your code can be compiled using g++.
49 Answers
93 Answers
DescriptionIn this final assignment, the students will demonstrate their ability to apply two majorconstructs of the C programming language – Functions and Arrays – to solve computationalproblems.Arrays provide a convenient way to store &
The standard path finding involves finding the (shortest) path from an origin to a destination, typically on a map. This is anPath finding involves finding a path from A to B. Typically we want the path to have certain properties,such as being the shortest or to avoid going through certain obstacles. As the main aim is to thinkabout path finding, we focus on the common task
Develop a program to emulate a purchase transaction at a retail store. This program will have two classes, a LineItem class and a Transaction class. The LineItem class will represent an individualDevelop a program to emulate a purchase transaction at a retail store. Thisprogram will have two classes, a LineItem class and a Transaction class. TheLineItem class will represent an individual line item of merchandise that acustomer is purchasing.
SeaPort Project series For this set of projects for the course, we wish to simulate some of the aspects of a number of Sea Ports. Here are the classes and their instance variables we wish to define:1 Project 1 Introduction - the SeaPort Project series For this set of projects for the course, we wish to simulate some of the aspects of a number of Sea Ports. Here are the classes and their instance variables we wish to define: SeaPortProgram e
Project 2 Introduction - the SeaPort Project series For this set of projects for the course, we wish to simulate some of the aspects of a number of Sea Ports. Here are the classes and their instance variables we wish to define:1 Project 2 Introduction - the SeaPort Project series For this set of projects for the course, we wish to simulate some of the aspects of a number of Sea Ports. Here are the classes and their instance variables we wish to define: SeaPortProgram e
The first programming project involves writing a program that parses, using recursive descent, a GUI definition language defined in an input file and generates the GUI that it defines. The grammar for this language is defined below:1Project 1The first programming project involves writing a program that parses, using recursive descent, a GUIdefinition language defined in an input file and generates the GUI that it defines. The grammar for thislanguage is defined below:gui ::= Wi
CMSC 335 Project SeaPort Solved Project 3 Introduction the SeaPort Project series For this set of projects for the course we wish to simulate some of the aspects of a number of Sea PortsCMSC 335 Project SolvedProject 3 Introduction - the SeaPort Project series For this set of projects for the course, we wish to simulate some of the aspects of a number of Sea Ports. Here are the classes and their instance variables we wish to define:
CMSC 335 Project 4 Solved SeaPort Introduction the SeaPort Project series For this set of projects for the course we wish to simulate some of the aspects of a number of Sea Ports Here are the classes and their instance variables we wish to defineProject 4 Introduction - the SeaPort Project series For this set of projects for the course, we wish to simulate some of the aspects of a number of Sea Ports. Here are the classes and their instance variables we wish to define: SeaPortProgram ext
CMSC 451 Project 2 Solved The second project involves completing and extending the C++ program that evaluates statements of an expression language contained in the module 3 case studyProject 2 The second project involves completing and extending the C++ program that evaluates statements of an expression language contained in the module 3 case study. The statements of that expression language consist of an arithmetic expression f
CMSC 451 Project 1 Solved The first project involves benchmarking the behavior of Java implementations of one of the following sorting algorithms bubble sort selection sort insertion sort Shell sort, merge sort quick sort or heap sortCMSC 451 Project 1The first project involves benchmarking the behavior of Java implementations of one of thefollowing sorting algorithms, bubble sort, selection sort, insertion sort, Shell sort, merge sort,quick sort or heap sort. You must post your
The ready solutions purchased from Library are already used solutions. Please do not submit them directly as it may lead to plagiarism. Once paid, the solution file download link will be sent to your provided email. Please either use them for learning purpose or re-write them in your own language. In case if you haven't get the email, do let us know via chat support.
Get Free Quote!
251 Experts Online