Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,20 +1,16 @@
package algorithms.curated170.medium.parallelcourses;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Queue;

public class ParallelCourses {
public class ParallelCoursesBFS {
List<List<Integer>> courseChiMap;
int[] inDegreeMap;
int numOfCourses;

public int minimumSemesters(int n, int[][] courses) {
inDegreeMap = new int[n + 1];
numOfCourses = n;

createMap(courses);
Expand Down Expand Up @@ -50,8 +46,8 @@ private int sortTopologically() {
}

private void createMap(int[][] courses) {

courseChiMap = new LinkedList<>();
inDegreeMap = new int[numOfCourses + 1];
courseChiMap = new ArrayList<>();
for (int i = 0; i <= numOfCourses; i++) {
courseChiMap.add(new ArrayList<>());
}
Expand All @@ -77,7 +73,7 @@ private void setFirstCourses(Queue<Integer> q) {
public static void main(String[] args) {
int[][] courses = new int[][] { { 1, 5 }, { 3, 2 }, { 3, 4 }, { 2, 6 }, { 4, 6 }, { 2, 5 }, { 5, 8 }, { 7, 8 },
{ 6, 9 }, { 7, 10 }, { 6, 8 }, { 8, 10 }, { 9, 10 } };
var solution = new ParallelCourses();
var solution = new ParallelCoursesBFS();
System.out.println(solution.minimumSemesters(10, courses));
}
}
Original file line number Diff line number Diff line change
@@ -1,36 +1,48 @@
package algorithms.curated170.medium.parallelcourses;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

public class ParallelCoursesDFS {

private int maxPathLength = Integer.MIN_VALUE;
private boolean hasCycle = false;
HashMap<Integer, List<Integer>> graph;
List<List<Integer>> courseChiMap;

boolean[] visited;
int[] depth;

public int minimumSemesters(int n, int[][] relations) {
graph = new HashMap<>();

createMap(n, relations);

searchNodes(n);

return hasCycle ? -1 : maxPathLength;
}

private void createMap(int n, int[][] relations) {
courseChiMap = new ArrayList<>();
for (int i = 0; i <= n; i++) {
courseChiMap.add(new ArrayList<>());
}

for (int[] relation : relations) {
graph.putIfAbsent(relation[0], new ArrayList<>());
graph.get(relation[0]).add(relation[1]);
courseChiMap.get(relation[0]).add(relation[1]);
}
}

private void searchNodes(int n) {
depth = new int[n + 1];
visited = new boolean[n + 1];
for (int i = 1; i <= n; i++) {
dfs(i);

if(hasCycle)
{
return -1;

if (hasCycle) {
return;
}
}
return hasCycle ? -1 : maxPathLength;

}

private int dfs(int root) {
Expand All @@ -45,11 +57,10 @@ private int dfs(int root) {
visited[root] = true;
int max = 0;

if (graph.containsKey(root)) {
for (int neighbor : graph.get(root)) {
max = Math.max(max, dfs(neighbor));
}
for (int neighbor : courseChiMap.get(root)) {
max = Math.max(max, dfs(neighbor));
}

visited[root] = false;
depth[root] = max + 1;
maxPathLength = Math.max(maxPathLength, depth[root]);
Expand All @@ -62,7 +73,7 @@ public static void main(String[] args) {
var solution = new ParallelCoursesDFS();
System.out.println(solution.minimumSemesters(10, courses));

int[][] loopingCourses = new int[][] {{1,3}, {3,2}, {2,1}};
int[][] loopingCourses = new int[][] { { 1, 3 }, { 3, 2 }, { 2, 1 } };
var solution1 = new ParallelCoursesDFS();
System.out.println(solution1.minimumSemesters(3, loopingCourses));
}
Expand Down