private bool orderCourses(CourseGraph graph, int numCourse, int[][] prerequisites) { int numDependencies = prerequisites.Count(); List <CourseGraphNode> nodes = graph.nodes; CourseGraphNode[] courses = new CourseGraphNode[nodes.Count]; //Cpature all courses which doesn't have any prerequisites int endOfList = addNodesWithNoDependencies(courses, nodes, 0); int toBeProcessed = 0; while (toBeProcessed < courses.Length) { var course = courses[toBeProcessed]; //Circular dependency detected if (course == null) { return(false); } //remove current element from dependency List <CourseGraphNode> children = course.children; foreach (var child in children) { //remove edge from removed node and child nodes child.decrementDependencies(); } //Add childrens that has no dependent nodes endOfList = addNodesWithNoDependencies(courses, children, endOfList); toBeProcessed++; } return(true); }
public void addNeighbours(CourseGraphNode node) { if (!map.ContainsKey(node.getCourse())) { children.Add(node); map.Add(node.getCourse(), node); node.incrementDependencies(); } }
//Get or Create Node of the graph from cache public CourseGraphNode getOrCreateNode(int course) { if (!map.ContainsKey(course)) { CourseGraphNode node = new CourseGraphNode(course); nodes.Add(node); map.Add(course, node); } return(map[course]); }
//Add edge between start and end node and add end as neigbour of end public void addEdge(CourseGraphNode currCourse, CourseGraphNode preRequisite) { preRequisite.addNeighbours(currCourse); }