-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmatrixAdjacency.h
More file actions
70 lines (60 loc) · 2.42 KB
/
matrixAdjacency.h
File metadata and controls
70 lines (60 loc) · 2.42 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
#ifndef MATRIXADJACENCY_H
#define MATRIXADJACENCY_H
#include <iostream>
using namespace std;
/*
What functions should be implemented?
- addEdge(int i, int j): add an edge between vertex i and vertex j
- removeEdge(int i, int j): remove the edge between vertex i and vertex j
- printMatrix(): print the adjacency matrix
*/
/*
How it works?
- The matrix is a 2D array of integers
- The size of the matrix is the number of vertices
- The matrix is initialized with 0s
- If there is an edge between vertex i and vertex j, then matrix[i][j] = 1
- If there is no edge between vertex i and vertex j, then matrix[i][j] = 0
Example:
- Let's say we have 4 vertices
- The matrix is a 4x4 2D array of integers
- The matrix is initialized with 0s
- If there is an edge between vertex 0 and vertex 1, then matrix[0][1] = 1
- If there is no edge between vertex 0 and vertex 1, then matrix[0][1] = 0
- If there is an edge between vertex 1 and vertex 2, then matrix[1][2] = 1
- If there is no edge between vertex 1 and vertex 2, then matrix[1][2] = 0
- If there is an edge between vertex 2 and vertex 3, then matrix[2][3] = 1
- If there is no edge between vertex 2 and vertex 3, then matrix[2][3] = 0
- If there is an edge between vertex 3 and vertex 0, then matrix[3][0] = 1
- If there is no edge between vertex 3 and vertex 0, then matrix[3][0] = 0
- The adjacency matrix is:
0 1 0 0
0 0 1 0
0 0 0 1
1 0 0 0
And what should I do do get weight of edge?
- You can use the matrix[i][j] to get the weight of the edge between vertex i and vertex j
okay but what if I want to get the weight of the edge between vertex 1 and vertex 2?
And how to change density of the graph?
- You can change the density of the graph by changing the values of the matrix
- If you want to increase the density of the graph, then you can change the values of the matrix to 1s
What else can I do?
- You can remove the edge between two vertices by setting the value of the matrix to 0
- You can add the edge between two vertices by setting the value of the matrix to 1
- You can print the adjacency matrix
- You can get the weight of the edge between two vertices
- You can change the density of the graph
*/
class MatrixAdjacency {
private:
int** matrix;
int size;
public:
MatrixAdjacency(int size);
~MatrixAdjacency();
void addEdge(int i, int j, int w);
void removeEdge(int i, int j);
int getWeight(int i, int j);
void printMatrix();
};
#endif // MATRIXADJACENCY_H