Question: C++ program that takes a graph as an adjacency list from a text file, creates a graph from it, then topologically sorts it. I need

C++ program that takes a graph as an adjacency list from a text file, creates a graph from it, then topologically sorts it. I need help reading the file properly. The format of the file is:

1: 2 2: 3 8 3: 4 4: 5 5: 3 6: 7 7: 3 6 8 8: 1 9 9: 1 

Where the first int of each line is a vertex, and the following ints are the vertices it has an edge to.

I have something like this so far:

void buildGraphFromFile(string filename, int numLines, int arr[10][10]) { ifstream fileStream; fileStream.open(filename);

while (!fileStream.is_open()) { fileStream.close(); fileStream.clear(); system("cls"); cout << "Error opening file"; system("pause"); }

int nextInt;

for (int i = 0; i < numLines; i++) { for (int j = 0; j < 10; j++) { fileStream >> nextInt; arr[i][j] = nextInt; } }

fileStream.close(); }

int main() { string file = selectInputGraph(); //gets input file from user int lines = countInputFileVertices(file); //gets number of lines in file Graph graph(lines); int arr[10][10]; for (int i = 0; i < 10; i++) { for (int j = 0; j < 10; j++) { arr[i][j] = 0; } }

buildGraphFromFile(file, lines, arr);

return 0; }

Thank you!

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!