Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Example Analysis of Topology sorting in leetcode

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

Editor to share with you the example analysis of topological sorting in leetcode. I hope you will get something after reading this article. Let's discuss it together.

In essence, it is a top scheduling problem. Top scheduling problem is actually a directed graph traversal problem, so it can be solved by dfs and bfs.

The problem of course selection

Now you have a total of n courses to take, marked 0 to nMel 1.

Some prerequisite courses are required before taking certain courses. For example, to learn course 0, you need to complete course 1, and we use a match to represent them: [0quotient 1]

Given the total number of courses and their prerequisites, determine whether it is possible to complete all courses?

Example 1:

Enter: 2, [[1pc0]]

Output: true

Explanation: there are a total of 2 courses. Before you can study course 1, you need to complete course 0. So it's possible.

Example 2:

Enter: 2, [[1jue 0], [0je 1]]

Output: false

Explanation: there are a total of 2 courses. You need to complete course 0 before you can study course 1, and you should also complete course 1 before learning course 0. It's impossible.

Description:

The prerequisite for input is the graph represented by the edge list, not the adjacency matrix. See the representation of the figure for details.

You can assume that there are no duplicate edges in the input prerequisites.

Tip:

This problem is equivalent to finding out whether a loop exists in a directed graph. If there is a loop, there is no topological sorting, so it is not possible to select all courses for study.

Related knowledge

Topological sorting via DFS-A wonderful video tutorial on Coursera (21 minutes) that introduces the basic concepts of topological sorting.

Topological sorting can also be done through BFS.

DFS's ideas for solving problems:

1, convert the edge list into the form of inverse adjacency matrix

The slice of inverse_ adjunct [I] indicates that all prefix nodes of I

2, the topic can be abstracted to judge whether a directed graph can be topologically sorted (whether it has a ring).

3, the loop starts from each vertex and traverses depth first

A, the current node is marked as 2 (traversing)

B, if the node does not have a prefix node (marked as 1 if the degree is 0)

C, if the node has a prefix node, depth first traversal

D, if all prefix nodes of that node are marked as 1, the node is marked as 1

E, if there is a node that is traversing in the prefix node (2), it has a loop and returns.

Func canFinish (numCourses int, prerequisites [] [] int) bool {inverse_adj:=make ([] [] int,numCourses) for

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 246

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report