Changeset 52 for sources/src/graph.cc
- Timestamp:
- Jan 22, 2013, 4:23:22 PM (12 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
sources/src/graph.cc
r27 r52 1 1 /*------------------------------------------------------------\ 2 | |3 | Tool : systemcass |4 | |5 | File : graph.cc |6 | |7 | Author : Petrot Frédéric |8 | Taktak Sami |9 | Buchmann Richard |10 | |11 | Date : 09_07_2004 |12 | |13 \------------------------------------------------------------*/2 | | 3 | Tool : systemcass | 4 | | 5 | File : graph.cc | 6 | | 7 | Author : Petrot Frédéric | 8 | Taktak Sami | 9 | Buchmann Richard | 10 | | 11 | Date : 09_07_2004 | 12 | | 13 \------------------------------------------------------------*/ 14 14 15 15 /* … … 121 121 /* Sorry for that, Mr Knuth :-) */ 122 122 123 Graph *gb_new_graph(int n) 124 { 125 Graph *g; 126 127 g = (Graph *) malloc(sizeof(Graph)); 128 g->n = n; 129 if (n) 130 { 131 g->vertices = (Vertex *) malloc(n * sizeof(Vertex)); 132 (void)memset(g->vertices, 0, n * sizeof(Vertex)); 133 } else { 134 g->vertices = NULL; 135 } 136 return g; 137 } 138 139 static void gb_new_arc(Vertex *u, Vertex *v, long l) 140 { 141 Arc *a; 142 143 a = (Arc *) malloc(sizeof(Arc)); 144 a->next = u->arcs; 145 u->arcs = a; 146 a->tip = v; 147 a->len = l; 123 Graph * gb_new_graph(int n) { 124 Graph * g; 125 126 g = (Graph *) malloc(sizeof(Graph)); 127 g->n = n; 128 if (n) { 129 g->vertices = (Vertex *) malloc(n * sizeof(Vertex)); 130 (void) memset(g->vertices, 0, n * sizeof(Vertex)); 131 } 132 else { 133 g->vertices = NULL; 134 } 135 return g; 136 } 137 138 139 static void gb_new_arc(Vertex * u, Vertex * v, long l) { 140 Arc * a; 141 a = (Arc *) malloc(sizeof(Arc)); 142 a->next = u->arcs; 143 u->arcs = a; 144 a->tip = v; 145 a->len = l; 148 146 } 149 147 … … 152 150 #define VERTEX 220898 153 151 154 #define type u.I 155 156 void new_arc(Vertex *u, Vertex *v) 157 { 158 Arc *a; 152 #define type u.I 153 154 void new_arc(Vertex * u, Vertex * v) { 155 Arc * a; 159 156 #ifdef CONFIG_DEBUG 160 if ((u == NULL) || (v == NULL)) 161 exit(29042004); 157 if ((u == NULL) || (v == NULL)) { 158 exit(29042004); 159 } 162 160 #endif 163 for (a = u->arcs; a; a = a->next) 164 if (a->tip == v) 165 return; 166 gb_new_arc(u, v, 0L); 167 } 161 for (a = u->arcs; a != NULL; a = a->next) { 162 if (a->tip == v) { 163 return; 164 } 165 } 166 gb_new_arc(u, v, 0L); 167 } 168 168 169 169 170 #define rank z.I … … 181 182 * to ones list the components contents (one or more vertices) */ 182 183 183 extern strong_component_list_t *strong_component(Graph *g) 184 { 185 long nn; 186 Vertex *active_stack; 187 Vertex *settled_stack; 188 Vertex *vv; 189 190 register Vertex *v; 191 192 strong_component_list_t *strongcomponents = new strong_component_list_t; 193 typedef std::vector<void*> void_list_t; 194 void_list_t *component; 195 196 if (g->vertices == NULL) 184 extern strong_component_list_t *strong_component(Graph * g) { 185 long nn; 186 Vertex * active_stack; 187 Vertex * settled_stack; 188 Vertex * vv; 189 190 register Vertex * v; 191 192 strong_component_list_t * strongcomponents = new strong_component_list_t; 193 typedef std::vector < void *>void_list_t; 194 void_list_t * component; 195 196 if (g->vertices == NULL) { 197 return strongcomponents; 198 } 199 200 /* Make all vertices unseen and all arcs untagged */ 201 for (v = g->vertices + g->n - 1; v >= g->vertices; v--) { 202 v->rank = 0; 203 v->untagged = v->arcs; 204 } 205 nn = 0; 206 active_stack = settled_stack = NULL; 207 208 for (vv = g->vertices; vv < g->vertices + g->n; vv++) { 209 if (vv->rank == 0) { /* vv is still unseen */ 210 v = vv; 211 v->parent = NULL; 212 213 v->rank = ++nn; 214 v->link = active_stack; 215 active_stack = v; 216 v->min = v; 217 218 do { /* Eplore one step from the current vertex v, possibly moving to another vertex 219 and calling it v */ 220 register Vertex *u; /* a vertex adjacent to v */ 221 register Arc *a = v->untagged; /* v's first remaining untagged arc, if any */ 222 if (a) { 223 u = a->tip; 224 v->untagged = a->next; /* tag the arc from v to u */ 225 if (u->rank) { /* we've seen u already */ 226 if (u->rank < v->min->rank) { /* non-tree arc, jutt update v->min */ 227 v->min = u; 228 } 229 } 230 else { /* u is presently unseen */ 231 u->parent = v; /* the arcfrom v to u is a new tree arc */ 232 v = u; /* u will now be the currnt vertex */ 233 v->rank = ++nn; /* make vertex v active */ 234 v->link = active_stack; 235 active_stack = v; 236 v->min = v; 237 } 238 } 239 else { /* all arcs from v are tagged, so v matures */ 240 u = v->parent; /* prepare to backtrack in the tree */ 241 if (v->min == v) { 242 /* Remove v and all its successors on the activestack from the tree, 243 and mark them as a strong component of the graph */ 244 register Vertex * t; /* runs though the vertices of the new strong component */ 245 246 t = active_stack; 247 active_stack = v->link; 248 v->link = settled_stack; 249 settled_stack = t; /* we've moved the top of one stack to the other */ 250 component = new void_list_t; 251 /* singe vetex */ 252 if (t != v) { /* NOT singe vetex */ 253 while (t != v) { 254 component->push_back(t->data); 255 t->rank = infinity; /* now t is settled */ 256 t->parent = v; /* and v represents the new strong component */ 257 t = t->link; 258 } 259 } 260 component->push_back(v->data); 261 strongcomponents->push_back(component); 262 v->rank = infinity; /* v too is settled */ 263 v->parent = v; /* and represents its own strong component */ 264 } 265 else { 266 /* the arc from u to v has just matured, making v->min visible from u */ 267 if (v->min->rank < u->min->rank) { 268 u->min = v->min; 269 } 270 } 271 v = u;/* the former parent of v is the new current vertex v */ 272 } 273 } while (v != NULL); 274 } 275 } 276 197 277 return strongcomponents; 198 199 /* Make all vertices unseen and all arcs untagged */ 200 for (v = g->vertices + g->n - 1; v >= g->vertices; v--) { 201 v->rank = 0; 202 v->untagged = v->arcs; 203 } 204 nn = 0; 205 active_stack = settled_stack = NULL; 206 207 for (vv = g->vertices; vv < g->vertices + g->n; vv++) 208 if (vv->rank == 0) { /* vv is still unseen */ 209 v = vv; 210 v->parent = NULL; 211 212 v->rank = ++nn; 213 v->link = active_stack; 214 active_stack = v; 215 v->min = v; 216 217 do { /* Eplore one step from the current vertex v, possibly moving to another vertex 218 and calling it v */ 219 register Vertex *u; /* a vertex adjacent to v */ 220 register Arc *a = v->untagged; /* v's first remaining untagged arc, if any */ 221 if (a) { 222 u = a->tip; 223 v->untagged = a->next; /* tag the arc from v to u */ 224 if (u->rank) { /* we've seen u already */ 225 if (u->rank < v->min->rank) /* non-tree arc, jutt update v->min */ 226 v->min = u; 227 } else { /* u is presently unseen */ 228 u->parent = v; /* the arcfrom v to u is a new tree arc */ 229 v = u; /* u will now be the currnt vertex */ 230 v->rank = ++nn; /* make vertex v active */ 231 v->link = active_stack; 232 active_stack = v; 233 v->min = v; 234 } 235 } else { /* all arcs from v are tagged, so v matures */ 236 u = v->parent; /* prepare to backtrack in the tree */ 237 if (v->min == v) { 238 /* Remove v and all its successors on the activestack from the tree, 239 and mark them as a strong component of the graph */ 240 register Vertex *t; /* runs though the vertices of the new strong component */ 241 242 t = active_stack; 243 active_stack = v->link; 244 v->link = settled_stack; 245 settled_stack = t; /* we've moved the top of one stack to the other */ 246 component = new void_list_t; 247 /* singe vetex */ 248 if (t != v) { /* NOT singe vetex */ 249 while (t != v) { 250 component->push_back(t->data); 251 t->rank = infinity; /* now t is settled */ 252 t->parent = v; /* and v represents the new strong component */ 253 t = t->link; 254 } 255 } 256 component->push_back(v->data); 257 strongcomponents->push_back(component); 258 v->rank = infinity; /* v too is settled */ 259 v->parent = v; /* and represents its own strong component */ 260 } else /* the arc from u to v has just matured, making v->min visible from u */ 261 if (v->min->rank < u->min->rank) u->min = v->min; 262 v = u; /* the former parent of v is the new current vertex v */ 263 } 264 } while (v != NULL); 265 } 266 267 return strongcomponents; 268 } 269 270 bool 271 has_cycle (const strong_component_list_t &l) 272 { 273 strong_component_list_t::const_iterator it; 274 for (it = l.begin (); it != l.end (); ++it) 275 { 276 if ((*it)->size() > 1) 277 return true; 278 } 279 return false; 280 } 281 282 #if 0 283 const component_list_t* 284 get_cycle (const strong_component_list_t &l) 285 { 286 strong_component_list_t::const_iterator it; 287 for (it = l.begin (); it != l.end (); ++it) 288 { 289 const component_list_t *c = *it; 290 if (c->size() > 1) 291 return c; 292 } 293 return NULL; 294 } 295 296 void 297 print_cycle (std::ostream &o, 298 const component_list_t *c) 299 { 300 if (c == NULL) 301 return; 302 component_list_t::const_iterator it; 303 for (it = c->begin (); it != c->end (); ++it) 304 { 305 void *v = *it; 306 o << hex << (int) v << " "; 307 } 308 o << "\n"; 309 } 310 #endif 311 278 } 279 280 281 bool has_cycle(const strong_component_list_t & l) { 282 strong_component_list_t::const_iterator it; 283 for (it = l.begin(); it != l.end(); ++it) { 284 if ((*it)->size() > 1) { 285 return true; 286 } 287 } 288 return false; 289 } 290 291 292 /* 293 # Local Variables: 294 # tab-width: 4; 295 # c-basic-offset: 4; 296 # c-file-offsets:((innamespace . 0)(inline-open . 0)); 297 # indent-tabs-mode: nil; 298 # End: 299 # 300 # vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=4:softtabstop=4 301 */ 302
Note: See TracChangeset
for help on using the changeset viewer.