MCPcopy Create free account
hub / github.com/davisking/dlib / make_random_graph

Function make_random_graph

dlib/test/graph_cuts.cpp:607–641  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

605
606 template <typename dgraph_type>
607 void make_random_graph(
608 dlib::rand& rnd,
609 dgraph_type& g,
610 unsigned long& source,
611 unsigned long& sink
612 )
613 {
614 typedef typename dgraph_type::edge_type edge_weight_type;
615 g.clear();
616 const unsigned int num_nodes = rnd.get_random_32bit_number()%7 + 2;
617 g.set_number_of_nodes(num_nodes);
618
619 const unsigned int num_edges = static_cast<unsigned int>(num_nodes*(num_nodes-1)/2*rnd.get_random_double() + 0.5);
620
621 // add the right number of randomly selected edges
622 unsigned int count = 0;
623 while (count < num_edges)
624 {
625 unsigned long parent = rnd.get_random_32bit_number()%g.number_of_nodes();
626 unsigned long child = rnd.get_random_32bit_number()%g.number_of_nodes();
627 if (parent != child && g.has_edge(parent, child) == false)
628 {
629 ++count;
630 g.add_edge(parent, child);
631 edge(g, parent, child) = static_cast<edge_weight_type>(rnd.get_random_double()*50);
632
633 // have to have edges both ways
634 swap(parent, child);
635 g.add_edge(parent, child);
636 edge(g, parent, child) = static_cast<edge_weight_type>(rnd.get_random_double()*50);
637 }
638 }
639
640 pick_random_source_and_sink(rnd, g, source, sink);
641 }
642
643// ----------------------------------------------------------------------------------------
644

Callers 1

test_graph_cutsFunction · 0.85

Calls 6

get_random_doubleMethod · 0.80
swapFunction · 0.70
clearMethod · 0.45
number_of_nodesMethod · 0.45

Tested by

no test coverage detected