Open In App

Implementation of a Hypergraph

Last Updated : 11 Jan, 2023
Comments
Improve
Suggest changes
Like Article
Like
Report

What is Hypergraph?

A hypergraph is a generalization of a graph, where an edge can connect any number of vertices. In a hypergraph, each edge is called a hyperedge and can connect any number of vertices, instead of just two vertices like in a traditional graph.

A hypergraph is represented by H(E, V) where E is the HyperEdge and V is the value linked with that edge.

To know more about the Hypergraph, refer here.

How to implement a Hypergraph in C++?

To implement a hypergraph, we can use you can use a map. Follow the steps mentioned below to implement a hypergraph in C++:

  • Create a map to store the hyperedges and their associated vertices. 
  • The key for the unordered_map can be a string representing the name of the hyperedge, and 
  • The value can be a vector containing the vertices that the hyperedge is connected to.

Below is the C++ code to implement the hypergraph:

C++
// C++ Implementation of the above graph
#include <bits/stdc++.h>
#include <iostream>
using namespace std;
// #include <string>
// #include <vector>

// Function to create Hypergraph
void hyperGraph()
{
    // Define a hypergraph with 3 hyperedges
    // and 6 vertices
    map<string, vector<int> > hypergraph
        = { { "e1", { 1, 2, 3 } },
            { "e2", { 3, 4, 5 } },
            { "e3", { 2, 4, 6 } } };

    // Add a new hyperedge
    hypergraph.insert({ "e4", { 1, 5, 6 } });

    // Remove a hyperedge
    hypergraph.erase("e2");

    // Modify a hyperedge
    hypergraph["e1"] = { 1, 2 };

    // Print the hypergraph
    cout << "Hypergraph:" << endl;
    for (auto a : hypergraph) {
        string edge = a.first;
        cout << edge << " : ";
        vector<int> vertices = a.second;
        for (int vertex : vertices) {
            cout << vertex << " ";
        }
        cout << endl;
    }
}

// Driver Code
int main()
{
    // Function call
    hyperGraph();

    return 0;
}
Java
// Java Implementation of the above graph

import java.io.*;
import java.util.*;

class GFG {

    // Function to create Hypergraph
    static void hyperGraph()
    {
        // Define a hypergraph with 3 hyperedges
        // and 6 vertices
        Map<String, List<Integer> > hypergraph
            = new HashMap<>();
        hypergraph.put("e1", Arrays.asList(1, 2, 3));
        hypergraph.put("e2", Arrays.asList(3, 4, 5));
        hypergraph.put("e3", Arrays.asList(2, 4, 6));

        // Add a new hyperedge
        hypergraph.put("e4", Arrays.asList(1, 5, 6));

        // Remove a hyperedge
        hypergraph.remove("e2");

        // Modify a hyperedge
        hypergraph.put("e1", Arrays.asList(1, 2));

        // Print the hypergraph
        System.out.println("Hypergraph:");
        for (Map.Entry<String, List<Integer> > entry :
             hypergraph.entrySet()) {
            String edge = entry.getKey();
            System.out.print(edge + " : ");
            List<Integer> vertices = entry.getValue();
            for (int vertex : vertices) {
                System.out.print(vertex + " ");
            }
            System.out.println();
        }
    }

    public static void main(String[] args)
    {
        // Function call
        hyperGraph();
    }
}

// This code is contributed by lokeshmvs21.
Python3
# Python Implementation of the above graph

# Function to create Hypergraph
def hyper_graph():
    # Define a hypergraph with 3 hyperedges
    # and 6 vertices
    hypergraph = {
        "e1": [1, 2, 3],
        "e2": [3, 4, 5],
        "e3": [2, 4, 6],
    }

    # Add a new hyperedge
    hypergraph["e4"] = [1, 5, 6]

    # Remove a hyperedge
    del hypergraph["e2"]

    # Modify a hyperedge
    hypergraph["e1"] = [1, 2]

    # Print the hypergraph
    print("Hypergraph:")
    for edge, vertices in hypergraph.items():
        print(f"{edge}: {vertices}")

# Function call
hyper_graph()

# This code is contributed by manav23lohani.
C#
// C# Implementation of the above graph

using System;
using System.Collections.Generic;  

class GFG {

    // Function to create Hypergraph
    static void hyperGraph()
    {
        // Define a hypergraph with 3 hyperedges
        // and 6 vertices
        Dictionary<string, List<int>> hypergraph =  
                       new Dictionary<string, List<int>>(); 
        hypergraph.Add("e1",  new List<int>{1, 2, 3});
        hypergraph.Add("e2",  new List<int>{3, 4, 5});
        hypergraph.Add("e3",  new List<int>{2, 4, 6});

        // Add a new hyperedge
        hypergraph.Add("e4",  new List<int>{1, 5, 6});

        // Remove a hyperedge
        hypergraph.Remove("e2");

        // Modify a hyperedge
        hypergraph["e1"] =  new List<int>{1, 2};

        // Print the hypergraph
        Console.WriteLine("Hypergraph:");
        foreach(KeyValuePair<string,List<int>> entry in hypergraph)
          {
              string edge = entry.Key;
              Console.Write(edge + " : ");
              List<int> vertices=entry.Value;
              foreach (var vertex in vertices) {
                Console.Write(vertex + " ");
              }
              Console.WriteLine();
          }
    }

    public static void Main()
    {
        // Function call
        hyperGraph();
    }
}

// This code is contributed by Pushpesh Raj
JavaScript
// JavaScript Implementation
function hyperGraph() {
  // Define a hypergraph with 3 hyperedges
  // and 6 vertices
  let hypergraph = {
    e1: [1, 2, 3],
    e2: [3, 4, 5],
    e3: [2, 4, 6]
  };

  // Add a new hyperedge
  hypergraph['e4'] = [1, 5, 6];

  // Remove a hyperedge
  delete hypergraph.e2;

  // Modify a hyperedge
  hypergraph.e1 = [1, 2];

  // Print the hypergraph
  console.log('Hypergraph:');
  for (let [edge, vertices] of Object.entries(hypergraph)) {
    console.log(`${edge} : ${vertices.join(' ')}`);
  }
}

// Driver Code
hyperGraph();

Output
Hypergraph:
e1 : 1 2 
e3 : 2 4 6 
e4 : 1 5 6 

Time Complexity: O(N) // since we are traversing all the elements inside the unordered map using a for loop
Auxiliary Space: O(N) // since we are using an unordered map to store all the elements

Related Article:


Next Article
Practice Tags :

Similar Reads