23 # ifndef SEARCH_MEMORY_LEAKS_ENABLED
24 # error SEARCH NOT ENABLED
26 # define new DEBUG_NEW
28 static char THIS_FILE[] = __FILE__;
73 const node source_node,
const node target_node)
90 const node source_node,
const node target_node,
97 this->
side = init_side;
108 const node source_node,
const node target_node,
126 const node source_node,
const node target_node,
133 this->
side = init_side;
163 while (edge_it != edges_end)
171 int real_node_weights = 0;
174 while (node_it != nodes_end)
233 list<edge> artificial_edges;
309 while (node_it != nodes_end)
311 if (
side[*node_it] ==
A)
326 while (node_it != nodes_end)
328 if (
side[*node_it] ==
B)
393 while (node_it != nodes_end)
409 list<edge>& artificial_edges)
419 while (root_it != rootes_end)
421 node edge_start = **root_it;
423 if (root_it != rootes_end)
427 artificial_edges.push_back(ne);
435 list<edge>::iterator edge_it = artificial_edges.begin();
436 list<edge>::iterator edges_end = artificial_edges.end();
437 while (edge_it != edges_end)
447 int cutsize_A2B, cutsize_B2A;
448 double cutratio_A2B, cutratio_B2A;
456 while (node_it != nodes_end)
481 while (node_it != nodes_end)
503 if (cutratio_B2A < cutratio_A2B)
529 while (edge_it != edges_end)
531 if ((
side[(*edge_it).source()] ==
A) &&
532 (
side[(*edge_it).target()] ==
A))
536 unlockedA[*edge_it].push_back((*edge_it).source());
537 unlockedA[*edge_it].push_back((*edge_it).target());
539 else if ((
side[(*edge_it).source()] ==
B) &&
540 (
side[(*edge_it).target()] ==
B))
544 unlockedB[*edge_it].push_back((*edge_it).source());
545 unlockedB[*edge_it].push_back((*edge_it).target());
547 else if ((
side[(*edge_it).source()] ==
A) &&
548 (
side[(*edge_it).target()] ==
B))
553 unlockedA[*edge_it].push_back((*edge_it).source());
554 unlockedB[*edge_it].push_back((*edge_it).target());
556 else if ((
side[(*edge_it).source()] ==
B) &&
557 (
side[(*edge_it).target()] ==
A))
562 unlockedA[*edge_it].push_back((*edge_it).target());
563 unlockedB[*edge_it].push_back((*edge_it).source());
584 bool first_A_node =
true;
585 bool first_B_node =
true;
592 while (node_it != nodes_end)
594 if (
side[*node_it] ==
A)
605 first_A_node =
false;
630 first_B_node =
false;
654 while (adj_edge_it != adj_edges_end)
656 if (
aside[*adj_edge_it] == 1)
660 if (
bside[*adj_edge_it] == 0)
675 while (adj_edge_it != adj_edges_end)
677 if (
bside[*adj_edge_it] == 1)
681 if (
aside[*adj_edge_it] == 0)
694 bool first_edge_found =
true;
698 while (edge_it != edges_end)
700 if (first_edge_found)
703 first_edge_found =
false;
719 while (node_it != nodes_end)
732 srand((
unsigned)
time(NULL));
734 int node_id = (int)floor((((
double)rand() / (
double)RAND_MAX) *
737 for (
int i = 1;
i <= node_id;
i++)
758 queue<node> next_nodes;
762 while (!next_nodes.empty())
764 cur_node = next_nodes.front();
769 while (adj_edge_it != adj_edges_end)
771 if ((*adj_edge_it).target() != cur_node)
773 next = (*adj_edge_it).target();
777 next = (*adj_edge_it).source();
781 next_nodes.push(next);
782 visited[
next] =
true;
803 int best_tentative_move = 0;
815 tentative_moves[step_number] = moved_node;
816 if (tentative_cutratio[best_tentative_move] >
cur_cutratio)
818 best_tentative_move = step_number;
822 else if (tentative_cutratio[best_tentative_move] ==
cur_cutratio)
826 best_tentative_move = step_number;
833 for (
int i = 1;
i <= best_tentative_move;
i++)
835 if (
side[tentative_moves[
i]] ==
A)
837 side[tentative_moves[
i]] =
B;
841 side[tentative_moves[
i]] =
A;
852 int best_tentative_move = 0;
864 tentative_moves[step_number] = moved_node;
865 if (tentative_cutratio[best_tentative_move] >
cur_cutratio)
867 best_tentative_move = step_number;
870 else if (tentative_cutratio[best_tentative_move] ==
cur_cutratio)
874 best_tentative_move = step_number;
881 for (
int i = 1;
i <= best_tentative_move;
i++)
883 if (
side[tentative_moves[
i]] ==
A)
885 side[tentative_moves[
i]] =
B;
889 side[tentative_moves[
i]] =
A;
905 moved_node = cons_nodeA;
924 moved_node = cons_nodeB;
937 node cons_node = node_list.front();
938 double ratio, best_ratio;
939 if (
side[cons_node] ==
A)
948 list<node>::iterator node_it = node_list.begin();
949 list<node>::iterator nodes_end = node_list.end();
950 while (node_it != nodes_end)
952 if (
side[cons_node] ==
A)
960 if (ratio > best_ratio)
963 cons_node = *node_it;
974 return ((
double)
cur_cutsize + number_of_nodes) / (
double)
1008 const bool init_mode)
1020 while (adj_edge_it != adj_edges_end)
1023 unlockedA[*adj_edge_it].remove(cur_node);
1024 --
aside[*adj_edge_it];
1025 if (
aside[*adj_edge_it] == 0)
1027 list<node>::iterator node_it =
unlockedB[*adj_edge_it].begin();
1028 list<node>::iterator nodes_end =
unlockedB[*adj_edge_it].end();
1029 while (node_it != nodes_end)
1038 else if (
aside[*adj_edge_it] == 1)
1040 list<node>::iterator node_it =
unlockedA[*adj_edge_it].begin();
1041 list<node>::iterator nodes_end =
unlockedA[*adj_edge_it].end();
1042 while (node_it != nodes_end)
1052 ++
bside[*adj_edge_it];
1053 if (
bside[*adj_edge_it] == 1)
1055 list<node>::iterator node_it =
unlockedA[*adj_edge_it].begin();
1056 list<node>::iterator nodes_end =
unlockedA[*adj_edge_it].end();
1057 while (node_it != nodes_end)
1066 else if (
bside[*adj_edge_it] == 2)
1068 list<node>::iterator node_it =
unlockedB[*adj_edge_it].begin();
1069 list<node>::iterator nodes_end =
unlockedB[*adj_edge_it].end();
1070 while (node_it != nodes_end)
1085 const bool init_mode)
1097 while (adj_edge_it != adj_edges_end)
1100 unlockedB[*adj_edge_it].remove(cur_node);
1101 bside[*adj_edge_it] -= 1;
1102 if (
bside[*adj_edge_it] == 0)
1104 list<node>::iterator node_it =
unlockedA[*adj_edge_it].begin();
1105 list<node>::iterator nodes_end =
unlockedA[*adj_edge_it].end();
1106 while (node_it != nodes_end)
1115 else if (
bside[*adj_edge_it] == 1)
1117 list<node>::iterator node_it =
unlockedB[*adj_edge_it].begin();
1118 list<node>::iterator nodes_end =
unlockedB[*adj_edge_it].end();
1119 while (node_it != nodes_end)
1129 aside[*adj_edge_it] += 1;
1130 if (
aside[*adj_edge_it] == 1)
1132 list<node>::iterator node_it =
unlockedB[*adj_edge_it].begin();
1133 list<node>::iterator nodes_end =
unlockedB[*adj_edge_it].end();
1134 while (node_it != nodes_end)
1143 else if (
aside[*adj_edge_it] == 2)
1145 list<node>::iterator node_it =
unlockedA[*adj_edge_it].begin();
1146 list<node>::iterator nodes_end =
unlockedA[*adj_edge_it].end();
1147 while (node_it != nodes_end)
1162 const int old_gain,
const int new_gain,
const bool init_mode)
1183 const int old_gain,
const int new_gain,
const bool init_mode)
1241 while (edge_it != edges_end)
1264 while (node_it != nodes_end)
1266 dest[*node_it] = source[*node_it];
1274 bool continue_loop =
true;
1278 while (continue_loop)
1293 continue_loop =
true;
1300 continue_loop =
false;
1316 continue_loop =
true;
1323 continue_loop =
false;
1332 bool improved_cutratio;
1340 while (improved_cutratio);
1346 int step_number = 0;
1347 int best_tentative_move = 0;
1358 tentative_moves[step_number] = moved_node;
1360 if (tentative_cutratio[best_tentative_move] >
cur_cutratio)
1362 best_tentative_move = step_number;
1366 else if (tentative_cutratio[best_tentative_move] ==
cur_cutratio)
1370 best_tentative_move = step_number;
1377 for (
int i = 1;
i <= best_tentative_move;
i++)
1379 if (
side[tentative_moves[
i]] ==
A)
1381 side[tentative_moves[
i]] =
B;
1385 side[tentative_moves[
i]] =
A;
1388 cur_cutratio = tentative_cutratio[best_tentative_move];
1390 if (best_tentative_move > 0)
1400 bool consA_ok =
false, consB_ok =
false;
1401 node cons_nodeA, cons_nodeB;
1410 node temp_node = cons_nodeA;
1437 node temp_node = cons_nodeB;
1458 if (consA_ok && consB_ok)
1462 if (ratio_A2B > ratio_B2A)
1464 moved_node = cons_nodeA;
1471 moved_node = cons_nodeB;
1479 moved_node = cons_nodeA;
1485 moved_node = cons_nodeB;
1504 while (edge_it != edges_end)
1506 if (
side[(*edge_it).source()] !=
side[(*edge_it).target()])
1521 while (node_it != nodes_end)
1523 if (
side[*node_it] ==
A)
1525 nodesA.push_back(*node_it);
1529 nodesB.push_back(*node_it);
1537 void ratio_cut_partition::print_bucketA()
1544 list<node>::iterator node_it =
bucketA[
i].begin();
1545 list<node>::iterator nodes_end =
bucketA[
i].end();
1546 while (node_it != nodes_end)
1558 void ratio_cut_partition::print_bucketB()
1565 list<node>::iterator node_it =
bucketB[
i].begin();
1566 list<node>::iterator nodes_end =
bucketB[
i].end();
1567 while (node_it != nodes_end)