https://github.com/emahtab/course-schedule-iv
https://github.com/emahtab/course-schedule-iv
floyd-warshall-algorithm graph leetcode
Last synced: 3 months ago
JSON representation
- Host: GitHub
- URL: https://github.com/emahtab/course-schedule-iv
- Owner: eMahtab
- Created: 2021-12-24T16:46:33.000Z (over 3 years ago)
- Default Branch: main
- Last Pushed: 2022-01-03T02:09:00.000Z (over 3 years ago)
- Last Synced: 2025-02-02T03:26:03.556Z (5 months ago)
- Topics: floyd-warshall-algorithm, graph, leetcode
- Homepage:
- Size: 49.8 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Course Schedule IV
## https://leetcode.com/problems/course-schedule-ivThere are a total of numCourses courses you have to take, labeled from 0 to numCourses - 1. You are given an array prerequisites where prerequisites[i] = [ai, bi] indicates that you must take course ai first if you want to take course bi.
For example, the pair [0, 1] indicates that you have to take course 0 before you can take course 1.
**Prerequisites can also be indirect. If course a is a prerequisite of course b, and course b is a prerequisite of course c, then course a is a prerequisite of course c.**
You are also given an array queries where queries[j] = [uj, vj]. For the jth query, you should answer whether course uj is a prerequisite of course vj or not.
Return a boolean array answer, where answer[j] is the answer to the jth query.

# Implementation : Floyd Warshall Algorithm O(n^3)
```java
class Solution {
public List checkIfPrerequisite(int n, int[][] prerequisites, int[][] queries) {
boolean[][] connected = new boolean[n][n];
for (int[] p : prerequisites)
connected[p[0]][p[1]] = true; // p[0] -> p[1]
for (int k = 0; k < n; k++)
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++)
connected[i][j] = connected[i][j] || connected[i][k] && connected[k][j];
List result = new ArrayList<>();
for (int[] q : queries)
result.add(connected[q[0]][q[1]]);
return result;
}
}
```# References :
https://www.youtube.com/watch?v=oNI0rf2P9gE (Abdul Bari, Floyd Warshall : All Pairs Shortest Path Algorithm)https://leetcode.com/problems/course-schedule-iv/discuss/660509/JavaPython-FloydWarshall-Algorithm-Clean-code-O(n3)