Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to realize the DFS and BFS of C++

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly explains "how to realize the DFS and BFS of C++". The content in the article is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "how to achieve the DFS and BFS of C++".

DFS and BFS

For nonlinear structures, traversal will be a problem in the first place. Like the traversal of a binary tree, there are depth-first search (DFS) and breadth-first search (BFS). The difference is that each vertex in the graph has no relationship between ancestors and descendants, so the pre-order, middle order and post-order are no longer meaningful. Following the traversal of a binary tree, you can easily complete DFS and BFS, but note that there may be loops in the diagram, so you must mark the visited vertices.

The most basic directed weighted network

# ifndef Graph_H # define Graph_H # include # include using namespace std; # include "Graphmem.h" template 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

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report