Add and Remove Vertex in Adjacency Matrix Representation of Graph


Including a vertex within the contagiousness network representation of a chart means expanding the measure of the network by one push and one column. The unused push and column speak to the associations of the recently included vertex with the existing vertices. Additionally, expelling a vertex requires evacuating its comparing push and column from the contagiousness lattice, subsequently altering the measure of the network in like manner. Including a vertex includes adding a push and column with beginning values of 0, whereas evacuating a vertex includes erasing the comparing push and column, viably decreasing the measure of the lattice.

Methods Used

  • Adjacency Matrix

Modified Dijkstra's Algorithm with Weighted Product

Within the modified Dijkstra's calculation with weighted items, we start by setting the weight of the source hub to boundlessness and the weight of all other hubs to boundlessness. During the execution of the calculation, rather than upgrading the separations with the entirety of weights, we overhauled them with the item of weights experienced so far. At each step, we select the hub with the minimum−weighted item and upgrade the weights of its neighbouring hubs in a similar manner. This preparation proceeds until we reach the goal hub. Eventually, the item of weights along this way will speak to the littlest conceivable, fulfilling the condition of weights being more prominent than or rising to 1.

Algorithm

Including a Vertex:

  • Increase the estimate of the framework by including a modern push and a modern column.

  • Initialise the values within the unused push and column as (or any other fitting default value).

  • Update the associations with existing vertices by upgrading the unused push and column accordingly.

Removing a Vertex:

  • Identify the file of the vertex you need to remove.

  • Remove the push and column at that file from the contagiousness matrix.

  • Shift the remaining lines and columns to fill the purge space made by the removal.

  • If the chart is coordinated, upgrade the files of the remaining vertices within the lattice in like manner to preserve the right associations.

Example

#include <iostream>
using namespace std;

class Graph {
private:
    bool** adjMatrix;
    int numVertices;

public:
    // Initialize the matrix to zero
    Graph(int numVertices) {
        this->numVertices = numVertices;
        adjMatrix = new bool* [numVertices];
        for (int i = 0; i < numVertices; i++) {
            adjMatrix[i] = new bool[numVertices];
            for (int j = 0; j < numVertices; j++)
                adjMatrix[i][j] = false;
        }
    }

    // Add an edge
    void addEdge(int src, int dest) {
        adjMatrix[src][dest] = true;
        adjMatrix[dest][src] = true;
    }

    // Remove an edge
    void removeEdge(int src, int dest) {
        adjMatrix[src][dest] = false;
        adjMatrix[dest][src] = false;
    }

    // Print the adjacency matrix
    void printAdjMatrix() {
        for (int i = 0; i < numVertices; i++) {
            cout << i << " : ";
            for (int j = 0; j < numVertices; j++)
                cout << adjMatrix[i][j] << " ";
            cout << "\n";
        }
    }

    ~Graph() {
        for (int i = 0; i < numVertices; i++)
            delete[] adjMatrix[i];
        delete[] adjMatrix;
    }
};

int main() {
    Graph graph(4);

    graph.addEdge(0, 1);
    graph.addEdge(0, 2);
    graph.addEdge(1, 2);
    graph.addEdge(2, 0);
    graph.addEdge(2, 3);

    graph.printAdjMatrix();

    return 0;
}

Output

0 : 0 1 1 0 
1 : 1 0 1 0 
2 : 1 1 0 1 
3 : 0 0 1 0 

Conclusion

This article examines the expansion and evacuation of vertices within the contagiousness lattice representation of a graph. It clarifies the method of counting a vertex by growing the network with a modern push and column, initialising their values, and upgrading the associations with existing vertices. Essentially, it covers the evacuation of a vertex by evacuating its comparing push and column, moving the remaining components, and altering the lists in the case of a coordinated chart. The C programme illustrates these operations and shows the overhauled contagiousness framework. This article gives a clear understanding of how to control charts utilising the contagiousness network representation.

Updated on: 13-Jul-2023

324 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements