I'm given this adjacency matrix which I have to read from a text file, and supposed to return the result of reading it breadth-first and depth-first.
I know that breadth-first uses a FIFO queue and that depth-first uses a LIFO stack. I'm able to get these searches when I have the graph, and manually. I'm just not sure how to approach this on the computer, and using a matrix, on C++.
I would appreciate guidance on how to solve this problem. Some questions I have:
ANS 1: Yes, it's better to read the input from the text file into a regular matrix.
void MyGraph::csv_import()
{
int temp, i=0;
string parsed;
fstream file("input.csv");
while (getline(file, parsed, ',')) {
temp = stoi(parsed);
arr[i/10][i%10] = temp; //10 x 10 Matrix
i++;
}
}
ANS 2: Select a starting node, call the BFS to display results of search. for example(in my case)
void MyGraph::BFS(int v)
{
memset(visited, false, sizeof(visited);
QQ.push(v); //push the starting vertex into queue
visited[v] = true; //mark the starting vertex visited
while (!QQ.empty()) //until queue is not empty
{
v = QQ.front(); //assign v the vertex on front of queue
cout << v << " "; //print the vertex to be popped
QQ.pop(); //pop the vertex on front
for (int c = 0; c< V; c++) //V is the number of nodes
{
//M[i][j] == 1, when i,j are connected
if (M[v][c] == 1 && visited[c] == false)
{ //if vertex has edge and is unvisited
QQ.push(c); //push to the queue
visited[c] = true; //mark it visited
Path[c] = p; //assign the path length
}
}
}
}