bool Weighted_graph::is_connected() const {
	// if empty graph with no vertex, return "unconnected"/false
	if (vertex_num == 0) {
		return false;
	}
	else {
		minimum_spanning_tree(0);
	}

	// Iterate through MST_node_visited
	// to see if it connects all the nodes
	// If not, return false; otherwise, return true
	for (int i = 0; i < vertex_num; ++i) {
		if (!MST_node_visited[i]) {
			return false;
		}
	}
	return true;
}
Beispiel #2
0
std::list<index_t> mst_heuristic::build_solution(tsp_sym& instance){
  // Using the symmetric problem derived from the general one.
  undirected_graph<distance_t> graph (instance.get_matrix());

  // Getting minimum spanning tree of associated graph under the form
  // of an adjacency list.
  std::unordered_map<index_t, std::list<index_t>> adjacency_list
    = minimum_spanning_tree(graph).get_adjacency_list();
  
  // Initializing the depth-first search of the minimum spanning tree
  // with any vertex. Using the list as a stack.
  std::list<index_t> df_list;

  index_t current_vertex = adjacency_list.begin()->first;
  df_list.push_back(current_vertex);

  std::list<index_t> tour;

  while(!df_list.empty()){
    current_vertex = df_list.back();
    df_list.pop_back();

    for(auto vertex = adjacency_list[current_vertex].cbegin();
        vertex != adjacency_list[current_vertex].cend();
        ++vertex){
      // Adding neighbour for further visit.
      df_list.push_back(*vertex);
      // Making sure current edge won't be used backward later.
      adjacency_list[*vertex].remove(current_vertex);
    }

    tour.push_back(current_vertex);
  }
  
  return tour;
}