Algorithm to determine if a graph is a tree

algorithm, data-structures, graph, tree

Solution

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.

Problem

What is a simple algorithm to determine if a graph, given as an adjacency matrix, is a tree?

Original source

Related problems