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 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838
|
#![cfg(feature = "quickcheck")]
extern crate alloc;
#[macro_use]
extern crate quickcheck;
extern crate petgraph;
extern crate rand;
/*#[macro_use]
extern crate defmac;*/
extern crate itertools;
/*extern crate odds;*/
mod maximal_cliques;
mod utils;
//use odds::prelude::*;
use utils::{Small, Tournament};
use alloc::collections::BTreeSet;
use core::hash::Hash;
use hashbrown::{HashMap, HashSet};
use itertools::assert_equal;
use itertools::cloned;
use quickcheck::{Arbitrary, Gen};
use rand::Rng;
#[cfg(feature = "stable_graph")]
use petgraph::algo::steiner_tree;
use petgraph::algo::{
astar, bellman_ford, bidirectional_dijkstra, bridges, condensation, connected_components,
dijkstra, dsatur_coloring, find_negative_cycle, floyd_warshall, ford_fulkerson,
greedy_feedback_arc_set, greedy_matching, is_cyclic_directed, is_cyclic_undirected,
is_isomorphic, is_isomorphic_matching, johnson, k_shortest_path, kosaraju_scc,
maximal_cliques as maximal_cliques_algo, maximum_matching, min_spanning_tree, page_rank, spfa,
tarjan_scc, toposort, Matching,
};
use petgraph::data::FromElements;
use petgraph::dot::{Config, Dot};
use petgraph::graph::{edge_index, node_index, IndexType};
use petgraph::graphmap::NodeTrait;
use petgraph::operator::complement;
use petgraph::prelude::*;
use petgraph::visit::{
EdgeFiltered, EdgeIndexable, IntoEdgeReferences, IntoEdges, IntoNeighbors, IntoNodeIdentifiers,
IntoNodeReferences, NodeCount, NodeIndexable, Reversed, Topo, VisitMap, Visitable,
};
use petgraph::EdgeType;
#[cfg(feature = "rayon")]
use petgraph::algo::parallel_johnson;
fn mst_graph<N, E, Ty, Ix>(g: &Graph<N, E, Ty, Ix>) -> Graph<N, E, Undirected, Ix>
where
Ty: EdgeType,
Ix: IndexType,
N: Clone,
E: Clone + PartialOrd,
{
Graph::from_elements(min_spanning_tree(&g))
}
use core::fmt;
use petgraph::algo::articulation_points::articulation_points;
quickcheck! {
fn mst_directed(g: Small<Graph<(), u32>>) -> bool {
// filter out isolated nodes
let no_singles = g.filter_map(
|nx, w| g.neighbors_undirected(nx).next().map(|_| w),
|_, w| Some(w));
for i in no_singles.node_indices() {
assert!(no_singles.neighbors_undirected(i).count() > 0);
}
assert_eq!(no_singles.edge_count(), g.edge_count());
let mst = mst_graph(&no_singles);
assert!(!is_cyclic_undirected(&mst));
true
}
}
quickcheck! {
fn mst_undirected(g: Graph<(), u32, Undirected>) -> bool {
// filter out isolated nodes
let no_singles = g.filter_map(
|nx, w| g.neighbors_undirected(nx).next().map(|_| w),
|_, w| Some(w));
for i in no_singles.node_indices() {
assert!(no_singles.neighbors_undirected(i).count() > 0);
}
assert_eq!(no_singles.edge_count(), g.edge_count());
let mst = mst_graph(&no_singles);
assert!(!is_cyclic_undirected(&mst));
true
}
}
quickcheck! {
fn reverse_undirected(g: Small<UnGraph<(), ()>>) -> bool {
let mut h = (*g).clone();
h.reverse();
is_isomorphic(&*g, &h)
}
}
fn assert_graph_consistent<N, E, Ty, Ix>(g: &Graph<N, E, Ty, Ix>)
where
Ty: EdgeType,
Ix: IndexType,
{
assert_eq!(g.node_count(), g.node_indices().count());
assert_eq!(g.edge_count(), g.edge_indices().count());
for edge in g.raw_edges() {
assert!(
g.find_edge(edge.source(), edge.target()).is_some(),
"Edge not in graph! {:?} to {:?}",
edge.source(),
edge.target()
);
}
}
#[test]
fn reverse_directed() {
fn prop<Ty: EdgeType>(mut g: Graph<(), (), Ty>) -> bool {
let node_outdegrees = g
.node_indices()
.map(|i| g.neighbors_directed(i, Outgoing).count())
.collect::<Vec<_>>();
let node_indegrees = g
.node_indices()
.map(|i| g.neighbors_directed(i, Incoming).count())
.collect::<Vec<_>>();
g.reverse();
let new_outdegrees = g
.node_indices()
.map(|i| g.neighbors_directed(i, Outgoing).count())
.collect::<Vec<_>>();
let new_indegrees = g
.node_indices()
.map(|i| g.neighbors_directed(i, Incoming).count())
.collect::<Vec<_>>();
assert_eq!(node_outdegrees, new_indegrees);
assert_eq!(node_indegrees, new_outdegrees);
assert_graph_consistent(&g);
true
}
quickcheck::quickcheck(prop as fn(Graph<_, _, Directed>) -> bool);
}
#[test]
fn graph_retain_nodes() {
fn prop<Ty: EdgeType>(mut g: Graph<i32, i32, Ty>) -> bool {
// Remove all negative nodes, these should be randomly spread
let og = g.clone();
let nodes = g.node_count();
let num_negs = g.raw_nodes().iter().filter(|n| n.weight < 0).count();
let mut removed = 0;
g.retain_nodes(|g, i| {
let keep = g[i] >= 0;
if !keep {
removed += 1;
}
keep
});
let num_negs_post = g.raw_nodes().iter().filter(|n| n.weight < 0).count();
let num_pos_post = g.raw_nodes().iter().filter(|n| n.weight >= 0).count();
assert_eq!(num_negs_post, 0);
assert_eq!(removed, num_negs);
assert_eq!(num_negs + g.node_count(), nodes);
assert_eq!(num_pos_post, g.node_count());
// check against filter_map
let filtered = og.filter_map(
|_, w| if *w >= 0 { Some(*w) } else { None },
|_, w| Some(*w),
);
assert_eq!(g.node_count(), filtered.node_count());
/*
println!("Iso of graph with nodes={}, edges={}",
g.node_count(), g.edge_count());
*/
assert!(is_isomorphic_matching(
&filtered,
&g,
PartialEq::eq,
PartialEq::eq
));
true
}
quickcheck::quickcheck(prop as fn(Graph<_, _, Directed>) -> bool);
quickcheck::quickcheck(prop as fn(Graph<_, _, Undirected>) -> bool);
}
#[test]
fn graph_retain_edges() {
fn prop<Ty: EdgeType>(mut g: Graph<(), i32, Ty>) -> bool {
// Remove all negative edges, these should be randomly spread
let og = g.clone();
let edges = g.edge_count();
let num_negs = g.raw_edges().iter().filter(|n| n.weight < 0).count();
let mut removed = 0;
g.retain_edges(|g, i| {
let keep = g[i] >= 0;
if !keep {
removed += 1;
}
keep
});
let num_negs_post = g.raw_edges().iter().filter(|n| n.weight < 0).count();
let num_pos_post = g.raw_edges().iter().filter(|n| n.weight >= 0).count();
assert_eq!(num_negs_post, 0);
assert_eq!(removed, num_negs);
assert_eq!(num_negs + g.edge_count(), edges);
assert_eq!(num_pos_post, g.edge_count());
if og.edge_count() < 30 {
// check against filter_map
let filtered = og.filter_map(
|_, _| Some(()),
|_, w| if *w >= 0 { Some(*w) } else { None },
);
assert_eq!(g.node_count(), filtered.node_count());
assert!(is_isomorphic(&filtered, &g));
}
true
}
quickcheck::quickcheck(prop as fn(Graph<_, _, Directed>) -> bool);
quickcheck::quickcheck(prop as fn(Graph<_, _, Undirected>) -> bool);
}
#[test]
fn stable_graph_retain_edges() {
fn prop<Ty: EdgeType>(mut g: StableGraph<(), i32, Ty>) -> bool {
// Remove all negative edges, these should be randomly spread
let og = g.clone();
let edges = g.edge_count();
let num_negs = g.edge_references().filter(|n| *n.weight() < 0).count();
let mut removed = 0;
g.retain_edges(|g, i| {
let keep = g[i] >= 0;
if !keep {
removed += 1;
}
keep
});
let num_negs_post = g.edge_references().filter(|n| *n.weight() < 0).count();
let num_pos_post = g.edge_references().filter(|n| *n.weight() >= 0).count();
assert_eq!(num_negs_post, 0);
assert_eq!(removed, num_negs);
assert_eq!(num_negs + g.edge_count(), edges);
assert_eq!(num_pos_post, g.edge_count());
if og.edge_count() < 30 {
// check against filter_map
let filtered = og.filter_map(
|_, _| Some(()),
|_, w| if *w >= 0 { Some(*w) } else { None },
);
assert_eq!(g.node_count(), filtered.node_count());
}
true
}
quickcheck::quickcheck(prop as fn(StableGraph<_, _, Directed>) -> bool);
quickcheck::quickcheck(prop as fn(StableGraph<_, _, Undirected>) -> bool);
}
#[test]
fn isomorphism_1() {
// using small weights so that duplicates are likely
fn prop<Ty: EdgeType>(g: Small<Graph<i8, i8, Ty>>) -> bool {
let mut rng = rand::thread_rng();
// several trials of different isomorphisms of the same graph
// mapping of node indices
let mut map = g.node_indices().collect::<Vec<_>>();
let mut ng = Graph::<_, _, Ty>::with_capacity(g.node_count(), g.edge_count());
for _ in 0..1 {
rng.shuffle(&mut map);
ng.clear();
for _ in g.node_indices() {
ng.add_node(0);
}
// Assign node weights
for i in g.node_indices() {
ng[map[i.index()]] = g[i];
}
// Add edges
for i in g.edge_indices() {
let (s, t) = g.edge_endpoints(i).unwrap();
ng.add_edge(map[s.index()], map[t.index()], g[i]);
}
if g.node_count() < 20 && g.edge_count() < 50 {
assert!(is_isomorphic(&*g, &ng));
}
assert!(is_isomorphic_matching(
&*g,
&ng,
PartialEq::eq,
PartialEq::eq
));
}
true
}
quickcheck::quickcheck(prop::<Undirected> as fn(_) -> bool);
quickcheck::quickcheck(prop::<Directed> as fn(_) -> bool);
}
#[test]
fn isomorphism_modify() {
// using small weights so that duplicates are likely
fn prop<Ty: EdgeType>(g: Small<Graph<i16, i8, Ty>>, node: u8, edge: u8) -> bool {
println!("graph {g:#?}");
let mut ng = (*g).clone();
let i = node_index(node as usize);
let j = edge_index(edge as usize);
if i.index() < g.node_count() {
ng[i] = (g[i] == 0) as i16;
}
if j.index() < g.edge_count() {
ng[j] = (g[j] == 0) as i8;
}
if i.index() < g.node_count() || j.index() < g.edge_count() {
assert!(!is_isomorphic_matching(
&*g,
&ng,
PartialEq::eq,
PartialEq::eq
));
} else {
assert!(is_isomorphic_matching(
&*g,
&ng,
PartialEq::eq,
PartialEq::eq
));
}
true
}
quickcheck::quickcheck(prop::<Undirected> as fn(_, _, _) -> bool);
quickcheck::quickcheck(prop::<Directed> as fn(_, _, _) -> bool);
}
#[test]
fn graph_remove_edge() {
fn prop<Ty: EdgeType>(mut g: Graph<(), (), Ty>, a: u8, b: u8) -> bool {
let a = node_index(a as usize);
let b = node_index(b as usize);
let edge = g.find_edge(a, b);
if !g.is_directed() {
assert_eq!(edge.is_some(), g.find_edge(b, a).is_some());
}
if let Some(ex) = edge {
assert!(g.remove_edge(ex).is_some());
}
assert_graph_consistent(&g);
assert!(g.find_edge(a, b).is_none());
assert!(!g.neighbors(a).any(|x| x == b));
if !g.is_directed() {
assert!(!g.neighbors(b).any(|x| x == a));
}
true
}
quickcheck::quickcheck(prop as fn(Graph<_, _, Undirected>, _, _) -> bool);
quickcheck::quickcheck(prop as fn(Graph<_, _, Directed>, _, _) -> bool);
}
#[cfg(feature = "stable_graph")]
#[test]
fn stable_graph_remove_edge() {
fn prop<Ty: EdgeType>(mut g: StableGraph<(), (), Ty>, a: u8, b: u8) -> bool {
let a = node_index(a as usize);
let b = node_index(b as usize);
let edge = g.find_edge(a, b);
if !g.is_directed() {
assert_eq!(edge.is_some(), g.find_edge(b, a).is_some());
}
if let Some(ex) = edge {
assert!(g.remove_edge(ex).is_some());
}
//assert_graph_consistent(&g);
assert!(g.find_edge(a, b).is_none());
assert!(!g.neighbors(a).any(|x| x == b));
if !g.is_directed() {
assert!(g.find_edge(b, a).is_none());
assert!(!g.neighbors(b).any(|x| x == a));
}
true
}
quickcheck::quickcheck(prop as fn(StableGraph<_, _, Undirected>, _, _) -> bool);
quickcheck::quickcheck(prop as fn(StableGraph<_, _, Directed>, _, _) -> bool);
}
#[cfg(feature = "stable_graph")]
#[test]
fn stable_graph_add_remove_edges() {
fn prop<Ty: EdgeType>(mut g: StableGraph<(), (), Ty>, edges: Vec<(u8, u8)>) -> bool {
for &(a, b) in &edges {
let a = node_index(a as usize);
let b = node_index(b as usize);
let edge = g.find_edge(a, b);
if edge.is_none() && g.contains_node(a) && g.contains_node(b) {
let _index = g.add_edge(a, b, ());
continue;
}
if !g.is_directed() {
assert_eq!(edge.is_some(), g.find_edge(b, a).is_some());
}
if let Some(ex) = edge {
assert!(g.remove_edge(ex).is_some());
}
//assert_graph_consistent(&g);
assert!(
g.find_edge(a, b).is_none(),
"failed to remove edge {:?} from graph {:?}",
(a, b),
g
);
assert!(!g.neighbors(a).any(|x| x == b));
if !g.is_directed() {
assert!(g.find_edge(b, a).is_none());
assert!(!g.neighbors(b).any(|x| x == a));
}
}
true
}
quickcheck::quickcheck(prop as fn(StableGraph<_, _, Undirected>, _) -> bool);
quickcheck::quickcheck(prop as fn(StableGraph<_, _, Directed>, _) -> bool);
}
fn assert_graphmap_consistent<N, E, Ty>(g: &GraphMap<N, E, Ty>)
where
Ty: EdgeType,
N: NodeTrait + fmt::Debug,
{
for (a, b, _weight) in g.all_edges() {
assert!(g.contains_edge(a, b), "Edge not in graph! {a:?} to {b:?}");
assert!(
g.neighbors(a).any(|x| x == b),
"Edge {:?} not in neighbor list for {:?}",
(a, b),
a
);
if !g.is_directed() {
assert!(
g.neighbors(b).any(|x| x == a),
"Edge {:?} not in neighbor list for {:?}",
(b, a),
b
);
}
}
}
#[test]
fn graphmap_remove() {
fn prop<Ty: EdgeType>(mut g: GraphMap<i8, (), Ty>, a: i8, b: i8) -> bool {
//if g.edge_count() > 20 { return true; }
assert_graphmap_consistent(&g);
let contains = g.contains_edge(a, b);
if !g.is_directed() {
assert_eq!(contains, g.contains_edge(b, a));
}
assert_eq!(g.remove_edge(a, b).is_some(), contains);
assert!(!g.contains_edge(a, b) && !g.neighbors(a).any(|x| x == b));
//(g.is_directed() || g.neighbors(b).find(|x| *x == a).is_none()));
assert!(g.remove_edge(a, b).is_none());
assert_graphmap_consistent(&g);
true
}
quickcheck::quickcheck(prop as fn(DiGraphMap<_, _>, _, _) -> bool);
quickcheck::quickcheck(prop as fn(UnGraphMap<_, _>, _, _) -> bool);
}
#[test]
fn graphmap_add_remove() {
fn prop(mut g: UnGraphMap<i8, ()>, a: i8, b: i8) -> bool {
assert_eq!(g.contains_edge(a, b), g.add_edge(a, b, ()).is_some());
g.remove_edge(a, b);
!g.contains_edge(a, b) && !g.neighbors(a).any(|x| x == b) && !g.neighbors(b).any(|x| x == a)
}
quickcheck::quickcheck(prop as fn(_, _, _) -> bool);
}
fn sort_sccs<T: Ord>(v: &mut [Vec<T>]) {
for scc in &mut *v {
scc.sort();
}
v.sort();
}
quickcheck! {
fn graph_sccs(g: Graph<(), ()>) -> bool {
let mut sccs = kosaraju_scc(&g);
let mut tsccs = tarjan_scc(&g);
sort_sccs(&mut sccs);
sort_sccs(&mut tsccs);
if sccs != tsccs {
println!("{:?}",
Dot::with_config(&g, &[Config::EdgeNoLabel,
Config::NodeIndexLabel]));
println!("Sccs {sccs:?}");
println!("Sccs (Tarjan) {tsccs:?}");
return false;
}
true
}
}
/*quickcheck! {
fn kosaraju_scc_is_topo_sort(g: Graph<(), ()>) -> bool {
let tsccs = kosaraju_scc(&g);
let firsts = tsccs.iter().rev().map(|v| v[0]).collect::<Vec<_>>();
subset_is_topo_order(&g, &firsts)
}
}*/
/*quickcheck! {
fn tarjan_scc_is_topo_sort(g: Graph<(), ()>) -> bool {
let tsccs = tarjan_scc(&g);
let firsts = tsccs.iter().rev().map(|v| v[0]).collect::<Vec<_>>();
subset_is_topo_order(&g, &firsts)
}
}*/
quickcheck! {
// Reversed edges gives the same sccs (when sorted)
fn graph_reverse_sccs(g: Graph<(), ()>) -> bool {
let mut sccs = kosaraju_scc(&g);
let mut tsccs = kosaraju_scc(Reversed(&g));
sort_sccs(&mut sccs);
sort_sccs(&mut tsccs);
if sccs != tsccs {
println!("{:?}",
Dot::with_config(&g, &[Config::EdgeNoLabel,
Config::NodeIndexLabel]));
println!("Sccs {sccs:?}");
println!("Sccs (Reversed) {tsccs:?}");
return false;
}
true
}
}
quickcheck! {
// Reversed edges gives the same sccs (when sorted)
fn graphmap_reverse_sccs(g: DiGraphMap<u16, ()>) -> bool {
let mut sccs = kosaraju_scc(&g);
let mut tsccs = kosaraju_scc(Reversed(&g));
sort_sccs(&mut sccs);
sort_sccs(&mut tsccs);
if sccs != tsccs {
println!("{:?}",
Dot::with_config(&g, &[Config::EdgeNoLabel,
Config::NodeIndexLabel]));
println!("Sccs {sccs:?}");
println!("Sccs (Reversed) {tsccs:?}");
return false;
}
true
}
}
#[test]
fn graph_condensation_acyclic() {
fn prop(g: Graph<(), ()>) -> bool {
!is_cyclic_directed(&condensation(g, /* make_acyclic */ true))
}
quickcheck::quickcheck(prop as fn(_) -> bool);
}
#[derive(Debug, Clone)]
struct Dag<N: Default + Clone + Send + 'static>(Graph<N, ()>);
impl<N: Default + Clone + Send + 'static> Arbitrary for Dag<N> {
fn arbitrary<G: Gen>(g: &mut G) -> Self {
let nodes = usize::arbitrary(g);
if nodes == 0 {
return Dag(Graph::with_capacity(0, 0));
}
let split = g.gen_range(0., 1.);
let max_width = f64::sqrt(nodes as f64) as usize;
let tall = (max_width as f64 * split) as usize;
let fat = max_width - tall;
let edge_prob = 1. - (1. - g.gen_range(0., 1.)) * (1. - g.gen_range(0., 1.));
let edges = ((nodes as f64).powi(2) * edge_prob) as usize;
let mut gr = Graph::with_capacity(nodes, edges);
let mut nodes = 0;
for _ in 0..tall {
let cur_nodes = g.gen_range(0, fat);
for _ in 0..cur_nodes {
gr.add_node(N::default());
}
for j in 0..nodes {
for k in 0..cur_nodes {
if g.gen_range(0., 1.) < edge_prob {
gr.add_edge(NodeIndex::new(j), NodeIndex::new(k + nodes), ());
}
}
}
nodes += cur_nodes;
}
Dag(gr)
}
// shrink the graph by splitting it in two by a very
// simple algorithm, just even and odd node indices
fn shrink(&self) -> Box<dyn Iterator<Item = Self>> {
let self_ = self.clone();
Box::new((0..2).filter_map(move |x| {
let gr = self_.0.filter_map(
|i, w| {
if i.index() % 2 == x {
Some(w.clone())
} else {
None
}
},
|_, _| Some(()),
);
// make sure we shrink
if gr.node_count() < self_.0.node_count() {
Some(Dag(gr))
} else {
None
}
}))
}
}
/*fn is_topo_order<N>(gr: &Graph<N, (), Directed>, order: &[NodeIndex]) -> bool {
if gr.node_count() != order.len() {
println!(
"Graph ({}) and count ({}) had different amount of nodes.",
gr.node_count(),
order.len()
);
return false;
}
// check all the edges of the graph
for edge in gr.raw_edges() {
let a = edge.source();
let b = edge.target();
let ai = order.find(&a).unwrap();
let bi = order.find(&b).unwrap();
if ai >= bi {
println!("{a:?} > {b:?} ");
return false;
}
}
true
}*/
/*fn subset_is_topo_order<N>(gr: &Graph<N, (), Directed>, order: &[NodeIndex]) -> bool {
if gr.node_count() < order.len() {
println!(
"Graph (len={}) had less nodes than order (len={})",
gr.node_count(),
order.len()
);
return false;
}
// check all the edges of the graph
for edge in gr.raw_edges() {
let a = edge.source();
let b = edge.target();
if a == b {
continue;
}
// skip those that are not in the subset
let ai = match order.find(&a) {
Some(i) => i,
None => continue,
};
let bi = match order.find(&b) {
Some(i) => i,
None => continue,
};
if ai >= bi {
println!("{a:?} > {b:?} ");
return false;
}
}
true
}*/
/*#[test]
fn full_topo() {
fn prop(Dag(gr): Dag<()>) -> bool {
let order = toposort(&gr, None).unwrap();
is_topo_order(&gr, &order)
}
quickcheck::quickcheck(prop as fn(_) -> bool);
}*/
/*#[test]
fn full_topo_generic() {
fn prop_generic(Dag(mut gr): Dag<usize>) -> bool {
assert!(!is_cyclic_directed(&gr));
let mut index = 0;
let mut topo = Topo::new(&gr);
while let Some(nx) = topo.next(&gr) {
gr[nx] = index;
index += 1;
}
let mut order = Vec::new();
index = 0;
let mut topo = Topo::new(&gr);
while let Some(nx) = topo.next(&gr) {
order.push(nx);
assert_eq!(gr[nx], index);
index += 1;
}
if !is_topo_order(&gr, &order) {
println!("{gr:?}");
return false;
}
{
order.clear();
let mut topo = Topo::new(&gr);
while let Some(nx) = topo.next(&gr) {
order.push(nx);
}
if !is_topo_order(&gr, &order) {
println!("{gr:?}");
return false;
}
}
{
order.clear();
let init_nodes = gr.node_identifiers().filter(|n| {
gr.neighbors_directed(*n, Direction::Incoming)
.next()
.is_none()
});
let mut topo = Topo::with_initials(&gr, init_nodes);
while let Some(nx) = topo.next(&gr) {
order.push(nx);
}
if !is_topo_order(&gr, &order) {
println!("{gr:?}");
return false;
}
}
{
order.clear();
let mut topo = Topo::with_initials(&gr, gr.node_identifiers());
while let Some(nx) = topo.next(&gr) {
order.push(nx);
}
if !is_topo_order(&gr, &order) {
println!("{gr:?}");
return false;
}
}
true
}
quickcheck::quickcheck(prop_generic as fn(_) -> bool);
}*/
quickcheck! {
// checks that the distances computed by dijkstra satisfy the triangle
// inequality.
fn dijkstra_triangle_ineq(g: Graph<u32, u32>, node: usize) -> bool {
if g.node_count() == 0 {
return true;
}
let v = node_index(node % g.node_count());
let distances = dijkstra(&g, v, None, |e| *e.weight());
for v2 in distances.keys() {
let dv2 = distances[v2];
// triangle inequality:
// d(v,u) <= d(v,v2) + w(v2,u)
for edge in g.edges(*v2) {
let u = edge.target();
let w = edge.weight();
if distances.contains_key(&u) && distances[&u] > dv2 + w {
return false;
}
}
}
true
}
}
#[test]
// checks that the distances computed by astar satisfy the triangle inequality.
fn astar_triangle_ineq() {
fn prop(g: Graph<(), u32, Undirected>, start: usize, end: usize) -> bool {
if g.node_count() == 0 {
return true;
}
let start_node = node_index(start % g.node_count());
let end_node = node_index(end % g.node_count());
if let Some((distance, _)) = astar(
&g,
start_node,
|node| node == end_node,
|e| *e.weight(),
|_| 0,
) {
for v in g.node_indices() {
if let Some(edge) = g.find_edge(v, end_node) {
let weight = g.edge_weight(edge).unwrap();
// triangle inequality:
// d(start_node, end_node) <= d(start_node, v) + w(v, end_node)
if let Some((distance_v, _)) =
astar(&g, start_node, |node| node == v, |e| *e.weight(), |_| 0)
{
if distance > distance_v + *weight {
return false;
}
}
}
}
}
true
}
quickcheck::quickcheck(prop as fn(Graph<(), u32, Undirected>, usize, usize) -> bool);
}
#[test]
// checks that the distances computed by astar is equivalent to dijkstra
fn astar_compare_with_dijkstra() {
fn prop(g: Graph<(), u32, Undirected>, start: usize, end: usize) -> bool {
if g.node_count() == 0 {
return true;
}
let start_node = node_index(start % g.node_count());
let end_node = node_index(end % g.node_count());
let astar_output = astar(
&g,
start_node,
|node| node == end_node,
|e| *e.weight(),
|_| 0,
);
let dijkstra_output = dijkstra(&g, start_node, Some(end_node), |e| *e.weight());
if let Some((astar_distance, _)) = astar_output {
if let Some(dijkstra_distance) = dijkstra_output.get(&end_node) {
if astar_distance != *dijkstra_distance {
return false;
}
} else {
return false;
}
} else if dijkstra_output.get(&end_node).is_some() {
return false;
}
true
}
quickcheck::quickcheck(prop as fn(Graph<(), u32, Undirected>, usize, usize) -> bool);
}
quickcheck! {
// checks that the distances computed by k'th shortest path is always greater or equal compared to their dijkstra computation
fn k_shortest_path_(g: Graph<u32, u32>, node: usize) -> bool {
if g.node_count() == 0 {
return true;
}
let v = node_index(node % g.node_count());
let second_best_distances = k_shortest_path(&g, v, None, 2, |e| *e.weight());
let dijkstra_distances = dijkstra(&g, v, None, |e| *e.weight());
for v in second_best_distances.keys() {
if second_best_distances[v] < dijkstra_distances[v] {
return false;
}
}
true
}
}
quickcheck! {
fn bidirectional_dijkstra_directed(g: Graph<u32, u32, Directed>) -> bool {
test_bidirectional_dijkstra_impl(g)
}
fn bidirectional_dijkstra_undirected(g: Graph<u32, u32, Undirected>) -> bool {
test_bidirectional_dijkstra_impl(g)
}
}
fn test_bidirectional_dijkstra_impl<Ty>(g: Graph<u32, u32, Ty>) -> bool
where
Ty: EdgeType,
{
if g.node_count() <= 1 || g.node_count() > 50 {
return true;
}
for node1 in g.node_identifiers() {
let dijkstra_res = dijkstra(&g, node1, None, |e| *e.weight());
for node2 in g.node_identifiers() {
if node1 == node2 {
continue;
}
let bidirectional_dijkstra_res =
bidirectional_dijkstra(&g, node1, node2, |e| *e.weight());
match (dijkstra_res.get(&node2), bidirectional_dijkstra_res) {
(None, None) => continue,
(Some(distance), Some(bidirectional_distance)) => {
if *distance != bidirectional_distance {
return false;
}
}
// Both algorithms must find a solution for the same problem.
(Some(_), None) | (None, Some(_)) => return false,
}
}
}
true
}
quickcheck! {
// checks floyd_warshall against dijkstra results
fn floyd_warshall_(g: Graph<u32, u32>) -> bool {
if g.node_count() == 0 {
return true;
}
let fw_res = floyd_warshall(&g, |e| *e.weight()).unwrap();
for node1 in g.node_identifiers() {
let dijkstra_res = dijkstra(&g, node1, None, |e| *e.weight());
for node2 in g.node_identifiers() {
// if dijkstra found a path then the results must be same
if let Some(distance) = dijkstra_res.get(&node2) {
let floyd_distance = fw_res.get(&(node1, node2)).unwrap();
if distance != floyd_distance {
return false;
}
} else {
// if there are no path between two nodes then floyd_warshall will return maximum value possible
if *fw_res.get(&(node1, node2)).unwrap() != u32::MAX {
return false;
}
}
}
}
true
}
}
quickcheck! {
// checks that the complement of the complement is the same as the input if the input does not contain self-loops
fn complement_(g: Graph<u32, u32>, _node: usize) -> bool {
if g.node_count() == 0 {
return true;
}
for x in g.node_indices() {
if g.contains_edge(x, x) {
return true;
}
}
let mut complement_graph: Graph<u32, u32> = Graph::new();
let mut result: Graph<u32, u32> = Graph::new();
complement(&g, &mut complement_graph, 0);
complement(&complement_graph, &mut result, 0);
for x in g.node_indices() {
for y in g.node_indices() {
if g.contains_edge(x, y) != result.contains_edge(x, y){
return false;
}
}
}
true
}
}
fn set<I>(iter: I) -> HashSet<I::Item>
where
I: IntoIterator,
I::Item: Hash + Eq,
{
iter.into_iter().collect()
}
quickcheck! {
fn dfs_visit(gr: Graph<(), ()>, node: usize) -> bool {
use petgraph::visit::{Visitable, VisitMap};
use petgraph::visit::DfsEvent::*;
use petgraph::visit::{Time, depth_first_search};
if gr.node_count() == 0 {
return true;
}
let start_node = node_index(node % gr.node_count());
let invalid_time = Time(!0);
let mut discover_time = vec![invalid_time; gr.node_count()];
let mut finish_time = vec![invalid_time; gr.node_count()];
let mut has_tree_edge = gr.visit_map();
let mut edges = HashSet::new();
depth_first_search(&gr, Some(start_node).into_iter().chain(gr.node_indices()),
|evt| {
match evt {
Discover(n, t) => discover_time[n.index()] = t,
Finish(n, t) => finish_time[n.index()] = t,
TreeEdge(u, v) => {
// v is an ancestor of u
assert!(has_tree_edge.visit(v), "Two tree edges to {v:?}!");
assert!(discover_time[v.index()] == invalid_time);
assert!(discover_time[u.index()] != invalid_time);
assert!(finish_time[u.index()] == invalid_time);
edges.insert((u, v));
}
BackEdge(u, v) => {
// u is an ancestor of v
assert!(discover_time[v.index()] != invalid_time);
assert!(finish_time[v.index()] == invalid_time);
edges.insert((u, v));
}
CrossForwardEdge(u, v) => {
edges.insert((u, v));
}
}
});
assert!(discover_time.iter().all(|x| *x != invalid_time));
assert!(finish_time.iter().all(|x| *x != invalid_time));
assert_eq!(edges.len(), gr.edge_count());
assert_eq!(edges, set(gr.edge_references().map(|e| (e.source(), e.target()))));
true
}
}
quickcheck! {
fn test_bellman_ford(gr: Graph<(), f32>) -> bool {
let mut gr = gr;
for elt in gr.edge_weights_mut() {
*elt = elt.abs();
}
if gr.node_count() == 0 {
return true;
}
for (i, start) in gr.node_indices().enumerate() {
if i >= 10 { break; } // testing all is too slow
if bellman_ford(&gr, start).is_err() {
return false;
}
}
true
}
}
quickcheck! {
fn test_find_negative_cycle(gr: Graph<(), f32>) -> bool {
if gr.node_count() == 0 {
return true;
}
for (i, start) in gr.node_indices().enumerate() {
if i >= 10 { break; } // testing all is too slow
if let Some(path) = find_negative_cycle(&gr, start) {
assert!(!path.is_empty());
}
}
true
}
}
quickcheck! {
fn test_bellman_ford_undir(gr: Graph<(), f32, Undirected>) -> bool {
let mut gr = gr;
for elt in gr.edge_weights_mut() {
*elt = elt.abs();
}
if gr.node_count() == 0 {
return true;
}
for (i, start) in gr.node_indices().enumerate() {
if i >= 10 { break; } // testing all is too slow
if bellman_ford(&gr, start).is_err() {
return false;
}
}
true
}
}
/*defmac!(iter_eq a, b => a.eq(b));
defmac!(nodes_eq ref a, ref b => a.node_references().eq(b.node_references()));
defmac!(edgew_eq ref a, ref b => a.edge_references().eq(b.edge_references()));
defmac!(edges_eq ref a, ref b =>
iter_eq!(
a.edge_references().map(|e| (e.source(), e.target())),
b.edge_references().map(|e| (e.source(), e.target()))));*/
quickcheck! {
/*fn test_di_from(gr1: DiGraph<i32, i32>) -> () {
let sgr = StableGraph::from(gr1.clone());
let gr2 = Graph::from(sgr);
assert!(nodes_eq!(&gr1, &gr2));
assert!(edgew_eq!(&gr1, &gr2));
assert!(edges_eq!(&gr1, &gr2));
}
fn test_un_from(gr1: UnGraph<i32, i32>) -> () {
let sgr = StableGraph::from(gr1.clone());
let gr2 = Graph::from(sgr);
assert!(nodes_eq!(&gr1, &gr2));
assert!(edgew_eq!(&gr1, &gr2));
assert!(edges_eq!(&gr1, &gr2));
}*/
fn test_graph_from_stable_graph(gr1: StableDiGraph<usize, usize>) -> () {
let mut gr1 = gr1;
let gr2 = Graph::from(gr1.clone());
// renumber the stablegraph nodes and put the new index in the
// associated data
let mut index = 0;
for i in 0..gr1.node_bound() {
let ni = node_index(i);
if gr1.contains_node(ni) {
gr1[ni] = index;
index += 1;
}
}
if let Some(edge_bound) = gr1.edge_references().next_back()
.map(|ed| ed.id().index() + 1)
{
index = 0;
for i in 0..edge_bound {
let ni = edge_index(i);
if gr1.edge_weight(ni).is_some() {
gr1[ni] = index;
index += 1;
}
}
}
assert_equal(
// Remap the stablegraph to compact indices
gr1.edge_references().map(|ed| (edge_index(*ed.weight()), gr1[ed.source()], gr1[ed.target()])),
gr2.edge_references().map(|ed| (ed.id(), ed.source().index(), ed.target().index()))
);
}
/*fn stable_di_graph_map_id(gr1: StableDiGraph<usize, usize>) -> () {
let gr2 = gr1.map(|_, &nw| nw, |_, &ew| ew);
assert!(nodes_eq!(&gr1, &gr2));
assert!(edgew_eq!(&gr1, &gr2));
assert!(edges_eq!(&gr1, &gr2));
}
fn stable_un_graph_map_id(gr1: StableUnGraph<usize, usize>) -> () {
let gr2 = gr1.map(|_, &nw| nw, |_, &ew| ew);
assert!(nodes_eq!(&gr1, &gr2));
assert!(edgew_eq!(&gr1, &gr2));
assert!(edges_eq!(&gr1, &gr2));
}
fn stable_di_graph_filter_map_id(gr1: StableDiGraph<usize, usize>) -> () {
let gr2 = gr1.filter_map(|_, &nw| Some(nw), |_, &ew| Some(ew));
assert!(nodes_eq!(&gr1, &gr2));
assert!(edgew_eq!(&gr1, &gr2));
assert!(edges_eq!(&gr1, &gr2));
}
fn test_stable_un_graph_filter_map_id(gr1: StableUnGraph<usize, usize>) -> () {
let gr2 = gr1.filter_map(|_, &nw| Some(nw), |_, &ew| Some(ew));
assert!(nodes_eq!(&gr1, &gr2));
assert!(edgew_eq!(&gr1, &gr2));
assert!(edges_eq!(&gr1, &gr2));
}*/
fn stable_di_graph_filter_map_remove(gr1: Small<StableDiGraph<i32, i32>>,
nodes: Vec<usize>,
edges: Vec<usize>) -> ()
{
let gr2 = gr1.filter_map(|ix, &nw| {
if !nodes.contains(&ix.index()) { Some(nw) } else { None }
},
|ix, &ew| {
if !edges.contains(&ix.index()) { Some(ew) } else { None }
});
let check_nodes = &set(gr1.node_indices()) - &set(cloned(&nodes).map(node_index));
let mut check_edges = &set(gr1.edge_indices()) - &set(cloned(&edges).map(edge_index));
// remove all edges with endpoint in removed nodes
for edge in gr1.edge_references() {
if nodes.contains(&edge.source().index()) ||
nodes.contains(&edge.target().index()) {
check_edges.remove(&edge.id());
}
}
// assert maintained
for i in check_nodes {
assert_eq!(gr1[i], gr2[i]);
}
for i in check_edges {
assert_eq!(gr1[i], gr2[i]);
assert_eq!(gr1.edge_endpoints(i), gr2.edge_endpoints(i));
}
// assert removals
for i in nodes {
assert!(gr2.node_weight(node_index(i)).is_none());
}
for i in edges {
assert!(gr2.edge_weight(edge_index(i)).is_none());
}
}
}
fn naive_closure_foreach<G, F>(g: G, mut f: F)
where
G: Visitable + IntoNeighbors + IntoNodeIdentifiers,
F: FnMut(G::NodeId, G::NodeId),
{
let mut dfs = Dfs::empty(&g);
for i in g.node_identifiers() {
dfs.reset(&g);
dfs.move_to(i);
while let Some(nx) = dfs.next(&g) {
if i != nx {
f(i, nx);
}
}
}
}
fn naive_closure<G>(g: G) -> Vec<(G::NodeId, G::NodeId)>
where
G: Visitable + IntoNodeIdentifiers + IntoNeighbors,
{
let mut res = Vec::new();
naive_closure_foreach(g, |a, b| res.push((a, b)));
res
}
fn naive_closure_edgecount<G>(g: G) -> usize
where
G: Visitable + IntoNodeIdentifiers + IntoNeighbors,
{
let mut res = 0;
naive_closure_foreach(g, |_, _| res += 1);
res
}
quickcheck! {
fn test_tred(g: Dag<()>) -> bool {
let acyclic = g.0;
println!("acyclic graph {:#?}", &acyclic);
let toposort = toposort(&acyclic, None).unwrap();
println!("Toposort:");
for (new, old) in toposort.iter().enumerate() {
println!("{} -> {}", old.index(), new);
}
let (toposorted, revtopo): (petgraph::adj::List<(), usize>, _) =
petgraph::algo::tred::dag_to_toposorted_adjacency_list(&acyclic, &toposort);
println!("checking revtopo");
for (i, ix) in toposort.iter().enumerate() {
assert_eq!(i, revtopo[ix.index()]);
}
println!("toposorted adjacency list: {:#?}", &toposorted);
let (tred, tclos) = petgraph::algo::tred::dag_transitive_reduction_closure(&toposorted);
println!("tred: {:#?}", &tred);
println!("tclos: {:#?}", &tclos);
if tred.node_count() != tclos.node_count() {
println!("Different node count");
return false;
}
if acyclic.node_count() != tclos.node_count() {
println!("Different node count from original graph");
return false;
}
// check the closure
let mut clos_edges: Vec<(_, _)> = tclos.edge_references().map(|i| (i.source(), i.target())).collect();
clos_edges.sort();
let mut tred_closure = naive_closure(&tred);
tred_closure.sort();
if tred_closure != clos_edges {
println!("tclos is not the transitive closure of tred");
return false
}
// check the transitive reduction is a transitive reduction
for i in tred.edge_references() {
let filtered = EdgeFiltered::from_fn(&tred, |edge| {
edge.source() !=i.source() || edge.target() != i.target()
});
let new = naive_closure_edgecount(&filtered);
if new >= clos_edges.len() {
println!("when removing ({} -> {}) the transitive closure does not shrink",
i.source().index(), i.target().index());
return false
}
}
// check that the transitive reduction is included in the original graph
for i in tred.edge_references() {
if acyclic.find_edge(toposort[i.source().index()], toposort[i.target().index()]).is_none() {
println!("tred is not included in the original graph");
return false
}
}
println!("ok!");
true
}
}
quickcheck! {
fn greedy_fas_remaining_graph_is_acyclic(g: StableDiGraph<(), ()>) -> bool {
let mut g = g;
let fas: Vec<EdgeIndex> = greedy_feedback_arc_set(&g).map(|e| e.id()).collect();
for edge_id in fas {
g.remove_edge(edge_id);
}
!is_cyclic_directed(&g)
}
/// Assert that the size of the feedback arc set of a tournament does not exceed
/// **|E| / 2 - |V| / 6**
fn greedy_fas_performance_within_bound(t: Tournament<(), ()>) -> bool {
let Tournament(g) = t;
let expected_bound = if g.node_count() < 2 {
0
} else {
((g.edge_count() as f64) / 2.0 - (g.node_count() as f64) / 6.0) as usize
};
let fas_size = greedy_feedback_arc_set(&g).count();
fas_size <= expected_bound
}
}
fn is_valid_matching<G: NodeIndexable>(m: &Matching<G>) -> bool {
// A set of edges is a matching if no two edges from the matching share an
// endpoint.
for (s1, t1) in m.edges() {
for (s2, t2) in m.edges() {
if s1 == s2 && t1 == t2 {
continue;
}
if s1 == s2 || s1 == t2 || t1 == s2 || t1 == t2 {
// Two edges share an endpoint.
return false;
}
}
}
true
}
fn is_maximum_matching<G: NodeIndexable + IntoEdges + IntoNodeIdentifiers + Visitable>(
g: G,
m: &Matching<G>,
) -> bool {
// Berge's lemma: a matching is maximum iff there is no augmenting path (a
// path that starts and ends in unmatched vertices, and alternates between
// matched and unmatched edges). Thus if we find an augmenting path, the
// matching is not maximum.
//
// Start with an unmatched node and traverse the graph alternating matched
// and unmatched edges. If an unmatched node is found, then an augmenting
// path was found.
for unmatched in g.node_identifiers().filter(|u| !m.contains_node(*u)) {
let visited = &mut g.visit_map();
let mut stack = Vec::new();
stack.push((unmatched, false));
while let Some((u, do_matched_edges)) = stack.pop() {
if visited.visit(u) {
for e in g.edges(u) {
if e.source() == e.target() {
// Ignore self-loops.
continue;
}
let is_matched = m.contains_edge(e.source(), e.target());
if do_matched_edges && is_matched || !do_matched_edges && !is_matched {
stack.push((e.target(), !do_matched_edges));
// Found another free node (other than the starting one)
// that is unmatched - an augmenting path.
if !is_matched && !m.contains_node(e.target()) && e.target() != unmatched {
return false;
}
}
}
}
}
}
true
}
fn is_perfect_matching<G: NodeCount + NodeIndexable>(g: G, m: &Matching<G>) -> bool {
// By definition.
g.node_count() % 2 == 0 && m.edges().count() == g.node_count() / 2
}
quickcheck! {
fn matching(g: Graph<(), (), Undirected>) -> bool {
let m1 = greedy_matching(&g);
let m2 = maximum_matching(&g);
assert!(is_valid_matching(&m1), "greedy_matching returned an invalid matching");
assert!(is_valid_matching(&m2), "maximum_matching returned an invalid matching");
assert!(is_maximum_matching(&g, &m2), "maximum_matching returned a matching that is not maximum");
assert_eq!(m1.is_perfect(), is_perfect_matching(&g, &m1), "greedy_matching incorrectly determined whether the matching is perfect");
assert_eq!(m2.is_perfect(), is_perfect_matching(&g, &m2), "maximum_matching incorrectly determined whether the matching is perfect");
true
}
fn matching_in_stable_graph(g: StableGraph<(), (), Undirected>) -> bool {
let m1 = greedy_matching(&g);
let m2 = maximum_matching(&g);
assert!(is_valid_matching(&m1), "greedy_matching returned an invalid matching");
assert!(is_valid_matching(&m2), "maximum_matching returned an invalid matching");
assert!(is_maximum_matching(&g, &m2), "maximum_matching returned a matching that is not maximum");
assert_eq!(m1.is_perfect(), is_perfect_matching(&g, &m1), "greedy_matching incorrectly determined whether the matching is perfect");
assert_eq!(m2.is_perfect(), is_perfect_matching(&g, &m2), "maximum_matching incorrectly determined whether the matching is perfect");
true
}
}
quickcheck! {
fn test_bridges(g: Graph<(), (), Undirected>) -> bool {
let num = connected_components(&g);
let br = bridges(&g).map(|edge| edge.id()).collect::<HashSet<_>>();
for &edge in &br {
let mut graph = g.clone();
graph.remove_edge(edge);
assert_eq!(connected_components(&graph), num+1);
}
for e in g.edge_references() {
if !br.contains(&e.id()) {
let mut graph = g.clone();
graph.remove_edge(e.id());
assert_eq!(connected_components(&graph), num);
}
}
true
}
}
quickcheck! {
// The ranks are probabilities,
// as such they are positive and they should sum up to 1.
fn test_page_rank_proba(gr: Graph<(), f32>) -> bool {
if gr.node_count() == 0 {
return true;
}
let tol = 1e-10;
let ranks: Vec<f64> = page_rank(&gr, 0.85_f64, 5);
let at_least_one_neg_rank = ranks.iter().any(|rank| *rank < 0.);
let not_sumup_to_one = (ranks.iter().sum::<f64>() - 1.).abs() > tol;
if at_least_one_neg_rank | not_sumup_to_one{
return false;
}
true
}
}
fn sum_flows<N, F: core::iter::Sum + Copy>(
gr: &Graph<N, F>,
flows: &[F],
node: NodeIndex,
dir: Direction,
) -> F {
gr.edges_directed(node, dir)
.map(|edge| flows[EdgeIndexable::to_index(&gr, edge.id())])
.sum::<F>()
}
quickcheck! {
// 1. (Capacity)
// The flows should be <= capacities
// 2. (Flow conservation)
// For every internal node (i.e a node different from the
// source node and the destination (or sink) node), the sum
// of incoming flows (i.e flows of incoming edges) is equal
// to the sum of the outgoing flows (i.e flows of outgoing edges).
// 3. (Maximum flow)
// It is equal to the sum of the destination node incoming flows and
// also the sum of the outgoing flows of the source node.
fn test_ford_fulkerson_flows(gr: Graph<usize, u32>) -> bool {
if gr.node_count() <= 1 || gr.edge_count() == 0 {
return true;
}
let source = NodeIndex::from(0);
let destination = NodeIndex::from(gr.node_count() as u32 / 2);
let (max_flow, flows) = ford_fulkerson(&gr, source, destination);
let capacity_constraint = flows
.iter()
.enumerate()
.all(|(ix, flow)| flow <= gr.edge_weight(EdgeIndexable::from_index(&gr, ix)).unwrap());
let flow_conservation_constraint = (0..gr.node_count()).all(|ix| {
let node = NodeIndexable::from_index(&gr, ix);
if (node != source) && (node != destination){
sum_flows(&gr, &flows, node, Direction::Outgoing)
== sum_flows(&gr, &flows, node, Direction::Incoming)
} else {true}
});
let max_flow_constaint = (sum_flows(&gr, &flows, source, Direction::Outgoing) == max_flow)
&& (sum_flows(&gr, &flows, destination, Direction::Incoming) == max_flow);
return capacity_constraint && flow_conservation_constraint && max_flow_constaint;
}
}
quickcheck! {
fn test_dynamic_toposort(g: DiGraph<(), ()>) -> bool {
use petgraph::acyclic::Acyclic;
use petgraph::data::{Build, Create};
use petgraph::algo::toposort;
use alloc::collections::BTreeMap;
use core::iter;
// We will re-build `g` from scratch, adding edges one by one.
let mut acylic_g =
Acyclic::<DiGraph<(), ()>>::with_capacity(g.node_count(), g.edge_count());
let mut new_g = DiGraph::<(), ()>::new();
// This test is quite slow, so we bound the number of nodes.
const MAX_NODES: usize = 30;
let nodes: BTreeSet<_> = g.node_indices().take(MAX_NODES).collect();
// Add all nodes
let acyclic_nodes: BTreeMap<_, _> = nodes
.iter()
.zip(iter::repeat_with(|| acylic_g.add_node(())))
.collect();
let new_nodes: BTreeMap<_, _> = nodes
.iter()
.zip(iter::repeat_with(|| new_g.add_node(())))
.collect();
// Now add edges one by one
for e in g.edge_indices() {
let (src, dst) = g.edge_endpoints(e).unwrap();
if !nodes.contains(&src) || !nodes.contains(&dst) {
continue;
}
let new_g_backup = new_g.clone();
// Add the edge to the new graph
new_g.add_edge(new_nodes[&src], new_nodes[&dst], ());
let is_dag_exp = toposort(&new_g, None).is_ok();
// Add the edge to the acyclic graph
let is_dag_dyn = acylic_g
.try_add_edge(acyclic_nodes[&src], acyclic_nodes[&dst], ())
.is_ok();
// Check that both approaches agree on whether the graph is a DAG
assert_eq!(is_dag_exp, is_dag_dyn);
if !is_dag_exp {
// Remove the edge that makes it non-acyclic
new_g = new_g_backup;
}
}
true
}
}
fn is_proper_coloring<G>(g: G, coloring: &HashMap<G::NodeId, usize>) -> bool
where
G: IntoNodeIdentifiers + IntoEdges,
G::NodeId: Eq + Hash,
{
for node in g.node_identifiers() {
for nbor in g.neighbors(node) {
if node != nbor && coloring[&node] == coloring[&nbor] {
return false;
}
}
}
true
}
quickcheck! {
fn dsatur_coloring_quickcheck(g: Graph<(), (), Undirected>) -> bool {
let (coloring, _) = dsatur_coloring(&g);
assert!(is_proper_coloring(&g, &coloring), "dsatur_coloring returned a non proper coloring");
true
}
}
quickcheck! {
// Test that removal of articulation points will always increase the amount of connected components.
fn test_articulation_points(g: Graph<(), u32, Undirected>) -> bool {
let articulation_points = articulation_points(&g);
let original_components = connected_components(&g);
for point in articulation_points {
let mut modified_graph = g.clone();
modified_graph.remove_node(point);
let new_components = connected_components(&modified_graph);
if new_components <= original_components {
return false;
}
}
true
}
}
#[cfg(feature = "stable_graph")]
#[test]
fn steiner_tree_spans_terminals() {
fn prop(g: UnGraph<(), u32>) -> bool {
if g.node_count() <= 1 {
return true; // We naturally don't support steiner trees with zero or one node
}
// Run the steiner tree algorithm on connected components, to test it on both
// connected and disconnected graphs.
let mut connected_components = Vec::new();
let mut visited = g.visit_map();
for node in g.node_indices() {
if !visited.is_visited(&node) {
let mut component = HashSet::new();
let mut dfs = Dfs::new(&g, node);
while let Some(nx) = dfs.next(&g) {
visited.visit(nx);
component.insert(nx);
}
connected_components.push(component);
}
}
for component in connected_components {
if component.len() < 2 {
continue; // We naturally don't support steiner trees with zero or one node
} else {
let g = g.filter_map(
|node_index, _| {
if component.contains(&node_index) {
Some(())
} else {
None
}
},
|edge_index, edge_weight| {
let edge = g.edge_endpoints(edge_index).unwrap();
if component.contains(&(edge.0)) && component.contains(&(edge.1)) {
Some(*edge_weight)
} else {
None
}
},
);
let terminals = g.node_indices().take(5).collect::<Vec<_>>();
let m_steiner_tree = steiner_tree(&g, &terminals);
let steiner_tree_nodes: Vec<NodeIndex> = m_steiner_tree.node_indices().collect();
let spans_terminals = terminals.iter().all(|&t| steiner_tree_nodes.contains(&t));
if !spans_terminals {
return false; // The steiner tree does not span all terminals
}
}
}
true
}
quickcheck::quickcheck(prop as fn(Graph<(), u32, Undirected>) -> bool);
}
#[test]
fn maximal_cliques_matches_ref_impl() {
use maximal_cliques::maximal_cliques_ref;
fn prop<Ty>(g: Graph<(), (), Ty>) -> bool
where
Ty: EdgeType,
{
// Our implementations of maximal cliques only works for undirected graphs
// or symmetric directed graphs. So we filter out directed edges if needed.
let g = if Ty::is_directed() {
g.filter_map(
|_, _| Some(()),
|edge_index, _| {
let (source, target) = g.edge_endpoints(edge_index).unwrap();
if g.contains_edge(target, source) {
Some(())
} else {
None
}
},
)
} else {
g
};
if g.edge_count() <= 200 && g.node_count() <= 200 {
let cliques = maximal_cliques_algo(&g);
let cliques_ref = maximal_cliques_ref(&g);
assert!(cliques.len() == cliques_ref.len(),
"Maximal cliques algo returned different number of cliques than the reference implementation: {} != {}",
cliques.len(),
cliques_ref.len()
);
for c in &cliques_ref {
assert!(
cliques.contains(c),
"Ref Clique {c:?} not found in the result of maximal_cliques_algo: {cliques:?}"
);
}
}
true
}
quickcheck::quickcheck(prop as fn(Graph<_, _, Undirected>) -> bool);
quickcheck::quickcheck(prop as fn(Graph<_, _, Directed>) -> bool);
}
quickcheck! {
fn test_spfa(gr: Graph<(), f32>) -> bool {
let mut gr = gr;
for elt in gr.edge_weights_mut() {
*elt = elt.abs();
}
if gr.node_count() == 0 {
return true;
}
for (i, start) in gr.node_indices().enumerate() {
if i >= 10 { break; } // testing all is too slow
let spfa_res = spfa(&gr, start, |edge| *edge.weight());
let bf_res = bellman_ford(&gr, start);
// We only compare the predecessors, since the algorithms use different actual values
// to represent inf weights.
if spfa_res.map(|p| p.predecessors) != bf_res.map(|p| p.predecessors) {
return false;
}
}
true
}
}
quickcheck! {
fn test_spfa_undir(gr: Graph<(), f32, Undirected>) -> bool {
let mut gr = gr;
for elt in gr.edge_weights_mut() {
*elt = elt.abs();
}
if gr.node_count() == 0 {
return true;
}
for (i, start) in gr.node_indices().enumerate() {
if i >= 10 { break; } // testing all is too slow
let spfa_res = spfa(&gr, start, |edge| *edge.weight());
let bf_res = bellman_ford(&gr, start);
// We only compare the predecessors, since the algorithms use different actual values
// to represent inf weight.
if spfa_res.map(|p| p.predecessors) != bf_res.map(|p| p.predecessors) {
return false;
}
}
true
}
}
quickcheck! {
// checks johnson against dijkstra results
fn johnson_(g: Graph<u32, u32>) -> bool {
if g.node_count() == 0 {
return true;
}
let johnson_res = johnson(&g, |e| *e.weight()).unwrap();
for node1 in g.node_identifiers() {
let dijkstra_res = dijkstra(&g, node1, None, |e| *e.weight());
for node2 in g.node_identifiers() {
// The results must be same
if johnson_res.get(&(node1, node2)) != dijkstra_res.get(&node2) {
return false;
}
}
}
true
}
}
#[cfg(feature = "rayon")]
quickcheck! {
// checks parallel_johnson against dijkstra results
fn parallel_johnson_(g: Graph<u32, u32>) -> bool {
if g.node_count() == 0 {
return true;
}
let johnson_res = parallel_johnson(&g, |e| *e.weight()).unwrap();
for node1 in g.node_identifiers() {
let dijkstra_res = dijkstra(&g, node1, None, |e| *e.weight());
for node2 in g.node_identifiers() {
// The results must be same
if johnson_res.get(&(node1, node2)) != dijkstra_res.get(&node2) {
return false;
}
}
}
true
}
}
|