-
Notifications
You must be signed in to change notification settings - Fork 0
/
adj_matrix.cpp
58 lines (47 loc) · 1.33 KB
/
adj_matrix.cpp
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
#include <iostream>
#include <vector>
using namespace std;
//make a square matrix of size sz and initialize it to 0
int **sq_matrix(size_t sz){
int **m = new int*[sz];
for(int i = 0; i < sz; ++i){
m[i] = new int[sz];
}
for(int i = 0; i < sz; ++i)
for(int j = 0; j < sz; ++j)
m[i][j] = 0;
return m;
}
//fill in an edge in an undirected graph (two places in the adj. matrix)
//graph verteces begin from 1 and the graph is simple
void edge(int **matrix, size_t sz, int edge1, int edge2){
if(edge1 <= sz && edge2 <= sz){
matrix[edge1-1][edge2-1] = 1;
matrix[edge2-1][edge1-1] = 1;
}
}
//print a square matrix
void print_matrix(int **matrix, size_t sz){
for(size_t i = 0; i < sz; ++i){
for(size_t j = 0; j < sz; ++j){
cout << matrix[i][j] << " ";
}
cout << endl;
}
}
//get a list of edges and display the adjecency matrix
int main(){
//the number of edges is given
int num_vertices;
cin >> num_vertices;
//initialize the matrix
int **M = sq_matrix(num_vertices);
vector<int> edges;
int current;
while(cin >> current)
edges.push_back(current);
for(int i = 0; i < edges.size(); i += 2)
edge(M, num_vertices, edges[i], edges[i+1]);
print_matrix(M, num_vertices);
return 0;
}