1 |
francois |
113 |
#include "gestionversion.h" |
2 |
francois |
283 |
#include "HypergraphLib_platform.h" |
3 |
|
|
|
4 |
|
|
#include "HypergraphLib_Graph.h" |
5 |
|
|
#include "HypergraphLib_Node.h" |
6 |
|
|
#include "HypergraphLib_Arc.h" |
7 |
|
|
|
8 |
|
|
#include <set> |
9 |
|
|
#include <vector> |
10 |
|
|
#include <map> |
11 |
|
|
|
12 |
|
|
|
13 |
|
|
/** |
14 |
|
|
* Algorithme de d�composition d'un graphe en filaments |
15 |
|
|
*/ |
16 |
|
|
|
17 |
|
|
namespace HypergraphLib { |
18 |
|
|
|
19 |
|
|
HYPERGRAPHLIB_ITEM void |
20 |
|
|
Filaments ( Graph * G, std::vector < std::vector < int > > & __filaments ) |
21 |
|
|
{ |
22 |
|
|
std::set <int> visited; |
23 |
|
|
std::set <int> next; |
24 |
|
|
std::vector <int> filament; |
25 |
|
|
std::set <int> unvisited; |
26 |
|
|
std::set <int> adjacentNodes; |
27 |
|
|
|
28 |
|
|
// Initialize the set of unvisited nodes |
29 |
|
|
for (Graph::MapNodesById::const_iterator itNodes = G->GetNodes().begin(); |
30 |
|
|
itNodes != G->GetNodes().end(); |
31 |
|
|
itNodes++ ) |
32 |
|
|
{ |
33 |
|
|
unvisited.insert (itNodes->first); |
34 |
|
|
} |
35 |
|
|
|
36 |
|
|
while (unvisited.size() > 0) |
37 |
|
|
{ |
38 |
|
|
Node * N = G->GetNode(*(unvisited.begin())); |
39 |
|
|
for (std::set<int>::const_iterator itAN = unvisited.begin(); |
40 |
|
|
itAN != unvisited.end(); |
41 |
|
|
itAN++ ) |
42 |
|
|
{ |
43 |
|
|
Node * N2 = G->GetNode(*itAN); |
44 |
|
|
if (N2->IncidentArcs().size() == 1) |
45 |
|
|
{ |
46 |
|
|
N = N2; |
47 |
|
|
break; |
48 |
|
|
} |
49 |
|
|
} |
50 |
|
|
|
51 |
|
|
visited.insert(N->Id()); |
52 |
|
|
unvisited.erase(unvisited.find(N->Id())); |
53 |
|
|
next.clear(); |
54 |
|
|
adjacentNodes.clear(); |
55 |
|
|
N->AdjacentNodes(adjacentNodes); |
56 |
|
|
for (std::set<int>::const_iterator itAN = adjacentNodes.begin(); |
57 |
|
|
itAN != adjacentNodes.end(); |
58 |
|
|
itAN++ ) |
59 |
|
|
{ |
60 |
|
|
if ( visited.find (*itAN) == visited.end() ) |
61 |
|
|
{ |
62 |
|
|
next.insert (*itAN); |
63 |
|
|
break; |
64 |
|
|
} |
65 |
|
|
} |
66 |
|
|
filament.clear(); |
67 |
|
|
filament.push_back (N->Id()); |
68 |
|
|
|
69 |
|
|
while (next.size() > 0) |
70 |
|
|
{ |
71 |
|
|
N = G->GetNode(*(next.begin())); |
72 |
|
|
visited.insert(N->Id()); |
73 |
|
|
unvisited.erase(unvisited.find(N->Id())); |
74 |
|
|
adjacentNodes.clear(); |
75 |
|
|
next.clear(); |
76 |
|
|
filament.push_back (N->Id()); |
77 |
|
|
|
78 |
|
|
N->AdjacentNodes(adjacentNodes); |
79 |
|
|
for (std::set<int>::const_iterator itAN = adjacentNodes.begin(); |
80 |
|
|
itAN != adjacentNodes.end(); |
81 |
|
|
itAN++ ) |
82 |
|
|
{ |
83 |
|
|
// printf("Candidate node %d\n",*itAN); |
84 |
|
|
if ( visited.find (*itAN) == visited.end() ) |
85 |
|
|
{ |
86 |
|
|
int count_filament_adjacency = 0; |
87 |
|
|
std::vector<int>::const_iterator it_fil = filament.begin(); |
88 |
|
|
bool is_adjacent_to_start=false; |
89 |
|
|
bool is_adjacent_to_end=false; |
90 |
|
|
Node * fN=NULL; |
91 |
|
|
|
92 |
|
|
it_fil = filament.begin(); |
93 |
|
|
fN = G->GetNode (*it_fil); |
94 |
|
|
is_adjacent_to_start = fN->IsAdjacentToNode(*itAN); |
95 |
|
|
|
96 |
|
|
|
97 |
|
|
it_fil = filament.end(); |
98 |
|
|
it_fil--; |
99 |
|
|
fN = G->GetNode (*it_fil); |
100 |
|
|
is_adjacent_to_end = fN->IsAdjacentToNode(*itAN); |
101 |
|
|
|
102 |
|
|
for ( it_fil = filament.begin(); |
103 |
|
|
it_fil != filament.end(); it_fil++) |
104 |
|
|
{ |
105 |
|
|
fN = G->GetNode (*it_fil); |
106 |
|
|
if (fN->IsAdjacentToNode(*itAN)) |
107 |
|
|
count_filament_adjacency++; |
108 |
|
|
} |
109 |
|
|
it_fil = filament.end(); |
110 |
|
|
it_fil--; |
111 |
|
|
// printf("count_filament_adjacency = %d, is_adjacent_to_(%d)=%d\n", count_filament_adjacency,*it_fil,is_adjacent_to_end); |
112 |
|
|
it_fil = filament.begin(); |
113 |
|
|
// printf("is_adjacent_to_(%d)=%d\n",*it_fil,is_adjacent_to_start); |
114 |
|
|
|
115 |
|
|
bool is_not_T = true; |
116 |
|
|
if ( is_adjacent_to_end && is_adjacent_to_start) |
117 |
|
|
{ |
118 |
|
|
|
119 |
|
|
for ( it_fil = filament.begin(); |
120 |
|
|
it_fil != filament.end(); it_fil++) |
121 |
|
|
{ |
122 |
|
|
fN = G->GetNode (*it_fil); |
123 |
|
|
for ( Node::MultimapArcsById::const_iterator itArcs = fN->IncidentArcs().begin(); |
124 |
|
|
itArcs != fN->IncidentArcs().end(); |
125 |
|
|
itArcs++ ) |
126 |
|
|
{ |
127 |
|
|
Arc * arc = itArcs->second; |
128 |
|
|
|
129 |
|
|
if ( arc->Nodes().find(*itAN) != arc->Nodes().end() |
130 |
|
|
&& arc->Nodes().find(filament[0]) != arc->Nodes().end() |
131 |
|
|
&& arc->Nodes().find(filament[filament.size()-1]) != arc->Nodes().end() ) |
132 |
|
|
{ |
133 |
|
|
is_not_T = false; |
134 |
|
|
} |
135 |
|
|
} |
136 |
|
|
} |
137 |
|
|
|
138 |
|
|
} |
139 |
|
|
|
140 |
|
|
if (count_filament_adjacency == 1 || ( is_adjacent_to_end && is_adjacent_to_start && is_not_T) ) |
141 |
|
|
{ |
142 |
|
|
next.insert (*itAN); |
143 |
|
|
// printf("Candidate Node %d choosen!\n", *itAN); |
144 |
|
|
break; |
145 |
|
|
} |
146 |
|
|
} |
147 |
|
|
} |
148 |
|
|
} |
149 |
|
|
__filaments.push_back(filament); |
150 |
|
|
} |
151 |
|
|
|
152 |
francois |
102 |
} |
153 |
francois |
283 |
} |
154 |
francois |
102 |
|