Java implementation of graph data structures and algorithms using adjacency matrices. This project provides a set of tools for graph manipulation and analysis, including multiple pathfinding algorithms and cycle detection.
- Create weighted directed graphs with a specified number of vertices
- Add weighted edges between vertices
- Visualize the graph structure through adjacency matrix representation
- Shortest Path Finding:
- Bellman-Ford algorithm for detecting negative cycles and finding shortest paths
- Dijkstra's algorithm for efficient shortest path calculation in non-negative weighted graphs
- Graph Analysis:
- Cycle detection using Depth-First Search (DFS)
- Tree height calculation from any starting node
- Pre-order graph traversal
The project consists of two main classes:
Graph: Contains all graph operations and algorithmsMain: Provides example usage and testing scenarios
- Uses an adjacency matrix representation (
int[][]) - Infinity is represented by
Integer.MAX_VALUE - Vertices are zero-indexed internally but displayed one-indexed in output
// Create a graph with 13 vertices
Graph graph = new Graph(13);
// Add weighted edges
graph.addEdge(0, 1, 200); // Add edge from vertex 1 to 2 with weight 200
graph.addEdge(0, 12, 250); // Add edge from vertex 1 to 13 with weight 250
// Find shortest path using Dijkstra's algorithm
graph.findShortestPathDijkstra(0, 2); // Find shortest path from vertex 1 to 3
// Detect cycles
List<List<Integer>> cycles = graph.findCycles();The program provides detailed output for all operations:
- Adjacency matrix visualization
- Shortest path routes and distances
- Cycle detection results
- Tree height calculations
- Pre-order traversal sequences
- Java Development Kit (JDK) 8 or higher
- Compile the project:
javac Main.java- Run the program:
java MainThis project is licensed under the MIT License.
- Implemented as part of a university project for graph theory and algorithms
- Based on classical graph theory algorithms and data structures