Graphs are an important non-linear data structure used to represent relationships and connections between different objects. Depending on how vertices and edges are connected, graphs are classified into several types. Understanding these graph types helps programmers choose the right structure for solving problems involving networks, navigation, social media, and data relationships.
Table of Contents
What Are Graphs?
A graph is a non-linear data structure consisting of vertices (nodes) and edges (connections) between them.
A graph is mathematically represented as: G = (V, E)
Where:
- G: Represents the graph.
- V: Represents the set of vertices.
- E: Represents the set of edges.
Graphs are used to represent relationships between objects. For example, in a social networking application, users can be represented as vertices and their connections as edges.
Graphs can be classified based on direction, weights, connectivity, cycles, and the number of edges between vertices.
Types of Graphs in Data Structures
There are several types of graphs used in computer science. Each type has different properties and applications.
The commonly used graph types include:
- Directed Graph
- Undirected Graph
- Weighted Graph
- Unweighted Graph
- Connected Graph
- Disconnected Graph
- Complete Graph
- Cyclic Graph
- Acyclic Graph
- Directed Acyclic Graph (DAG)
- Simple Graph
- Multigraph
- Bipartite Graph
- Sparse Graph
- Dense Graph
Directed Graph
A Directed Graph is a graph in which every edge has a specific direction.
The connections between vertices are represented using arrows. If there is an edge from vertex A to vertex B, it does not automatically mean that an edge exists from B to A.
Example: A social media platform where one user follows another user can be represented using a directed graph.
JavaScript Example:
// This program creates a directed graph using an adjacency list.
const graph = {
A: ["B", "C"],
B: ["D"],
C: ["D"],
D: []
};
console.log(graph);
Output:
{
A: [ 'B', 'C' ],
B: [ 'D' ],
C: [ 'D' ],
D: []
}
Explanation:
- Vertex A has outgoing edges to B and C.
- Vertex B has an outgoing edge to D.
- Vertex C also has an outgoing edge to D.
- Vertex D has no outgoing edges.
The graph represents one-way connections between vertices.
Undirected Graph
An Undirected Graph is a graph in which edges do not have a specific direction. If vertex A is connected to vertex B, the connection can be followed in both directions.
Example: A friendship network is a common example of an undirected graph because a mutual friendship connects both users.
JavaScript Example:
// This program creates an undirected graph using an adjacency list.
const graph = {};
function addEdge(u, v) {
if (!graph[u]) graph[u] = [];
if (!graph[v]) graph[v] = [];
graph[u].push(v);
graph[v].push(u);
}
addEdge("A", "B");
addEdge("A", "C");
addEdge("B", "D");
console.log(graph);
Output:
{
A: [ 'B', 'C' ],
B: [ 'A', 'D' ],
C: [ 'A' ],
D: [ 'B' ]
}
Explanation:
- The addEdge() function creates a connection between two vertices.
- Each vertex is added to the other's adjacency list.
- This allows the connection to be followed in both directions.
Weighted Graph
A Weighted Graph is a graph in which each edge has an associated numerical value called a weight. Weights can represent distance, cost, time, or any other measurable quantity.
Example: A road network can be represented using a weighted graph where each edge stores the distance between two cities.
JavaScript Example:
// This program creates a weighted graph using an adjacency list.
const graph = {
A: [{ node: "B", weight: 10 }, { node: "C", weight: 20 }],
B: [{ node: "A", weight: 10 }, { node: "D", weight: 15 }],
C: [{ node: "A", weight: 20 }],
D: [{ node: "B", weight: 15 }]
};
console.log(graph);
Output:
{
A: [ { node: 'B', weight: 10 }, { node: 'C', weight: 20 } ],
B: [ { node: 'A', weight: 10 }, { node: 'D', weight: 15 } ],
C: [ { node: 'A', weight: 20 } ],
D: [ { node: 'B', weight: 15 } ]
}
Explanation:
- Each vertex stores its neighboring vertices.
- Every connection has a numerical weight.
- The connection between A and B has a weight of 10.
- The connection between A and C has a weight of 20.
Weighted graphs are useful for finding shortest paths and minimum-cost routes.
Unweighted Graph
An Unweighted Graph is a graph in which edges do not have associated numerical weights. Every edge represents a connection without assigning a specific cost or distance.
Example: A simple network showing which computers are connected to each other can be represented using an unweighted graph.
JavaScript Example:
// This program creates an unweighted graph using an adjacency list.
const graph = {
A: ["B", "C"],
B: ["A", "D"],
C: ["A"],
D: ["B"]
};
console.log(graph);
Output:
{
A: [ 'B', 'C' ],
B: [ 'A', 'D' ],
C: [ 'A' ],
D: [ 'B' ]
}
Explanation:
- Each vertex stores its connected neighbors.
- No numerical values are assigned to edges.
- Every connection is treated equally.
Connected Graph
A Connected Graph is an undirected graph in which there is a path between every pair of vertices. This means that every vertex can be reached from any other vertex by following one or more edges.
Example: A transportation network in which every city can be reached from every other city is an example of a connected graph. A graph with only one vertex and no edges is also considered connected.
Disconnected Graph
A Disconnected Graph is an undirected graph in which at least one pair of vertices has no path connecting them.
It contains two or more connected components. A connected component is a maximal group of vertices where every vertex can be reached from every other vertex within that group.
Example: A computer network containing separate groups of computers with no connection between the groups can be represented as a disconnected graph.
Complete Graph
A Complete Graph is a simple undirected graph in which every pair of distinct vertices is directly connected by an edge. In a complete graph, each vertex is connected to all other vertices.
The number of edges in a complete undirected graph is calculated using the formula: E = V(V - 1) / 2
Where:
- E: Number of edges.
- V: Number of vertices.
For example, a complete graph containing 5 vertices has:
E = 5 × 4 / 2 = 10 edges
Complete graphs are useful in studying fully connected networks and graph theory problems.
Cyclic Graph
A Cyclic Graph is a graph that contains at least one cycle. A cycle is a path that starts and ends at the same vertex without repeating other vertices. Cycles can occur in both directed and undirected graphs.
Example: A road network containing a circular route that allows a person to return to the starting location is an example of a cyclic graph.
Cycles are important in detecting circular dependencies and identifying repeated paths.
Acyclic Graph
An Acyclic Graph is a graph that does not contain any cycles. This means there is no path that starts at a vertex and returns to the same vertex by following a cycle.
A tree is a special type of connected, undirected, acyclic graph.
Acyclic graphs are useful when relationships must not form circular connections.
Directed Acyclic Graph (DAG)
A Directed Acyclic Graph, commonly called a DAG, is a directed graph that does not contain any directed cycles.
Its edges have specific directions, but following those directions can never lead back to the starting vertex through a cycle.
Example: Task scheduling systems use DAGs to represent dependencies between tasks. A task must be completed before another dependent task can begin.
DAGs are also used in:
- Build systems.
- Workflow management.
- Dependency resolution.
- Data processing pipelines.
- Version control systems.
One important algorithm used with DAGs is topological sorting, which produces an ordering of vertices that respects their dependencies.
Simple Graph
A Simple Graph is an undirected graph that does not contain self-loops or multiple edges between the same pair of vertices. A self-loop is an edge that connects a vertex to itself.
In a simple graph:
- A vertex cannot have an edge connecting back to itself.
- Only one edge can exist between any pair of distinct vertices.
- Edges do not have directions.
Simple graphs are commonly used in basic graph theory and network representation.
Multigraph
A Multigraph is a graph that allows multiple edges between the same pair of vertices. Depending on the definition being used, some multigraph models may also allow self-loops.
Example: A transportation network can use multiple edges to represent different routes between the same two cities, such as separate roads or railway connections.
Multigraphs are useful when multiple relationships exist between the same objects.
Bipartite Graph
A Bipartite Graph is a graph whose vertices can be divided into two separate sets such that every edge connects a vertex from one set to a vertex from the other set.
There are no edges connecting two vertices within the same set.
Example:
A job recruitment system can be represented as a bipartite graph:
- One set contains job applicants.
- Another set contains available jobs.
- An edge represents an applicant's application for a job.
A graph is bipartite if and only if it contains no odd-length cycle.
Bipartite graphs are used in matching problems, assignment systems, and recommendation applications.
Sparse Graph and Dense Graph
Graphs can also be classified based on the number of edges they contain.
1. Sparse Graph
A Sparse Graph is a graph that contains relatively few edges compared with the maximum possible number of edges. It is commonly represented using an adjacency list because this representation stores only existing connections.
Example: A large computer network in which each computer is connected to only a few other computers can be represented as a sparse graph.
2. Dense Graph
A Dense Graph is a graph that contains a relatively large number of edges compared with the maximum possible number of edges. An adjacency matrix can be useful for dense graphs because it allows constant-time edge-existence checks.
Example: A network in which almost every device is directly connected to many other devices can be represented as a dense graph.
Comparison of Different Graph Types
| Graph Type | Main Characteristic |
|---|---|
| Directed Graph | Edges have directions. |
| Undirected Graph | Edges have no directions. |
| Weighted Graph | Edges contain numerical weights. |
| Unweighted Graph | Edges have no assigned weights. |
| Connected Graph | Every pair of vertices has a path between them. |
| Disconnected Graph | At least one pair of vertices has no connecting path. |
| Complete Graph | Every pair of distinct vertices is directly connected. |
| Cyclic Graph | Contains at least one cycle. |
| Acyclic Graph | Contains no cycles. |
| DAG | Directed graph without directed cycles. |
| Simple Graph | No self-loops or parallel edges. |
| Multigraph | Allows multiple edges between the same vertices. |
| Bipartite Graph | Vertices can be divided into two sets with edges only between the sets. |
| Sparse Graph | Contains relatively few edges. |
| Dense Graph | Contains relatively many edges. |
Applications of Different Graph Types
Different graph types are used to solve different real-world problems.
- Directed Graphs: Used in social media follow systems, web page links, and task dependencies.
- Undirected Graphs: Used in friendship networks, mutual connections, and two-way communication networks.
- Weighted Graphs: Used in navigation systems to calculate distance, travel time, and transportation costs.
- Unweighted Graphs: Used in basic connectivity networks and situations where all connections have equal importance.
- Connected Graphs: Used to represent fully reachable networks.
- Complete Graphs: Used in theoretical networking problems and fully connected communication models.
- DAGs: Used in project scheduling, build systems, and dependency management.
- Bipartite Graphs: Used in job assignment, matching systems, and recommendation applications.
- Sparse Graphs: Used in large networks where each node has relatively few connections.
- Dense Graphs: Used in highly connected network models.
Common Mistakes to Avoid
-
Confusing Directed and Undirected Graphs: Remember that directed edges have a specific direction, while undirected edges can be followed both ways.
-
Mixing Weighted and Unweighted Graphs: Weighted graphs assign values to edges, while unweighted graphs do not.
-
Assuming Every Acyclic Graph Is a Tree: A tree must also be connected and undirected.
-
Confusing Cycles with Self-Loops: A self-loop is an edge from a vertex to itself, while a cycle can involve multiple vertices.
- Ignoring Graph Connectivity: A graph traversal from one starting vertex may not visit every vertex if the graph is disconnected.
Conclusion
Understanding the different types of graphs is essential for learning graph algorithms and solving programming problems. Each graph type has its own properties and use cases, making it important to select the appropriate graph representation for a particular problem. These concepts also provide a strong foundation for technical interviews and advanced topics such as shortest path algorithms, minimum spanning trees, and topological sorting.
Frequently Asked Questions
1. What are the main types of graphs in data structures?
Common graph types include directed, undirected, weighted, unweighted, connected, disconnected, complete, cyclic, acyclic, bipartite, and directed acyclic graphs.
2. What is the difference between a directed and an undirected graph?
A directed graph contains edges with specific directions, while an undirected graph contains edges that can be followed in both directions.
3. What is a weighted graph?
A weighted graph is a graph in which edges have numerical values representing distance, cost, time, or other measurements.
4. What is a DAG in data structures?
A Directed Acyclic Graph is a directed graph that does not contain any directed cycles. It is commonly used to represent task dependencies.
5. What is the difference between a sparse graph and a dense graph?
A sparse graph contains relatively few edges compared with the maximum possible number of edges, while a dense graph contains relatively many edges.
0 Comments