What is a Graph, and how does it differ from a Tree?

Instruction: Describe the characteristics of Graphs and Trees, and explain how they differ from each other.

Context: This question is designed to evaluate the candidate's ability to distinguish between Graphs and Trees, highlighting the properties and uses of each.

Updated

Official answer

A graph consists of vertices and edges representing relationships. Depending on the model, edges can be directed, undirected, weighted, repeated or self-loops; specify the convention.

For a finite simple undirected graph, a tree is connected and acyclic. With n≥1 vertices, it has n−1 edges and exactly one simple path between each pair of vertices. A forest is an acyclic graph that can have several connected components.

A tree does not inherently need a root. Choosing one vertex as a root adds parent/child relationships and orients traversal. Rooted data structures such as binary trees use that extra structure. A directed acyclic graph need not be a tree: a vertex may have multiple incoming paths or the underlying undirected graph may contain a cycle.

For example, edges A–B and B–C form a tree. Adding A–C creates a cycle; removing B–C disconnects the three-vertex graph. Networks with multiple routes need a general graph model, while a single-parent hierarchy may fit a rooted tree.

Related Questions