This project was developed as part of the Algorithms: Theory, Design and Implementation (5SENG003C.2 / 5SENG003W) coursework at the University of Westminster / Informatics Institute of Technology.
The aim of this coursework is to implement an algorithm to check whether a given directed graph is acyclic. If the graph contains a cycle, the program detects and outputs the cycle. The implementation is done in Java, using the Depth-First Search (DFS) algorithm.
- Representation of directed graphs using efficient Java data structures (
HashMap,HashSet,ArrayList). - Input parser to read graph edges from a file.
- Cycle detection using DFS with recursion and backtracking.
- Produces detailed output, showing traversal and detected cycles.
- Benchmark tests on acyclic and cyclic examples.
- Includes performance analysis (time and space complexity).
This project addresses the following coursework tasks:
- Project Setup – Java project setup and structure.
- Data Structures – Graph representation using adjacency lists.
- Input Parser – Reads graph data from text files containing pairs of numbers (edges).
- Algorithm Implementation – Determines if the graph is acyclic using DFS.
- Cycle Detection – Outputs a cycle if found in the graph.
- Report – Explains choice of algorithm and data structures, benchmarks, and complexity analysis.
- Programming Language: Java (JDK 17)
- Core Data Structures:
HashMap<Integer, ArrayList<Integer>>→ For adjacency list representation of graphsHashSet<Integer>→ For tracking visited vertices and current recursion stackArrayList<Integer>→ For storing detected cycles and neighbor lists
Algorithm
- Depth-First Search (DFS): Chosen for its efficiency in cycle detection and memory optimization
- Time Complexity: O(|V| + |E|) where V is vertices and E is edges
- Space Complexity: O(|V|) in worst case due to recursion stack
Algo_Coursework/
├── src/
│ ├── Main.java
│ ├── AcyclicDetector.java
│ ├── DirectedGraph.java
│ ├── GraphParser.java
│ └── graphInfo.txt
├── w1867067_algo_report.docx
├── w1867067_algo_report.pdf
└── README.md
- Java JDK 17 or higher
- Git (for cloning the repository)
git clone https://github.com/SandeepaInduwaraSamaranayake/Acyclic_graph_detector.git
cd Acyclic_graph_detector
javac src/*.java
source_vertex target_vertex
source_vertex target_vertex
Example:
1 2
2 3
3 1
java -cp src Main
- Sandeepa Induwara Samaranayake
- IIT No: 20210302
- UOW No: w1867067
- BEng (Hons) Software Engineering
- Informatics Institute of Technology / University of Westminster
This project was developed for educational purposes as part of university coursework and open sourced under MIT license.