Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/aleksandrhovhannisyan/pythongraphcycledetection
Python script for detecting cycles in a directed graph.
https://github.com/aleksandrhovhannisyan/pythongraphcycledetection
algorithm cycle-detection deadlock-detection detecting-cycles graphs python
Last synced: 11 days ago
JSON representation
Python script for detecting cycles in a directed graph.
- Host: GitHub
- URL: https://github.com/aleksandrhovhannisyan/pythongraphcycledetection
- Owner: AleksandrHovhannisyan
- Created: 2018-11-30T12:47:44.000Z (about 6 years ago)
- Default Branch: master
- Last Pushed: 2019-05-31T18:57:11.000Z (over 5 years ago)
- Last Synced: 2024-11-06T07:27:05.959Z (about 2 months ago)
- Topics: algorithm, cycle-detection, deadlock-detection, detecting-cycles, graphs, python
- Language: Python
- Homepage:
- Size: 26.4 KB
- Stars: 0
- Watchers: 2
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Python Graph Cycle Detection
A simple Python program to detect cycles in a directed graph. Below is a sample image of the graph used for testing [source: Modern Operating Systems, 4th ed]. The left image shows the original nodes in the graph. The right image shows the reduced graph with all identified cycles.
![Image of a graph from Modern Operating Systems, Chapter 6](graph.PNG)
### Using the Class
The `CycleFinder` class expects to be fed an instance of `DirectedGraph` (see `graph.py`).
This class accepts a dictionary that maps a node to its neighbors. Optionally, you can avoid initializing it with any dictionary and manually `insert` your nodes into the graph.
You'll need the following imports:
`from cycle_finder import CycleFinder`
`from graph import DirectedGraph`
### Testing
See the bottom of `cycle_finder.py` for an example test to identify the cycle in the image above. Note that CycleFinder *will* modify the underlying dictionary once the `findCycles` method is invoked.
Note: If a node does not have any edges originating from it (such as `S` in the sample image provided above), do one of the following:
- If you're initializing a `DirectedGraph` object with a dictionary, then specify that node's neighbors as the empty list (`[]`)
- If you're instead manually inserting start-end pairs, then specify `end` as `None`