Introduction to Graph Theory for GATE Exam

Graph theory is a branch of mathematics that deals with the study of graphs, which are mathematical structures used to represent relationships between objects. In graph theory, a "graph" consists of two main components: vertices (or nodes) and edges (or links). Vertices represent the objects, and edges represent the connections or relationships between those objects. Graphs can be used to model and analyze a wide range of real-world phenomena and are a versatile data structure with applications in various fields. Here's an overview of graph theory and its applications in different domains:

  1. Computer Science:
    • Data Structures: Graphs are a fundamental data structure in computer science. They are used to represent and solve various problems, such as representing networks, maps, and hierarchical structures.
    • Algorithms: Graph algorithms, such as Dijkstra's algorithm for shortest path, breadth-first search, and depth-first search, are crucial for solving problems related to routing, network analysis, and more.
    • Social Networks: Graphs are used to model and analyze social networks like Facebook, Twitter, and LinkedIn, where individuals (vertices) are connected by friendships or interactions (edges).
  2. Transportation:
    • Routing and Navigation: Graphs are used to model road networks, public transportation systems, and airline routes. Algorithms like Dijkstra's and A* are used for finding the shortest or quickest routes.
    • Traffic Analysis: Graphs help analyze traffic flow, congestion patterns, and optimize traffic signal timings in urban planning.
  3. Social Sciences:
    • Social Network Analysis: Researchers use graph theory to study social interactions, influence, and community structures within various populations.
    • Epidemiology: Graphs are used to model the spread of diseases within populations, helping in understanding and controlling epidemics.
  4. Biology:
    • Genomics: In genomics, graphs are used to represent genetic relationships, gene expression networks, and protein-protein interaction networks.
    • Neuroscience: Graph theory is applied to model and study the brain's connectivity, helping researchers understand neural networks and brain disorders.
  5. Recommendation Systems:
    • Collaborative Filtering: Recommender systems often use user-item interaction data to create user-item graphs, enabling personalized recommendations.
  6. Cybersecurity:
    • Network Security: Graphs are used to model and analyze network traffic, identifying patterns of intrusion or abnormal behavior.
  7. Finance:
    • Portfolio Optimization: Graphs can represent correlations between financial assets, helping investors optimize portfolios.
    • Fraud Detection: Graphs can model transaction data to identify unusual patterns indicative of fraud.
  8. Natural Language Processing:
    • Semantic Networks: Graphs represent relationships between words or concepts in language, aiding in understanding context and meaning.

The importance of graphs as a versatile data structure lies in their ability to capture complex relationships and structures in a wide range of applications. They provide a powerful and intuitive way to model and analyze real-world problems, making them an essential tool in various fields of science, engineering, and beyond. Graph theory continues to play a crucial role in advancing our understanding of complex systems and optimizing processes in our interconnected world.

Basic Graph Concepts

A graph is a mathematical structure that consists of a collection of vertices (or nodes) and edges (or links) that connect these vertices. In a graph, the vertices represent objects or entities, while the edges represent relationships or connections between them. Graphs are a fundamental concept in graph theory and have various applications in modeling and solving real-world problems.

Here are the concepts of directed and undirected graphs:

1. Undirected Graph:

  • In an undirected graph, the edges have no direction. This means that if there is an edge between vertex A and vertex B, it implies that there is a symmetric relationship: A is connected to B, and B is connected to A.
  • Undirected graphs are used to represent symmetric relationships or connections, such as friendships in a social network, connections between cities in a transportation network, or physical connections between devices in a computer network.
  • Example: A social network where individuals are represented as vertices, and friendships between them are represented as undirected edges.

2. Directed Graph (Digraph):

  • In a directed graph, the edges have a direction associated with them. This means that if there is an edge from vertex A to vertex B, it does not imply the existence of an edge from B to A.
  • Directed graphs are used to represent asymmetric relationships or connections, such as one-way streets in a road network, dependencies between tasks in a project, or the flow of information in a computer network.
  • Example: A flowchart representing a series of tasks in which each task is a vertex, and directed edges indicate the order in which the tasks should be executed.

3. Transportation Networks:

  • Road networks, where cities or intersections are represented as vertices, and roads connecting them are represented as edges in an undirected graph.
  • Airline routes, where airports are represented as vertices, and flight routes between them are represented as directed edges in a digraph.

4. Social Networks:

  • Social media platforms, where users are represented as vertices, and friendships or follows are represented as undirected edges.
  • Twitter's retweet and follow relationships, where users are vertices, and retweets and follows are represented as directed edges in a digraph.

5. Internet and Computer Networks:

  • The internet, where routers and servers are represented as vertices, and network connections between them are represented as directed edges.
  • Web page links, where web pages are vertices, and hyperlinks between pages are represented as directed edges.

6. Project Management:

  • Project scheduling, where tasks or activities are represented as vertices, and dependencies between tasks are represented as directed edges in a digraph.

7. Biology:

  • Protein-protein interaction networks, where proteins are represented as vertices, and interactions between proteins are represented as undirected edges.

8. Recommendation Systems:

  • Collaborative filtering systems, where users and items are represented as vertices, and user-item interactions (e.g., ratings) are represented as weighted edges in a bipartite graph.

These examples illustrate how graphs can model a wide range of relationships and structures in various domains, making them a versatile tool for problem-solving and analysis.

Types of Graphs

Let's introduce different types of graphs along with their characteristics and common use cases:

1. Undirected Graphs:

  • Characteristics: In undirected graphs, edges have no direction. They represent symmetric relationships between vertices, meaning if there is an edge from vertex A to vertex B, there is also an edge from B to A.
  • Use Cases: Undirected graphs are used to model relationships or connections where the order doesn't matter and where there is no inherent direction. Examples include social networks (friendship relationships), transportation networks (roads and connections between cities), and computer networks (connections between devices).

2. Directed Graphs (Digraphs):

  • Characteristics: In directed graphs, edges have a direction. They represent asymmetric relationships, indicating a one-way connection from one vertex to another.
  • Use Cases: Directed graphs are used when modeling relationships with a clear direction or flow. Common applications include representing dependencies in project management (task precedence), information flow in computer networks (data packets), and road networks (one-way streets).

3. Weighted Graphs:

  • Characteristics: Weighted graphs assign numerical values (weights) to edges, representing the cost, distance, or some other measure associated with each edge.
  • Use Cases: Weighted graphs are used when the relationships between vertices have associated numerical values. Examples include distance maps (road networks with travel distances), financial networks (transaction costs), and optimization problems (minimizing or maximizing a certain metric).

4. Bipartite Graphs:

  • Characteristics: Bipartite graphs are divided into two disjoint sets of vertices, and all edges connect vertices from one set to the other. There are no edges connecting vertices within the same set.
  • Use Cases: Bipartite graphs are often used to represent relationships between two different types of entities. Common applications include modeling user-item interactions in recommendation systems (users and products), representing relationships in social networks (people and events), and matching problems (e.g., assigning tasks to workers).

5. Cyclic and Acyclic Graphs:

  • Cyclic Graphs: Cyclic graphs contain at least one cycle, which is a path that starts and ends at the same vertex. These cycles can represent feedback loops or recurring patterns in relationships.
  • Acyclic Graphs: Acyclic graphs, as the name suggests, do not contain any cycles. They represent acyclic relationships or hierarchies.
  • Use Cases:
    • Cyclic Graphs: They are used to model scenarios with feedback loops, such as in control systems, where changes in one variable affect another.
    • Acyclic Graphs: These are commonly used for representing hierarchical structures, like organizational charts, family trees, and dependency graphs in software or project management.

Understanding these different types of graphs and their characteristics is crucial for selecting the appropriate representation for various real-world problems. Choosing the right type of graph allows for more accurate modeling and efficient problem-solving in fields like computer science, mathematics, engineering, and social sciences.

Graph Representation

Graphs can be represented using various data structures, each with its own advantages and disadvantages. Two common methods for representing graphs are the adjacency matrix and the adjacency list:

1. Adjacency Matrix:

  • Definition: An adjacency matrix is a 2D array where each cell (i, j) represents whether there is an edge between vertex i and vertex j. Typically, it's a binary matrix for unweighted graphs, where a value of 1 indicates the presence of an edge, and 0 indicates the absence.


  • Simplicity: It's straightforward to implement and understand, especially for dense graphs with many edges.
  • Edge Existence Checking: Determining if an edge exists between two vertices is efficient, as it's a constant-time operation.
  • Space Efficiency: For dense graphs, it can be more space-efficient than adjacency lists, as it uses n^2 space, where n is the number of vertices.


  • Space Inefficiency: For sparse graphs (few edges), it can be very space-inefficient, as it allocates space for all possible edges, even if they don't exist.
  • Inefficient for Iteration: Iterating through neighbors of a vertex can be inefficient, as it requires checking all columns for a row.

2. Adjacency List:

  • Definition: An adjacency list is a collection of lists or arrays, where each vertex has a list of its adjacent vertices (neighbors).
  • Pros:
    • Space Efficiency: It's more space-efficient for sparse graphs since it only stores information about existing edges.
    • Efficient for Iteration: Iterating through neighbors of a vertex is efficient, as it only involves traversing the list of adjacent vertices.
    • Dynamic: It can easily accommodate changes in the graph's structure, such as adding or removing edges.
  • Cons:
    • Edge Existence Checking: Determining if an edge exists between two vertices can be less efficient than in an adjacency matrix, as it requires searching through the list of neighbors.
    • Less Efficient for Dense Graphs: In very dense graphs, adjacency lists may consume more memory than an adjacency matrix.

The choice between these representations depends on the characteristics of the graph and the specific operations you need to perform:

  • Use Adjacency Matrix When:
    • The graph is dense (many edges).
    • Edge existence checks are frequent, and you need constant-time performance.
    • Memory usage is not a concern for your application.
  • Use Adjacency List When:
    • The graph is sparse (few edges).
    • You need to efficiently iterate through neighbors of a vertex.
    • Memory efficiency is a concern, especially for large graphs.
    • The graph may change frequently, as adjacency lists are more flexible for dynamic updates.

In practice, many graph algorithms and applications leverage both representations based on the specific tasks they need to perform. For example, some algorithms may use adjacency lists for efficient neighbor traversal but switch to an adjacency matrix for quick edge existence checks. Understanding the trade-offs between these representations is crucial for efficient graph modeling and algorithm design.

Basic Graph Algorithms

Graph Traversal:

Depth-First Search (DFS) and Breadth-First Search (BFS) are two fundamental graph traversal algorithms used to explore and analyze graphs. They have different strategies for visiting vertices and edges within a graph and serve various purposes in problem-solving and analysis.

Depth-First Search (DFS):

  • How it works: DFS starts at an initial vertex and explores as far as possible along each branch before backtracking. It explores a single branch of the graph as deeply as possible before moving to another branch.
  • Algorithm:
    1. Start at an initial vertex.
    2. Explore an adjacent, unvisited vertex.
    3. Repeat step 2 until there are no unvisited adjacent vertices.
    4. Backtrack to the previous vertex and explore other unvisited vertices.
    5. Repeat steps 2-4 until all vertices have been visited.
  • Applications:
    • Finding connected components in a graph.
    • Solving maze and puzzle problems.
    • Topological sorting in directed acyclic graphs.
    • Detecting cycles in a graph.
    • Pathfinding algorithms (with some modifications).

Breadth-First Search (BFS):

  • How it works: BFS explores all vertices at the current level before moving to the next level. It visits all neighbors of the starting vertex before visiting their neighbors.
  • Algorithm:
    1. Start at an initial vertex and mark it as visited.
    2. Enqueue the initial vertex.
    3. While the queue is not empty:
      • Dequeue a vertex from the queue.
      • Explore all unvisited neighbors of the dequeued vertex and mark them as visited.
      • Enqueue these neighbors.
  • Applications:
    • Shortest path finding in unweighted graphs (due to the level-wise exploration).
    • Web crawling and network broadcasting.
    • Puzzle solving, like the classic "15 Puzzle."
    • Connected component detection.
    • Bipartite graph detection.


  1. Finding Paths:
    • DFS: DFS can be used to find paths between two vertices in a graph. It explores down one branch as deeply as possible and backtracks only when necessary.
    • BFS: BFS is often used to find the shortest path between two vertices in an unweighted graph because it explores all vertices level by level, ensuring that the shortest path is found first.
  2. Connected Components:
    • DFS: DFS can be used to find connected components in an undirected graph. It starts at an arbitrary vertex, explores all vertices reachable from it, marks them as part of one connected component, and repeats the process for unvisited vertices.
    • BFS: BFS can also find connected components, but it may visit the vertices in a different order. In either case, it helps identify groups of vertices that are connected to each other.

In summary, DFS and BFS are essential graph traversal algorithms that serve various purposes in graph analysis and problem-solving. DFS is more suited for tasks that involve deep exploration of a graph, while BFS is often used for finding the shortest path or exploring a graph layer by layer. Both algorithms have applications in various domains, including computer science, social network analysis, and artificial intelligence.

Shortest Path Algorithms:

Dijkstra's Algorithm for Finding Shortest Paths in Weighted Graphs:

Dijkstra's algorithm is a widely used method for finding the shortest path from a source vertex to all other vertices in a weighted graph, where each edge has a non-negative weight. It ensures that the shortest path to each vertex is found iteratively and is guaranteed to work correctly on non-negative weight graphs.

Here's how Dijkstra's algorithm works:

  1. Initialize a distance array that keeps track of the shortest distance from the source vertex to all other vertices. Initially, set the distance of the source vertex to 0 and all other vertices to infinity.
  2. Create a set (or priority queue) of unprocessed vertices, initially containing all vertices.
  3. While there are unprocessed vertices: a. Choose the vertex with the smallest distance value from the set. b. For the selected vertex, consider all its neighbors (vertices connected by an edge). c. For each neighbor, calculate the tentative distance from the source through the selected vertex. If this distance is less than the current distance stored for that neighbor, update the distance. d. Mark the selected vertex as processed (remove it from the set).
  4. Repeat step 3 until all vertices are processed.

Dijkstra's algorithm guarantees finding the shortest path from the source vertex to all other vertices in a non-negative weighted graph. However, it does not work correctly when there are negative-weight edges or cycles.

Bellman-Ford Algorithm for Handling Graphs with Negative-Weight Edges:

The Bellman-Ford algorithm is used to find the shortest path in a graph, even if it contains edges with negative weights or cycles. It detects negative-weight cycles and reports when no shortest path exists due to negative cycles.

Here's how the Bellman-Ford algorithm works:

  1. Initialize a distance array with all distances set to infinity, except the source vertex's distance, which is set to 0.
  2. Iterate through all edges (|V| - 1) times, where |V| is the number of vertices. In each iteration, update the distance array by considering all edges.
  3. For each edge (u, v) with weight w, if the distance to vertex u plus w is less than the distance to vertex v, update the distance to v with this new value.
  4. After the iterations, if any further relaxation of distances occurs, it indicates the presence of a negative-weight cycle. In this case, the algorithm reports that there is no shortest path due to the negative cycle.
  5. If no negative-weight cycle is detected, the distance array will contain the shortest path distances from the source vertex to all other vertices.


Let's consider an example of finding the shortest path in a road network using Dijkstra's algorithm:

  • Suppose we have a road network with cities (vertices) connected by roads (edges), and each road has a distance or travel time associated with it as a weight.
  • We want to find the shortest path from a source city to a destination city using Dijkstra's algorithm, taking into account the road distances.

For the Bellman-Ford algorithm:

  • Suppose we have a network of flights between cities, where each flight has a cost (possibly negative) associated with it.
  • We want to find the cheapest way to travel from one city to another, taking into account both positive and negative costs associated with flights.

These algorithms are valuable tools for optimizing travel and logistics in various real-world scenarios, such as finding the shortest route for navigation or the most cost-effective way to reach a destination in transportation and airline industries.


1. Graph Representation using Adjacency List:


2. Depth-First Search (DFS) Algorithm:


3. Dijkstra's Algorithm for Shortest Path:


These code examples and exercises provide hands-on practice with graph representations and algorithms in Python. Students can use these as a starting point to understand, implement, and experiment with graph-related concepts, helping them gain a deeper understanding of the subject.


In conclusion, graph theory is a foundational and versatile field of mathematics and computer science with wide-ranging applications. It provides a powerful framework for modeling and analyzing complex relationships and structures in various domains. We've explored key graph theory concepts, representations, and algorithms, empowering you to apply these principles in practical problem-solving scenarios.

Key Takeaways:

  • Graphs consist of vertices and edges to represent relationships between objects.
  • Types of graphs include undirected, directed, weighted, bipartite, and cyclic/acyclic graphs, each serving specific modeling needs.
  • Adjacency matrices and adjacency lists are common representations with different space and efficiency characteristics.
  • DFS and BFS are fundamental graph traversal algorithms used for tasks like pathfinding, connected component detection, and more.
  • Dijkstra's Algorithm finds the shortest path in weighted graphs with non-negative edge weights.
  • Bellman-Ford Algorithm handles graphs with negative-weight edges and detects negative-weight cycles.
  • Graph theory applies across computer science, transportation, social networks, biology, recommendation systems, and more, offering solutions to diverse problems.

By mastering these graph theory fundamentals, you'll be well-prepared to address complex challenges in both academic and practical contexts.

Practice Questions

1: In graph theory, what do vertices represent?

a. The connections between objects. b. The weight of edges. c. The objects or entities. d. The paths between nodes.


c. The objects or entities.

2: Which type of graph is suitable for representing asymmetric relationships, such as one-way streets in a road network?

a. Directed graph (digraph). b. Bipartite graph. c. Weighted graph. d. Undirected graph.


a. Directed graph (digraph).

3: What is the primary difference between Dijkstra's algorithm and Bellman-Ford algorithm?

a. Dijkstra's algorithm is for finding the shortest path in weighted graphs with negative edges, while Bellman-Ford is for non-negative edges. b. Dijkstra's algorithm works only for directed graphs, while Bellman-Ford works for undirected graphs. c. Dijkstra's algorithm cannot handle negative-weight edges, while Bellman-Ford can. d. Dijkstra's algorithm guarantees finding the shortest path to all vertices, while Bellman-Ford does not.


c. Dijkstra's algorithm cannot handle negative-weight edges, while Bellman-Ford can.

4: In a bipartite graph, the vertices can be divided into how many disjoint sets?

a. One b. Two c. Three d. Four


b. Two

5: Which graph traversal algorithm explores vertices level by level and is often used to find the shortest path in unweighted graphs?

a. Depth-First Search (DFS) b. Breadth-First Search (BFS) c. Dijkstra's algorithm d. Bellman-Ford algorithm


b. Breadth-First Search (BFS)

Module 3: Programming, Data Structures, and AlgorithmsIntroduction to Graph Theory for GATE Exam

Top Tutorials

Related Articles

Made with heartin Bengaluru, India
  • Official Address
  • 4th floor, 133/2, Janardhan Towers, Residency Road, Bengaluru, Karnataka, 560025
  • Communication Address
  • 4th floor, 315 Work Avenue, Siddhivinayak Tower, 152, 1st Cross Rd., 1st Block, Koramangala, Bengaluru, Karnataka, 560034
  • Follow Us
  • facebookinstagramlinkedintwitteryoutubetelegram

© 2024 AlmaBetter