Graph Data Structure – Complete Beginner to Advanced Guide with BFS, DFS and Examples

Graph Data Structure – Complete Beginner to Advanced Guide with BFS, DFS and Examples

Graphs are one of the most powerful and widely used data structures in computer science. They are used to represent networks such as social media connections, road maps, computer networks, recommendation systems, and many real-world problems.

What is a Graph?

A graph is a non-linear data structure that consists of a set of vertices (nodes) and a set of edges that connect these vertices.

Basic Terminology

  • Vertex (Node): An individual point in the graph.
  • Edge: A connection between two vertices.
  • Degree: Number of edges connected to a vertex.
  • Path: A sequence of vertices connected by edges.
  • Cycle: A path that starts and ends at the same vertex.

Types of Graphs

  • Undirected Graph
  • Directed Graph (Digraph)
  • Weighted Graph
  • Unweighted Graph
  • Cyclic Graph
  • Acyclic Graph

Graph Representation

1. Adjacency Matrix

A 2D matrix where rows and columns represent vertices and values represent edges.

2. Adjacency List

Each vertex stores a list of connected vertices. This is more memory efficient.

Graph Traversal Algorithms

1. Breadth First Search (BFS)

BFS explores the graph level by level using a queue.

Applications of BFS

  • Finding shortest path in unweighted graphs
  • Web crawling
  • Social network analysis

2. Depth First Search (DFS)

DFS explores as deep as possible before backtracking. It uses recursion or stack.

Applications of DFS

  • Detecting cycles
  • Topological sorting
  • Finding connected components

Shortest Path Algorithms

  • Dijkstra’s Algorithm
  • Bellman-Ford Algorithm
  • Floyd-Warshall Algorithm

Minimum Spanning Tree (MST)

  • Prim’s Algorithm
  • Kruskal’s Algorithm

Real Life Applications of Graphs

  • Google Maps and GPS navigation
  • Social media friend suggestions
  • Computer networks routing
  • Search engines
  • Recommendation systems

Graph Interview Questions

  • What is a graph?
  • Difference between BFS and DFS?
  • What is a directed graph?
  • What is a cycle in a graph?
  • Explain Dijkstra’s algorithm.

Common Graph Problems

  • Detect cycle in a graph
  • Find shortest path
  • Count connected components
  • Topological sorting
  • Find bridges and articulation points

Practice Tips

  • Start with small graphs and draw them
  • Implement BFS and DFS from scratch
  • Solve graph problems daily
  • Understand recursion and queue usage

Conclusion

Graphs are extremely important in both academics and real-world applications. Mastering graph concepts and algorithms will greatly improve your problem-solving skills and help you crack technical interviews.

Next Post: Sorting and Searching Algorithms – Complete Guide with Time Complexity

Comments

Popular posts from this blog

5 JavaScript Console Methods You're Not Using (But Should Be)

Build a Simple Calculator with HTML, CSS & JavaScript

How to Center a Div - The 3 Modern Ways (2026)