# DepthFirstSearchIterator

vtk-examples/Cxx/Graphs/DepthFirstSearchIterator

### Description¶

This example traverses a tree in a depth first fashion.

Question

### Code¶

DepthFirstSearchIterator.cxx

#include <vtkBoostPrimMinimumSpanningTree.h>
#include <vtkDataSetAttributes.h>
#include <vtkDoubleArray.h>
#include <vtkMutableUndirectedGraph.h>
#include <vtkNew.h>
#include <vtkTree.h>
#include <vtkTreeDFSIterator.h>

int main(int, char*[])
{
vtkNew<vtkMutableUndirectedGraph> g;

// Create 3 vertices

// Create a fully connected graph

// Create the edge weight array
vtkNew<vtkDoubleArray> weights;
weights->SetNumberOfComponents(1);
weights->SetName("Weights");

// Set the edge weights
weights->InsertNextValue(1.0);
weights->InsertNextValue(1.0);
weights->InsertNextValue(2.0);

// Add the edge weight array to the graph

// Output original graph info
std::cout << "Original Graph" << std::endl << "----------" << std::endl;
std::cout << "Number of vertices: " << g->GetNumberOfVertices() << std::endl;
std::cout << "Number of edges: " << g->GetNumberOfEdges() << std::endl;

// Setup the minimum spanning tree filter
vtkNew<vtkBoostPrimMinimumSpanningTree> minimumSpanningTreeFilter;
minimumSpanningTreeFilter->SetOriginVertex(v1);
minimumSpanningTreeFilter->SetInput(g);
minimumSpanningTreeFilter->SetEdgeWeightArrayName("Weights");

// Compute the minimum spanning tree
minimumSpanningTreeFilter->Update();

// Get the output tree
vtkNew<vtkTree> minimumSpanningTree;
minimumSpanningTree->ShallowCopy(minimumSpanningTreeFilter->GetOutput());

// Output information about the minimum spanning tree
std::cout << std::endl;
std::cout << "Minimum spanning tree" << std::endl
<< "----------" << std::endl;
std::cout << "Number of vertices: "
<< minimumSpanningTree->GetNumberOfVertices() << std::endl;
std::cout << "Number of edges: " << minimumSpanningTree->GetNumberOfEdges()
<< std::endl;

vtkIdType root = minimumSpanningTree->GetRoot();
std::cout << "Root: " << root << std::endl;

vtkNew<vtkTreeDFSIterator> dFS;
dFS->SetStartVertex(root);
dFS->SetTree(minimumSpanningTree);

// Traverse the tree in a depth first fashion
while (dFS->HasNext())
{
vtkIdType NextVertex = dFS->Next();
std::cout << "Next vertex: " << NextVertex
<< " level: " << minimumSpanningTree->GetLevel(NextVertex)
<< std::endl;
}

return EXIT_SUCCESS;
}

### CMakeLists.txt¶

cmake_minimum_required(VERSION 3.12 FATAL_ERROR)

project(DepthFirstSearchIterator)

find_package(VTK COMPONENTS
CommonCore
CommonDataModel
InfovisBoostGraphAlgorithms
)

if (NOT VTK_FOUND)
message(FATAL_ERROR "DepthFirstSearchIterator: Unable to find the VTK build folder.")
endif()

# Prevent a "command line is too long" failure in Windows.
set(CMAKE_NINJA_FORCE_RESPONSE_FILE "ON" CACHE BOOL "Force Ninja to use response files.")
target_link_libraries(DepthFirstSearchIterator PRIVATE ${VTK_LIBRARIES} ) # vtk_module_autoinit is needed vtk_module_autoinit( TARGETS DepthFirstSearchIterator MODULES${VTK_LIBRARIES}
)

cd DepthFirstSearchIterator/build

If VTK is installed:

cmake ..

If VTK is not installed but compiled on your system, you will need to specify the path to your VTK build:

cmake -DVTK_DIR:PATH=/home/me/vtk_build ..

Build the project:

make

and run it:

./DepthFirstSearchIterator

WINDOWS USERS

Be sure to add the VTK bin directory to your path. This will resolve the VTK dll's at run time.