6. One of the basic storage methods for C ++ graph applications

Source: Internet
Author: User

I am afraid that the graph application isC ++All data structures are the most extensive, but it is doomed that there is nothing to talk about when talking about "data structure graphs"-the most important thing about graphs is algorithms, in addition, a considerable number of them are very professional, and generally people will not be able to access them. Relatively speaking, the structure is very lightweight. You can see that there are very few operations on the elements in the graph, far from the list of "interfaces" listed in a single-chain table ". -- If a structure is complex, operations that can be defined exactly are limited.

I will introduce the graph application in detail from the following aspects: basic storage method, DFS and BFS, undirected graph, Minimum Spanning Tree, shortest path, and active network (AOV and AOE. This is the first article in this series. It mainly introduces the basic storage methods of graphs.

Basic Storage Method

In any case, you must first save the image. Do not list many methods on the books. There is only one-the adjacent matrix. If the matrix is sparse, you can use the cross-linked list to store the matrix (see the sparse matrix of C ++ Data Structure Learning ). If we only relate to the relationship of rows, It is the adjacent table (the out-of-edge table). On the contrary, we only care about the relationship between columns, that is, the inverse adjacent table (The in-edge table ).

The following two storage methods are provided.

 
 
  1. # Ifndef Graphmem_H 
  2. # Define Graphmem_H 
  3.  
  4. # Include  
  5. # Include  
  6. Using NamespaceStd;
  7.  
  8. Template<ClassName,ClassDist,ClassMem>ClassNetwork;
  9.  
  10. Const IntMaxV = 20;// Maximum number of nodes 
  11. Template<ClassName,ClassDist>
  12. ClassAdjMatrix
  13. {
  14. Friend ClassNetwork >;
  15. Public:
  16. AdjMatrix (): vNum (0), eNum (0)
  17. {
  18. Vertex =NewName [maxV]; edge =NewDist * [maxV];
  19. For(IntI = 0; I <maxV; I ++) edge [I] =NewDist [maxV];
  20. }
  21. ~ AdjMatrix ()
  22. {
  23. For(IntI = 0; I <maxV; I ++)Delete[] Edge [I];
  24. Delete[] Edge;Delete[] Vertex;
  25. }
  26. BoolInsertV (name v)
  27. {
  28. If(Find (v ))Return False;
  29. Vertex [vNum] = v;
  30. For(IntI = 0; I <maxV; I ++) edge [vNum] [I] = NoEdge;
  31. VNum ++;Return True;
  32. }
  33. BoolInsertE (name v1, name v2, dist cost)
  34. {
  35. IntI, j;
  36. If(V1 = v2 |! Find (v1, I) |! Find (v2, j ))Return False;
  37. If(Edge [I] [j]! = NoEdge)Return False;
  38. Edge [I] [j] = cost; eNum ++;Return True;
  39. }
  40. Name & getV (IntN ){ReturnVertex [n];}// No cross-border check 
  41. IntNextV (IntM,IntN)// Returns the first adjacent vertex after vertex n of vertex m.-1 is not returned. 
  42. {
  43. For(IntI = n + 1; I <vNum; I ++)If(Edge [m] [I]! = NoEdge)ReturnI;
  44. Return-1;
  45. }
  46. Private:
  47. IntVNum, eNum;
  48. Dist NoEdge, ** edge; name * vertex;
  49. BoolFind (ConstName & v)
  50. {
  51. For(IntI = 0; I <vNum; I ++)If(V = vertex [I])Return True;
  52. Return False;
  53. }
  54. BoolFind (ConstName & v,Int& I)
  55. {
  56. For(I = 0; I <vNum; I ++)If(V = vertex [I])Return True;
  57. Return False;
  58. }
  59. };
  60.  
  61. Template<ClassName,ClassDist>
  62. ClassShortlist
  63. {
  64. Friend ClassNetwork >;
  65. Public:
  66. Partition list (): vNum (0), eNum (0 ){}
  67. ~ Partition list ()
  68. {
  69. For(IntI = 0; I <vNum; I ++)DeleteVertices [I]. e;
  70. }
  71. BoolInsertV (name v)
  72. {
  73. If(Find (v ))Return False;
  74. Vertices. push_back (vertex (v,NewList ));
  75. VNum ++;Return True;
  76. }
  77. BoolInsertE (ConstName & v1,ConstName & v2,ConstDist & cost)
  78. {
  79. IntI, j;
  80. If(V1 = v2 |! Find (v1, I) |! Find (v2, j ))Return False;
  81. For(List : Iterator iter = vertices [I]. e-> begin ();
  82. Iter! = Vertices [I]. e-> end () & iter-> vID <j; iter ++ );
  83. If(Iter = vertices [I]. e-> end ())
  84. {
  85. Vertices [I]. e-> push_back (edge (j, cost); eNum ++;Return True;
  86. }
  87. If(Iter-> vID = j)Return False;
  88. Vertices [I]. e-> insert (iter, edge (j, cost); eNum ++;Return True;
  89. }
  90. Name & getV (IntN ){ReturnVertices [n]. v ;}// No cross-border check 
  91. IntNextV (IntM,IntN)// Returns the first adjacent vertex after vertex n of vertex m.-1 is not returned. 
  92. {
  93. For(List : Iterator iter = vertices [m]. e-> begin ();
  94. Iter! = Vertices [m]. e-> end (); iter ++)If(Iter-> vID> n)ReturnIter-> vID;
  95. Return-1;
  96. }
  97.  
  98. Private:
  99. BoolFind (ConstName & v)
  100. {
  101. For(IntI = 0; I <vNum; I ++)If(V = vertices [I]. v)Return True;
  102. Return False;
  103. }
  104. BoolFind (ConstName & v,Int& I)
  105. {
  106. For(I = 0; I <vNum; I ++)If(V = vertices [I]. v)Return True;
  107. Return False;
  108. }
  109. StructEdge
  110. {
  111. Edge (){}
  112. Edge (IntVID, dist cost): vID (vID), cost (cost ){}
  113. IntVID;
  114. Dist cost;
  115. };
  116. StructVertex
  117. {
  118. Vertex (){}
  119. Vertex (name v, list * E): v (v), e (e ){}
  120. Name v;
  121. List * E;
  122. };
  123. IntVNum, eNum;
  124. Vector Vertices;
  125. };
  126.  
  127. # Endif 

This implementation is very simple, but it should be able to meet the requirements described later. There is nothing to do now. Don't worry. In the next article, we will describe the DFS and BFS of the graph.

  1. Classical 4-lecture C ++ sorting
  2. Common c ++ programming tools
  3. 50 tips for C ++ beginners
  4. The 20 most basic rules of c ++
  5. Programmers must read the summary of c ++ pen questions

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.