| 570 | typename graph_type |
| 571 | > |
| 572 | void create_moral_graph ( |
| 573 | const directed_graph_type& g, |
| 574 | graph_type& moral_graph |
| 575 | ) |
| 576 | { |
| 577 | // make sure requires clause is not broken |
| 578 | DLIB_ASSERT(graph_contains_directed_cycle(g) == false, |
| 579 | "\tvoid create_moral_graph(g, moral_graph)" |
| 580 | << "\n\tYou can only make moral graphs if g doesn't have directed cycles" |
| 581 | ); |
| 582 | COMPILE_TIME_ASSERT(is_graph<graph_type>::value); |
| 583 | COMPILE_TIME_ASSERT(is_directed_graph<directed_graph_type>::value); |
| 584 | |
| 585 | copy_graph_structure(g, moral_graph); |
| 586 | |
| 587 | // now marry all the parents (i.e. add edges between parent nodes) |
| 588 | for (unsigned long i = 0; i < g.number_of_nodes(); ++i) |
| 589 | { |
| 590 | // loop over all combinations of parents of g.node(i) |
| 591 | for (unsigned long j = 0; j < g.node(i).number_of_parents(); ++j) |
| 592 | { |
| 593 | for (unsigned long k = 0; k < g.node(i).number_of_parents(); ++k) |
| 594 | { |
| 595 | const unsigned long p1 = g.node(i).parent(j).index(); |
| 596 | const unsigned long p2 = g.node(i).parent(k).index(); |
| 597 | if (p1 == p2) |
| 598 | continue; |
| 599 | |
| 600 | if (moral_graph.has_edge(p1,p2) == false) |
| 601 | moral_graph.add_edge(p1,p2); |
| 602 | } |
| 603 | } |
| 604 | } |
| 605 | } |
| 606 | |
| 607 | // ---------------------------------------------------------------------------------------- |
| 608 | |