Hello. I have a problem with elementary search in depth. I just began to study the graphs and I can not implement. Here is the code itself http://pastebin.com/at0Lrehc Thank you in advance.
# include & lt; iostream & gt;
#Include & lt; Cstdio & gt;
#Include & lt; Cstdlib & gt;
#Include & lt; Cassert & GT;
#Include & lt; Cmath & GT;
#Include & lt; Ctime & gt;
#Include & lt; CString & GT;
#Include & lt; CCType & gt;
#Include & lt; algorithm & gt;
#Include & lt; String & GT;
#Include & lt; vector & gt;
#InClude & lt; Iomanip & gt;
Using Namespace STD;
Vector & lt; vector & lt; int & gt; & gt; g; // Graph
int n; // Number of vertices
Vector & lt; bool & gt; used;
Void DFS (Int V)
{
USED [V] = TRUE;
COUT & LT; & LT; v;
For (vector & lt; int & gt; :: iterator i = g [v] .begin (); i! = G [v] .end (); ++ i)
If (! USED [* i])
DFS (* i);
}
INT MAIN ()
{
n = 4;
int i;
For (i = 1; i & lt; = n; ++ i) {
DFS (I);
}
}
Answer 1, Authority 100%
in the code: It would be nice to fill in the graph with the tops / ribs).
And now a small advice:
If you want to use graphs, it is better to use excellent library from boost – bgl (here Link on the book), in it the search in the depth is already implemented, as well as in width, the path search algorithms and much more!
Well, if you want to implement these algorithms yourself, then, IMHO, it is better to use a normal representation of the graph from BGL, and not this list of adjacency.
p.s. Do not forget about the principle of DRY (Don’t Repeat Yourself)!
UPD :
Here is the code of the example from dfs.cpp
, see the approximate output and code of the Main function, IMHO, very loacious.
# include & lt; boost / config.hpp & gt;
#Include & lt; assert.h & gt;
#Include & lt; iostream & gt;
#Include & lt; vector & gt;
#Include & lt; algorithm & gt;
#Include & lt; Utility & gt;
#Include & lt; boost / graph / adjacency_list.hpp & gt;
#Include & lt; boost / graph / depth_first_search.hpp & gt;
#Include & lt; Boost / Graph / Visitors.hpp & gt;
/ *
This Calculates The Discover Finishing Time.
Sample Output.
Tree EDGE: 0 - & gt; 2.
Tree EDGE: 2 - & gt; 1
Back Edge: 1 - & gt; 1
Tree EDGE: 1 - & gt; 3.
Back Edge: 3 - & gt; 1
Tree EDGE: 3 - & gt; 4
Back Edge: 4 - & gt; 0.
Back Edge: 4 - & gt; 1
Forward or Cross Edge: 2 - & gt; 3.
1 10.
3 8.
2 9.
4 7.
5 6.
* /
Using Namespace Boost;
Using Namespace STD;
TEMPLATE & LT; Class VisitorList & GT;
Struct Edge_categorizer: Public Dfs_Visitor & LT; VisitorList & GT; {
TypeDef DFS_VISTOR & LT; VisitorList & GT; Base;
Edge_categorizer (Const VisitorList & Amp; V = NULL_VISITOR ()): Base (V) {}
TEMPLATE & LT; Class Edge, Class Graph & GT;
Void Tree_Edge (EDGE E, Graph & Amp; G) {
COUT & LT; & LT; "Tree EDGE:" & lt; & lt; Source (E, G) & LT; & LT;
"- & gt;" & lt; & lt; Target (E, G) & LT; & LT; Endl;
Base :: Tree_Edge (E, G);
}
TEMPLATE & LT; Class Edge, Class Graph & GT;
void back_edge (Edge E, Graph & amp; G) {
COUT & LT; & LT; "Back Edge:" & lt; & lt; Source (E, G)
& lt; & lt; "- & gt;" & lt; & lt; Target (E, G) & LT; & LT; Endl;
Base :: Back_Edge (E, G);
}
TEMPLATE & LT; Class Edge, Class Graph & GT;
void forward_or_cross_edge (EDGE E, Graph & Amp; G) {
COUT & LT; & LT; "FORWARD OR CROSS EDGE:" & LT; & LT; Source (E, G)
& lt; & lt; "- & gt;" & lt; & lt; Target (E, G) & LT; & LT; Endl;
Base :: forward_or_cross_edge (E, G);
}
};
TEMPLATE & LT; Class VisitorList & GT;
Edge_categorizer & lt; VisitorList & GT;
Categorize_Edges (Const VisitorList & Amp; V) {
Return Edge_categorizer & lt; VisitorList & GT; (v);
}
int.
Main (int, char * [])
{
Using Namespace Boost;
TypeDef Adjacency_List & LT; & GT; Graph;
GRAPH G (5);
add_edge (0, 2, g);
add_edge (1, 1, g);
add_edge (1, 3, g);
add_edge (2, 1, g);
add_edge (2, 3, g);
add_edge (3, 1, g);
add_edge (3, 4, g);
add_edge (4, 0, g);
add_edge (4, 1, g);
TypeDef Graph_traits & LT; Graph & GT; :: Vertex_Descriptor Vertex;
Typedef Graph_traits & LT; Graph & GT; :: Vertices_Size_Type Size_Type;
STD :: Vector & LT; Size_Type & GT; d (num_vertices (g));
STD :: Vector & LT; Size_Type & GT; f (num_vertices (g));
int t = 0;
Depth_first_Search (G, Visitor (Categorize_Edges (
make_pair (stamp_times (& amp; d [0], t, on_discover_vertex ()),
stamp_times (& amp; f [0], t, on_finish_vertex ())))));
Std :: Vector & LT; Size_Type & gt; :: Iterator I, J;
For (i = d.begin (), j = f.begin (); i! = d.end (); ++ i, ++ j)
COUT & LT; & LT; * i & lt; & lt; "" & lt; & lt; * j & lt; & lt; Endl;
Return 0;
}
Answer 2
# include & lt; iostream & gt;
#Include & lt; vector & gt;
Using Namespace STD;
Vector & lt; bool & gt; WAS; // Vector in which we stored if we were in the top
Vector & lt; vector & lt; int & gt; & gt; Graph; // Vector in which we store our graph in the form of a fitness list
Void DFS (int v) {
if (WAS [V]) {// if we were in this top, then go out of it
Return;
}
WAS [V] = 1; // We say that they were already in this top
For (AUTO I: Graph [V]) {
DFS (I); // Run a new DFS from each neighbor
}
}
INT MAIN () {
int n, m;
CIN & GT; & GT; n & gt; & gt; m; // Read the number of vertices and ribs
graph.Resize (N);
Was.Resize (N);
For (int i = 0; i & lt; n; i ++) {
WAS [i] = 0; // We say that we have not been anywhere
}
For (int i = 0; i & lt; m; i ++) {
int a, b;
CIN & GT; & GT; a & gt; & gt; b;
a--; b--; // Usually in the input data the vertex is numbered from the unit
Graph [a] .push_back (b); // Read the entire oriented graph
}
DFS (0); // Start DFS from the initial vertex for which the zero took
Return 0;
}