What is a simple algorithm to determine if a graph, given as an adjacency matrix, is a tree?
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
A tree is a graph without cycles, so to detect if your graph is a tree, check to see if it has any cycles. This can be done by traversing the matrix, retaining a history of every visited node and upon visiting a node, checking to see if it was in the set of nodes visited.
Here’s a previous SO post about detecting cycles. It’s a starting point:
How to detect if a directed graph is cyclic?
You can also study up on graph traversals and adjacency matrices, to give you a better grounding in what you need to do.
If after all of this, you still need help, you can post what you have so far.