Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

DATABASE Administrator Make A Difference

Authors: Mohciine Elmourabit

1st Edition

B0CGM7XG75, 978-1722657802

More Books

Students also viewed these Databases questions

Question

Outline the kinds of services provided by EAPs.

Answered: 1 week ago