leetcdoe curriculum

topic

Now you need to choose the course a total of n, denoted 0to n-1.

Need some Advanced Placement courses before elective certain. For example, you want to study courses 0, you need to complete Lesson 1, we use a match to represent them:[0,1]

A given amount of courses and their prerequisites, determine whether it is possible to complete all courses of study?

Example 1:

输入: 2, [[1,0]] 
输出: true
解释: 总共有 2 门课程。学习课程 1 之前,你需要完成课程 0。所以这是可能的。

Example 2:

输入: 2, [[1,0],[0,1]]
输出: false
解释: 总共有 2 门课程。学习课程 1 之前,你需要先完成​课程 0;
并且学习课程 0 之前,你还应先完成课程 1。这是不可能的。

Description:
Prerequisites input pattern is represented by an edge list, rather than adjacency matrix. Please refer to Figure representation of.
You can assume that the prerequisites are no duplicate input side.
Tip:
This problem is equivalent to find a loop exists in a directed graph. If there is circulation, topological sorting not exist, it is not possible to select all courses to learn.
Topological sorting through DFS - wonderful video tutorial on Coursera's (21 minutes), introduces the basic concepts of topological sorting.
Topological sorting can also be done through the BFS.

Code (java)

Method a: the degree table (breadth first traversal)

Directed graph

class Solution {

    public boolean canFinish(int numCourses, int prerequisites) {
        int[] indegrees = new int[numCourses];
        for(int[] cp : prerequisites) indegrees[cp[0]]++;//入度增加
        LinkedList<Integer> queue = new LinkedList<>();
        for(int i = 0; i < numCourses; i++){
            if(indegrees[i] == 0) queue.addLast(i);
        }

        while(!queue.isEmpty()) {
            Integer pre = queue.removeFirst();
            numCourses--;
            for(int[] req : prerequisites) {//循环二维数组
                if(req[1] != pre) continue;//判断是不是依赖这一门课,如果不是,就跳过
                //如果是这门课,入席数组减减
                if(--indegrees[req[0]] == 0) queue.add(req[0]);
            }
        }
        return numCourses == 0;
    }
}

Method Two: depth-first traversal

Step 1: Construction of an inverse adjacency table;

Step 2: Each node recursive process has not been accessed, the specific approach is very simple: for a node, the first output point to all of its vertices, and then output itself.

Step 3: If the vertex has not been traversed, recursively traversing it, all nodes are directed to its output, and then outputs themselves. Note: When accessing a node, it should first descend predecessor node, the node is not up until precursor predecessor node.

import java.util.HashSet;

public class Solution6 {

    public boolean canFinish(int numCourses, int[][] prerequisites) {
        if (numCourses <= 0) {
            return false;
        }
        int plen = prerequisites.length;
        if (plen == 0) {
            return true;
        }
        int[] marked = new int[numCourses];

        // 初始化有向图 begin
        HashSet<Integer>[] graph = new HashSet[numCourses];
        for (int i = 0; i < numCourses; i++) {
            graph[i] = new HashSet<>();
        }
        // 初始化有向图 end
        // 有向图的 key 是前驱结点,value 是后继结点的集合
        for (int[] p : prerequisites) {
            graph[p[1]].add(p[0]);
        }

        for (int i = 0; i < numCourses; i++) {
            if (dfs(i, graph, marked)) {
                // 注意方法的语义,如果图中存在环,表示课程任务不能完成,应该返回 false
                return false;
            }
        }
        // 在遍历的过程中,一直 dfs 都没有遇到已经重复访问的结点,就表示有向图中没有环
        // 所有课程任务可以完成,应该返回 true
        return true;
    }

    /**
     * 注意这个 dfs 方法的语义
     * @param i      当前访问的课程结点
     * @param graph
     * @param marked 如果 == 1 表示正在访问中,如果 == 2 表示已经访问完了
     * @return true 表示图中存在环,false 表示访问过了,不用再访问了
     */
    private boolean dfs(int i,
                        HashSet<Integer>[] graph,
                        int[] marked) {
        // 如果访问过了,就不用再访问了
        if (marked[i] == 1) {
            // 从正在访问中,到正在访问中,表示遇到了环
            return true;
        }

        if (marked[i] == 2) {
            // 表示在访问的过程中没有遇到环,这个节点访问过了
            return false;
        }
        // 走到这里,是因为初始化呢,此时 marked[i] == 0
        // 表示正在访问中
        marked[i] = 1;
        // 后继结点的集合
        HashSet<Integer> successorNodes = graph[i];

        for (Integer successor : successorNodes) {
            if (dfs(successor, graph, marked)) {
                // 层层递归返回 true ,表示图中存在环
                return true;
            }
        }
        // i 的所有后继结点都访问完了,都没有存在环,则这个结点就可以被标记为已经访问结束
        // 状态设置为 2
        marked[i] = 2;
        // false 表示图中不存在环
        return false;
    }
}

Directed graph

Directed graph

Difference predecessor nodes and immediate predecessor

For a directed graph, it has A-> B-> C precursors: for C, its precursor of A, B immediate predecessor: for C, its immediate precursor of B.

Code (cpp)

Method One: breadth-first (cpp)

#include <stdio.h>

#include <vector>
#include <queue>

struct GraphNode{
	int label;
	std::vector<GraphNode *> neighbors;
	GraphNode(int x) : label(x) {};
};

class Solution {
public:
    bool canFinish(int numCourses,
		std::vector<std::pair<int, int> >& prerequisites) {
		std::vector<GraphNode*> graph;
		std::vector<int> degree;
		for (int i = 0; i < numCourses; i++){
			degree.push_back(0);
			graph.push_back(new GraphNode(i));
		}
		for (int i = 0; i < prerequisites.size(); i++){
			GraphNode *begin = graph[prerequisites[i].second];
			GraphNode *end = graph[prerequisites[i].first];
			begin->neighbors.push_back(end);
			degree[prerequisites[i].first]++;
		}		
		std::queue<GraphNode *> Q;
		for (int i = 0; i < numCourses; i++){
			if (degree[i] == 0){
				Q.push(graph[i]);
			}
		}
		while(!Q.empty()){
			GraphNode *node = Q.front();
			Q.pop();
			for (int i = 0; i < node->neighbors.size(); i++){
				degree[node->neighbors[i]->label]--;
				if (degree[node->neighbors[i]->label] == 0){
					Q.push(node->neighbors[i]);
				}
			}
		}		
		for (int i = 0; i < graph.size(); i++){
			delete graph[i];
		}		
		for (int i = 0; i < degree.size(); i++){
			if (degree[i]){
				return false;
			}
		}
		return true;
    }
};

int main(){	
	std::vector<std::pair<int, int> > prerequisites;
	prerequisites.push_back(std::make_pair(1, 0));
	prerequisites.push_back(std::make_pair(2, 0));
	prerequisites.push_back(std::make_pair(3, 1));
	prerequisites.push_back(std::make_pair(3, 2));
	Solution solve;
	printf("%d\n", solve.canFinish(4, prerequisites));	
	return 0;
}

Method two: depth-first (CPP)

#include <stdio.h>

#include <vector>
struct GraphNode{
	int label;
	std::vector<GraphNode *> neighbors;
	GraphNode(int x) : label(x) {};
};
//节点状态,-1没有访问过,0代表正在访问,1代表已完成访问
bool DFS_graph(GraphNode *node, std::vector<int> &visit){
	visit[node->label] = 0;
	for (int i = 0; i < node->neighbors.size(); i++){
		if (visit[node->neighbors[i]->label] == -1){
			if (DFS_graph(node->neighbors[i], visit) == 0){
				return false;
			}
		}
		else if (visit[node->neighbors[i]->label] == 0){
			return false;
		}
	}
	visit[node->label] = 1;
	return true;
}

class Solution {
public:
    bool canFinish(int numCourses,
		std::vector<std::pair<int, int> >& prerequisites) {
		std::vector<GraphNode*> graph;
		std::vector<int> visit;
		for (int i = 0; i < numCourses; i++){
			graph.push_back(new GraphNode(i));
			visit.push_back(-1);
		}
		//创建图,连接图的顶点
		for (int i = 0; i < prerequisites.size(); i++){
			GraphNode *begin = graph[prerequisites[i].second];
			GraphNode *end = graph[prerequisites[i].first];
			begin->neighbors.push_back(end);
		}
		for (int i = 0; i < graph.size(); i++){
			//如果节点没有访问过,进行DFS,如果DFS遇到环,返回无法完成课程。
			if (visit[i] == -1 && !DFS_graph(graph[i], visit)){
				return false;
			}
		}
		for (int i = 0; i < numCourses; i++){
			delete graph[i];
		}
		return true;//返回可以完成
    }
};

int main(){	
	std::vector<std::pair<int, int> > prerequisites;
	prerequisites.push_back(std::make_pair(1, 0));
	prerequisites.push_back(std::make_pair(2, 0));
	prerequisites.push_back(std::make_pair(3, 1));
	prerequisites.push_back(std::make_pair(3, 2));
	Solution solve;
	printf("%d\n", solve.canFinish(4, prerequisites));
	return 0;
}

Reference material

Class Schedule

Published 151 original articles · won praise 47 · Views 230,000 +

Guess you like

Origin blog.csdn.net/e891377/article/details/103671001