Representation of Graph Data Structure"๐Ÿ˜Š

     "Representation of Graph Data Structure"๐Ÿ˜Š


A graph data structure represents relationships between entities. It consists of:

  • Vertices (Nodes): entities

  • Edges (Links): connections between entities

There are several common ways to represent a graph, each with trade-offs.


1. Adjacency Matrix

A 2D array V × V where V is the number of vertices.

Example



๐Ÿ‘‰Graph with vertices A, B, C:

A B C A 0 1 1 B 1 0 0 C 1 0 0
  • 1 indicates an edge, 0 indicates no edge

  • For weighted graphs, store weights instead of 1

Properties

  • Space: O(V²)

  • Edge lookup: O(1)

  • Best for: Dense graphs


2. Adjacency List

Each vertex stores a list of adjacent vertices.

Example

Properties

  • Space: O(V + E)

  • Edge lookup: O(deg(V))

  • Best for: Sparse graphs


3. Edge List

Graph is represented as a list of edges.

Example

(A, B) (A, C) (B, A) (C, A)

Properties

  • Space: O(E)

  • Edge lookup: O(E)

  • Best for: Simple algorithms like Kruskal’s


4. Incidence Matrix

A matrix with rows as vertices and columns as edges.

Example

Properties

  • Space: O(V × E)

  • Used in: Mathematical graph theory


Comparison Table

RepresentationSpaceEdge CheckBest Use Case
Adjacency MatrixO(V²)O(1)Dense graphs
Adjacency ListO(V + E) O(deg V)Sparse graphs
Edge ListO(E)O(E)MST, simple input
Incidence MatrixO(V × E)O(E)Theoretical graphs

๐Ÿ‘‰Directed vs Undirected

  • Undirected graph: edges are bidirectional

  • Directed graph: edges have direction (u → v).


๐Ÿ‘Representation of Graph Data Structure (Short)

A graph is represented using vertices (nodes) and edges (connections). Common representations are:

  1. Adjacency Matrix

    • Uses a 2D array

    • 1 means edge exists, 0 means no edge

    • Best for dense graphs

  2. Adjacency List

    • Each vertex stores a list of connected vertices

    • Saves memory

    • Best for sparse graphs

  3. Edge List

    • Graph stored as a list of (u, v) pairs

    • Simple and easy to use

    • Used in Kruskal’s algorithm

  4. Incidence Matrix

    • Rows = vertices, columns = edges

    • Mainly used in theory

Comments

Popular posts from this blog

Graph

Graph Traversal :-"BFS"