tangorishi / learnJava

LearnJava - Hacktoberfest 2023 šŸš€ Join us in celebrating Hacktoberfest 2023 by contributing to this Java programming repository! Whether you're a beginner or an expert, dive into Java, share your knowledge, and make Hacktoberfest unforgettable. šŸ“ššŸ’» Happy coding and contributing! šŸš€šŸŒŸ
MIT License
26 stars 44 forks source link

Create Floyd-Warshall-Algorithm.java #71

Closed competitiveblood closed 11 months ago

competitiveblood commented 11 months ago

Description: This Java code implements the Floyd-Warshall algorithm to find the shortest distances between all pairs of vertices in a weighted directed graph, displaying the results using basic data structures.

The Floyd-Warshall algorithm is a dynamic programming approach used to find the shortest paths between all pairs of vertices in a weighted graph. It iteratively updates distance information, considering all possible paths through intermediate vertices. It guarantees the shortest paths for both positive and negative edge weights, making it versatile for various graph scenarios.

Changes Made: The code defines a directed graph with weights. The floydWarshall method computes all-pair shortest distances using nested loops. The main method initializes the graph and prints the shortest distances, displaying "INF" for unreachable pairs.

The Floyd-Warshall algorithm employs three nested loops. The outermost loop (k loop) iterates through vertices, treating each as a potential intermediary (k). This allows the algorithm to explore using vertex k to potentially enhance all pairs of shortest paths. The middle loop (i loop) iterates over source vertices (i), evaluating paths to all destination vertices using the intermediary k. The innermost loop (j loop) checks if the path from vertex i to j via k is shorter than the current known path from i to j, updating the distance matrix accordingly. These loops systematically compute all-pair shortest paths in a weighted graph.

Testing Done: Shortest distances between all pairs of vertices: 0 5 8 9 5 0 3 4 8 3 0 1 9 4 1 0

Additional Notes: The Floyd-Warshall algorithm is named after its inventors, Robert W. Floyd and Stephen Warshall, who independently discovered it in 1962, showcasing simultaneous innovation in computer science.

The algorithm has a cubic time complexity, which means that it can be slow for large graphs. Second, the algorithm can be difficult to debug, especially if there are errors in the implementation.

It is based on a simple dynamic programming approach.

Made the required changes. Kindly merge by PR :)