6. Application 2 of the C ++ diagram DFS and BFS

Source: Internet
Author: User
Tags getv

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

 
 
  1. #ifndef Graph_H   
  2. #define Graph_H   
  3.  
  4. #include     
  5. #include     
  6. using namespace std;   
  7. #include "Graphmem.h"   
  8.  
  9. template <class name, class dist, class mem>   
  10. class Network   
  11. {   
  12. public:   
  13. Network() {}   
  14. Network(dist maxdist) { data.NoEdge = maxdist; }   
  15. ~Network() {}   
  16. bool insertV(name v) { return data.insertV(v); }   
  17. bool insertE(name v1, name v2, dist cost) { return data.insertE(v1, v2, cost); }   
  18. name& getV(int n) { return data.getV(n); }   
  19. int nextV(int m, int n = -1) { return data.nextV(m, n); }   
  20. int vNum() { return data.vNum; }   
  21. int eNum() { return data.eNum; }   
  22. protected:   
  23. bool* visited;   
  24. static void print(name v) { cout << v; }   
  25. private:   
  26. mem data;   
  27. };   
  28. #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

 
 
  1. Public:
  2. VoidDFS (Void(* Visit) (name v) = print)
  3. {
  4. Visited =New Bool[VNum ()];
  5. For(IntI = 0; I <vNum (); I ++) visited [I] =False;
  6. DFS (0, visit );
  7. Delete[] Visited;
  8. }
  9. Protected:
  10. VoidDFS (IntI,Void(* Visit) (name v) = print)
  11. {
  12. Visit (getV (I); visited [I] =True;
  13. For(IntN = nextV (I); n! =-1; n = nextV (I, n ))
  14. If(! Visited [n]) DFS (n, visit );
  15. }
  16. Public:
  17. VoidBFS (IntI = 0,Void(* Visit) (name v) = print)// N no cross-border check 
  18. {
  19. Visited =New Bool[VNum ()]; queue <Int>;IntN;
  20. For(N = 0; n <vNum (); n ++) visited [n] =False;
  21. Visited [I] =True;
  22. While(I! =-1)// This judgment may be useless. 
  23. {
  24. Visit (getV (I ));
  25. For(N = nextV (I); n! =-1; n = nextV (I, n ))
  26. If(! Visited [n]) {a. push (n); visited [n] =True;}
  27. If(A. empty ())Break;
  28. I = a. front (); a. pop ();
  29. }
  30. Delete[] Visited;
  31. }

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

 
 
  1. #include     
  2. using namespace std;   
  3. #include "Graph.h"   
  4. int main()   
  5. {   
  6. Network<char, int, LinkedList<char, int> > a;   
  7. a.insertV('A'); a.insertV('B');   
  8. a.insertV('C'); a.insertV('D');   
  9. a.insertE('A', 'B', 1); a.insertE('A', 'C', 2);   
  10. a.insertE('B', 'D', 3);   
  11. cout << "DFS: "; a.DFS(); cout << endl;   
  12. cout << "BFS: "; a.BFS(); cout << endl;   
  13. return 0;   
  14. }  

To be honest, this class is really not very convenient to use. But it is good to explain the problem.

  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.