Now, the tricky part is that almost everywhere DFS algorithm is given in general form, and on his forums advised to first use on unweighted graphs. In this regard, the beginner can easily get confused. At the same time, the algorithm is implemented DFS great and weighted graphs, allowing you to find a long path, and restore itself put.Realizatsiya algorithm DFS. In general, the implementation of the algorithm in C + + as follows: Example 1. The general form of the algorithm DFS. A vector>> g; 2 vector used; 3 void dfs (int v) 4 5 used v = true; 6 for (size_t i = 0; i As I mentioned, this implementation is recursive. That is the function at some point calls itself. In our example, the function calls itself at line 9.
Approximately the same DFS implementation can be found in Wikipedia (reference). It differs only in the structure of the container that contains a graph (there is presented an option for unweighted graph), and use the iterator in the loop for (we'll do a variable of type size_t). Now consider the algorithm in detail. I rely on some knowledge of the reader sintakisisa C / C + + and will not explain the very basic things. The vector g contains a description of the graph. For each vertex v, there is a nested vector of pairs (pair), where the first number of couples - related peak, while the second number of pairs - the edge length, ie distance between vertex v and vertex whose number is recorded in the first element of the pair.
Approximately the same DFS implementation can be found in Wikipedia (reference). It differs only in the structure of the container that contains a graph (there is presented an option for unweighted graph), and use the iterator in the loop for (we'll do a variable of type size_t). Now consider the algorithm in detail. I rely on some knowledge of the reader sintakisisa C / C + + and will not explain the very basic things. The vector g contains a description of the graph. For each vertex v, there is a nested vector of pairs (pair), where the first number of couples - related peak, while the second number of pairs - the edge length, ie distance between vertex v and vertex whose number is recorded in the first element of the pair.

Posted by: |