What Is a Graph in Data Structures? Types and Complete Graphs

A graph in data structures is a non-linear structure made up of vertices (nodes) and edges (connections between nodes). It formally represents any many-to-many relationship and is written as G = (V, E). Graphs power Google Maps, social networks, fraud detection and compiler design, making them essential for every computer science student and developer.

  • Key Takeaway 1: A graph consists of a set of vertices and edges; edges can be directed or undirected.
  • Key Takeaway 2: A complete graph with n vertices has exactly n(n-1)/2 edges.
  • Key Takeaway 3: Graphs are represented in memory using an adjacency matrix or an adjacency list.
  • Key Takeaway 4: BFS and DFS are the two standard graph traversal algorithms tested in almost every technical interview.
  • Key Takeaway 5: Graphs power Google Maps, social networks, fraud detection systems and recommendation engines.

Graph Basics: Vertices and Edges

Every graph in data structures has two fundamental components. A vertex (plural: vertices) is a single point or node in the graph. An edge is the connection between two vertices. Think of a city map: cities are vertices, and roads connecting them are edges.

Formally, a graph G is written as G = (V, E), where V is the set of vertices and E is the set of edges. This notation appears in every university textbook, from IIT Bombay’s algorithms course to GATE preparation material and NPTEL’s Data Structures and Algorithms course, which has over 200,000 registered learners.

Core Graph Terminology

Before going further, get these terms locked in. They come up in every data structures exam and every technical screening round.

Term Definition Example
Vertex (Node) A single entity in the graph A user in a social network
Edge A link between two vertices A friendship between two users
Degree Number of edges connected to a vertex A user with 5 friends has degree 5
Path A sequence of vertices connected by edges Delhi to Mumbai via Jaipur
Cycle A path that starts and ends at the same vertex A, B, C, A
Weight A value assigned to an edge Distance or cost between two cities

One mistake beginners make constantly: confusing degree with weight. Degree counts connections; weight measures the cost or strength of a connection. Mixing them up in an exam answer will cost you marks.

Students just getting started with programming can build a strong foundation by learning how to write, compile and run a C program, since most graph algorithms are first taught in C.

Types of Graphs in Data Structures

Graph classification comes down to how edges behave. There are several distinct types of graphs in data structures, each suited to different problems.

Directed vs Undirected Graphs

In an undirected graph, edges have no direction. If vertex A is connected to vertex B, then B is connected to A too. Think of a friendship on Facebook. In a directed graph (also called a digraph), edges point one way. Following someone on Instagram is a directed relationship: you follow them, but they might not follow back.

Weighted vs Unweighted Graphs

A weighted graph assigns a numerical value to each edge. Google Maps uses a weighted graph where edge weights represent distance or travel time. An unweighted graph treats all edges as equal, useful when you only care whether a connection exists, not how strong it is.

Other Important Graph Types

  • Connected Graph: Every vertex is reachable from every other vertex.
  • Disconnected Graph: At least one vertex has no path to another.
  • Cyclic Graph: Contains at least one cycle.
  • Acyclic Graph: Has no cycles. A tree is a special case of an acyclic undirected graph.
  • DAG (Directed Acyclic Graph): A directed graph with no cycles. Used heavily in task scheduling and compiler design.
  • Bipartite Graph: Vertices can be split into two groups where edges only go between groups, never within the same group.

The GATE CS exam, which over 1.3 lakh candidates appear for each year according to IIT Bombay’s official GATE statistics, regularly tests questions on directed graphs, DAGs and bipartite graphs. Knowing the definitions precisely is not optional.

If you’re studying for GATE or a university exam, check out the BSc Computer Science subjects and scope guide to understand where graphs fit into your full curriculum.

Complete Graph in Data Structure: Formula and Examples

A complete graph is a graph where every vertex is directly connected to every other vertex by exactly one edge. It is the densest possible undirected graph you can build for a given number of vertices.

Complete Graph Formula: n(n-1)/2

For a complete graph with n vertices, the total number of edges is always n(n-1)/2. Each of the n vertices connects to n-1 others, and you divide by 2 to avoid counting each edge twice.

Vertices (n) Edges: n(n-1)/2 Graph Name
3 3 K3 (Triangle)
4 6 K4
5 10 K5
6 15 K6
10 45 K10

Complete graphs are written as Kn in graph theory notation. The complete graph in data structure problems often appears in questions about the maximum number of edges possible, or in network topology where full mesh connectivity is needed.

A practical real-world analogy: if 5 students in a study group all share their notes with each other directly, that is K5, a complete graph with 10 connections. Add one more student and you jump to K6 with 15 connections. The edge count grows fast, which is why complete graphs are expensive to store and process.

Why Complete Graphs Matter in Interviews

Interviewers at product companies like Flipkart, Infosys and TCS regularly ask candidates to calculate the maximum number of edges in a graph with n nodes. The answer is always n(n-1)/2 for an undirected graph. For a directed complete graph, every pair has two directed edges, so the formula becomes n(n-1).

Adjacency List vs Adjacency Matrix: How Graphs Are Stored

Knowing what a graph is in data structures is one thing. Knowing how to store and traverse it in code is what actually gets you hired.

Adjacency Matrix

An adjacency matrix is a 2D array of size V x V, where V is the number of vertices. If there is an edge between vertex i and vertex j, the cell matrix[i][j] is set to 1 (or the edge weight). Otherwise it is 0.

The advantage: checking whether an edge exists is O(1). The disadvantage: it always uses O(V²) space, even for sparse graphs with very few edges. For a graph with 1,000 vertices and only 2,000 edges, an adjacency matrix wastes enormous memory.

Adjacency List

An adjacency list stores each vertex’s neighbours in a list. In Python, this is typically a dictionary where each key is a vertex and its value is a list of connected vertices. In C, you would implement it with an array of linked lists.

The adjacency list uses O(V + E) space, which is much more efficient for sparse graphs. Most real-world graphs, including social networks and road maps, are sparse. That is why production systems almost always prefer the adjacency list.

You can implement a basic graph adjacency list in Python in under 20 lines of code. If you have not set up Python yet, start with this guide on how to write and run a Python program.

Graph Traversal Algorithms: BFS vs DFS

Breadth-First Search (BFS) explores a graph level by level, starting from a source vertex and visiting all neighbours before moving deeper. It uses a queue internally and is the algorithm behind shortest-path finding in unweighted graphs.

Depth-First Search (DFS) explores as far as possible along one path before backtracking. It uses a stack (or recursion) and is used for cycle detection, topological sorting and connected component analysis.

According to GeeksforGeeks, which serves over 30 million developers and students monthly, BFS and DFS are among the most frequently asked topics in data structures interviews at Indian IT companies. Both algorithms have time complexity O(V + E) when using an adjacency list. The Stack Overflow Developer Survey 2024 found that 51% of professional developers use Python as their primary language, making it the most popular choice for implementing graph algorithms and data science pipelines globally.

Real-World Uses of Graphs

  • Google Maps and navigation apps: Dijkstra’s algorithm on a weighted directed graph finds the shortest route.
  • LinkedIn and social networks: Friend suggestions use BFS to find second and third-degree connections.
  • Fraud detection: Banks model transactions as graphs to spot suspicious patterns between accounts.
  • Compiler design: DAGs represent instruction dependencies in code optimisation.
  • Recommendation engines: Flipkart and Amazon use bipartite graphs to match users with products.
  • Cybersecurity: Network topology mapping uses graphs to identify attack paths and vulnerabilities.
  • Food delivery: Platforms like Zomato and Swiggy use weighted directed graphs to optimise delivery routing across Indian cities.

Python’s networkx library is the standard tool for graph analysis in data science projects. To see which tools companies actually use for this kind of work, the guide to top data science tools companies use is worth reading next.

For those building careers in tech, understanding graphs deeply opens doors in software development, data engineering and cybersecurity. The jobs and salary after MCA guide gives a clear picture of where these skills take you.

If you are deciding which tools and languages to focus on first, the software development tools to learn first guide helps you prioritise smartly.

Frequently Asked Questions

What is a graph in data structures?

A graph in data structures is a non-linear data structure made up of vertices (nodes) and edges (connections between nodes). It can represent any many-to-many relationship, such as road networks, social connections or computer networks. Graphs are written formally as G = (V, E) where V is the vertex set and E is the edge set.

What is a complete graph?

A complete graph is an undirected graph where every vertex is directly connected to every other vertex by exactly one edge. A complete graph with n vertices has n(n-1)/2 edges and is written as Kn. For example, K4 has 4 vertices and 6 edges.

What are the types of graphs in data structures?

The main types of graphs in data structures are directed, undirected, weighted, unweighted, cyclic, acyclic, connected, disconnected, complete, bipartite and DAG (Directed Acyclic Graph). Each type suits different problem domains: DAGs work well for task scheduling, weighted graphs for shortest-path problems, and bipartite graphs for matching problems.

How is a graph represented in memory?

A graph is stored in memory using either an adjacency matrix or an adjacency list. An adjacency matrix is a V x V 2D array offering O(1) edge lookup but O(V²) space. An adjacency list stores each vertex’s neighbours in a list, using O(V + E) space, which is far more efficient for sparse graphs.

What is the difference between BFS and DFS in graph traversal?

BFS (Breadth-First Search) explores a graph level by level using a queue and is used for shortest-path finding in unweighted graphs. DFS (Depth-First Search) explores as far as possible along one branch using a stack or recursion and is used for cycle detection and topological sorting. Both have O(V + E) time complexity with an adjacency list.

Where are graphs used in real life?

Graphs are used in Google Maps for route finding, social networks for friend suggestions, fraud detection in banking, recommendation engines in e-commerce platforms like Flipkart, delivery routing at Zomato and Swiggy, and network security analysis in cybersecurity. Any system that models relationships between entities is likely using a graph under the hood.

Graphs are a core building block of computer science, and understanding them well separates candidates who clear technical interviews from those who do not. Here are three things you can do this week to move forward:

  1. Implement a graph using an adjacency list in Python or C and run BFS and DFS on it manually with a small example of 5 vertices.
  2. Solve 5 graph problems on a platform like GeeksforGeeks, starting with basic connectivity and shortest-path problems before moving to harder topics.
  3. Explore 3.0 University’s online certification courses in Cybersecurity, AI and Ethical Hacking, where graph-based thinking applies directly to network analysis, threat modelling and attack path mapping. If you want to start with a specific career-focused programme, the CEH v13 AI online course builds on exactly these programming fundamentals.

Last updated: June 2025. Reviewed by the 3University editorial team.

We create, review, and maintain educational content covering Cybersecurity, Ethical Hacking, Artificial Intelligence, Blockchain, Web3, and emerging technologies.

Read More Articles