-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGraph.cpp
More file actions
70 lines (49 loc) · 1.21 KB
/
Graph.cpp
File metadata and controls
70 lines (49 loc) · 1.21 KB
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#include "Graph.h"
#include <time.h>
#include <stdlib.h>
#include <cmath>
#include <cstring>
#include <string>
#include <iostream>
#include <iomanip>
using namespace std;
Graph::Graph(int no_vertices) {
this->data = new int* [no_vertices];
this->no_vertices = no_vertices;
this->density_percentage = density_percentage;
for (int i = 0; i < no_vertices; i++) {
int* help = new int[no_vertices];
for (int j = 0; j < no_vertices; j++) {
if (j == i)
help[j] = -1;
else
help[j] = this->rand_weight();
}
this->data[i] = help;
}
}
Graph::~Graph() {
for (int i = 0; i < no_vertices; ++i) {
delete[] this->data[i];
}
delete[] data;
}
void Graph::add_edge(int vertex_start, int vertex_end, int weight) {
this->data[vertex_start][vertex_end] = weight;
}
int Graph::rand_weight() {
return rand() % 10 + 1;
}
int Graph::edge_weight(int vertex_start, int vertex_end) {
return this->data[vertex_start][vertex_end];
}
void Graph::print() {
for (auto i = 0; i < this->no_vertices; i++)
{
for (auto j = 0; j < this->no_vertices; j++)
{
std::cout << std::setw(5) << this->data[i][j] << " ";
}
}
std::cout << std::endl;
}