Community

Start a Thread


Notifications
Subscribe You’re not receiving notifications from this thread.

Is This Graph a Tree (Main Thread)

Here is the interview question prompt, presented for reference.

Given an undirected graph, can you see if it's a tree? If so, return true and false otherwise.

An undirected graph is a tree based on the following conditions: first, there cannot be a cycle. Second, the graph must be connected.

This is an example of a graph that is a tree:

1 - 2
    |
    3 - 4

This one is an example of one that is not:

         1
        / \
       2 - 3
      /
     4

You'll be given two parameters: n for number of nodes, and a multidimensional array of edges like such: [[1, 2], [2, 3]], each pair representing the vertices connected by the edge.

Constraints

  • Number of vertices in the graph <= 100000
  • Number of edges <= 100000
  • The array edges can be empty
  • Expected time complexity : O(|V| + |E|) where |V| be the number of nodes n in the graph and |E| to be the number of edges in the graph
  • Expected space complexity : O(|V|)

You can see the full challenge with visuals at this link.

Challenges • Asked almost 7 years ago by Jake from AlgoDaily

Jake from AlgoDaily Commented on Nov 30, 2017:

This is the main discussion thread generated for Is This Graph a Tree.