What Is Graph in Data Structure With Example?


Scott Campbell

What Is Graph in Data Structure With Example?

In data structure, a graph is a non-linear data structure that consists of a set of vertices (also called nodes) and a set of edges that connect these vertices. Graphs are widely used in computer science and real-world applications to represent various relationships between objects.

Graph Terminology

Before diving into the graph examples, let’s understand some basic terminology associated with graphs:

  • Vertex: A vertex, also known as a node, represents an entity or an object. In the context of social networks, a vertex can represent a person.
  • Edge: An edge connects two vertices and represents the relationship between them. For example, in a social network graph, an edge can represent friendship between two individuals.
  • Weight: Some graphs have weights assigned to their edges.

    These weights can represent various properties such as distance or cost.

  • Directed Graph: In a directed graph, the edges have an associated direction. The relationship between vertices is one-way.
  • Undirected Graph: In an undirected graph, the edges do not have any specific direction. The relationship between vertices is bidirectional.

Example: Social Network Graph

Let’s consider an example of a social network graph to understand how graphs work in practice.

Suppose we have four individuals: Alice, Bob, Charlie, and Dave. We want to represent their friendships using a graph.

We can start by creating four vertices representing each individual:


Now, let’s add the edges to represent their friendships:

  <li>Alice -> Bob</li>
  <li>Alice -> Charlie</li>
  <li>Bob -> Charlie</li<

In this social network graph, Alice is friends with Bob and Charlie, and Bob is also friends with Charlie.

Representation of the Social Network Graph:

Alice ----- Bob
 |          /
 |         /
 Charlie -- Dave 

This representation provides a visual understanding of the relationships between individuals. It helps us analyze and solve various problems efficiently.

Traits of Graphs:

Graphs have several unique traits that make them suitable for solving different types of problems. Some key traits include:

  • Connectivity: Graphs help determine how connected or disconnected a set of entities are.
  • Cycle Detection: Graphs are useful for detecting cycles or loops within a set of entities.
  • MST (Minimum Spanning Tree): Graphs can be used to find the minimum cost tree that connects all vertices.
  • Shortest Path: Graphs help find the shortest distance between two vertices using algorithms like Dijkstra’s algorithm.

These traits make graphs an essential tool for solving complex problems efficiently in various domains, including computer science, social networks, transportation systems, and more.

In conclusion, a graph is a powerful data structure that allows us to represent relationships between objects. By using vertices and edges, we can model various real-world scenarios easily.

Understanding the terminology and traits of graphs is crucial for effectively applying graph algorithms and solving problems efficiently.