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