Graph Terminology is an important topic in data structures that helps beginners understand how graphs work and how their components are connected. Graphs are widely used in computer networks, social media platforms, navigation systems, and recommendation engines. Learning basic graph terms such as vertices, edges, degree, path, cycle, and connectivity provides a strong foundation for understanding graph algorithms and solving programming problems.
Table of Contents
What Is a Graph?
A graph is a non-linear data structure consisting of vertices and edges. Vertices represent individual objects, while edges represent connections or relationships between those objects.
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.
For example, in a social networking platform, users can be represented as vertices, and their friendships or follow relationships can be represented as edges.
To understand graphs properly, it is important to learn the terminology used to describe their structure and connections.
Basic Graph Terminology
Graph terminology refers to the important terms used to describe vertices, edges, paths, cycles, and relationships in a graph.
Some commonly used graph terms include:
- Vertex
- Edge
- Adjacent Vertices
- Degree
- In-Degree
- Out-Degree
- Path
- Cycle
- Connected Graph
- Disconnected Graph
- Subgraph
- Loop
- Parallel Edges
- Walk
- Trail
- Simple Path
These terms are used in graph algorithms such as Breadth-First Search (BFS), Depth-First Search (DFS), Dijkstra's Algorithm, and Minimum Spanning Tree algorithms.
Vertex (Node)
A Vertex, also called a Node, is an individual element of a graph that represents an object or entity.
A graph can contain multiple vertices, and each vertex can be connected to other vertices through edges.
Example: In a social networking application, every user can be represented as a vertex. If a network contains five users, the graph contains five vertices.
Important Points:
- A vertex represents an individual object.
- Vertices are connected through edges.
- A graph can contain one or more vertices.
- A vertex may exist without being connected to any other vertex.
Edge
An Edge is a connection between two vertices in a graph. Edges represent relationships between objects and can be directed or undirected.
Example: In a road network, an edge represents a road connecting two locations.
Important Points:
- An edge connects two vertices.
- An undirected edge can be followed in both directions.
- A directed edge has a specific direction.
- Edges may contain weights representing distance, cost, or time.
For a simple undirected graph with V vertices, the maximum number of edges is:
[E_(max) = {V(V-1)}/{2}]
Adjacent Vertices
Two vertices are called Adjacent Vertices if an edge directly connects them.
For example, if an edge connects vertex A to vertex B, then A and B are adjacent.
In an undirected graph, adjacency works in both directions. In a directed graph, adjacency is determined by the direction of the edge.
Important Points:
- Adjacent vertices share a direct connection.
- Vertices without a direct edge are not adjacent.
- Adjacency is used in graph traversal algorithms.
Degree of a Vertex
The Degree of a Vertex is the number of edges connected to that vertex in an undirected graph. For example, if a vertex is connected to three other vertices through three edges, its degree is 3.
Important Points:
- Degree measures how many edges are connected to a vertex.
- A vertex with no connected edges has degree 0.
- A vertex connected to one edge has degree 1.
- A vertex with a high degree has many connections.
Important Formula:
For an undirected graph:
[\sum_{v \in V} \deg(v) = 2E]
This is known as the Handshaking Lemma. Every edge contributes 2 to the total degree because it connects two endpoints. A self-loop contributes 2 to the degree of its vertex.
In-Degree and Out-Degree
In a Directed Graph, edges have specific directions. Therefore, the degree of a vertex is divided into two types.
1. In-Degree: In-Degree is the number of directed edges coming into a vertex.
For example, if three edges point toward vertex C, its in-degree is 3.
2. Out-Degree: Out-Degree is the number of directed edges going out from a vertex.
For example, if two edges start from vertex A and point toward other vertices, its out-degree is 2.
Important Formula:
For a directed graph:
[\sum_{v \in V} \text{InDegree}(v) = E]
[\sum_{v \in V} \text{OutDegree}(v) = E]
Every directed edge contributes exactly one to the total in-degree and one to the total out-degree.
Path
A Path is a sequence of vertices connected through edges. It represents a route through which we can travel from one vertex to another.
For example, if A is connected to B, B is connected to C, and C is connected to D, then A to B to C to D represents a path.
Important Points:
- A path connects vertices through edges.
- The length of a path is the number of edges it contains.
- A simple path does not repeat vertices.
- Paths are important in shortest-path algorithms.
Cycle
A Cycle is a path that starts and ends at the same vertex. In a simple cycle, no vertex is repeated except the starting vertex, which appears again at the end.
For example, a route from A to B, B to C, and C back to A forms a cycle.
Important Points:
- A cycle starts and ends at the same vertex.
- Cycles can exist in directed and undirected graphs.
- A graph containing at least one cycle is called a cyclic graph.
- A graph without cycles is called an acyclic graph.
Cycle detection is an important operation in graph algorithms and dependency management.
Connected and Disconnected Graphs
Graphs can be classified based on whether their vertices are reachable from one another.
1. Connected Graph: An undirected graph is called Connected if there is a path between every pair of vertices.
This means every vertex can be reached from any other vertex.
2. Disconnected Graph: An undirected graph is called Disconnected if at least one pair of vertices has no path connecting them. A disconnected graph contains two or more connected components.
3. Connected Component: A Connected Component is a maximal group of vertices in which every vertex can be reached from every other vertex within that group. Connected components are useful for analyzing networks and identifying separate groups of connected objects.
Subgraph
A Subgraph is a graph formed using a subset of the vertices and edges of another graph. If a graph contains vertices A, B, C, and D, a smaller graph containing A, B, and the edge connecting them can be considered a subgraph.
Important Points:
- A subgraph is derived from an existing graph.
- It contains a subset of the original vertices and edges.
- Every edge in the subgraph must connect vertices included in that subgraph.
- Subgraphs are useful for studying smaller parts of complex networks.
Loop and Parallel Edges
1. Loop (Self-Loop): A Loop is an edge that connects a vertex to itself.
For example, an edge from vertex A back to vertex A is called a self-loop.
In an undirected graph, a self-loop contributes 2 to the degree of its vertex. In a directed graph, it contributes 1 to both the in-degree and out-degree.
2. Parallel Edges
Parallel Edges are two or more distinct edges connecting the same pair of vertices.
For example, two different roads connecting the same pair of cities can be represented using parallel edges.
Simple graphs do not allow self-loops or parallel edges. Multigraphs allow parallel edges, and some graph definitions also permit self-loops.
Walk, Trail, and Simple Path
These three terms describe different ways of moving through a graph.
- Walk: A Walk is a sequence of vertices where each consecutive pair is connected by an edge. A walk can repeat vertices and edges.
- Trail: A Trail is a walk in which no edge is repeated. However, vertices may be visited more than once.
- Simple Path: A Simple Path is a path in which no vertex is repeated.
Difference Between Walk, Trail, and Simple Path:
| Term | Can Repeat Vertices? | Can Repeat Edges? |
|---|---|---|
| Walk | Yes | Yes |
| Trail | Yes | No |
| Simple Path | No | No |
Understanding these terms is useful when studying graph traversal, pathfinding, and cycle detection.
Weighted and Unweighted Edges
Edges can also be classified based on whether they contain numerical values.
1. Weighted Edge: A Weighted Edge contains a numerical value called a weight.The weight can represent distance, cost, time, or any other measurable quantity.
For example, a road network can assign distances to roads connecting different cities.
2. Unweighted Edge: An Unweighted Edge does not contain an assigned numerical weight. Every connection is treated equally, without considering a separate distance or cost. These concepts are important when working with shortest-path algorithms such as Dijkstra's Algorithm.
Directed and Undirected Edges
Edges can be classified based on whether they have a specific direction.
1. Directed Edge: A Directed Edge connects two vertices in a specific direction.
For example, an edge from A to B does not automatically create a connection from B to A.
Directed edges are commonly used in follower relationships, web page links, and task dependencies.
2. Undirected Edge: An Undirected Edge connects two vertices without specifying a direction.
For example, a mutual friendship between two users can be represented using an undirected edge.
In an undirected graph, a connection can be followed in both directions.
Graph Terminology Example in JavaScript
The following JavaScript program creates an undirected graph and calculates the degree of each vertex.
JavaScript Example:
// This program creates an undirected graph and calculates vertex degrees.
const graph = {
A: ["B", "C"],
B: ["A", "D"],
C: ["A", "D"],
D: ["B", "C"]
};
function calculateDegree(graph) {
for (const vertex in graph) {
console.log(
"Degree of", vertex, ":", graph[vertex].length
);
}
}
calculateDegree(graph);
Output:
Degree of A : 2
Degree of B : 2
Degree of C : 2
Degree of D : 2
Explanation:
- The graph is represented using an adjacency list.
- Each vertex stores an array containing its neighboring vertices.
- The calculateDegree() function visits every vertex.
- The length of each adjacency list gives the degree of that vertex because this example contains no self-loops or parallel edges.
- The degree of every vertex is 2.
This example demonstrates how graph connections can be stored and how basic graph terminology can be applied in JavaScript.
Applications of Graph Terminology
Understanding graph terminology is useful in many real-world applications.
- Social Networking: Vertices represent users, while edges represent friendships, followers, or connections.
- Computer Networks: Vertices represent computers or routers, while edges represent communication links.
- Navigation Systems: Vertices represent locations, while weighted edges represent distances or travel costs.
- Web Page Linking: Vertices represent web pages, while directed edges represent hyperlinks.
- Project Scheduling: Directed edges represent dependencies between different tasks.
- Recommendation Systems: Graphs represent relationships between users, products, movies, and interests.
- Transportation Networks: Vertices represent stations or cities, while edges represent available routes.
1Common Mistakes to Avoid
- Confusing Vertices and Edges: Vertices represent objects, while edges represent connections between objects.
- Mixing Degree with In-Degree and Out-Degree: Degree is commonly used for undirected graphs, while in-degree and out-degree describe directed graphs.
- Confusing Paths and Cycles: A path connects vertices, while a cycle starts and ends at the same vertex.
- Ignoring Edge Direction: Directed edges must be followed according to their specified direction.
- Confusing Walk, Trail, and Simple Path: A walk can repeat vertices and edges, a trail cannot repeat edges, and a simple path cannot repeat vertices.
Conclusion
Graph terminology is the foundation of understanding graph data structures and algorithms. Learning concepts such as vertices, edges, degree, paths, cycles, and connectivity makes it easier to understand graph representations and traversal techniques. These concepts are also important for solving coding problems and preparing for technical interviews.Frequently Asked Questions
1. What are the basic terms used in graph data structures?
Common graph terms include vertices, edges, adjacent vertices, degree, path, cycle, connected components, subgraphs, and loops.
2. What is the difference between a vertex and an edge?
A vertex represents an individual object or node, while an edge represents a connection between two vertices.
3. What is the degree of a vertex?
The degree of a vertex in an undirected graph is the total number of edges connected to it.
4. What is the difference between a path and a cycle?
A path is a sequence of vertices connected through edges, while a cycle is a closed path that starts and ends at the same vertex.
5. What is the difference between a walk and a trail?
A walk can repeat vertices and edges, while a trail does not repeat any edge.
0 Comments