visit
As we know data structures are very important, If we want to store data in an efficient way. So in this article, we will discuss about a data structure which is quite famous: Graph Data Structure💡. In this article, we will discuss on the following topics :
Graph Data Structure is a popular non-linear data structure, consists of finite number of vertices (nodes) and edges .
And the main thing which makes Graph Data Structure so popular is its uses in real-world applications (or problems) . Examples of its applications are telephone networks, road networks, social networks, its use in circuit networks and in all those problems in which we need a shortest path between two points .
It basically used for representing random connections, from anybody to anybody (or friendship relation, opposed of Tree Data Structure which denotes parent-child relation).
Each graph is denoted by a pair of vertex set and edge set.Graph, G ( V, E )
^ ^
| |
Vertex Edge set
set
In the above graph, set of vertices are V = {1, 2, 3, 4} and set of edges are E = {(1, 2), (2, 3), (3, 4), (4, 2)} .
Adjacent : two vertices are said to be adjacent, if an edge is present between them . Like in above Graph, 1 and 2 are adjacent.
Walk (path) : a sequence of edges, we get while travelling through the vertices of Graph . In some places, walk is said path. And in this, repetition of edge ( hence of vertex also) is allow. Like in above Graph: 1- 2, 2- 3, 3- 2 is a walk.
Path (simple path) : It is a walk, without repeated edge and vertex . In some places, path is said simple path. Like in above Graph : 1- 2, 2- 3 is a path.
Cyclic : if a path exists in the Graph, which starts and end with same vertex then Graph is said to be cyclic. It can be Directed or Undirected Graph.
Acyclic : When a Graph is not Cyclic, then it is Acyclic . It also, can be Directed or Undirected Graph .
1. Undirected Graph : an Edge in Undirected Graph has no specific direction . They are bidirectional in nature .
2. Directed Graph: an Edge in Directed Graph has a specific direction . They are also known as digraph.
It is same as Simplex Mode of communication, in which communication is unidirectional .
3. Weighted Graph : In weighted graph, values are associated with edges known as weights . These weights can represent cost ( like traffic in computer networks ) or length ( like distance in road networks ) .
4. Unweighted Graph : Graph in which no weights (or values) are associated with edges . By default, all graphs are unweighted, unless values are associated with edge. They can be Directed or Undirected also .
For Undirected Graph :
For Directed Graph :
1 . Adjacency Matrix : Graph is represented by a 2D-matrix, called adjacency matrix . The size of matrix is V * V, where V is number of vertex .
Each row and column represents a vertex, and value at row i column j, matrix[i][j] = 1 if there is an edge between vertex i and vertex j. otherwise matrix[i][j] = 0.Before we discuss second way to store Graph, let we first discuss some Properties of Adjacency Matrix :
Space required : theta (V * V) Time required for Operations :Problem with this method ? If you look at the matrix, you find that it stores redundant information (information of vertex, having no connection).
2. Adjacency List : In this method, Graph is represented by an array of list. And an array list can be a dynamic size array or linked list .
A single index, A[i] represents the list of nodes(or vertex) adjacent to the vertex i .Why this? The reason for using this method is that it stores only relevant information (information of only adjacent vertex). And a common operation, which used in many algorithms (like DFS and BFS), operation of finding all adjacent vertex is fast in this.
Some properties of Adjacency Lists are :
Space required :Searching for a node or a path (simple path) require Graph traversal . There are two ways available,in which we can find a node or traverse a Graph :