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

Function make_random_grid_graph

dlib/test/graph_cuts.cpp:672–734  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

670
671 template <typename dgraph_type>
672 void make_random_grid_graph(
673 dlib::rand& rnd,
674 dgraph_type& g,
675 unsigned long& source,
676 unsigned long& sink
677 )
678 /*!
679 ensures
680 - makes a grid graph like the kind used for potts models.
681 !*/
682 {
683 typedef typename dgraph_type::edge_type edge_weight_type;
684 g.clear();
685 const long nr = rnd.get_random_32bit_number()%2 + 2;
686 const long nc = rnd.get_random_32bit_number()%2 + 2;
687 g.set_number_of_nodes(nr*nc+2);
688
689 const rectangle rect(0,0,nc-1,nr-1);
690 for (long r = 0; r < nr; ++r)
691 {
692 for (long c = 0; c < nc; ++c)
693 {
694 const point p(c,r);
695 const unsigned long i = p.y()*nc + p.x();
696
697 const point n2(c-1,r);
698 if (rect.contains(n2))
699 {
700 const unsigned long j = n2.y()*nc + n2.x();
701 g.add_edge(i,j);
702 g.add_edge(j,i);
703 edge(g,i,j) = static_cast<edge_weight_type>(rnd.get_random_double()*50);
704 edge(g,j,i) = static_cast<edge_weight_type>(rnd.get_random_double()*50);
705 }
706
707 const point n4(c,r-1);
708 if (rect.contains(n4))
709 {
710 const unsigned long j = n4.y()*nc + n4.x();
711 g.add_edge(i,j);
712 g.add_edge(j,i);
713 edge(g,i,j) = static_cast<edge_weight_type>(rnd.get_random_double()*50);
714 edge(g,j,i) = static_cast<edge_weight_type>(rnd.get_random_double()*50);
715 }
716 }
717 }
718
719 // use the last two nodes as source and sink. Also connect them to all the other nodes.
720 source = g.number_of_nodes()-1;
721 sink = g.number_of_nodes()-2;
722 for (unsigned long i = 0; i < g.number_of_nodes()-2; ++i)
723 {
724 g.add_edge(i,source);
725 g.add_edge(source,i);
726 g.add_edge(i,sink);
727 g.add_edge(sink,i);
728
729 edge(g,i,source) = static_cast<edge_weight_type>(rnd.get_random_double()*50);

Callers 1

test_graph_cutsFunction · 0.85

Calls 5

get_random_doubleMethod · 0.80
clearMethod · 0.45
containsMethod · 0.45
number_of_nodesMethod · 0.45

Tested by

no test coverage detected