HomeUncategorizedGraph Data Structure

Graph Data Structure

Published on

spot_img

Graph Data Structure

A graph data structure consists of nodes (vertices) that hold data and are connected by edges (relationships).

Example: Facebook

On Facebook, everything is a node: User, Photo, Album, Event, Group, Page, Comment, Story, Video, Link, Note, etc. Relationships such as posting a photo, joining a group, or liking a page create edges between these nodes.

Explanation Using Facebook

  • Nodes: Represent users, groups, pages, events, etc.
  • Edges: Represent relationships like friendships, group memberships, page likes.

Facebook is essentially a large graph, composed of these nodes and edges, utilizing a graph data structure to store and manage its data.

image 8

Graph Definition

A graph is defined as ( G = (V, E) ) where:

  • V: A collection of vertices (nodes).
  • E: A collection of edges, represented as ordered pairs of vertices (u, v).

Example Graph

  • Vertices: ( V = {0, 1, 2, 3} )
  • Edges: ( E = {(0,1), (0,2), (0,3), (1,2)} )
  • Graph: ( G = {V, E} )

Graph Terminology

  • Adjacency: Two vertices are adjacent if there is an edge between them. For example, vertices 2 and 3 are not adjacent because there is no edge between them.
  • Path: A sequence of edges that allows travel from one vertex to another. For example, 0-1, 1-2, and 0-2 are paths from vertex 0 to vertex 2.
  • Directed Graph: A graph where an edge (u, v) does not necessarily mean there is an edge (v, u). Edges are shown with arrows indicating direction.

Graph Representation

Graphs can be represented in two common ways:

1. Adjacency Matrix

An adjacency matrix is a 2D array of ( V \times V ) vertices. Each row and column represent a vertex. If element ( a[i][j] ) is 1, there is an edge between vertices ( i ) and ( j ).

Example Adjacency Matrix for the Given Graph:

0123
00111
11010
21100
31000

Since it is an undirected graph, for edge (0,2), we also mark edge (2,0), making the matrix symmetric about the diagonal.

Advantages: Fast edge lookup.

Disadvantages: Requires more space (( V \times V )).

2. Adjacency List

An adjacency list represents a graph as an array of linked lists. The index of the array represents a vertex, and each element in its linked list represents the vertices connected by edges.

Example Adjacency List for the Given Graph:

  • 0: 1 -> 2 -> 3
  • 1: 0 -> 2
  • 2: 0 -> 1
  • 3: 0

Advantages: Efficient storage.

Disadvantages: Slower edge lookup compared to adjacency matrix.

Graph Operations

Common operations on graphs include:

  1. Check Presence: Determine if a vertex or edge is present in the graph.
  2. Graph Traversal: Explore the vertices and edges of the graph (e.g., Depth-First Search, Breadth-First Search).
  3. Add Elements: Insert new vertices or edges.
  4. Find Path: Determine a path from one vertex to another.

Read More …

How to teach Math With Coding – https://kamleshsingad.com/how-to-teach-math-with-coding/

Introduction to Trees: Data Structure and Algorithm Tutorial – https://kamleshsingad.com/introduction-to-trees-data-structure-and-algorithm-tutorial/

What is a Stack? – https://kamleshsingad.com/what-is-a-stack/

Latest articles

Social Proof Marketing: How to Build Trust and Boost Conversions Fast

Social Proof Marketing is a powerful strategy to build trust and influence customer decisions using reviews, testimonials, and real experiences. In this blog, learn how businesses and every digital marketer in 2026 use social proof to Boost Conversions Fast and grow using smart digital marketing services.

7 Proven Scarcity Marketing Strategies to Boost Sales Fast

Learn how Scarcity Marketing Strategies can help you create urgency, attract more customers, and boost conversions. This blog explains simple and proven techniques every digital marketer in 2026 can use to grow their digital marketing services and drive Sales Fast.

FOMO Marketing Strategy: How Brands Use Scarcity to Drive Instant Sales

Learn how the FOMO Marketing Strategy helps Brands create urgency and drive instant sales. This blog explains simple techniques used by every digital marketer in 2026 to boost conversions using smart digital marketing services. Discover how scarcity, social proof, and limited-time offers influence customer decisions and increase engagement.

The Psychology Behind Viral Content: Why Content Goes Viral

Discover the Psychology Behind Viral Content and learn why content goes viral. This guide helps every digital marketer in 2026 create engaging Content using smart digital marketing services.

More like this

Social Proof Marketing: How to Build Trust and Boost Conversions Fast

Social Proof Marketing is a powerful strategy to build trust and influence customer decisions using reviews, testimonials, and real experiences. In this blog, learn how businesses and every digital marketer in 2026 use social proof to Boost Conversions Fast and grow using smart digital marketing services.

7 Proven Scarcity Marketing Strategies to Boost Sales Fast

Learn how Scarcity Marketing Strategies can help you create urgency, attract more customers, and boost conversions. This blog explains simple and proven techniques every digital marketer in 2026 can use to grow their digital marketing services and drive Sales Fast.

FOMO Marketing Strategy: How Brands Use Scarcity to Drive Instant Sales

Learn how the FOMO Marketing Strategy helps Brands create urgency and drive instant sales. This blog explains simple techniques used by every digital marketer in 2026 to boost conversions using smart digital marketing services. Discover how scarcity, social proof, and limited-time offers influence customer decisions and increase engagement.