We're sorry this tutorial wasn't helpful. Please tell us what we can do better.
Types of Graph in Data Structure
Last updated:
Graphs come in many varieties, depending on whether edges have a direction or a weight, whether cycles are allowed, and how the vertices are connected. The list below defines the common types; each comes with a small example diagram.
Types of Graph in Data Structure
Finite Graph: A graph whose vertex set and edge set are both finite. Every graph stored in a computer is finite.
Infinite Graph: A graph with infinitely many vertices or edges, such as an endless grid. Such graphs appear in mathematics and are handled in programs only by generating the part that is needed.
Trivial Graph: A graph with exactly one vertex and no edges, the smallest possible non-empty graph.
Directed Graph: A graph in which every edge has a direction, from one vertex to another; it is also called a digraph. An edge (u, v) lets you go from u to v but not back unless (v, u) also exists.
Undirected Graph: A graph whose edges have no direction: an edge {u, v} connects u and v both ways, like a two-way road.
Weighted Graph: A graph in which every edge carries a number (its weight), such as a distance, time or cost. Shortest-path and minimum-spanning-tree algorithms work on weighted graphs.
Cyclic Graph: A graph that contains at least one cycle, a path that starts and ends at the same vertex without reusing an edge.
Acyclic Graph: A graph with no cycles. A connected, undirected acyclic graph is a tree.
Directed Acyclic Graph: A directed graph with no directed cycle, usually called a DAG. DAGs model dependencies such as build steps, course prerequisites or task schedules, and their vertices can be put in topological order.
Simple Graph: An undirected graph with no self-loops and at most one edge between any pair of vertices.
Multi Graph: A graph that allows more than one edge (parallel edges) between the same pair of vertices. Under the most common convention a multigraph has no self-loops; a graph that also has loops is a pseudograph.
Connected Graph: An undirected graph in which there is a path between every pair of vertices.
Disconnected Graph: An undirected graph in which at least one pair of vertices has no path between them. It splits into two or more connected components.
Complete Graph: A simple undirected graph in which every pair of distinct vertices is joined by an edge. With n vertices every vertex has degree n − 1 and the graph has n(n − 1)/2 edges; it is written Kn.
Null Graph: A graph that has vertices but no edges at all. (A graph with a single vertex and no edges is the trivial graph above.)
Pseudo Graph: A graph that may contain self-loops (an edge from a vertex to itself) and, usually, parallel edges as well.
Regular Graph: A graph in which every vertex has the same degree. If every degree is k, the graph is called k-regular.
Subgraph: A graph H is a subgraph of G if every vertex of H is a vertex of G and every edge of H is an edge of G.
Frequently Asked Questions (FAQ)
Q1. What is the difference between a simple graph and a multigraph?
A simple graph has no self-loops and at most one edge between any two vertices. A multigraph allows several edges between the same pair of vertices, such as multiple flights between two cities.
Q2. What is a complete graph, and how many edges does it have?
In a complete graph, every vertex is connected to every other vertex. An undirected complete graph with n vertices has n(n - 1) / 2 edges; for 5 vertices that is 10 edges.
Q3. What is a connected graph?
An undirected graph is connected if there is a path between every pair of vertices. A directed graph is strongly connected if every vertex can reach every other vertex following the edge directions.
Q4. What is a directed acyclic graph (DAG)?
A DAG is a directed graph with no cycles. DAGs model tasks with dependencies, such as build steps, course prerequisites and spreadsheet formulas, and they can be put in a topological order.
Q5. Write a Java method for a topological sort of a DAG.
TopologicalSort.java
import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.List;
import java.util.Queue;
// Kahn's algorithm: repeatedly take vertices with no incoming edges
static List<Integer> topologicalSort(List<List<Integer>> graph) {
int[] inDegree = new int[graph.size()];
for (List<Integer> edges : graph) {
for (int v : edges) {
inDegree[v]++;
}
}
Queue<Integer> queue = new ArrayDeque<>();
for (int v = 0; v < graph.size(); v++) {
if (inDegree[v] == 0) queue.offer(v);
}
List<Integer> order = new ArrayList<>();
while (!queue.isEmpty()) {
int v = queue.poll();
order.add(v);
for (int next : graph.get(v)) {
if (--inDegree[next] == 0) queue.offer(next);
}
}
return order; // fewer than V vertices means the graph has a cycle
}
Q6. What is a bipartite graph?
A bipartite graph's vertices can be split into two groups so that every edge connects a vertex in one group to a vertex in the other. Job-to-applicant matching is a typical example. A graph is bipartite exactly when it has no odd-length cycle.
Q7. Write a Java method to check whether a graph is bipartite.
IsBipartite.java
import java.util.ArrayDeque;
import java.util.Arrays;
import java.util.List;
import java.util.Queue;
static boolean isBipartite(List<List<Integer>> graph) {
int[] color = new int[graph.size()];
Arrays.fill(color, -1);
for (int start = 0; start < graph.size(); start++) {
if (color[start] != -1) continue;
color[start] = 0;
Queue<Integer> queue = new ArrayDeque<>();
queue.offer(start);
while (!queue.isEmpty()) {
int v = queue.poll();
for (int next : graph.get(v)) {
if (color[next] == -1) {
color[next] = 1 - color[v]; // opposite color
queue.offer(next);
} else if (color[next] == color[v]) {
return false; // same color on both ends
}
}
}
}
return true;
}
Q8. What is the difference between a sparse and a dense graph?
A sparse graph has far fewer edges than the maximum, close to V. A dense graph has close to V2 edges. Use an adjacency list for sparse graphs and consider an adjacency matrix for dense graphs.
Q9. Is a tree a type of graph?
Yes. A tree is a connected, acyclic, undirected graph. A tree with n vertices always has exactly n - 1 edges.
Q10. What is a cyclic graph versus an acyclic graph?
A cyclic graph contains at least one cycle; an acyclic graph contains none. An undirected acyclic graph is a forest (a set of trees), and a directed acyclic graph is a DAG.