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
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