39. (tree, graph, algorithm)
(2 ).
Find a cut point of a directed connected graph. The cut point is defined as: If you remove this vertex and Its Related edge,
The directed graph is no longer connected. It describes the algorithm.
Idea: there is a problem here. By default, strong connectivity is required for graph connectivity. The simplest method is to delete an edge each time and determine whether the graph is connected. If the connection fails, the vertex is considered as a cut point.
The connectivity judgment also adopts an intuitive and simple method, that is, to determine whether each vertex has an inner side and an exclusive side. (Question:Is this intuitive method wrong ?)
/* 39. (tree, graph, algorithm) (2 ). find a cut point of a directed connected graph. The cut point is defined as that if the knots and their related edges are removed, the directed graph is no longer connected and describes the algorithm. */# Include <stdio. h> # define max_vertex_num 20 # define infinity 10000 typedef struct arccell {int adj;} arccell, adjmatrix [partition] [max_vertex_num]; typedef struct mgraph {int vexs [max_vertex_num]; adjmatrix arcs; int vexnum, arcnum;} mgraph; // locate the vertex int locatevex (mgraph g, int v) {for (INT I = 0; I <G. vexnum; I ++) {If (G. vexs [I] = V) return I;} return-1; // means error} void createdn (mgraph & G) // generated Into directed graph {printf ("input the vexnum:"); scanf ("% d", & G. vexnum); printf ("input the arcnum:"); scanf ("% d", & G. arcnum); For (INT I = 0; I <G. vexnum; I ++) {printf ("input the % d Vex:", I); scanf ("% d", & G. vexs [I]) ;}for (INT I = 0; I <G. vexnum; I ++) for (Int J = 0; j <G. vexnum; j ++) g. ARCs [I] [J]. adj = infinity; For (int K = 0; k <G. arcnum; k ++) {int V1, V2, W; printf ("input the arcs Vex and weight:"); scan F ("% d", & V1, & V2, & W); int I = locatevex (G, V1); Int J = locatevex (G, v2); G. ARCs [I] [J]. adj = W ;}// whether the directed graph is strongly connected to bool isconnected (mgraph g) {bool connected = true; For (INT I = 0; I <G. vexnum; I ++) {bool haveconnectedin = false; bool haveconnectedout = false; For (Int J = 0; j <G. vexnum; j ++) {If (G. ARCs [I] [J]. adj <infinity) haveconnectedout = true; If (G. ARCs [J] [I]. adj <infinity) haveconnecte Din = true;} If (haveconnectedout! = True | haveconnectedin! = True) {connected = false; break;} return connected;} // obtain the graph mgraph deleteonevex (mgraph g, int Vex) after the directed graph G removes a vertex and its adjacent edge) {mgraph DG; DG. vexnum = G. vexnum-1; Int J = 0; For (INT I = 0; I <G. vexnum; I ++) {if (I! = Vex) {DG. vexs [J ++] = G. vexs [I] ;}} DG. arcnum = 0; For (INT I = 0; I <G. vexnum; I ++) for (Int J = 0; j <G. vexnum; j ++) if (I! = Vex & J! = Vex) {int v = (I> Vex )? I-1: I; int u = (j> Vex )? J-1: J; DG. ARCs [v] [u]. adj = G. ARCs [I] [J]. adj; DG. arcnum ++;} return DG ;}// find the graph cut void getgutset (mgraph g) {bool isconnect = isconnected (g); If (isconnect = false) {printf ("the graph is not connected. \ n "); return;} int n = 0; If (G. vexnum <1) {printf ("no vex");} else if (G. vexnum = 1) {printf ("cut is % d \ n", G. vexs [0]);} else {for (INT I = 0; I <G. vexnum; I ++) {mgraph DG = deleteonevex (G, I); bool isconnect = isconnected (DG); If (isconnect = false) {printf ("the % d cut Vex is % d \ n", N, G. vexs [I]) ;}}} int main () {mgraph g; createdn (g); getgutset (g); Return 0 ;}
We can see dedicated algorithms on the Internet and are still learning.
[Programming question] Find a cut point for a directed connected graph. The cut point is defined as that if this cut point is removed from its related edge, the directed graph will no longer be connected.