Graph and its representations
Graph is a data structure that consists of the following two components:
- A finite set of vertices also called nodes.
- A finite set of ordered pair of the form (u, v) called edge. The pair is ordered because (u, v) is not the same as (v, u) in the case of a directed graph(di-graph). The pair of the form (u, v) indicates that there is an edge from vertex u to vertex v. The edges may contain weight/value/cost.
Following is an example of an undirected graph with 5 vertices.
Graphs are used to represent many real-life applications: Graphs are used to represent networks. The networks may include paths in a city or telephone network or circuit network. Graphs are also used in social networks like linkedIn, Facebook. For example, in Facebook, each person is represented with a vertex(or node). Each node is a structure and contains information like person id, name, gender, and locale. See this for more applications of graph.
In computer science, a graph is a data structure that is used to represent connections or relationships between objects. A graph consists of a set of vertices (also known as nodes) and a set of edges (also known as arcs) that connect the vertices. The vertices can represent anything from cities in a map to web pages in a network, and the edges can represent the relationships between them, such as roads or links.
A graph can be visualized as a collection of points (vertices) connected by lines (edges), where each vertex represents a point of interest and each edge represents a connection between two points. The edges can be directed or undirected, meaning they can either have a specific direction or be bidirectional. For example, a map of a city may have directed edges that represent the direction of one-way streets, while a social network may have undirected edges that represent friendships between individuals.
Representations of Graphs:
The following two are the most commonly used representations of a graph.
- Adjacency Matrix
- Adjacency List
There are other representations also like, Incidence Matrix and Incidence List. The choice of graph representation is situation-specific. It totally depends on the type of operations to be performed and the ease of use.
Adjacency List: An adjacency list is a simple way to represent a graph as a list of vertices, where each vertex has a list of its adjacent vertices. Here's an example of an adjacency list for an undirected graph with 4 vertices: makefile Copy code 0: 1 3 1: 0 2 2: 1 3 3: 0 2 In this example, vertex 0 is adjacent to vertices 1 and 3, vertex 1 is adjacent to vertices 0 and 2, and so on. Adjacency Matrix: An adjacency matrix is a two-dimensional array that represents the graph by storing a 1 at position (i,j) if there is an edge from vertex i to vertex j, and 0 otherwise. Here's an example of an adjacency matrix for the same undirected graph: Copy code 0 1 2 3 0 0 1 0 1 1 1 0 1 0 2 0 1 0 1 3 1 0 1 0 In this example, there is an edge from vertex 0 to vertex 1 (represented by a 1 in position (0,1)), an edge from vertex 1 to vertex 0 (represented by a 1 in position (1,0)), and so on. Incidence Matrix: An incidence matrix is a two-dimensional array that represents the graph by storing a 1 at position (i,j) if vertex i is incident on edge j, and 0 otherwise. Here's an example of an incidence matrix for the same undirected graph: Copy code 0 1 2 3 0 1 1 0 1 1 1 0 1 0 2 0 1 1 0 3 1 0 0 1 In this example, vertex 0 is incident on edges 0, 1, and 3 (represented by a 1 in positions (0,0), (0,1), and (0,3)), vertex 1 is incident on edges 0, 2 (represented by a 1 in positions (1,0) and (1,2)), and so on. Each representation has its own advantages and disadvantages depending on the application, and choosing the right representation can have a significant impact on the performance of graph algorithms.
Adjacency Matrix is a 2D array of size V x V where V is the number of vertices in a graph. Let the 2D array be adj, a slot adj[i][j] = 1 indicates that there is an edge from vertex i to vertex j. The adjacency matrix for an undirected graph is always symmetric.
Adjacency Matrix is also used to represent weighted graphs. If adj[i][j] = w, then there is an edge from vertex i to vertex j with weight w.
We follow the below pattern to use the adjacency matrix in code:
- In the case of an undirected graph, we need to show that there is an edge from vertex i to vertex j and vice versa. In code, we assign adj[i][j] = 1 and adj[j][i] = 1.
- In the case of a directed graph, if there is an edge from vertex i to vertex j then we just assign adj[i][j]=1.\
See the undirected graph shown below:
The adjacency matrix for the above example graph is:
Advantages of Adjacency Matrix:
- Representation is easier to implement and follow.
- Removing an edge takes O(1) time.
- Queries like whether there is an edge from vertex ‘u’ to vertex ‘v’ are efficient and can be done O(1).
Disadvantages of Adjacency Matrix:
- Consumes more space O(V2). Even if the graph is sparse(contains less number of edges), it consumes the same space.
- Adding a vertex takes O(V2) time. Computing all neighbors of a vertex takes O(V) time (Not efficient).
Implementation of Adjacency Matrix:
An array of linked lists is used. The size of the array is equal to the number of vertices. Let the array be an array. An entry array[i] represents the linked list of vertices adjacent to the ith vertex.
This representation can also be used to represent a weighted graph. The weights of edges can be represented as lists of pairs.
Consider the following graph:
Following is the adjacency list representation of the above graph.
Advantages of Adjacency List:
- Saves space. Space taken is O(|V|+|E|). In the worst case, there can be C(V, 2) number of edges in a graph thus consuming O(V2) space.
- Adding a vertex is easier.
- Computing all neighbors of a vertex takes optimal time.
Disadvantages of Adjacency List:
Queries like whether there is an edge from vertex u to vertex v are not efficient and can be done O(V).
Implementation of Adjacency List:
Note that in the below implementation, we use dynamic arrays (vector in C++/ArrayList in Java) to represent adjacency lists instead of the linked list. The vector implementation has the advantage of cache friendliness.
Adjacency list of vertex 0 head -> 1-> 4 Adjacency list of vertex 1 head -> 0-> 2-> 3-> 4 Adjacency list of vertex 2 head -> 1-> 3 Adjacency list of vertex 3 head -> 1-> 2-> 4 Adjacency list of vertex 4 head -> 0-> 1-> 3
Graph representation using STL for competitive programming | Set 1 (DFS of Unweighted and Undirected)
Graph implementation using STL for competitive programming | Set 2 (Weighted graph)
This article is compiled by Aashish Barnwal and reviewed by GeeksforGeeks team. Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Please Login to comment...