VOOZH about

URL: https://www.geeksforgeeks.org/dsa/graph-representations-using-set-hash/

⇱ Graph representations using set and hash - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Graph representations using set and hash

Last Updated : 8 Mar, 2023

We have introduced Graph implementation using array of vectors in Graph implementation using STL for competitive programming | Set 1. In this post, a different implementation is used which can be used to implement graphs using sets. The implementation is for adjacency list representation of graph.

A set is different from a vector in two ways: it stores elements in a sorted way, and duplicate elements are not allowed. Therefore, this approach cannot be used for graphs containing parallel edges. Since sets are internally implemented as binary search trees, an edge between two vertices can be searched in O(logV) time, where V is the number of vertices in the graph. Sets in python are unordered and not indexed. Hence, for python we will be using dictionary which will have source vertex as key and its adjacency list will be stored in a set format as value for that key.

Following is an example of an undirected and unweighted graph with 5 vertices. 

👁 8


Below is adjacency list representation of this graph using array of sets

👁 9


Below is the code for adjacency list representation of an undirected graph using sets: 


Output
Adjacency list of vertex 0
1 4 

Adjacency list of vertex 1
0 2 3 4 

Adjacency list of vertex 2
1 3 

Adjacency list of vertex 3
1 2 4 

Adjacency list of vertex 4
0 1 3 

Edge from 2 to 1 found.

Edge from 0 to 3 not found.

Pros: Queries like whether there is an edge from vertex u to vertex v can be done in O(log V).
Cons

  • Adding an edge takes O(log V), as opposed to O(1) in vector implementation.
  • Graphs containing parallel edge(s) cannot be implemented through this method.

Space Complexity: O(V+E), where V is the number of vertices and E is the number of edges in the graph. This is because the code uses an adjacency list to store the graph, which takes linear space.

Further Optimization of Edge Search Operation using unordered_set (or hashing): The edge search operation can be further optimized to O(1) using unordered_set which uses hashing internally.

Implementation:


Output
Adjacency list of vertex 0
4 1 

Adjacency list of vertex 1
4 3 2 0 

Adjacency list of vertex 2
3 1 

Adjacency list of vertex 3
4 2 1 

Adjacency list of vertex 4
3 1 0 

Edge from 2 to 1 found.

Edge from 0 to 3 not found.


Time Complexity: The time complexity of creating a graph using adjacency list is O(V + E), where V is the number of vertices and E is the number of edges in the graph.

Space Complexity: The space complexity of creating a graph using adjacency list is O(V + E), where V is the number of vertices and E is the number of edges in the graph.

Pros

  • Queries like whether there is an edge from vertex u to vertex v can be done in O(1).
  • Adding an edge takes O(1).

Cons

  • Graphs containing parallel edge(s) cannot be implemented through this method.
  • Edges are stored in any order.

Note : adjacency matrix representation is the most optimized for edge search, but space requirements of adjacency matrix are comparatively high for big sparse graphs. Moreover adjacency matrix has other disadvantages as well like BFS and DFS become costly as we can't quickly get all adjacent of a node. 

Comment
Article Tags: