Introduction to Algorithms 22.4 topology sequencing exercises summary (reprint)

Source: Internet
Author: User

22.4-1 gives the node order generated by the algorithm Topological-sort when running on Figure 22-8. All the assumptions here are the same as the practice 22.3-2.

ANSWER:


22.4-2 please give a linear time algorithm, the input of the algorithm is a direction-free graph G = (V, E) and two nodes s and T, the output of the algorithm is the number of simple paths from node s to node T. For example, for a direction-free graph shown in Figure 22-8, there are 4 simple paths from node p to node V, namely POV, PORYV, Posryv, and Psryv. (the subject only requires counting the number of simple paths, without requiring the simple path itself to be enumerated.) )

ANSWER: append a count attribute to the node time, initialize T.time = 1, the other time is 0, the S is the source node for DFS, and once the t is searched, T is immediately black (that is, does not continue to search for the descendants of T). Whenever you end a search for a node, the time property of the node = the sum of the time of all the nodes that the node points to. The last S.time is the number of path bars.

22-8 p to V, last V.time = 1, y.time = 1, r.time = 1, s.time = 1, o.time = 1, p.time.

Solution Two

There is a direction to the g= graph (v,e), and the number of paths between the points s and E is calculated. The problem begins with the point S starting as DFS, getting a Dfs tree of all the points that start from S, but the number of paths needs to be detailed.

Define P (x), which represents the number of paths of points s to X, P (s) = 1, that is, s to itself has a path, the remaining number of all paths is initialized to 0.

Path from S to point X, you must reach the upper node of x, assuming that there are n on the previous node at the end of X, i.e. A1,a2,..., an, by the addition Law, p (x) = P (A1) + p (A2) + ... + p (an), which is a recursive process from top to bottom, a bit similar to dynamic programming.

To sum up, we just get an arbitrary point into the adjacency table (also called the inverse adjacency table), from figure G to get its transpose GT, which holds any point of the upper node. And then recursion from top to bottom, just take advantage of the sequence obtained by topological ordering, because the definition of the topological sort can guarantee the order relationship of nodes, so recursion is effective. The following algorithm steps:

(1) from Figure G get its transpose chart GT to get all points into the adjacency table

(2) Start as DFS with point S and get the topological sequence from point S to point E

(3) In order of this topological sequence, get p value one by one, finally get P (e), that is, the number of paths of S to E

The solid line in the figure is the tree edge, the curve is forward and cross edge, the number of paths from P to V, the recursive process is as follows:

P (P) = 1

P (o) = P (p) = 1

P (s) = P (p) + P (o) = 2

P (r) = P (o) +p (s) = 3

P (Y) = P (r) = 3

P (v) = P (y) +p (o) = 4

Step (1) The code is as follows

Static intGraph_add_edge (structLink_graph *g,intUindexintVindex) {    structLink_edge *e =NULL; structLink_vertex *u = G->v +Uindex; E=malloc(sizeof(structLink_edge)); if(!e)return-1; E->vindex =Vindex; E->type =Edge_none; List_add (&e->node, &u->head); return 0;}structlink_graph* Graph_transpos (structLink_graph *G) {    inti =0; structLink_edge *e =NULL; structLink_vertex *u =NULL; structLink_graph *GT =NULL; GT=malloc(sizeof(structlink_graph)); if(!GT)returnNULL; GT->v =malloc(G->vcount *sizeof(structLink_vertex)); if(! Gt->v) { Free(GT); returnNULL; } GT->vcount = g->Vcount; GT->ecount = g->Ecount;  for(i =0; I < gt->vcount;i++) {u= Gt->v +i; U->vindex =i; Init_list_head (&u->head); }     for(i =0; I < g->vcount;i++) {u= G->v +i; List_for_each_entry (E,&u->Head, node) {Graph_add_edge (GT, E-Vindex, i); }    }    returnGT;}
View Code

Step (2) The code is as follows

voidDfs_visit_topo (structLink_graph *g,structLink_vertex *u,int*color,structList_head *head) {    structLink_edge *le =NULL; Color[u->vindex] =Color_gray; List_for_each_entry (LE,&u->Head, node) {        if(Color[le->vindex] = =color_white) {Dfs_visit_topo (g, G->v + le->Vindex, color, head); Le->type =Edge_tree; }        Else if(Color[le->vindex] = =Color_gray) {Le->type =Edge_back; printf ("G has cycle and cannot topology sort\n"); }} color[u->vindex] =Color_black; List_add (&u->Qnode, head);}intGraph_topo_sort (structLink_graph *g,intSstructList_head *thead) {    inti; int*color; Color=malloc(sizeof(int) * g->vcount);  for(i =0; I < g->vcount;i++) {Color[i]=Color_white; } Dfs_visit_topo (g, G->v +s, color, thead);  Free(color); return 0;}
View Code

Step (3) The code is as follows

intGraph_count_path (structLink_graph *g,intSintend) {    int*P; structList_head thead; structLink_vertex *u = NULL, *ut =NULL; structLink_edge *e =NULL; structLink_graph *GT =NULL; GT=Graph_transpos (G); if(GT = =NULL)return-1; P=malloc(G->vcount *sizeof(int)); memset (P,0, G->vcount *sizeof(int)); P[s]=1; Init_list_head (&thead); Graph_topo_sort (G, S,&thead); printf ("The topology sort from%d is below:\n", s); List_for_each_entry (U,&THEAD, Qnode) {printf ("%4d", u->Vindex); if(U->vindex = =s)Continue; UT= Gt->v + u->Vindex; List_for_each_entry (E,&ut->Head, node) {P[u->vindex] + = p[e->Vindex]; }        if(U->vindex = =end) {printf ("\n\nthere is%d paths from%d to%d\n", P[end], S, end);  Break;    }} link_graph_exit (GT); return 0;}
View Code

22.4-3 gives an algorithm to determine whether a given graph G = (V, E) contains a loop. The algorithm should run at O (V) Orders of magnitude, and with | e| Independent.

ANSWER

We all know that for an infographic, if it can be represented as a tree, then it must not have a loop, and there is | e|=| V|-1, if an edge is added to the tree, it forms a loop, and if one edge is removed from the tree it becomes a forest (note: If it is only limited | e|<| V|-1 It is not necessarily a forest, it may have undirected connected sub-graphs.

for this topic we can do with DFS, whenever the adjacency table of the current node is accessed, if there is an element of an adjacency that has been marked as access state, and this element is not the parent node of the current element , then this diagram is the existence of the loop. The total time cost is O (| e|+| v|), because e<=| V|-1 (If | e|>| V|-1 according to the nature of the non-direction graph, then this non-direction diagram must exist loop, so O (| e|+| v|) =o (| v|)

22.4-4 prove or disprove the following assertion: If there is a loop to figure g, then the number of "bad" edges in figure g that are inconsistent with the generated sequence is the least in the sequence of nodes generated by the algorithm Topological-sort (g).

ANSWER:

22.4-5 performing topological ordering on a direction-free graph G = (V, E) There is also a way to repeat the search for a node with an entry level of 0, output the node, and delete the node and its emitted edges. Please explain how to realize this idea in O (v+e) time. What happens if figure G contains a loop?

ANSWER: First use DFS to record the degree of each node in O (v+e) time. Each node is then updated with each node after it has been deleted. The time to delete the node is O (V), and the update node-in time is O (E), so the total time is O (v+e).

If figure g contains loops, the junction in the loop will be more than 0, and cannot be deleted.

Introduction to Algorithms 22.4 topology sequencing exercises summary (reprint)

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.