AntInLoop

Adjacency Matrix Graph Representation Code Implementations

Production-ready Adjacency Matrix Graph Representation data structure implementations in C++, Python, Java, and JavaScript on AntInLoop.

Study Adjacency Matrix Graph Representation Guide ยท Launch Visualizer

CPP Implementation

#include <iostream>
#include <vector>
#include <cmath>

using namespace std;

// Structure for a node in the Fibonacci Heap
struct Node {
    Node* parent;
    Node* child;
    Node* left;
    Node* right;
    int key;
    int degree;
    bool marked;
};

// Class for the Fibonacci Heap
class FibonacciHeap {
private:
    Node* min;
    int n;
public:
    // Constructor
    FibonacciHeap() : min(NULL), n(0) {}

    // Method to insert a node
    void insert(int key) {
        Node* node = new Node;
        node->key = key;
        node->degree = 0;
        node->marked = false;
        node->parent = NULL;
        node->child = NULL;
        node->left = node;
        node->right = node;
        if (min != NULL) {
            min->left->right = node;
            node->right = min;
            node->left = min->left;
            min->left = node;
            if (node->key < min->key) {
                min = node;
            }
        } else {
            min = node;
        }
        n++;
    }

    // Method to check if the heap is empty
    bool isEmpty() {
        return min == NULL;
    }

    // Method to return the size of the heap
    int size() {
        return n;
    }

    // Method to search for a node with a given key
    Node* search(int key) {
        return search(min, key);
    }

private:
    // Helper method for the search function
    Node* search(Node* node, int key) {
        Node* x = node;
        Node* p = NULL;
        if (x != NULL) {
            do {
                if (x->key == key) {
                    p = x;
                    break;
                } else if ((p = search(x->child, key)) != NULL) {
                    break;
                }
                x = x->right;
            } while (x != node);
        }
        return p;
    }
};