Question
Using the low-level, structure-based version of the graph.h interface, design and implement a function void readGraph(SimpleGraph & g, istream & infile); that reads a text
Using the low-level, structure-based version of the graph.h interface, design and implement a function
void readGraph(SimpleGraph & g, istream & infile);
that reads a text description of a graph from infile into the graph g passed by the client. The input stream, which must already be open, consists of lines that can be in any of these three forms:
x Defines a node with name x
x - y Defines the bidirectional arc xy
x -> y Defines the directional arc xy
The names x and y are arbitrary strings that do not contain a hyphen. Either of the two connection formats should also allow the user to specify the cost of the arc by enclosing a number in parentheses at the end of the line. If no parenthesized value appears, the cost of the arc should be initialized to 1. The definition of the graph ends with a blank line or the end of the file. New nodes are defined whenever a new name appears in the data file. Thus, if every node is connected to some other node, it is sufficient to include only the arcs in the data file, because defining an arc automatically defines the nodes at its endpoints. If you need to represent a graph containing isolated nodes, you must specify the names of those nodes on separate lines somewhere in the data file. When reading in an arc description, your implementation should discard leading and trailing spaces from the node names, but retain any internal spaces. The line
San Francisco - Denver (954)
should therefore define nodes with the names "San Francisco" and "Denver", and then create connections between the two nodes in each direction, initializing both arcs to have a cost of 954. As an example, calling readGraph on the following data file would produce the airline graph that appears in the chapter as Figure:
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