Graph and Tree Models
-
Reading Vertices and Edges in a Graph
A graph names a set of vertices and a set or collection of edges connecting pairs of vertices.
-
Directed and Undirected Connections
A directed edge describes a permitted move in one direction. An undirected edge relates its endpoints in both directions.
-
Parallel Edges and Self-Loops
Some graph models permit more than one edge between a pair of vertices and edges whose two ends coincide.
-
Counting the Degree of a Vertex
Degree counts incident edge ends. A directed graph distinguishes incoming from outgoing edges.
-
Walks, Paths, and Repeated Vertices
A walk follows consecutive edges and can return to a vertex. A simple path visits each vertex at most once.
-
Testing Reachability from One Vertex
A vertex is reachable when a sequence of permitted edges leads to it from the source, including the trivial route to the source itself.
-
Connected Components by Hand
A connected component of an undirected graph contains all vertices reachable from one another. Components form a partition of the vertex set.
-
Mutual Reachability in a Directed Graph
Strong connectivity requires that each vertex in a component can reach each other through directed edges.
-
A Breadth-First Search by Hand
Breadth-first search expands the earliest waiting vertex and discovers reachable vertices in layers by unweighted distance.
-
A Depth-First Search by Hand
Depth-first search follows one path as far as it can before returning to explore unfinished alternatives.
-
Measuring Shortest Routes by Edge Count
In a graph where each permitted step has equal cost, a shortest route minimizes the number of edges traversed.
-
Adding Edge Weights Along a Route
A weighted path's additive cost is the sum of the weights of its edges. The best route can have more edges than an alternative.
-
Recovering a Path from Predecessors
A search can store one predecessor per discovered vertex and reconstruct a path only for a requested target.
-
A Tree as a Connected Acyclic Graph
An undirected tree is connected and contains no cycles. There is exactly one simple path between each distinct pair of vertices.
-
Parents, Children, and Depth in Rooted Trees
Rooting a tree gives each nonroot vertex one parent on its path to the root. Depth is its distance in edges from that root.
-
Finding the Lowest Common Ancestor by Hand
For two vertices in a rooted tree, their lowest common ancestor is the deepest vertex belonging to both parent chains.
-
Building a Spanning Tree from Search
A search in a connected graph can record one discovery edge for each vertex except the source, forming a spanning tree.
-
Counting Edges in a Forest
Count edges in a forest by adding the edge counts of its disjoint tree components.
-
A Cycle Witness in an Undirected Graph
A simple undirected graph has a cycle when some walk returns to its start through distinct edges and otherwise distinct vertices.
-
A Directed Cycle in Prerequisites
A directed cycle among prerequisite edges makes it impossible to satisfy all of them by placing every prerequisite first.
-
Topological Ordering by Removing Ready Tasks
In a directed acyclic dependency graph, tasks with no remaining incoming prerequisites can be placed next.
-
Checking a Graph with Two Colors
A graph is bipartite when vertices can be assigned one of two parts and every edge crosses between the parts.
-
Turning a Grid into a Graph
A grid search can treat each permitted cell as a vertex and each legal move as a directed or undirected edge.
-
Building Graphs from Input Records
Input records can describe vertices, connections, and attributes. A validation step resolves which named endpoints exist and which repeats are allowed.
-
Reversing All Edges in a Directed Graph
The reverse graph keeps the same vertices and reverses the start and end of each directed edge.
-
Counting Edges of a Complete Simple Graph
A complete simple undirected graph connects every pair of distinct vertices with one edge.
-
Sparse and Dense Graph Representations
The number of actual edges guides how much work and storage different graph representations need.
-
Measuring Distance from Several Sources
An unweighted graph can assign each vertex the distance to its nearest source by starting all source vertices in the same zero-distance layer.
-
Taking the Subgraph Induced by a Vertex Set
An induced subgraph keeps a chosen set of vertices and every original edge with both endpoints in that set.
-
Modeling Task Prerequisites as a Graph
A prerequisite graph represents allowable execution orders and identifies tasks that may be independent under its stated requirements.