-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstructures.h
More file actions
72 lines (70 loc) · 2.59 KB
/
Copy pathstructures.h
File metadata and controls
72 lines (70 loc) · 2.59 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
71
72
#pragma once
#include<string.h>
#include<queue>
#include<stdio.h>
struct adjacency_matrix{
int **matrix;
int number_of_vertices;
adjacency_matrix(){
matrix = new int*[0];
matrix[0] = new int[0];
number_of_vertices = 0;
};
adjacency_matrix(int number_of_vertices){
matrix = new int*[number_of_vertices];
for(int i = 0; i < number_of_vertices; i++){
matrix[i] = new int[number_of_vertices];
memset(matrix[i],0,number_of_vertices*sizeof(int));
}
this->number_of_vertices = number_of_vertices;
};
void print(){
printf(" |");
for(int i = 0; i < this->number_of_vertices; i++){
printf("%3d ", i);
}
printf("\n----");
for(int i = 0; i < this->number_of_vertices; i++){
printf("----");
}
printf("\n");
for(int i = 0; i < this->number_of_vertices; i++){
printf("%3d|",i);
for(int j = 0; j < this->number_of_vertices; j++){
printf("%3d ",this->matrix[i][j]); //matrix[row][col]
}
printf("\n");
}
}
void add_vertex(){
this->number_of_vertices = this->number_of_vertices+1;
int **temp_matrix = new int*[this->number_of_vertices];
memcpy(temp_matrix,this->matrix,(this->number_of_vertices-1)*sizeof(int));
for(int i = 0; i < this->number_of_vertices-1; i++){
temp_matrix[i] = new int[number_of_vertices];
memcpy(temp_matrix[i],this->matrix[i],(this->number_of_vertices-1)*sizeof(int));
temp_matrix[i][number_of_vertices-1] = 0;
}
temp_matrix[number_of_vertices-1] = new int[number_of_vertices];
memset(temp_matrix[number_of_vertices-1],0,number_of_vertices*sizeof(int));
delete[] this->matrix;
this->matrix = temp_matrix;
};
bool add_edge_dir(int src_vertex, int dst_vertex, int weight = 1){
if(src_vertex < this->number_of_vertices && dst_vertex < this->number_of_vertices && src_vertex >= 0 && dst_vertex >= 0){
this->matrix[src_vertex][dst_vertex] = weight;
return true;
}else{
return false;
}
};
bool add_edge_undir(int src_vertex, int dst_vertex, int weight = 1){
if(src_vertex < this->number_of_vertices && dst_vertex < this->number_of_vertices && src_vertex >= 0 && dst_vertex >= 0){
this->matrix[src_vertex][dst_vertex] = weight;
this->matrix[dst_vertex][src_vertex] = weight;
return true;
}else{
return false;
}
};
};