10 Trees and Graph Algorithms

A structured guide to graph foundations, tree properties, spanning trees, rooted representations, traversals, fundamental graph algorithms, and practical applications.

Graph Foundations

A graph is an ordered pair G=(V,E)G=(V,E), where VV is a set of vertices and EE is a set of edges. In an undirected graph, an edge joins two vertices without an orientation; in a directed graph, an edge is an ordered pair representing an arc from one vertex to another.

A walk may repeat vertices, whereas a path does not repeat vertices. A cycle is a closed path whose first and last vertices coincide. A graph is connected when every pair of vertices is joined by a path.

Two common representations emphasize different trade-offs:

  • An adjacency matrix uses O(∣V∣2)O(\lvert V\rvert^2) space and tests whether a particular edge exists in O(1)O(1) time.

  • An adjacency list uses O(∣V∣+∣E∣)O(\lvert V\rvert+\lvert E\rvert) space and is usually preferable for sparse graphs.

These definitions provide the vocabulary needed to understand trees and graph searches.

Trees and Their Structure

A is connected and acyclic. Several equivalent properties make trees easy to recognize:

  • There is exactly one simple path between every pair of vertices.

  • A with nn vertices has exactly n−1n-1 edges.

  • Removing any edge disconnects the graph.

  • Adding any new edge creates exactly one cycle.

For example, the edges {a,b},{a,c},{b,d},{b,e}\{a,b\},\{a,c\},\{b,d\},\{b,e\} form a with five vertices and four edges. Its leaves are cc, dd, and ee, and the unique path from cc to ee is:

c→a→b→ec\to a\to b\to e

A leaf is a vertex of degree 11. Every with at least two vertices has at least two leaves; the endpoints of a longest path must be leaves.

Takeaway: Connectivity supplies reachability, while the absence of cycles supplies uniqueness and minimality.

Spanning Trees and Minimum-Cost Connections

A keeps every vertex of a connected graph but removes enough edges to eliminate all cycles. If the graph has vertex set VV, every has ∣V∣−1\lvert V\rvert-1 edges. One way to obtain one is to remove edges from cycles until no cycle remains.

In a weighted undirected graph, a minimizes the sum of the selected edge weights. Two standard greedy methods are:

  • Kruskal's algorithm: Sort edges by increasing weight and add an edge when it joins two previously disconnected components. A disjoint-set structure can detect whether the edge would create a cycle.

  • Prim's algorithm: Begin with one vertex and repeatedly add the cheapest edge connecting the current to a vertex outside it.

For a graph with vertices A,B,C,DA,B,C,D and edges AB,AC,BC,BD,CDAB,AC,BC,BD,CD, removing BCBC leaves AB,AC,BD,CDAB,AC,BD,CD. These edges connect all four vertices without a cycle, so they form a .

Takeaway: A is a connected backbone; a is the least-cost such backbone.

Rooted and Binary Trees

Choosing a root gives an undirected a hierarchical interpretation. The parent of a vertex is the next vertex on its path toward the root, and its children are the vertices immediately below it. Vertices with the same parent are siblings. Ancestors lie on the path toward the root, while descendants lie in the vertex's subtree.

The depth of a vertex is its distance from the root. The height of the is the greatest depth of any vertex. A vertex with no children is a leaf.

Suppose rr is the root, with children aa and bb; aa has children cc and dd; and bb has child ee. Then cc and dd are siblings, rr is an ancestor of every vertex, and the height is 22.

An ordered specifies the order of each vertex's children. A binary is a in which each vertex has at most two children, conventionally called the left and right children. This structure supports searching, expression parsing, sorting, and decision processes.

Traversals

A visits every vertex according to a systematic rule. With adjacency lists, a traversal normally takes O(n)O(n) time on a with nn vertices because each vertex and edge is processed only a constant number of times.

uses a queue and visits vertices level by level. In a , it processes all vertices at depth 00, then depth 11, then depth 22, and so on. With equal edge weights, BFS computes the shortest distance from the root to every reachable vertex.

follows one branch as far as possible before backtracking. Its principal orders are:

  • Preorder: process a vertex before its children.

  • Postorder: process all children before the vertex.

  • Inorder: in a binary , process the left subtree, then the vertex, then the right subtree.

For the binary with root AA, children BB and CC, and children D,ED,E under BB, the orders are:

  • Preorder: A,B,D,E,CA,B,D,E,C

  • Inorder: D,B,E,A,CD,B,E,A,C

  • Postorder: D,E,B,C,AD,E,B,C,A

  • Level order: A,B,C,D,EA,B,C,D,E

Takeaway: BFS emphasizes distance and levels; DFS emphasizes depth, backtracking, and subtree order.

Core Graph Algorithms

BFS and DFS extend naturally from trees to general graphs. With adjacency lists, both run in O(∣V∣+∣E∣)O(\lvert V\rvert+\lvert E\rvert) time because each vertex and edge is examined only a constant number of times.

Reachability and Components

Starting BFS or DFS at a vertex ss visits exactly the vertices reachable from ss. To find every connected component of an undirected graph, repeatedly begin a search from an unvisited vertex. Each search identifies one component.

Cycle Detection and Testing

During DFS in an undirected graph, an already visited neighbor that is not the current vertex's parent indicates a cycle. A connected undirected graph is a exactly when it has ∣E∣=∣V∣−1\lvert E\rvert=\lvert V\rvert-1 and contains no cycle. Thus, a practical test combines a connectivity search with an edge-count check.

Shortest Paths in Unweighted Graphs

BFS explores vertices in nondecreasing order of distance from its source. Therefore, when a vertex is first discovered, its distance is minimal. Storing a predecessor for each newly discovered vertex allows a shortest path to be reconstructed by following predecessor links backward.

Those predecessor links form a shortest-path rooted at the source: the records one selected shortest route to every reachable vertex.

Applications and Algorithm Selection

A directed acyclic graph, or DAG, can be arranged in a . For every directed edge u→vu\to v, the vertex uu must occur before vv. A DFS-based method records vertices in postorder and reverses that order. A directed cycle prevents any because the required precedence relations would contradict one another.

Trees and graph algorithms appear in many practical structures:

  • File systems: Directories form a rooted hierarchy, and a path records successive child choices from the root.

  • Organization and classification: Supervisory structures and taxonomies use parent-child relationships.

  • Expression trees: Operators are internal vertices and operands are leaves. For (a+b)×c(a+b)\times c, multiplication is the root, addition is its left child, and cc is its right child. Postorder lists operands before their operators, making evaluation convenient.

  • Decision trees: Internal vertices hold tests, branches represent outcomes, and leaves represent final decisions.

  • Infrastructure networks: A supplies a connected backbone without redundant links, while a minimizes construction or maintenance cost.

  • Data structures: Binary search trees support ordered searching, heaps support priority queues, and tries organize strings by shared prefixes.

Choose the method according to the task:

  • Visit reachable vertices: BFS or DFS.

  • Find shortest paths with equal edge weights: BFS.

  • Explore deeply or process dependencies: DFS.

  • Find connected components: repeated BFS or DFS.

  • Detect cycles: BFS or DFS with parent or recursion-state tracking.

  • Produce a : DFS or indegree-based processing on a DAG.

  • Connect all vertices at minimum total weight: Kruskal's or Prim's algorithm.

Final takeaway: Trees simplify connectivity by removing cycles, while BFS, DFS, and greedy spanning- algorithms turn that structure into practical methods for searching, optimization, organization, and decision-making.