6. Application of C ++ diagram 3 undirected graph (1)

Source: Internet
Author: User

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

 
 
  1. template <class name, class dist, class mem>  
  2. class Graph : public Network  
  3. {  
  4. public:  
  5. Graph() {}  
  6. Graph(dist maxdist) : Network  (maxdist) {}  
  7. bool insertE(name v1, name v2, dist cost)  
  8. {  
  9. if (Network ::insertE(v1, v2, cost))  
  10. return Network ::insertE(v2, v1, cost);  
  11. return false;  
  12. }  
  13. }; 

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.

 
 
  1. void components()  
  2. {  
  3. visited = new bool[vNum()]; int i, j = 0;  
  4. for (i = 0; i < vNum(); i++) visited[i] = false;  
  5. cout << "Components:" << endl;  
  6. for (i = 0; i < vNum(); i++)  
  7. {  
  8. if (!visited[i]) { cout << '(' << ++j << ')'; DFS(i); cout << endl; }  
  9. }  
  10. delete []visited;  


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.