/**************************************************************** **** **** This file belongs with the course **** Introduction to Scientific Programming in C++/Fortran2003 **** copyright 2017-2023 Victor Eijkhout eijkhout@tacc.utexas.edu **** **** singlesource.cxx : shortest path exploration **** ****************************************************************/ #include using std::max; #include using std::cin, std::cout; #include using std::setw; #include using std::stringstream; #include using std::string; #include using std::map; #include #include using std::vector; #include float chance() { static std::default_random_engine static_engine{1}; std::uniform_real_distribution p(0.f,1.f); return p(static_engine); }; int random_degree( int n ) { static std::default_random_engine static_engine{2}; std::uniform_int_distribution<> p(n/2,3*n/2); return p(static_engine); }; int random_int( int n ) { static std::default_random_engine static_engine{3}; std::uniform_int_distribution<> p(0,n-1); return p(static_engine); }; using distance = float; const distance inf_distance=-1; class Dag { private: vector< vector > dag; public: // Make Dag of `n' nodes, no edges for now. Dag( int n ) : dag( vector< vector >(n) ) {}; const auto& neighbors( unsigned i ) const { return dag.at(i); }; void make_edges( int avg_degree ) { const int n = dag.size(); assert( avg_degree>=0 and avg_degree=0 && degree=0 && neighbor "; for ( auto e : neighbors ) ss << setw(3) << e << ", "; ss << '\n'; } return ss.str(); }; }; int main() { const int graph_size = 20; Dag graph(graph_size); graph.make_edges(3); cout << graph.as_string(); map distances; int starting_node = 0; distances[starting_node] = 0; // while not done for (;;) { int updates{0}; // iterate over all mapped nodes, get distance ell from each for ( auto [node,dist] : distances ) { // iterate over all of their neighbors, giving them d=ell+1 cout << "Investigate node " << node << " with dist " << dist << '\n'; for ( const auto& neighbor : graph.neighbors(node) ) { if ( auto find_neighbor = distances.find(neighbor) ; find_neighbor==distances.end() ) { cout << " .. neighbor " << neighbor << " distance " << dist+1 << '\n'; distances[neighbor] = dist+1; ++updates; } } } if ( distances.size()==graph_size ) { cout << "Whole graph mapped\n"; break; } if (updates==0) { cout << "Graph is not simply connected\n"; break; } } return 0; }