| 369 | return len(self.graph[u]) |
| 370 | |
| 371 | def cycle_nodes(self): |
| 372 | stack = [] |
| 373 | visited = [] |
| 374 | s = next(iter(self.graph)) |
| 375 | stack.append(s) |
| 376 | visited.append(s) |
| 377 | parent = -2 |
| 378 | indirect_parents = [] |
| 379 | ss = s |
| 380 | on_the_way_back = False |
| 381 | anticipating_nodes = set() |
| 382 | |
| 383 | while True: |
| 384 | # check if there is any non isolated nodes |
| 385 | if len(self.graph[s]) != 0: |
| 386 | ss = s |
| 387 | for node in self.graph[s]: |
| 388 | if ( |
| 389 | visited.count(node[1]) > 0 |
| 390 | and node[1] != parent |
| 391 | and indirect_parents.count(node[1]) > 0 |
| 392 | and not on_the_way_back |
| 393 | ): |
| 394 | len_stack = len(stack) - 1 |
| 395 | while len_stack >= 0: |
| 396 | if stack[len_stack] == node[1]: |
| 397 | anticipating_nodes.add(node[1]) |
| 398 | break |
| 399 | else: |
| 400 | anticipating_nodes.add(stack[len_stack]) |
| 401 | len_stack -= 1 |
| 402 | if visited.count(node[1]) < 1: |
| 403 | stack.append(node[1]) |
| 404 | visited.append(node[1]) |
| 405 | ss = node[1] |
| 406 | break |
| 407 | |
| 408 | # check if all the children are visited |
| 409 | if s == ss: |
| 410 | stack.pop() |
| 411 | on_the_way_back = True |
| 412 | if len(stack) != 0: |
| 413 | s = stack[len(stack) - 1] |
| 414 | else: |
| 415 | on_the_way_back = False |
| 416 | indirect_parents.append(parent) |
| 417 | parent = s |
| 418 | s = ss |
| 419 | |
| 420 | # check if se have reached the starting point |
| 421 | if len(stack) == 0: |
| 422 | return list(anticipating_nodes) |
| 423 | |
| 424 | def has_cycle(self): |
| 425 | stack = [] |