[Topology Sorting] Job Scheduling in the workshop and Topology workshop scheduling

Source: Internet
Author: User

[Topology Sorting] Job Scheduling in the workshop and Topology workshop scheduling
1. DFS(Deep Priority Search)

Depth-First-Search is a Search algorithm. It traverses Tree nodes in depth along the tree, and searches tree branches as deep as possible. When all the edges of node v have been explored, the search will trace back to the Start Node of the edge of node v. This process continues until all nodes that can be reached from the source node are found. If no node is found, select one of the nodes as the source node and repeat the above process until all nodes are accessed.

Deep priority search is a classic algorithm in graph theory. The deep priority search algorithm can be used to generate the corresponding topological sorting table of the target graph. the topological sorting table can be used to easily solve many related graph problems, such as the maximum path. Generally, we use the heap data structure to help implement the DFS algorithm.

Steps for depth-first graph Traversal Algorithms:

1. Access vertex v;

2. traverse the graph depth first from the inaccessible neighboring contacts of v until all vertices with the same path with v are accessed;

3. If there are still unaccessed vertices in the graph, start from an unaccessed vertex and re-traverse the depth first until all vertices in the graph are accessed.

The preceding description may be abstract. For example:

After a certain vertex v starts in the access graph, DFS starts from v and accesses any of its adjacent vertex w1. Then, it starts from w1, access the vertex w2 that is adjacent to w1 but has not yet been accessed; then start from w2 and perform similar access ,... This continues until the vertex u is accessed by all adjacent vertices.

Next, let's go back to the previously accessed vertex to see if there are any other unaccessed vertex. If yes, access this vertex, and then start from this vertex for access similar to the aforementioned; if not, return to the search. Repeat the above process until all vertices in the connected graph are accessed.

2. BFS (breadth-first search)

Breadth-First-Search is a graphic Search algorithm. Simply put, BFS traverses the node of the tree (graph) from the root node along the width of the tree (graph. If all nodes are accessed, the algorithm is aborted. Generally, the queue data structure is used to help implement the BFS algorithm.

Algorithm steps:

1. First, add the root node to the queue.

2. Retrieve the first node from the queue and check whether it is the target.

    • If the target is found, the search is completed and the result is returned.
    • Otherwise, add all the unverified direct subnodes to the queue.

3. If the queue is empty, it indicates that the entire graph has been checked-that is, the graph has no target to be searched. End the search and return "the target cannot be found ".

4. Repeat Step 2.

3. topological sorting

  Algorithm idea:

1. Select a vertex without a direct precursor in the AOV network and output it;

2. Delete the vertex and delete all directed edges it emits;

3. Repeat the preceding steps

◆ All vertices have been output, and the topological sequence is formed. The topological sorting is completed;

◆ There are still unoutput vertices in the graph, but the processing cycle has exceeded. This shows that there are still some vertices in the graph. They all have a direct precursor and no longer find any vertices without the precursor. At this time, the AOV network must have a directed ring.

4. Topology Sorting by depth-first or breadth-first traversal.

Applications:

1. Determine whether a directed graph has a loop. This is the longest path of the directed graph.

2. queuing

 

The following is an example of queuing:

The question requirements are as follows:

The procedure is as follows:

1 # include <iostream> 2 # include <queue> 3 # include <stack> 4 using namespace std; 5 void breadth_sort (int ** neighbors, int count, queue <int> & topological_order); 6 void depth_sort (int ** neighbors, int count, stack <int> & topological_order); 7 void trim (int ** neighbors, int v, bool * visited, stack <int> & topological_order); 8 int main () 9 {// in order to test the width and depth, the program outputs 10 int number, com at the same time in both sorts; 11 int task_numbe R; 12 int ** array; 13 int * B; 14 queue <int> breadth_order; 15 stack <int> depth_order; 16 cin> task_number; 17 for (int I = 0; I <task_number; I ++) // The number of tasks required by the question is 18 {19 cin> number> com; 20 array = new int * [number + 1]; 21 B = new int [number + 1]; 22 for (int k = 0; k <= number; k ++) 23 {24 array [k] = new int [number + 1]; 25 B [k] = 0; 26} 27 for (int m = 0; m <= number; m ++) 28 for (int n = 0; n <= number; n ++) // The Matrix initializes 29 array [m] [n] = 0; 30 for (int j = 0; j <com; j ++) 31 {32 int p, q; 33 cin> p> q; 34 array [p] [B [p] = q; // Add a successor 35 B [p] ++; // Add a successor; Value: + 136} 37 breadth_sort (array, number, breadth_order ); // sort by breadth 38 while (breadth_order.size ()! = NULL) 39 {40 cout <breadth_order.front () <''; 41 breadth_order.pop (); 42} 43 cout <endl; 44 depth_sort (array, number, depth_order ); // deep sorting 45 while (depth_order.size ()! = NULL) 46 {47 cout <depth_order.top () <''; 48 depth_order.pop (); 49} 50 cout <endl; 51} 52 return 0; 53} 54 void breadth_sort (int ** neighbors, int count, queue <int> & topological_order) 55 {// The breadth is prioritized, with no prior output, 56 int v; 57 int * predecessor_count = new int [count + 1]; 58 for (v = 1; v <= count; v ++) predecessor_count [v] = 0; 59 for (v = 1; v <= count; v ++) 60 for (int I = 0; neighbors [v] [I]! = 0; I ++) {61 predecessor_count [neighbors [v] [I] ++; 62} 63 queue <int> ready_to_process; 64 for (v = 1; v <= count; v ++) 65 if (predecessor_count [v] = 0) 66 ready_to_process.push (v); 67 while (! Ready_to_process.empty () {68 v = ready_to_process.front (); 69 topological_order.push (v); 70 for (int j = 0; neighbors [v] [j]! = 0; j ++) {71 predecessor_count [neighbors [v] [j] --; 72 if (predecessor_count [neighbors [v] [j] = 0) 73 ready_to_process.push (neighbors [v] [j]); 74} 75 ready_to_process.pop (); 76} 77} 78 void depth_sort (int ** neighbors, int count, stack <int> & topological_order) 79 {// deep priority sorting, no first output of the precursor, using stack 80 bool * visited = new bool [count + 1]; 81 int v; 82 for (v = 1; v <= count; v ++) visited [v] = false; 83 for (v = 1; v <= count; v ++) 84 if (! Visited [v]) 85 recursive_depth_sort (neighbors, v, visited, topological_order); 86} 87 88 void merge (int ** neighbors, int v, bool * visited, stack <int> & topological_order) 89 {90 visited [v] = true; 91 for (int I = 0; neighbors [v] [I]! = 0; I ++) {92 93 if (! Visited [neighbors [v] [I]) 94 labels (neighbors, neighbors [v] [I], visited, topological_order); 95} 96 topological_order.push (v); 97}

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.