1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
|
#include <map>
#include <utility>
#include <vector>
#include <iostream>
// each edge is a pair (vertex to, weight)
template < typename VERTEX_TYPE, typename WEIGHT_TYPE >
using edge_type = std::pair< VERTEX_TYPE, WEIGHT_TYPE > ;
// adjacency_list for a weighted directed graph:
// maps each vertex to the collection of edges radiating out of that vertex
template < typename VERTEX_TYPE, typename WEIGHT_TYPE >
using adjacency_list_type =
std::map< VERTEX_TYPE, std::vector< edge_type<VERTEX_TYPE,WEIGHT_TYPE> > > ;
// example: adjacency list where vertex type is int, weight type is double
using vertex_t = int ;
using weight_t = double ;
using adjacency_list_t = adjacency_list_type<vertex_t,weight_t> ;
// input is a tuple for each edge in a DAG (vertex_from, vertex_to, weight)
// 1 7 2.3 => edge from 1 -------> 7, weight 2.3
// 2 7 1.6 => edge from 2 -------> 7, weight 1.6
// 1 5 5.0 => edge from 1 -------> 5, weight 5.0
// etc.
adjacency_list_t make_adjacency_list( std::istream& stm )
{
adjacency_list_t graph ;
vertex_t from, to ;
weight_t w ;
while( stm >> from >> to >> w ) { graph[from].emplace_back(to,w) ; graph[to] ; }
return graph ;
}
|