-
Notifications
You must be signed in to change notification settings - Fork 0
/
adjMatrixGraph.hpp
82 lines (72 loc) · 2.22 KB
/
adjMatrixGraph.hpp
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
73
74
75
76
77
78
79
80
81
82
//
// adjMatrixGraph.hpp
// datastructure
//
// Created by duchunhui on 2018/12/16.
// Copyright © 2018 duchunhui. All rights reserved.
//
#ifndef adjMatrixGraph_hpp
#define adjMatrixGraph_hpp
#include <stdio.h>
#include "graph.hpp"
template<class TypeOfVer, class TypeOfEdge>
class adjMatrixGraph:public graph<TypeOfVer, TypeOfEdge>{
public:
adjMatrixGraph(int vSize, const TypeOfVer d[], const TypeOfEdge noEdgeFlag);
void insert(TypeOfVer x, TypeOfVer y, TypeOfEdge w);
void remove(TypeOfVer x, TypeOfVer y);
bool exist(TypeOfVer x, TypeOfVer y) const;
~adjMatrixGraph();
private:
TypeOfEdge **edge;//adjoin matrix
TypeOfVer *ver;
TypeOfEdge noEdge;
int find(TypeOfVer v) const{
for (int i=0; i<this->Vers; ++i)
if(ver[i]==v) return i;
}
};
template<class TypeOfVer, class TypeOfEdge>
adjMatrixGraph<TypeOfVer, TypeOfEdge>::adjMatrixGraph(int vSize, const TypeOfVer d[], const TypeOfEdge noEdgeFlag) {
int i,j;
this->Vers = vSize;
this->Edges = 0;
noEdge = noEdgeFlag;
//initialize ver
ver = new TypeOfVer[this->Vers];
for (i=0; i<this->Vers; ++i)
ver[i] = d[i];
//initialzie edge
edge = new TypeOfEdge*[this->vers];
for (i=0; i<this->Vers; ++i) {
edge[i] = new TypeOfEdge[this->Vers];
for (j=0; j<this->Vers; ++j)
edge[i][j] = noEdgeFlag;
edge[i][i] = 0;
}
}
template<class TypeOfVer, class TypeOfEdge>
adjMatrixGraph<TypeOfVer, TypeOfEdge>::~adjMatrixGraph(){
delete ver;
for(int i=0;i<this->Vers;++i)
delete edge[i];
delete edge;
}
template<class TypeOfVer, class TypeOfEdge>
void adjMatrixGraph<TypeOfVer, TypeOfEdge>::insert(TypeOfVer x, TypeOfVer y, TypeOfEdge w){
int u = find(x), v = find(y);
edge[u][v] = w;
++this->Edges;
}
template<class TypeOfVer, class TypeOfEdge>
void adjMatrixGraph<TypeOfVer, TypeOfEdge>::remove(TypeOfVer x, TypeOfVer y){
int u = find(x), v = find(y);
edge[u][v] = noEdge;
++this->Edges;
}
template<class TypeOfVer, class TypeOfEdge>
bool adjMatrixGraph<TypeOfVer, TypeOfEdge>::exist(TypeOfVer x, TypeOfVer y) const{
int u = find(x), v = find(y);
return edge[u][v] != noEdge;
}
#endif /* adjMatrixGraph_hpp */