1. (10 pts) Implement a function that determines if there exist a route between
ID: 3713734 • Letter: 1
Question
1. (10 pts) Implement a function that determines if there exist a route between two nodes in an undirected graph. The graph will be represented with its adjacency matrix which for this problem is a binary 2D matrix. For instance, the graph shown in Fig. 1 has the following adjacency matrix: 1 1 0 0 1 0 1 0 1 0 1 0 0 1 0 1 0 0 0 0 1 0 1 1 1 1 0 1 0 0 0 0 0 1 0 0 where Gl614| = 1 means that we can reach node 4 from node 6, and Gl611| = 0 mleans that we cannot reach node 1 from node 6. Your function will return TRUE if there is a path from node 6 to node 1, which in this case there is at least one: 6- 4 - 5 - 1, and return FALSE otherwise. Your function must have the following signature int IsThereAPath (const int** adjacency_matrix, const int num_nodes, const int source, const int destination);Explanation / Answer
//Please ask any doubts in the comments, and rate the answer if you fins it helpful
bool isThereAPath(const int** graph, int V, int s, int d)
{
if (s == d)
return true;
bool *visited = new bool[V];
for (int i = 0; i < V; i++)
visited[i] = false;
queue<int> q;
visited[s] = true;
q.push(s);
while (!q.empty())
{
s = q.front();
q.pop();
for (int i = 0; i < V; i++)
{
if (graph[s][d] == 1)
return true;
if (graph[s][i] == 1 && !visited[i])
{
visited[i] = true;
q.push(i);
}
}
}
return false;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.