
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Implementation of a Hypergraph
In this tutorial, we will learn to implement the hypergraph in C++.
Definition ? The hypergraph is a special version of the graph. In which the single can connect 2 or more vertices.
In a normal graph, the single edge can connect only 2 vertices, but a hypergraph is a generalization of the graph and can be used to connect more than 2 vertices with the single edge.
In the hypergraph, the edge is called the hyperedge. We can represent the hypergraph with H(E, V), where E is a hyperedge and v is the set of vertices connected by the single hyperedge.
Here, we have implemented the hypergraph.
Example
In the example below, we have demonstrated the implementation of the hypergraph using the map data structure in C++. In the map, we store the edge name as a key and the set of vertices connected by the edge as a value.
After that, we used the erase() method to remove the ?edge2' from the graph. Also, use the insert() method to insert the ?edge4' connecting 4 vertices into the graph.
At last, we have printed all the edges of the graph with the vertices it connects.
#include <bits/stdc++.h> #include <iostream> using namespace std; void createHyperGraph() { // Creating the hypergraph map<string, vector<int>> h_graph = {{"edge1", {32, 21, 90}}, {"edge2", {21, 47, 54}}, {"edge3", {43, 76}}}; // Removing edge from the hypergraph h_graph.erase("edge2"); // Inserting a new edge in the hypergraph h_graph.insert({"edge4", {48, 61, 93, 52, 89}}); cout << "The hypergraph is :-" << endl; for (auto ele : h_graph) { string edge = ele.first; cout << edge << " : "; vector<int> vert = ele.second; for (int v : vert) { cout << v << " "; } cout << endl; } } int main() { createHyperGraph(); return 0; }
Output
The hypergraph is :- edge1 : 32 21 90 edge3 : 43 76 edge4 : 48 61 93 52 89
Time complexity ? O(N) to traverse all edges.
Space complexity ? O(N) to store N edges.
In the above example, we have seen that the hyperedge can connect the different vertices.
Real?life use Cases of Hypergraph
When we look at the implementation of the hypergraph over the normal graph, the first question is why we should use the hypergraph. Here, we will see some real?life use cases where hypergraphs can be used.
Social networks ? We can use the hypergraph to represent the social network. In social networks, people may connect with different relationships such as friendship, work colleagues, family, etc. So, we can use each edge as a relationship and each person as a vertex of the graph. Now, we can think that there might be more than 2 persons in each relationship. For example, the family contains 4 to 5 people and a group of 10 friends.
Database modeling ? We can use the hypergraph to model the database in which we need to connect multiple attributes of the table in a single relationship.
Complex System representation ? Another use case for using the hypergraph is while developing complex systems such as transportation systems, biological interaction, etc.
Types of Hypergraph
Here, we will discuss 5 types of the hypergraph.
Uniform Hypergraphs: Each edge of the uniform hypergraph contains the same number of vertices.
Bipartite Hypergraphs: In bipartite hypergraphs, each vertex is divided into two disjoint sets. Also, each hyperedge contains the vertices from both sets.
Directed Hypergraphs: In the directed hypergraph, each hyperedge has direction. So, we need to consider each hyperedge's order of connected vertices.
Hypergraphs with Weights: We can assign a weight for each connection of vertices to assign different importance to each connection.
Hypergraphs with Labels: We can add a label for each connection of the vertex to convey more information about the vertex.
Here, we have implemented the basic hypergraph. However, in real?time development, a single hyperedge can connect hundreds of graph vertices. Also, we have seen the types of hypergraphs and real?life use cases.