![]() |
VOOZH | about |
Graph is a non-linear data structure consisting of vertices and edges. The vertices are sometimes also referred to as nodes and the edges are lines or arcs that connect any two nodes in the graph. More formally a Graph is composed of a set of vertices( V ) and a set of edges( E ). The graph is denoted by G(V, E).
Imagine a game of football as a web of connections, where players are the nodes and their interactions on the field are the edges. This web of connections is exactly what a graph data structure represents, and itβs the key to unlocking insights into team performance and player dynamics in sports.
explore in detail about - Types of Graphs in Data Structures and Algorithms
There are multiple ways to store a graph, following are the most common representations:
In this method, the graph is stored in the form of the 2D matrix where rows and columns denote vertices. Each entry in the matrix represents the weight of the edge between those vertices.
π adjacency_mat1-(1)-copyBelow is the Python implementation of Graph Data Structure represented using Adjacency Matrix:
Adjacency Matrix: 0 1 1 0 1 0 1 0 1 1 0 1 0 0 1 0
Explore in detail about - Adjacency Matrix Representation
This graph is represented as a collection of linked lists. There is an array of pointer which points to the edges connected to that vertex.
π adjacency_listBelow is the Python implementation of Graph Data Structure represented using Adjacency List:
Adjacency List Representation: 0: 1 2 1: 0 2 2: 0 1 3 3: 2
Explore in detail about - Adjacency List Representation
When the graph contains a large number of edges then it is good to store it as a matrix because only some entries in the matrix will be empty. An algorithm such as Primβs and Dijkstra adjacency matrix is used to have less complexity.
| Action | Adjacency Matrix | Adjacency List |
|---|---|---|
| Adding Edge | O(1) | O(1) |
| Removing an edge | O(1) | O(N) |
| Initializing | O(N*N) | O(N) |
Below are the basic operations on the graph:
Tree is a restricted type of Graph Data Structure, just with some more rules. Every tree will always be a graph but not all graphs will be trees. Linked List, Trees, and Heaps all are special cases of graphs.
read more about - Real-Life Applications, Advantages and Disadvantages of Graph Data Structure