In this C++ program, you will use concepts from Chapter 9 to read data from a file so that you can construct a weighted directed graph data structure in a "modified" adjacency list format. Your graph will support the following operations: (1) printing the adjacency list, (2) printing the single-source shortest path to all vertexes using Dijkstra's algorithm, (3) printing the indegree of each vertex, and (4) printing a topological sort of the graph.
You will prompt for and read in a data file that is used to build the weighted directed graph data structure
The input format of the file is as files: one line per vertex, where the first element (a character) is the vertex for that entry. The optional remaining elements are organized by any number of distance (an integer) and vertex (a character) pairs, such B 6 D 5 E, which shows the data for the vertex B, connected to vertex D through a distance of 9 and E through a distance of 5.
You are initially given the base Vertex class that you may add to as follows:
#include < map>
using namespace std;
class Vertex
{
public:
Vertex();
~Vertex();
private:
char name;
map< char, int> adj;
bool known;
int dist;
char path;
int indegree;
};
For example, you may add a copy constructor, a move constructor, a copy or move assignment operator =, in addition to any methods, such as accessors and mutators, but you may not modify or remove any of the given private data members. The adjacency list for each vertex is supported by the map class that contains the adjacent vertex (a character) and its distance (an integer). It is expected that you will have a Vertex.h and a Vertex.cpp file for your implementation of the Vertex class.
You will display a menu with the following options in a loop until the user enters the proper value to exit the menu (and the program):
Enter option choice 1 - 5:
(1) Print Adjacency List
(2) Print Single-Source Shortest Path
(3) Print Indegree of Each Vertex
(4) Print Topological Sort of Graph
(5) Exit Program
>
All other options will result in an error message followed by the menu choices again.
Your code should be well documented in terms of comments. For example, good comments in general consist of a header (with your name, course section, date, and brief description), comments for each variable, and commented blocks of code.
The results in the following sample output are based on the input file:
A 9 B 2 D 5 C
B 6 D 5 E
C 5 E
D 4 C 4 E
E
This data represents the following weighted, directed graph: see image.
$ more in1.txt
A 9 B 2 D 5 C
B 6 D 5 E
C 5 E
D 4 C 4 E
E
$ ./a.out
Enter the name of the input file: in1.txt
File in1.txt successfully loaded into graph.
Enter option choice 1 - 5:
(1) Print Adjacency List
(2) Print Single-Source Shortest Path
(3) Print Indegree of Each Vertex
(4) Print Topological Sort of Graph
(5) Exit Program
> 1
Adjacency List:
A : ->(B,9)->(C,5)->(D,2)
B : ->(D,6)->(E,5)
C : ->(E,5)
D : ->(C,4)->(E,4)
E :
Enter option choice 1 - 5:
(1) Print Adjacency List
(2) Print Single-Source Shortest Path
(3) Print Indegree of Each Vertex
(4) Print Topological Sort of Graph
(5) Exit Program
> 2
Enter Source Vertex (char) for Shortest Path: A Single-Source Shortest Path from A: A -> A : 0
A -> B : 9
A -> C : 5
A -> D : 2
A -> D -> E : 6
Enter option choice 1 - 5:
(1) Print Adjacency List
(2) Print Single-Source Shortest Path
(3) Print Indegree of Each Vertex
(4) Print Topological Sort of Graph
(5) Exit Program > 2
Enter Source Vertex (char) for Shortest Path: B Single-Source Shortest Path from B:
Warning: Not All Vertices May Be Reached.
B -> B : 0
B -> D -> C : 10
B -> D : 6
B -> E : 5
Enter option choice 1 - 5:
(1) Print Adjacency List
(2) Print Single-Source Shortest Path
(3) Print Indegree of Each Vertex
(4) Print Topological Sort of Graph
(5) Exit Program > 2
Enter Source Vertex (char) for Shortest Path: C Single-Source Shortest Path from C:
Warning: Not All Vertices May Be Reached.
Warning: Not All Vertices May Be Reached.
Warning: Not All Vertices May Be Reached.
C -> C : 0
C -> E : 5
Enter option choice 1 - 5:
(1) Print Adjacency List
(2) Print Single-Source Shortest Path
(3) Print Indegree of Each Vertex
(4) Print Topological Sort of Graph
(5) Exit Program
> 2
Enter Source Vertex (char) for Shortest Path: D Single-Source Shortest Path from D: Warning: Not All Vertices May Be Reached.
Warning: Not All Vertices May Be Reached.
D -> C : 4 D -> D : 0 D -> E : 4
Enter option choice 1 - 5:
(1) Print Adjacency List
(2) Print Single-Source Shortest Path
(3) Print Indegree of Each Vertex
(4) Print Topological Sort of Graph
(5) Exit Program
> 2
Enter Source Vertex (char) for Shortest Path: E Single-Source Shortest Path from E:
Warning: Not All Vertices May Be Reached.
Warning: Not All Vertices May Be Reached.
Warning: Not All Vertices May Be Reached.
Warning: Not All Vertices May Be Reached.
E -> E : 0
Enter option choice 1 - 5:
(1) Print Adjacency List
(2) Print Single-Source Shortest Path
(3) Print Indegree of Each Vertex
(4) Print Topological Sort of Graph
(5) Exit Program
> 3
Indegree of Each Vertex: A : 0
B : 1
C : 2
D : 2
E : 3
Enter option choice 1 - 5:
(1) Print Adjacency List
(2) Print Single-Source Shortest Path
(3) Print Indegree of Each Vertex
(4) Print Topological Sort of Graph
(5) Exit Program
> 4
Topological Sort of Graph: A -> B -> D -> C -> E Enter option choice 1 - 5:
(1) Print Adjacency List
(2) Print Single-Source Shortest Path
(3) Print Indegree of Each Vertex
(4) Print Topological Sort of Graph
(5) Exit Program
> 5
Terminating Program...