Port City International University: Submitted To
Port City International University: Submitted To
SUBMITTED TO
SUBMITTED BY
int adj[MAX_NODES][MAX_NODES];
int visited[MAX_NODES];
int main() {
int n, edges, u, v, start, target;
dfs(start, n);
if (visited[target]) {
cout << "Nodes " << start << " and " << target << " are connected" << endl;
} else {
cout << "Nodes " << start << " and " << target << " are not connected" <<
endl;
}
return 0;
}
Output:
Discussion:
Here we are using dfs to check if any two nodes are connected in a graph.
Dfs(depth first search) works in a way that goes to the maximum depth meaning it
will run until a node with no children is found. While visiting the nodes they will
keep track of the visited nodes. When we check the connectivity we start dfs from
the node that is the source and then check either the destination node is visited the
from the source node if it’s visited we can say they are connected if not they are
not connected.