#pragma once
#include <iostream>
#include <utility>
#include <vector>
typedef struct {
int v1,v2;
int w;
}EdgeEx;
int shortest_path(const std::vector< EdgeEx>& edges,std::vector<int> &qurePath,const int start_v,const int end_v);
int minimum_spanning_tree(const std::vector< EdgeEx>& edges, std::vector<EdgeEx> &qurePath);
#include "boost_graph.h"
#include <boost/graph/undirected_graph.hpp>
#include <boost/graph/adjacency_list.hpp>
#include <boost/graph/dijkstra_shortest_paths.hpp>
#include <boost/graph/kruskal_min_spanning_tree.hpp>
int shortest_path(const std::vector< EdgeEx>& edges, std::vector<int> &path,
const int start_v, const int end_v)
{
using namespace std;
using namespace boost;
typedef property<edge_weight_t, int> EdgeWeightProperty; //边的权重
typedef adjacency_list<listS, vecS, undirectedS, no_property, EdgeWeightProperty> Graph;
typedef graph_traits < Graph >::vertex_descriptor vertex_descriptor;
typedef std::pair<int, int> Edge;
Graph mygraph;
for (auto e : edges) {
add_edge(e.v1, e.v2, e.w, mygraph);
}
std::vector<vertex_descriptor> p(num_vertices(mygraph));
std::vector<int> d(num_vertices(mygraph));
vertex_descriptor s = vertex(start_v, mygraph);
dijkstra_shortest_paths(mygraph, s,
predecessor_map(boost::make_iterator_property_map(p.begin(), get(boost::vertex_index, mygraph))).
distance_map(boost::make_iterator_property_map(d.begin(), get(boost::vertex_index, mygraph))));
graph_traits < Graph >::vertex_iterator vi, vend;
int t = end_v;
if(path.size())
path.clear();
for (; t != start_v; t = p[t])
path.push_back(t);
path.push_back(start_v);
reverse(path.begin(), path.end());
return 1;
}
int minimum_spanning_tree(const std::vector< EdgeEx>& edges, std::vector<EdgeEx> &qureTree)
{
using namespace std;
using namespace boost;
typedef property<edge_weight_t, int> EdgeWeightProperty; //边的权重
typedef adjacency_list<listS, vecS, undirectedS, no_property, EdgeWeightProperty> Graph;
typedef graph_traits < Graph >::vertex_descriptor vertex_descriptor;
typedef Graph::edge_descriptor Edge;
Graph mygraph;
for (auto e : edges) {
add_edge(e.v1, e.v2, e.w, mygraph);
}
std::list < Edge > spanning_tree;
kruskal_minimum_spanning_tree(mygraph, std::back_inserter(spanning_tree));
qureTree.clear();
for (std::list < Edge >::iterator ei = spanning_tree.begin(); ei != spanning_tree.end(); ++ei)
{
//boost::graph_traits<Graph>::edge_iterator v_begin, v_end;
//cout << (*ei) << " \n";
//std::cout << ei->m_source << ","<<ei->m_target<<"\n";
qureTree.push_back({ int(ei->m_source),int(ei->m_target),0});
}
//cout << "/n";
return 1;
}
///test
#include "boost_graph.h"
int main(){
enum { A, B, C, D, E, F, N };
int ipot_st, ipot_end;
ipot_st = A; //起始点
ipot_end = F; //结束
vector<int> path;
vector<EdgeEx> vedges;
vedges.push_back({ A, B, 6 });
vedges.push_back({ B, C, 6 });
vedges.push_back({ B, D, 3 });
vedges.push_back({ D, E, 4 });
vedges.push_back({ D, F, 2 });
vedges.push_back({ E, F, 4 });
vedges.push_back({ C, F, 9 });
shortest_path(vedges, path, ipot_st, ipot_end);
for (int i = 0; i < path.size(); i++)
{
std::cout << path[i] << "->";
}
std::cout <<"end\n";
vector<EdgeEx> tree;
minimum_spanning_tree(vedges,tree);
for (auto t : tree) {
std::cout << t.v1 << "-" << t.v2 << "\n";
}
system("pause");
return 0;
}