Skip to content

MinimumSpanningTree

vtk-examples/Cxx/Graphs/MinimumSpanningTree

Description

This example shows how to find the minimum spanning tree. The input graph is:

and the minimum spanning tree computed is :

You must have compiled VTK with VTK_USE_BOOST=ON to use this functionality. You must also link to vtkInfovis.

Question

If you have a question about this example, please use the VTK Discourse Forum

Code

MinimumSpanningTree.cxx

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

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

  // Create 3 vertices
  vtkIdType v1 = g->AddVertex();
  vtkIdType v2 = g->AddVertex();
  vtkIdType v3 = g->AddVertex();

  // Create a fully connected graph
  g->AddEdge(v1, v2);
  g->AddEdge(v2, v3);
  g->AddEdge(v1, v3);

  // 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
  g->GetEdgeData()->AddArray(weights);

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

  // 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
  vtkSmartPointer<vtkTree> minimumSpanningTree;
  minimumSpanningTree->ShallowCopy(minimumSpanningTreeFilter->GetOutput());

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

  return EXIT_SUCCESS;
}

CMakeLists.txt

cmake_minimum_required(VERSION 3.12 FATAL_ERROR)

project(MinimumSpanningTree)

find_package(VTK COMPONENTS 
  CommonCore
  CommonDataModel
  InfovisBoostGraphAlgorithms
)

if (NOT VTK_FOUND)
  message(FATAL_ERROR "MinimumSpanningTree: 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.")
add_executable(MinimumSpanningTree MACOSX_BUNDLE MinimumSpanningTree.cxx )
  target_link_libraries(MinimumSpanningTree PRIVATE ${VTK_LIBRARIES}
)
# vtk_module_autoinit is needed
vtk_module_autoinit(
  TARGETS MinimumSpanningTree
  MODULES ${VTK_LIBRARIES}
)

Download and Build MinimumSpanningTree

Click here to download MinimumSpanningTree and its CMakeLists.txt file. Once the tarball MinimumSpanningTree.tar has been downloaded and extracted,

cd MinimumSpanningTree/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:

./MinimumSpanningTree

WINDOWS USERS

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