1 | #include <iostream> |
---|
2 | #include <vector> |
---|
3 | #include <string> |
---|
4 | |
---|
5 | #include <marci_list_graph.hh> |
---|
6 | #include <marci_graph_traits.hh> |
---|
7 | #include <marci_property_vector.hh> |
---|
8 | #include <marci_bfs.hh> |
---|
9 | #include <marci_max_flow.hh> |
---|
10 | |
---|
11 | using namespace marci; |
---|
12 | |
---|
13 | int main (int, char*[]) |
---|
14 | { |
---|
15 | typedef graph_traits<list_graph>::node_iterator node_iterator; |
---|
16 | typedef graph_traits<list_graph>::edge_iterator edge_iterator; |
---|
17 | typedef graph_traits<list_graph>::each_node_iterator each_node_iterator; |
---|
18 | typedef graph_traits<list_graph>::each_edge_iterator each_edge_iterator; |
---|
19 | typedef graph_traits<list_graph>::out_edge_iterator out_edge_iterator; |
---|
20 | typedef graph_traits<list_graph>::in_edge_iterator in_edge_iterator; |
---|
21 | typedef graph_traits<list_graph>::sym_edge_iterator sym_edge_iterator; |
---|
22 | |
---|
23 | list_graph G; |
---|
24 | std::vector<node_iterator> vector_of_node_iterators; |
---|
25 | for(int i=0; i!=8; ++i) vector_of_node_iterators.push_back(G.add_node()); |
---|
26 | for(int i=0; i!=8; ++i) |
---|
27 | for(int j=0; j!=8; ++j) { |
---|
28 | if ((i<j)&&(i+j)%3) G.add_edge(vector_of_node_iterators[i], vector_of_node_iterators[j]); |
---|
29 | } |
---|
30 | |
---|
31 | std::cout << "We construct a directed graph on the node set {0,1,2,...,7}," <<std::endl << "i-->j is arc iff i<j and (i+j)%3." << std::endl; |
---|
32 | std::cout << "number of nodes: " << number_of(G.first_node()) << std::endl; |
---|
33 | |
---|
34 | for(each_node_iterator i=G.first_node(); i.is_valid(); ++i) { |
---|
35 | std::cout << "node " << G.id(i) << std::endl; |
---|
36 | std::cout << " outdegree (out_edge_iterator): " << number_of(G.first_out_edge(i)) << " "; |
---|
37 | for(out_edge_iterator j=G.first_out_edge(i); j.is_valid(); ++j) { |
---|
38 | std::cout << "(" << G.id(G.tail(j)) << "--" << G.id(j) << "->" << G.id(G.head(j)) << ") "; |
---|
39 | } |
---|
40 | std::cout << std::endl; |
---|
41 | |
---|
42 | std::cout<< " "; |
---|
43 | for(out_edge_iterator j=G.first_out_edge(i); j.is_valid(); ++j) { |
---|
44 | std::cout << G.a_node(j) << "->" << G.b_node(j) << " "; } |
---|
45 | std::cout<<std::endl; |
---|
46 | |
---|
47 | std::cout << " indegree: (in_edge_oterator) " << number_of(G.first_in_edge(i)) << " "; |
---|
48 | for(in_edge_iterator j=G.first_in_edge(i); j.is_valid(); ++j) { |
---|
49 | std::cout << j << " "; } |
---|
50 | std::cout << std::endl; |
---|
51 | |
---|
52 | std::cout<< " "; |
---|
53 | for(in_edge_iterator j=G.first_in_edge(i); j.is_valid(); ++j) { |
---|
54 | std::cout << G.a_node(j) << "->" << G.b_node(j) << " "; } |
---|
55 | std::cout<<std::endl; |
---|
56 | |
---|
57 | std::cout << " degree: (sym_edge_iterator) " << number_of(G.first_sym_edge(i)) << " "; |
---|
58 | for(sym_edge_iterator j=G.first_sym_edge(i); j.is_valid(); ++j) { |
---|
59 | std::cout << j << " "; } |
---|
60 | std::cout<<std::endl; |
---|
61 | |
---|
62 | std::cout<< " "; |
---|
63 | for(sym_edge_iterator j=G.first_sym_edge(i); j.is_valid(); ++j) { |
---|
64 | std::cout << G.a_node(j) << "->" << G.b_node(j) << " "; } |
---|
65 | std::cout<<std::endl; |
---|
66 | } |
---|
67 | |
---|
68 | std::cout << "all edges: "; |
---|
69 | for(each_edge_iterator i=G.first_edge(); i.is_valid(); ++i) { |
---|
70 | std::cout << i << " "; |
---|
71 | } |
---|
72 | std::cout << std::endl; |
---|
73 | |
---|
74 | std::cout << "node property array test" << std::endl; |
---|
75 | node_property_vector<list_graph, int> my_property_vector(G); |
---|
76 | each_node_iterator v; |
---|
77 | G.get_first(v); |
---|
78 | my_property_vector.put(v, 42); |
---|
79 | my_property_vector.put(++G.first_node(), 314); |
---|
80 | my_property_vector.put(++++G.first_node(), 1956); |
---|
81 | my_property_vector.put(vector_of_node_iterators[3], 1989); |
---|
82 | my_property_vector.put(vector_of_node_iterators[4], 2003); |
---|
83 | my_property_vector.put(vector_of_node_iterators[7], 1978); |
---|
84 | std::cout << "some node property values..." << std::endl; |
---|
85 | for(each_node_iterator i=G.first_node(); i.is_valid(); ++i) { |
---|
86 | std::cout << my_property_vector.get(i) << std::endl; |
---|
87 | } |
---|
88 | int _i=1; |
---|
89 | int _ii=1; |
---|
90 | edge_property_vector<list_graph, int> my_edge_property(G); |
---|
91 | for(each_edge_iterator i=G.first_edge(); i.is_valid(); ++i) { |
---|
92 | my_edge_property.put(i, _i); |
---|
93 | _i*=_ii; ++_ii; |
---|
94 | } |
---|
95 | |
---|
96 | std::cout << "node and edge property values on the tails and heads of edges..." << std::endl; |
---|
97 | for(each_edge_iterator j=G.first_edge(); j.is_valid(); ++j) { |
---|
98 | std::cout << my_property_vector.get(G.tail(j)) << "--" << my_edge_property.get(j) << "-->" << my_property_vector.get(G.head(j)) << " "; |
---|
99 | } |
---|
100 | std::cout << std::endl; |
---|
101 | |
---|
102 | //std::cout << "the same for inedges of the nodes..." << std::endl; |
---|
103 | //k=0; |
---|
104 | //for(each_node_iterator i=G.first_node(); i.is_valid(); ++i) { |
---|
105 | // for(in_edge_iterator j=G.first_in_edge(i); j.is_valid(); ++j) { |
---|
106 | // std::cout << my_property_vector.get(G.tail(j)) << "-->" << my_property_vector.get(G.head(j)) << " "; |
---|
107 | // } |
---|
108 | // std::cout << std::endl; |
---|
109 | //} |
---|
110 | |
---|
111 | std::cout << "bfs from the first node" << std::endl; |
---|
112 | bfs<list_graph> bfs_test(G, G.first_node()); |
---|
113 | bfs_test.run(); |
---|
114 | std::cout << "reached: "; |
---|
115 | for(each_node_iterator i=G.first_node(); i.is_valid(); ++i) { |
---|
116 | std::cout << bfs_test.reached.get(i) << " "; |
---|
117 | } |
---|
118 | std::cout<<std::endl; |
---|
119 | std::cout << "dist: "; |
---|
120 | for(each_node_iterator i=G.first_node(); i.is_valid(); ++i) { |
---|
121 | std::cout << bfs_test.dist.get(i) << " "; |
---|
122 | } |
---|
123 | std::cout<<std::endl; |
---|
124 | |
---|
125 | |
---|
126 | std::cout << "augmenting path flow algorithm test..." << std::endl; |
---|
127 | list_graph flow_test; |
---|
128 | |
---|
129 | node_iterator s=flow_test.add_node(); |
---|
130 | node_iterator v1=flow_test.add_node(); |
---|
131 | node_iterator v2=flow_test.add_node(); |
---|
132 | node_iterator v3=flow_test.add_node(); |
---|
133 | node_iterator v4=flow_test.add_node(); |
---|
134 | node_iterator t=flow_test.add_node(); |
---|
135 | |
---|
136 | node_property_vector<list_graph, std::string> node_name(flow_test); |
---|
137 | node_name.put(s, "s"); |
---|
138 | node_name.put(v1, "v1"); |
---|
139 | node_name.put(v2, "v2"); |
---|
140 | node_name.put(v3, "v3"); |
---|
141 | node_name.put(v4, "v4"); |
---|
142 | node_name.put(t, "t"); |
---|
143 | |
---|
144 | edge_iterator s_v1=flow_test.add_edge(s, v1); |
---|
145 | edge_iterator s_v2=flow_test.add_edge(s, v2); |
---|
146 | edge_iterator v1_v2=flow_test.add_edge(v1, v2); |
---|
147 | edge_iterator v2_v1=flow_test.add_edge(v2, v1); |
---|
148 | edge_iterator v1_v3=flow_test.add_edge(v1, v3); |
---|
149 | edge_iterator v3_v2=flow_test.add_edge(v3, v2); |
---|
150 | edge_iterator v2_v4=flow_test.add_edge(v2, v4); |
---|
151 | edge_iterator v4_v3=flow_test.add_edge(v4, v3); |
---|
152 | edge_iterator v3_t=flow_test.add_edge(v3, t); |
---|
153 | edge_iterator v4_t=flow_test.add_edge(v4, t); |
---|
154 | |
---|
155 | edge_property_vector<list_graph, int> cap(flow_test); |
---|
156 | |
---|
157 | cap.put(s_v1, 16); |
---|
158 | cap.put(s_v2, 13); |
---|
159 | cap.put(v1_v2, 10); |
---|
160 | cap.put(v2_v1, 4); |
---|
161 | cap.put(v1_v3, 12); |
---|
162 | cap.put(v3_v2, 9); |
---|
163 | cap.put(v2_v4, 14); |
---|
164 | cap.put(v4_v3, 7); |
---|
165 | cap.put(v3_t, 20); |
---|
166 | cap.put(v4_t, 4); |
---|
167 | |
---|
168 | std::cout << "on directed graph graph" << std::endl; //<< flow_test; |
---|
169 | std::cout << "names and capacity values" << std::endl; |
---|
170 | for(each_node_iterator i=flow_test.first_node(); i.is_valid(); ++i) { |
---|
171 | std::cout << node_name.get(i) << ": "; |
---|
172 | std::cout << "out edges: "; |
---|
173 | for(out_edge_iterator j=flow_test.first_out_edge(i); j.is_valid(); ++j) |
---|
174 | std::cout << node_name.get(flow_test.tail(j)) << "-"<< cap.get(j) << "->" << node_name.get(flow_test.head(j)) << " "; |
---|
175 | std::cout << "in edges: "; |
---|
176 | for(in_edge_iterator j=flow_test.first_in_edge(i); j.is_valid(); ++j) |
---|
177 | std::cout << node_name.get(flow_test.tail(j)) << "-"<< cap.get(j) << "->" << node_name.get(flow_test.head(j)) << " "; |
---|
178 | std::cout << std::endl; |
---|
179 | } |
---|
180 | |
---|
181 | |
---|
182 | //for(each_node_iterator i=flow_test.first_node(); i.is_valid(); ++i) { |
---|
183 | // std::cout << i << " "; |
---|
184 | //} |
---|
185 | |
---|
186 | max_flow_type<list_graph, int> max_flow_test(flow_test, s, t, cap); |
---|
187 | max_flow_test.run(); |
---|
188 | |
---|
189 | return 0; |
---|
190 | } |
---|