I will introduce the basic storage methods, DFS and BFS, undirected graphs, Minimum Spanning Tree, shortest path, and active network (AOV and AOE) in detail.C ++Graph application. We have introduced basic storage methods, DFS and BFS before.Undirected graph.
Undirected graph
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
- {
- public:
- Graph() {}
- Graph(dist maxdist) : Network (maxdist) {}
- bool insertE(name v1, name v2, dist cost)
- {
- if (Network ::insertE(v1, v2, cost))
- return Network ::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;
- }