-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgraph.cpp
46 lines (46 loc) · 910 Bytes
/
graph.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
#include<stdlib.h>
#include<vector>
#include"graph.h"
Graph::Graph(int e) {
m_max=e;
m_arr=(bool*)calloc(m_max*m_max,sizeof(bool));
}
//distructor
Graph::~Graph() {
free(m_arr);
m_arr=0;
}
//get length
int Graph::Length()const {
return m_max;
}
//add edge with edge check
bool Graph::AddEdge(int a,int b) {
if(a>=m_max) {
return false;
}
if(b>=m_max) {
return false;
}
m_arr[p_LinearConvert(a,b)]=true;
m_arr[p_LinearConvert(b,a)]=true;
return true;
}
//detect connection with edge check
bool Graph::IsConnected(int a,int b)const {
if(a>=m_max) {
return false;
}
if(b>=m_max) {
return false;
}
return m_arr[p_LinearConvert(a,b)];
}
std::vector<int> Graph::Span(int a)const{
std::vector<int> list;
for(int i=0;i<m_max;i++){
if(IsConnected(a,i))
list.push_back(i);
}
return list;
}