Sắp xếp tô pô bằng cách sử dụng topological sort using breadth first search (bfs)

14 1 0
Sắp xếp tô pô bằng cách sử dụng   topological sort using breadth first search (bfs)

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

Thông tin tài liệu

OpenGenus IQ: Learn Computer Science ☰    Topological Sort using Breadth First Search (BFS) Join the strongest 💪 computer science community in the World for free Algorithms Graph Algorithms In this article, we have explored how to perform topological sort using Breadth First Search (BFS) along with an implementation We have compared it with Topological sort using Depth First Search (DFS) Let us consider a scenario where a university offers a bunch of courses There are some dependent courses too Example : Machine Learning is dependent on Python and Calculus , CSS dependent on HTML etc  All these dependencies can be documented into a directed graph Now the university wants to decide which courses to offer first so that each student has the necessary prerequisite satisfied for the course Thus , Topological sort comes to our aid and satisfies our need  OpenGenus IQ: Learn Computer Science   All the above dependencies can be represented using a Directed Graph The graph in the above diagram suggests that inorder to learn ML ,Python and Calculus are a prerequisite and similarly HTML is a prerequisite for CSS and CSS for Javascript Hence the graph represents the order in which the subjects depend on each other and the topological sort of the graph gives the order in which they must be offered to students Since the graph above is less complicated than what is expected in  most applications it is easier to sort it topologically by-hand but complex graphs require algorithms to process them hence this post!!  Topological Sorting can be done by both DFS as well as BFS,this post however is concerned with the BFS approach of topological sorting OpenGenus IQ: Learn Computer Science popularly know as Khan's Algorithm   Topological Sort Topological sorting for Directed Acyclic Graph (DAG) is a linear ordering of vertices such that for every directed edge u->v, vertex u comes before v in the ordering Topological Sorting for a graph is not possible if the graph is not a DAG Why specifically for DAG? In order to have a topological sorting the graph must not contain any cycles In order to prove it, let's assume there is a cycle made of the vertices v1,v2,v3,v4  That means there is a directed edge between vi and vi+1 (1>n; vector graph; for(int i=1;i>v1>>v2; g.addEdge(v1, v2); } cout

Ngày đăng: 25/03/2023, 07:21

Tài liệu cùng người dùng

  • Đang cập nhật ...

Tài liệu liên quan