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. In the previous article, we introduced the basic storage methods.DFSAndBFS.
DFS and BFS
For non-linear structures, traversal will first become a problem. Like binary tree traversal, a graph also has two types: Deep preference search (DFS) and breadth preference search (BFS. The difference is that each vertex in the graph does not have the relationship between the ancestor and the descendant. Therefore, the pre-order, middle-order, and post-order are no longer meaningful. Similar to the traversal of a binary tree, you can easily complete DFS and BFS, but note that there may be loops in the graph. Therefore, you must mark the accessed vertices.
The most basic redirection Network
- #ifndef Graph_H
- #define Graph_H
-
- #include
- #include
- using namespace std;
- #include "Graphmem.h"
-
- template <class name, class dist, class mem>
- class Network
- {
- public:
- Network() {}
- Network(dist maxdist) { data.NoEdge = maxdist; }
- ~Network() {}
- bool insertV(name v) { return data.insertV(v); }
- bool insertE(name v1, name v2, dist cost) { return data.insertE(v1, v2, cost); }
- name& getV(int n) { return data.getV(n); }
- int nextV(int m, int n = -1) { return data.nextV(m, n); }
- int vNum() { return data.vNum; }
- int eNum() { return data.eNum; }
- protected:
- bool* visited;
- static void print(name v) { cout << v; }
- private:
- mem data;
- };
- #endif
As you can see, a shell is added to the data stored in mem mode. As shown in the figure, logically divided into directed, undirected, weighted, and without permission. The storage structure contains an adjacent matrix and an adjacent table. That is to say, there are eight classes separately. In order to reuse code to the maximum extent, the inheritance relationship is very complicated. However, multi-inheritance is a very annoying thing. What is coverage? What is virtual inheritance? I don't want to spend a lot of time talking about language features. Therefore, I use the storage method as the third template parameter, which saves the trouble of instantiating the Network, however, this can be solved through typedef or shell class, so I will not write it. It is only for everyone to understand that it is best to write special classes when they are actually used. For example, the undirected and non-having matrix graph may lead to a mess of inheritance relationships.
Implementation of DFS and BFS
- Public:
- VoidDFS (Void(* Visit) (name v) = print)
- {
- Visited =New Bool[VNum ()];
- For(IntI = 0; I <vNum (); I ++) visited [I] =False;
- DFS (0, visit );
- Delete[] Visited;
- }
- Protected:
- VoidDFS (IntI,Void(* Visit) (name v) = print)
- {
- Visit (getV (I); visited [I] =True;
- For(IntN = nextV (I); n! =-1; n = nextV (I, n ))
- If(! Visited [n]) DFS (n, visit );
- }
- Public:
- VoidBFS (IntI = 0,Void(* Visit) (name v) = print)// N no cross-border check
- {
- Visited =New Bool[VNum ()]; queue <Int>;IntN;
- For(N = 0; n <vNum (); n ++) visited [n] =False;
- Visited [I] =True;
- While(I! =-1)// This judgment may be useless.
- {
- Visit (getV (I ));
- For(N = nextV (I); n! =-1; n = nextV (I, n ))
- If(! Visited [n]) {a. push (n); visited [n] =True;}
- If(A. empty ())Break;
- I = a. front (); a. pop ();
- }
- Delete[] Visited;
- }
DFS and BFS functions are difficult to write as common as tree traversal methods. This will be seen later, although we use the ideas of DFS and BFS, however, the above functions cannot be used directly. Because the tree information is mainly on the node, and there is information on the edge of the graph.
Test procedure
- #include
- using namespace std;
- #include "Graph.h"
- int main()
- {
- Network<char, int, LinkedList<char, int> > a;
- a.insertV('A'); a.insertV('B');
- a.insertV('C'); a.insertV('D');
- a.insertE('A', 'B', 1); a.insertE('A', 'C', 2);
- a.insertE('B', 'D', 3);
- cout << "DFS: "; a.DFS(); cout << endl;
- cout << "BFS: "; a.BFS(); cout << endl;
- return 0;
- }
To be honest, this class is really not very convenient to use. But it is good to explain the problem.
- Classical 4-lecture C ++ sorting
- Common c ++ programming tools
- 50 tips for C ++ beginners
- The 20 most basic rules of c ++
- Programmers must read the summary of c ++ pen questions