0% found this document useful (0 votes)
8 views1 page

2 CPP

The document contains a C++ implementation of a Graph class using an adjacency matrix to represent edges and weights between vertices. It initializes a graph with a specified number of vertices, allows adding edges with weights, and displays the adjacency matrix. The main function demonstrates creating a graph with 5 vertices and adding several edges with their respective weights.

Uploaded by

aakarsh.narang
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views1 page

2 CPP

The document contains a C++ implementation of a Graph class using an adjacency matrix to represent edges and weights between vertices. It initializes a graph with a specified number of vertices, allows adding edges with weights, and displays the adjacency matrix. The main function demonstrates creating a graph with 5 vertices and adding several edges with their respective weights.

Uploaded by

aakarsh.narang
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd

#include <bits/stdc++.

h>
using namespace std;

class Graph {
private:
vector<vector<int>> adjMatrix;
int vertices;

public:
Graph(int v) : vertices(v) {
[Link](v, vector<int>(v, 0));
}

void addEdge(int u, int v, int weight) {


adjMatrix[u][v] = weight;
adjMatrix[v][u] = weight;
}

void displayMatrix() {
cout << "Adjacency Matrix:" << endl;
for (int i = 0; i < vertices; i++) {
for (int j = 0; j < vertices; j++) {
cout << adjMatrix[i][j] << " ";
}
cout << endl;
}
}
};

int main() {

int vertices = 5;

Graph g(vertices);

[Link](0, 1, 5); // Edge 0-1 with weight 5


[Link](1, 2, 7); // Edge 1-2 with weight 7
[Link](2, 3, 3); // Edge 2-3 with weight 3
[Link](3, 4, 6); // Edge 3-4 with weight 6
[Link](1, 3, 10); // Edge 1-3 with weight 10

[Link]();

return 0;
}

You might also like