Graphs
Understand the different types of graphs and their properties.
We'll cover the following
Graph
Formally, a simple graph is a pair of sets where is an arbitrary non-empty finite set, whose elements are called vertices or nodes, and is a set of pairs of elements of , which we call edges. In an undirected graph, the edges are unordered pairs or just sets of size two; We usually write instead of to denote the undirected edge between and . In a directed graph, the edges are ordered pairs of vertices; We usually write instead of to denote the directed edge from to .
Following standard (but admittedly confusing) practice, we will also use to denote the number of vertices in a graph and to denote the number of edges. Thus, in any undirected graph, we have , and in any directed graph, we have .
The endpoints of an edge or are its vertices and . We distinguish the endpoints of a directed edge by calling the tail and the head.
The definition of a graph as a pair of sets forbids multiple undirected edges with the same endpoints or multiple directed edges with the same head and the same tail. (The same directed graph can contain both a directed edge and its reversal .) Similarly, the definition of an undirected edge as a set of vertices forbids an undirected edge from a vertex to itself. Graphs without loops and parallel edges are often called simple graphs; non-simple graphs are sometimes called multigraphs. Despite the formal definitional gap, most algorithms for simple graphs extend to multigraphs with little or no modification, and for that reason, we see no need for a formal definition here.
For any edge in an undirected graph, we call a neighbor of and vice versa, and we say that and are adjacent. The degree of a node is its number of neighbors. In directed graphs, we distinguish two kinds of neighbors. For any directed edge , we call a predecessor of , and we call a successor of . The in-degree of a vertex is its number of predecessors; the out-degree is its number of successors.
A graph is a subgraph of if and . A proper subgraph of is any subgraph other than itself.
Walk and path
A walk in an undirected graph is a sequence of vertices, where each adjacent pair of vertices are adjacent in ; informally, we can also think of a walk as a sequence of edges. A walk is called a path if it visits each vertex at most once. For any two vertices and in a graph we say that is reachable from if contains a walk (and therefore a path) between and . An undirected graph is connected if every vertex is reachable from every other vertex. Every undirected graph consists of one or more components, which are its maximal connected subgraphs; two vertices are in the same component if and only if there is a path between them.
A walk is closed if it starts and ends at the same vertex; a cycle is a closed walk that enters and leaves each vertex at most once. An undirected graph is acyclic if no subgraph is a cycle; acyclic graphs are also called forests. A tree is a connected acyclic graph, or equivalently, one component of a forest. A spanning tree of an undirected graph is a subgraph that is a tree and contains every vertex of . A graph has a spanning tree if and only if it is connected. A spanning forest of is a collection of spanning trees, one for each component of .
Directed graphs require slightly different definitions. A directed walk is a sequence of vertices such that is a directed edge for every index ; directed paths and directed cycles are defined similarly. Vertex is reachable from vertex in a directed graph if and only if contains a directed walk (and therefore a directed path) from to . A directed graph is strongly connected if every vertex is reachable from every other vertex. A directed graph is acyclic if it does not contain a directed cycle; directed acyclic graphs are often called dags.
Create a free account to access the full course.
By signing up, you agree to Educative's Terms of Service and Privacy Policy