|
14 | 14 | #define PUBLIC
|
15 | 15 | #endif
|
16 | 16 |
|
17 |
| -namespace GraphAlgorithm { |
18 |
| - |
19 |
| - //适配器类型声明 |
20 |
| - |
21 |
| - //@Param idx_v:节点编号 |
22 |
| - //@Return:节点idx_v相连的第一条边的编号,无边时返回NIL |
23 |
| - typedef std::function<int(int idx_v)> FirstEdgeAdapter; |
24 |
| - |
25 |
| - //@Param: idx_v:节点编号 |
26 |
| - //@Param: idx_e:当前扫描到的边 |
27 |
| - //@Return:返回下一条边的id,无边时返回NIL |
28 |
| - typedef std::function<int(int idx_v, int idx_e)> NextEdgeAdapter; |
29 |
| - |
30 |
| - //@Param: idx_v:节点编号 |
31 |
| - //@Param: idx_e:当前扫描到的边 |
32 |
| - //@Return:返回指向节点的id,无边时返回NIL |
33 |
| - typedef std::function<int(int idx_v, int idx_e)> EdgeToAdapter; |
34 |
| - |
35 |
| - //@Param idx_v:表示此边与v相连 |
36 |
| - //@Param idx_e:表示此边编号 |
37 |
| - //@Return:节点idx_v的编号为idx_e的边的长度 |
38 |
| - typedef std::function<double(int idx_v, int idx_e)> EdgeLengthAdapter; |
39 |
| - |
40 |
| - //@Param idx_v:表示节点idx_v |
41 |
| - //@Param idx_e:表示与idx_v相连的编号为idx_e的边 |
42 |
| - //@Return:节点idx_v的编号为idx_e的边的容量 |
43 |
| - typedef std::function <double(int idx_v, int idx_e)> EdgeContainAdapter; |
44 |
| - |
45 |
| - //@Param first:表示点的序列 |
46 |
| - //@Param second:表示边的序列 |
47 |
| - //@Description:如果无路径,则点的序列为空 |
48 |
| - typedef std::pair<std::list<int>,std::list<int>> PathType; |
| 17 | +namespace Algorithm { |
49 | 18 |
|
50 | 19 | //不存在的编号,长度..
|
51 | 20 | const int NIL = -1;
|
52 |
| - const static PathType NIL_PATH = std::make_pair(std::list<int>(), std::list<int>()); |
53 |
| - |
54 |
| - struct GraphAdapter { |
55 |
| - FirstEdgeAdapter firstOf; |
56 |
| - NextEdgeAdapter nextOf; |
57 |
| - EdgeToAdapter destOf; |
58 |
| - EdgeLengthAdapter lengthOf; |
59 |
| - EdgeContainAdapter containOf; |
60 |
| - }; |
61 |
| - |
62 |
| - //@Param idx_start_v:最初的节点编号 |
63 |
| - //@Param idx_dest_v:目标节点编号 |
64 |
| - //@Param firstOf,nextOf,edgeTo:图的属性 |
65 |
| - //@Return: 返回所需的步数 |
66 |
| - |
67 |
| - PUBLIC PathType |
68 |
| - bfs( |
69 |
| - int idx_start_v,int idx_dest_v, |
70 |
| - const FirstEdgeAdapter & firstOf,const NextEdgeAdapter & nextOf, |
71 |
| - const EdgeToAdapter & destOf); |
72 |
| - |
73 |
| - inline int |
74 |
| - bfsStep( |
75 |
| - int idx_start_v, int idx_dest_v, |
76 |
| - const FirstEdgeAdapter & firstOf, const NextEdgeAdapter & nextOf, |
77 |
| - const EdgeToAdapter & destOf) { |
78 |
| - |
79 |
| - PathType tmp = bfs(idx_start_v, idx_dest_v, firstOf, nextOf, destOf); |
80 |
| - return tmp.first.size() - 1; |
81 |
| - }; |
82 |
| - |
83 |
| - inline int |
84 |
| - bfsStep( |
85 |
| - int idx_start_v, int idx_dest_v, |
86 |
| - const GraphAdapter & graph) { |
87 |
| - return bfsStep(idx_start_v, idx_dest_v, |
| 21 | + |
| 22 | + namespace Graph { |
| 23 | + |
| 24 | + //适配器类型声明 |
| 25 | + |
| 26 | + //@Param idx_v:节点编号 |
| 27 | + //@Return:节点idx_v相连的第一条边的编号,无边时返回NIL |
| 28 | + typedef std::function<int(int idx_v)> FirstEdgeAdapter; |
| 29 | + |
| 30 | + //@Param: idx_v:节点编号 |
| 31 | + //@Param: idx_e:当前扫描到的边 |
| 32 | + //@Return:返回下一条边的id,无边时返回NIL |
| 33 | + typedef std::function<int(int idx_v, int idx_e)> NextEdgeAdapter; |
| 34 | + |
| 35 | + //@Param: idx_v:节点编号 |
| 36 | + //@Param: idx_e:当前扫描到的边 |
| 37 | + //@Return:返回指向节点的id,无边时返回NIL |
| 38 | + typedef std::function<int(int idx_v, int idx_e)> EdgeToAdapter; |
| 39 | + |
| 40 | + //@Param idx_v:表示此边与v相连 |
| 41 | + //@Param idx_e:表示此边编号 |
| 42 | + //@Return:节点idx_v的编号为idx_e的边的长度 |
| 43 | + typedef std::function<double(int idx_v, int idx_e)> EdgeLengthAdapter; |
| 44 | + |
| 45 | + //@Param idx_v:表示节点idx_v |
| 46 | + //@Param idx_e:表示与idx_v相连的编号为idx_e的边 |
| 47 | + //@Return:节点idx_v的编号为idx_e的边的容量 |
| 48 | + typedef std::function <double(int idx_v, int idx_e)> EdgeContainAdapter; |
| 49 | + |
| 50 | + //@Param first:表示点的序列 |
| 51 | + //@Param second:表示边的序列 |
| 52 | + //@Description:如果无路径,则点的序列为空 |
| 53 | + typedef std::pair<std::list<int>, std::list<int>> PathType; |
| 54 | + |
| 55 | + const static PathType NIL_PATH = std::make_pair(std::list<int>(), std::list<int>()); |
| 56 | + |
| 57 | + struct GraphAdapter { |
| 58 | + FirstEdgeAdapter firstOf; |
| 59 | + NextEdgeAdapter nextOf; |
| 60 | + EdgeToAdapter destOf; |
| 61 | + EdgeLengthAdapter lengthOf; |
| 62 | + EdgeContainAdapter containOf; |
| 63 | + }; |
| 64 | + |
| 65 | + //@Param idx_start_v:最初的节点编号 |
| 66 | + //@Param idx_dest_v:目标节点编号 |
| 67 | + //@Param firstOf,nextOf,edgeTo:图的属性 |
| 68 | + //@Return: 返回所走的路径 |
| 69 | + PUBLIC PathType |
| 70 | + bfs( |
| 71 | + int idx_start_v, int idx_dest_v, |
| 72 | + const FirstEdgeAdapter & firstOf, const NextEdgeAdapter & nextOf, |
| 73 | + const EdgeToAdapter & destOf); |
| 74 | + |
| 75 | + //@Param idx_start_v:最初的节点编号 |
| 76 | + //@Param idx_dest_v:目标节点编号 |
| 77 | + //@Param graph:图的属性 |
| 78 | + //@Return: 返回所走的路径 |
| 79 | + inline PathType |
| 80 | + bfs( |
| 81 | + int idx_start_v, int idx_dest_v, |
| 82 | + const GraphAdapter& graph) { |
| 83 | + return bfs(idx_start_v, idx_dest_v, |
| 84 | + graph.firstOf, graph.nextOf, graph.destOf); |
| 85 | + } |
| 86 | + |
| 87 | + //@Param idx_start_v:最初的节点编号 |
| 88 | + //@Param idx_dest_v:目标节点编号 |
| 89 | + //@Param firstOf,nextOf,edgeTo:图的属性 |
| 90 | + //@Return: 返回所需的步数 |
| 91 | + inline int |
| 92 | + bfsStep( |
| 93 | + int idx_start_v, int idx_dest_v, |
| 94 | + const FirstEdgeAdapter & firstOf, const NextEdgeAdapter & nextOf, |
| 95 | + const EdgeToAdapter & destOf) { |
| 96 | + |
| 97 | + PathType tmp = bfs(idx_start_v, idx_dest_v, firstOf, nextOf, destOf); |
| 98 | + return tmp.first.size() - 1; |
| 99 | + }; |
| 100 | + |
| 101 | + //@Param idx_start_v:最初的节点编号 |
| 102 | + //@Param idx_dest_v:目标节点编号 |
| 103 | + //@Param graph:图的属性 |
| 104 | + //@Return: 返回所需的步数 |
| 105 | + inline int |
| 106 | + bfsStep( |
| 107 | + int idx_start_v, int idx_dest_v, |
| 108 | + const GraphAdapter & graph) { |
| 109 | + return bfsStep(idx_start_v, idx_dest_v, |
88 | 110 | graph.firstOf, graph.nextOf, graph.destOf);
|
| 111 | + } |
89 | 112 | }
|
90 | 113 | }
|
0 commit comments