Graph in Data Structures

A Graph is a nonlinear data structure used to represent connections between different things. Unlike trees, a graph does not have a root node, and it does not follow a strict parent-child structure. A graph is made up of:

  • Vertices (Nodes) – represent objects
  • Edges – represent connections between objects

Simple Real-Life Example of Graph

  • Cities connected by roads
  • People connected in social media
  • Airports connected by flights
  • Websites connected by links

All of these form a network, and networks are best represented using graphs.

What is a Graph in Simple Words?

A graph is a collection of nodes connected by edges. If you want to show relationships between things, graph is the best structure.

Basic Graph Terminology

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

Types of Graphs

Here are the main types of graphs explained simply

1. Undirected Graph

  • Connection goes both ways.
  • Edges have no direction.
  • If A is connected to B, then B is also connected to A.
  • Example: Facebook friendships, Road Network.
A - B

2. Directed Graph (Digraph)

  • Connections have direction.
  • If A points to B, it does not mean B points to A.
  • Example: Instagram followers, Website links
A -> B

3. Weighted Graph

  • Edges have weights (cost, distance, time).
  • Example: Google Maps distance calculation
A -5km- B

4. Unweighted Graph

  • No weights on edges
  • Only the connection matters
        A
       / \
      B   C
       \ /
        D

How Graphs are Stored in a Computer

There are two main ways for Graph Representation:

1. Adjacency Matrix

  • 2D array
  • Good for dense graphs
  • Uses more memory

2. Adjacency List

  • List of neighbors
  • Memory efficient
  • Used in most real applications

Why is Graph Important?

Graphs are extremely important because Most real-world problems are graph problems.

  • Used in Google Maps
  • Used in Facebook friend suggestions
  • Used in AI
  • Used in network systems
  • Used in blockchain
  • Used in recommendation systems

Frequently Asked Questions

A graph is a non-linear data structure made of vertices and edges that represent relationships between objects.

Undirected graph, Directed graph, Weighted graph, and Unweighted graph.

Graphs are used in maps, social media networks, web links, AI systems, and routing algorithms.

A tree is a special type of graph with no cycles and a root node, while a graph can have cycles and no fixed root.

Why Students Must Learn Graph

Graph is one of the most important topics in data structures. If you understand graphs:

  • BFS and DFS become easy
  • Shortest path algorithms make sense
  • Competitive programming improves
  • Interview questions become manageable