You are on page 1of 3

NAMA : SULTAN FIRDAUS

KELAS : 03TPLM009
NIM : 221011401637
#include <iostream>
#include <cstdlib>
using namespace std;
const int MAX_NODES = 100;
class AdjacencyMatrixGraph {
private:
int nodes;
int matrix[MAX_NODES][MAX_NODES];
public:
// Constructor
AdjacencyMatrixGraph(int n) {
nodes = n;
// Inisialisasi matriks dengan 0 (tidak ada edge)
for (int i = 0; i < MAX_NODES; ++i) {
for (int j = 0; j < MAX_NODES; ++j) {
matrix[i][j] = 0;
}
}
}
// Menambahkan edge antara dua node
void addEdge(int node1, int node2) {
matrix[node1][node2] = 1;
matrix[node2][node1] = 1; // Karena graf tak berarah
}
// Menampilkan adjacency matrix
void display() {
for (int i = 0; i < nodes; ++i) {
for (int j = 0; j < nodes; ++j) {
cout << matrix[i][j] << " ";
}
cout << endl;
}
}
};

int main() {

cout << "NAMA : SULTAN FIRDAUS "<<endl;


cout << "NIM : 221011401637 "<<endl;
cout << "MATAKULIAH : Struktur Data "<<endl<<endl;

int nodes, edges;


cout << "Masukkan jumlah node dan edge: ";
cin >> nodes >> edges;

AdjacencyMatrixGraph graph(nodes);

// Menambahkan edge secara otomatis


for (int i = 0; i < edges; ++i) {
int node1 = rand() % nodes;
int node2 = rand() % nodes;
graph.addEdge(node1, node2);
}

cout << "Adjacency Matrix:" << endl;


graph.display();

return 0;
}

You might also like