Introduction to algorithms Chapter 1 Basic Algorithms of graph 22nd China Unicom Branch

Source: Internet
Author: User
I. Summary

Definition:

 

Ii. Code
# Include <iostream> using namespace STD; # define N 10 # define white 0 # define gray 1 # define black 2 // side node Structure struct edge {int start; // int end of the directed graph; // edge of the directed graph * Next; // point to the next edge int type of the same starting point; // edge type of the edge (INT s, int e): start (s), end (E), next (null) {}}; // vertex node Structure struct vertex {int ID; edge * head; // point to the next edge int color starting from this vertex; // The vertex color vertex * P; // point to the parent node int D, F of the traversal result; // The first detected time and the end time of the check vertex (int I): Head (null), color (white), P (n Ull), D (0x7fffffff), ID (I) {}}; // graph structure struct graph {vertex * V [n + 1]; // n vertices graph () {int I; for (I = 1; I <= N; I ++) V [I] = new vertex (I );}~ Graph () {int I; for (I = 1; I <= N; I ++) delete V [I] ;}}; int time = 0; bool flag = 0; int sort [n + 1] = {n}; // insert edge void insertedge (graph * g, edge * E) {// if no edge with the same starting point exists (G-> V [E-> Start]-> head = NULL) g-> V [E-> Start]-> head = E; // If yes, add it to the linked list and sort it in ascending order, it is easy to re-query the insert of else {// linked list. Edge * e1 = G-> V [E-> Start]-> head, * e2 = e1; while (E1 & E1-> end <e-> end) {e2 = e1; e1 = e1-> next ;} if (E1 & E1-> end = e-> end) return; If (E1 = e2) {e-> next = e1; g-> V [E-> Start]-> head = E;} else {e2-> next = E; e-> next = e1 ;}}} // transpose graph * reverse (graph * g) {graph * ret = new graph; int I; // traverse each edge in graph G, starting from the end point, end with the starting point and add it to the ret of The New Graph for (I = 1; I <= N; I ++) {edge * E = G-> V [I]-> head; while (e) {edge * E = new edge (e-> end, e-> Start ); insertedge (Ret, e); E = e-> next;} return ret;} // access a vertex void dfs_visit (graph * g, vertex * U) {// output if (FLAG) cout <char ('P' + U-> ID) <''during the first access ''; // set u to black U-> color = gray; // add value to the global variable time ++; // record the new value of time as the discovery time u-> d = time; // check each vertex adjacent to u vvertex * V; edge * E = u-> head; while (e) {v = G-> V [E-> end]; // If the vertex is white if (V-> color = white) {// recursively access the vertex v-> P = u; dfs_visit (G, V); // tree edge e-> type = 1 ;} else if (V-> color = gray) {// reverse side e-> type = 2;} else if (V-> color = black) {// forward edge if (U-> d <v-> d) E-> type = 3; // cross edge elsee-> type = 4 ;} E = e-> next;} // after all edges starting from u are searched, set u to black U-> color = black; // record the completion time in time ++ of F [u]; U-> F = time; // Save the result in the ascending to smallest order of F in the SORT array if (flag = 0) {sort [sort [0] = u-> ID; sort [0] -- ;}/// depth-first search void DFS (graph * g) {int I; // initialize for (I = 1; I <= N; I ++) {G-> V [I]-> id = I; G-> V [I]-> color = white; g-> V [I]-> P = NULL;} // time stamp initialization time = 0; // retrieves vertices in V in sequence. When a white vertex is found, call dfs_visit to access the vertex for (I = 1; I <= N; I ++) {Int J; // The first time is in the normal order if (flag = 0) j = I; // The second time is in the order of F from the largest to the smallest. This order is stored in the SORT array for the first DFS time. Else J = sort [I]; // when a white vertex is found, call dfs_visit to access the vertex if (G-> V [J]-> color = white) {dfs_visit (G, g-> V [J]); If (FLAG) cout <Endl ;}}void strongly_connected_component (graph * g) {// first DFS, calculate the fdfs (G) of each vertex; // transpose, compute GT graph * g2 = reverse (g); // The first DFS is slightly different from the second DFS, use flag to differentiate the DFS with flag = 1; // for the second time, follow the order of F from large to small DFS (G2 );} /* q s v w s q w q t y q t x z x u y r u */INT main () {// construct an Empty Graph graph * g = new graph; edge * E; // input edge int I; char start, end; for (I = 1; I <= 14; I ++) {CIN> Start> end; E = new edge (START-'P', end-'P'); insertedge (G, e); // undirected graph, add two edges // E = new edge (end, start); // insertedge (G, e );} // calculate the strong Unicom component strongly_connected_component (g); Return 0 ;}

 

Iii. Exercise 22.5-1

Unchanged or reduced

 

22.5-2

First DFS result:

R u q t y x z s v w

G transpose result:

Q: YR: S: Q wt: Qu: RV: SW: q vx: t zy: r t uz: X result of the second DFS: ruq y TX Zs W v 22.5-3 error. See the 2nd floor. 22.5-5 Introduction to Algorithms
22.5-5 O (V + E) Find the branch graph of the directed graph 22.5-6 to be solved. The question doesn't quite understand. I found an answer online, mainly because the fourth step doesn't understand it. The requested figure is G2:
Step 1: Use the algorithm in 22.5 to calculate all strongly connected branches
Step 2: Use the 22.5 neutralization algorithm to calculate the branch graph G | SCC
Step 3: Initialize the edge set of G2 to be empty
Step 4: Construct the vertex in each strongly connected branch into a ring. For example, if the strongly connected branch A contains the ea1, ea2, ea3, and ea4 vertices, add edge ea1-> ea2, ea2-> ea3, ea3-> ea4 to G2, ea4-> ea1
Step 5: add the edge of the branch graph to G2. For example, if the branch graph contains an edge from strongly connected branch a to strongly connected branch B, an edge from EA to EB is added to G2. Where EA is any vertex in A and EB is any vertex in B.
Explanation:
Strongly Connected branches:
Branch chart:
G and G2 have the same strongly connected branches ==> the vertices of G2 are the same as those of G.
G has the same branch chart as G2 ==> G2 has at least G | edge ==> step5 in SCC
G2 has the smallest edge ==> with the least edge of a strongly connected graph is a ring, the number of edges and vertex number of the same ==> STEP422.5-7STEP1: use the algorithm in 22.5-5 to obtain the SCC. Step 2: Calculate the topological sequence (V1, V2 ,......, VK) Step 3: If an edge (V1, V2), (V2, V3),... exists in the edge SCC ),......, (Vk-1, VK), then graph G is semi-connected.

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.