-
Notifications
You must be signed in to change notification settings - Fork 0
/
graphfunctions.h
43 lines (34 loc) · 1.29 KB
/
graphfunctions.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#ifndef GRAPHFUNCTIONS_H
#define GRAPHFUNCTIONS_H
#include <iostream>
#include <vector>
#include <cstdlib>
#include <ctime>
// Function to generate an adjacency matrix
std::vector<std::vector<int>> generateAdjacencyMatrix(int numNodes, double density) {
// Seed the random number generator
std::srand(std::time(0));
// Initialize the adjacency matrix with zeros
std::vector<std::vector<int>> adjacencyMatrix(numNodes, std::vector<int>(numNodes, 0));
// Populate the adjacency matrix based on density
for (int i = 0; i < numNodes; ++i) {
for (int j = i + 1; j < numNodes; ++j) {
if ((std::rand() % 100) < density * 100) {
// If the random value is less than density, connect nodes i and j
adjacencyMatrix[i][j] = 1;
adjacencyMatrix[j][i] = 1;
}
}
}
return adjacencyMatrix;
}
// Function to print the adjacency matrix
void printAdjacencyMatrix(const std::vector<std::vector<int>>& adjacencyMatrix) {
for (const auto& row : adjacencyMatrix) {
for (int value : row) {
std::cout << value << " ";
}
std::cout << std::endl;
}
}
#endif