Data Structure Learning (C ++)-Figure [3] (undirected graph) (top)

Source: Internet
Author: User
Tags getv

If you draw images on paper or just give a demonstration of the graph, most people will choose an undirected graph. However, in a computer, an undirected graph is stored as a directed graph-two directed edges are saved. In fact, when we talk about "undirected", we just ignore the direction-draw a line on the paper, and the line "broken" appears, not from one end to the other?

Undirected graphs have several unique concepts: connected components, closed nodes, and minimal spanning trees. The following describes how to perform basic operations on undirected graph classes.

Undirected graph type

Template <class name, class Dist, class mem>

Class graph: Public Network <name, DIST, Mem>

{

Public:

Graph (){}

Graph (Dist maxdist): Network <name, DIST, Mem> (maxdist ){}

Bool inserte (name V1, name V2, DIST cost)

{

If (Network <name, DIST, Mem >:: inserte (V1, V2, cost ))

Return Network <name, DIST, Mem >:: inserte (V2, V1, cost );

Return false;

}

};

It is very easy to add a reverse edge when adding an edge.

Connected Component

This is unique to undirected graphs. Directed Graphs are much more complex (strong, single, and weak connections), because they can be used to access the edges of an undirected graph, the edges of a directed graph seem to fall into the bottomless abyss and they will no longer be able to climb. With DFS, the algorithm for determining the connected component becomes very simple-you can call DFS for each unaccessed vertex.

Void components ()

{

Visited = new bool [vnum ()]; int I, j = 0;

For (I = 0; I <vnum (); I ++) visited [I] = false;

Cout <"components:" <Endl;

For (I = 0; I <vnum (); I ++)

{

If (! Visited [I]) {cout <'(' <++ j <')'; DFS (I); cout <Endl ;}

}

Delete [] visited;

}

The simhei part is the core.

Shut down nodes

The lower definition is a way for people to understand things, because concepts allow people to distinguish things-there is an absolute movement and a relatively static Philosophical Viewpoint about it (river water is always flowing, but the Yangtze River is also called the Yangtze river. Do you remember the famous "it is impossible to step into the same river ?) Therefore, whether an accurate concept is often a sign of the degree of development of a discipline, and whether an accurate definition reflects a person's thinking ability. There is only one reason for talking so much nonsense. I have no idea what it is called "shutting down nodes". The reference books are limited and cannot be carefully studied. If there is a misunderstanding, I hope to correct it.

Strictly speaking: if you delete a vertexOne connected componentIt is divided into two or more connected components and called the vertex as a critical node. -- Although it is not mentioned that the graph must be undirected, the connected component is already an undirected graph by default.

Yin said this: In an undirected graph ,...... (The remaining version is the same as the strict version ).

The problem arises. Can a non-connected graph be used to discuss whether a node is included? Can we say that a connected component contains nodes? Unfortunately, after the strict version leaves this problem, the algorithm given later follows the connected graph. In this way, the result is wrong when the graph is not connected. Yin edition is even more slippery. It only outputs the reconnection component and does not output the nodes. Although it assumes that the graph is connected, it does not have the connectivity judgment.

After turning over discrete mathematics, I couldn't find any "Close nodes", but only "cut points". It is like this: an undirected connected graph. If a vertex is changed to a non-connected graph after it is deleted, this vertex is called a cut point. When the "cut point" is "Shut down node", the algorithm should at least determine whether to connect? But the books are connected directly ......

I will not elaborate on Algorithms in books. In the following example, you can output the "nodes" of each connected component (I don't know if this can be called ). Dfn stores the access numbers of each vertex. Low is the smallest access number of each child of a non-leaf vertex on the tree that can be reached through the back edge. Considering the sides pointing to the two sides as the back side does not affect the judgment. Therefore, you do not have to differentiate them. Yin versions are clearly differentiated, which is a superfluous element. In this way, if (low [N]> = dfn [I]) cout <getv (I);> = in the judgment of this output node is very embarrassing, because it can only be equal to or not greater. Note that the root of the Spanning Tree (the starting point of DFS) is determined separately.

Void articul ()

{

Dfn = new int [vnum ()]; Low = new int [vnum ()]; int I, j = 0, N;

For (I = 0; I <vnum (); I ++) dfn [I] = low [I] = 0; // Initialization

For (I = 0; I <vnum (); I ++)

{

If (! Dfn [I])

{

Cout <'(' <+ + j <')'; dfn [I] = low [I] = COUNT = 1;

If (n = nextv (I ))! =-1) articul (n); bool out = false; // access the root of the tree

While (n = nextv (I, n ))! =-1)

{

If (dfn [N]) continue;

If (! Out) {cout <getv (I); out = true;} // there are more than one child in the root of the tree.

Articul (n); // access other children

}

Cout <Endl;

}

}

Delete [] dfn; Delete [] low;

}

 

PRIVATE:

Void articul (int I)

{

Dfn [I] = low [I] = ++ count;

For (INT n = nextv (I); n! =-1; n = nextv (I, n ))

{

If (! Dfn [N])

{

Articul (N );

If (low [N] <low [I]) low [I] = low [N];

If (low [N]> = dfn [I]) cout <getv (I); // only possible =

}

Else if (dfn [N] <low [I]) low [I] = dfn [N]; // return edge judgment

}

}

Int * dfn, * low, count;

Related Article

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.