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
use{
super::iterators::*,
crate::{
traits::*,
iter::*,
GraphErrors,
graph::{
NodeContainer,
Graph
}
},
sampling::histogram::{
HistUsizeFast,
HistogramVal
},
std::{
convert::*,
collections::*,
marker::*,
io::Write
},
rand::Rng
};
use std::num::NonZeroUsize;
#[cfg(feature = "serde_support")]
use serde::{Serialize, Deserialize};
/// # Generic graph implementation
/// * contains multiple measurable quantities
#[derive(Debug, Clone)]
#[cfg_attr(feature = "serde_support", derive(Serialize, Deserialize))]
pub struct GenericGraph<T, A>
{
pub(crate) next_id: usize,
pub(crate) edge_count: usize,
pub(crate) vertices: Vec<A>,
pub(crate) phantom: PhantomData<T>,
}
impl<T, A> GenericGraph<T, A>
where T: Node,
A: AdjContainer<T> {
/// Create new graph with `size` nodes
/// and no edges
pub fn new(size: usize) -> Self {
let mut vertices = Vec::with_capacity(size);
vertices.extend(
(0..size)
.map(|i| A::new(i, T::new_from_index(i)))
);
Self{
vertices,
next_id: size,
edge_count: 0,
phantom: PhantomData,
}
}
/// Use the topology from another graph and create a new one with
/// the same topology but the function 'map' is used to
/// map what is contained in the original Graph to the newly created one.
pub fn clone_topology<F, T2>(&self, mut map: F) -> GenericGraph<T2, NodeContainer<T2>>
where F: FnMut (&T) -> T2
{
let nodes: Vec<_> = self.vertices
.iter()
.enumerate()
.map(
|(index,node)|
{
let neighbors = node.neighbors().into_vec();
NodeContainer{
id: index,
adj: neighbors,
node: map(node.contained())
}
}
).collect();
GenericGraph{
next_id: nodes.len(),
edge_count: self.edge_count,
vertices: nodes,
phantom: PhantomData
}
}
}
impl<T, A> GenericGraph<T, A>
{
/// Get internal vertice slice
pub fn get_vertices(&self) -> &[A]
{
&self.vertices
}
}
impl<T, A> GenericGraph<T, A>
where A: AdjContainer<T>
{
/// # create a new graph
/// * graph will contain `contained.len()` vertices, which will contain the corresponding entries
/// of the vector `contained`
/// * graph will not contain any edges upon creation
/// ```
/// use net_ensembles::{GenericGraph, graph::NodeContainer};
///
/// let graph = GenericGraph::<_, NodeContainer::<_>>::from_vec(vec!["first", "second", "third"]);
/// assert_eq!(graph.at(0), &"first");
/// assert_eq!(graph.at(1), &"second");
/// assert_eq!(graph.at(2), &"third");
/// assert_eq!(graph.vertex_count(), 3);
/// assert_eq!(graph.edge_count(), 0);
/// ```
pub fn from_vec(contained: Vec<T>) -> Self
{
let vertices: Vec<_> = contained.into_iter()
.enumerate()
.map(|(index, t)| A::new(index, t))
.collect();
Self{
next_id: vertices.len(),
vertices,
edge_count: 0,
phantom: PhantomData
}
}
}
impl<T, A> GenericGraph<T, A>
where A: AdjContainer<T>
{
/// # removes all edges from the graph
/// * inexpensive O(1), if there are no edges to begin with
/// * O(vertices) otherwise
pub fn clear_edges(&mut self) {
if self.edge_count() != 0 {
self.edge_count = 0;
for container in self.vertices.iter_mut() {
unsafe { container.clear_edges(); }
}
}
}
/// # initialize Ring
/// * every node is connected with its neighbors, which are
/// not more than distance away
pub(crate) fn init_ring(&mut self, distance: NonZeroUsize) -> Result<(), GraphErrors>
{
self.clear_edges();
let n = self.vertex_count();
for i in 0..n
{
for add in 1..=distance.get(){
let sum = i + add;
let index2 = if sum >= n {
sum - n
} else {
sum
};
self.add_edge(i, index2)?
}
}
Ok(())
}
/// # Sort adjecency lists
/// If you depend on the order of the adjecency lists, you can sort them
/// # Performance
/// * internally uses [pattern-defeating quicksort](https://github.com/orlp/pdqsort)
/// as long as that is the standard
/// * sorts an adjecency list with length `d` in worst-case: `O(d log(d))`
/// * is called for each adjecency list, i.e., `self.vertex_count()` times
pub fn sort_adj(&mut self) {
for container in self.vertices.iter_mut() {
container.sort_adj();
}
}
/// Shuffles all adjacency lists
/// * This will not change the topology. It will change the internal order and thereby
/// randomize the order of the neighbors in the respective iterators
pub fn shuffle_adjs<R: Rng>(&mut self, rng: &mut R)
{
self.vertices.iter_mut()
.for_each(
|v|
v.shuffle_adj(rng)
);
}
/// # Shuffles adjacency list
/// * panics if index is out of bounds
/// * This will not change the topology. It will change the internal order and thereby
/// randomize the order of the neighbors in the respective iterators
pub fn shuffle_adj<R: Rng>(&mut self, rng: &mut R, index: usize)
{
self.vertices[index].shuffle_adj(rng)
}
/// # get `AdjContainer` of vertex `index`
/// * **panics** if index out of bounds
pub fn container(&self, index: usize) -> &A {
&self.vertices[index]
}
/// # get `AdjContainer` of vertex `index`
/// * `None` if index is out of bounds
/// * `Some(&A)` else
pub fn container_checked(&self, index: usize) -> Option<&A>
{
self.vertices.get(index)
}
/// * get iterator over AdjContainer in order of the indices
/// * iterator returns `AdjContainer<Node>`
pub fn container_iter(&self) -> std::slice::Iter::<A> {
self.vertices.iter()
}
/// * iterate over `AdjContainer` of neighbors of vertex `index`
/// * iterator returns `AdjContainer<Node>`
/// * `sort_adj` will affect the order
///
/// If `let mut iter = self.contained_iter_neighbors()` is called directly after
/// `self.sort_adj()`, the following will be true (as long as `iter` does not return `None` of cause):
/// `iter.next().unwrap().id() < iter.next().unwrap.id() < ...` Note, that `...id()` returns the
/// index of the corresponding vertex
/// * **panics** if index out of bounds
pub fn container_iter_neighbors(&self, index: usize) -> NContainerIter<T, A, IterWrapper> {
NContainerIter::new(
self.vertices.as_slice(),
self.vertices[index].neighbors()
)
}
/// * get iterator over additional information stored at each vertex in order of the indices
/// * iterator returns a `Node` (for example `EmptyNode` or whatever you used)
/// * similar to `self.container_iter().map(|container| container.contained())`
pub fn contained_iter(&self) -> ContainedIter<T, A> {
ContainedIter::new(self.vertices.as_slice())
}
/// * same as `contained_iter`, but mutable
pub fn contained_iter_mut(&mut self) -> ContainedIterMut<T, A> {
ContainedIterMut::new (
self.vertices.iter_mut()
)
}
/// * iterate over additional information of neighbors of vertex `index`
/// * iterator returns `&T`
/// * `sort_adj` will affect the order
/// * **panics** if index out of bounds
pub fn contained_iter_neighbors(&self, index: usize) -> NContainedIter<T, A, IterWrapper> {
NContainedIter::new(
self.vertices.as_slice(),
self.vertices[index].neighbors()
)
}
/// * iterate over additional information of neighbors of vertex `index`
/// * iterator returns (`index_neighbor`,`&T`)
/// * `sort_adj` will affect the order
/// * **panics** if index out of bounds
pub fn contained_iter_neighbors_with_index(&self, index: usize) -> NIContainedIter<T, A> {
NIContainedIter::new(
self.vertices.as_slice(),
self.vertices[index].neighbors()
)
}
/// * iterate over mutable additional information of neighbors of vertex `index`
/// * iterator returns `&mut T`
/// * `sort_adj` will affect the order
/// * **panics** if index out of bounds
/// * See also: [`GraphIteratorsMut`](../traits/trait.GraphIteratorsMut.html)
pub fn contained_iter_neighbors_mut(&mut self, index: usize) -> NContainedIterMut<T, A, IterWrapper> {
assert!(
index < self.vertices.len(),
"contained_iter_neighbors_mut - index out of bounds"
);
let ptr = self.vertices.as_mut_ptr();
let iter_helper: &mut A = unsafe { &mut *ptr.add(index) };
let iter = iter_helper.neighbors();
NContainedIterMut::new(
self.vertices.as_mut_slice(),
iter
)
}
/// * iterate over mutable additional information of neighbors of vertex `index`
/// * iterator returns `(index_neighbor: usize, neighbor: &mut T)`
/// * `sort_adj` will affect the order
/// * **panics** if index out of bounds
/// * See also: [`GraphIteratorsMut`](../traits/trait.GraphIteratorsMut.html)
pub fn contained_iter_neighbors_mut_with_index(&mut self, index: usize) -> INContainedIterMut<T, A> {
assert!(
index < self.vertices.len(),
"contained_iter_neighbors_mut_with_index - index out of bounds"
);
let ptr = self.vertices.as_mut_ptr();
let iter_helper: &mut A = unsafe { &mut *ptr.add(index) };
let iter = iter_helper.neighbors();
INContainedIterMut::new(
self.vertices.as_mut_slice(),
iter
)
}
pub(crate) fn container_mut(&mut self, index: usize) -> &mut A {
&mut self.vertices[index]
}
/// # For your calculations etc.
/// * **read access** to **your struct** T, stored at **each vertex**, that implements `Node` trait
/// ## Note
/// * **panics** if index out of bounds
pub fn at(&self, index: usize) -> &T {
self.container(index).contained()
}
/// # For your calculations etc.
/// * **read access** to **your struct** T, stored at **each vertex**, that implements `Node` trait
/// * `None` if index is out of bounds
pub fn at_checked(&self, index: usize) -> Option<&T>
{
self.container_checked(index)
.map(|container| container.contained())
}
/// # For your calculations etc.
/// * **write access** to **your struct** T, stored at **each vertex**, that implements `Node` trait
/// # Note
/// * **panics** if index out of bounds
pub fn at_mut(&mut self, index: usize) -> &mut T {
self.container_mut(index).contained_mut()
}
/// returns number of vertices present in graph
pub fn vertex_count(&self) -> usize {
self.next_id
}
/// calculates the average degree of the graph
/// * `(2 * edge_count) / vertex_count`
pub fn average_degree(&self) -> f32 {
(2 * self.edge_count()) as f32 / self.vertex_count() as f32
}
/// # Get mutable vertex
/// * panics if index out of range
pub(crate) fn get_mut_unchecked(&mut self, index: usize) -> &mut A {
&mut self.vertices[index]
}
/// Returns two mutable references in a tuple
/// ## panics in debug if:
/// * index out of bounds
/// * if indices are not unique
pub(crate) fn get_2_mut(&mut self, index0: usize, index1: usize) -> (&mut A, &mut A)
{
debug_assert!(
index0 < self.next_id &&
index1 < self.next_id,
"net_ensembles - panic - index out of bounds! \
vertex_count: {}, index_0: {}, index1: {} - \
error probably results from trying to add or remove edges",
self.vertex_count(),
index0,
index1
);
debug_assert!(
index0 != index1,
"net_ensembles - panic - indices have to be unique! \
error probably results from trying to add or remove self-loops"
);
let r1: &mut A;
let r2: &mut A;
let ptr = self.vertices.as_mut_ptr();
unsafe {
r1 = &mut *ptr.add(index0);
r2 = &mut *ptr.add(index1);
}
(r1, r2)
}
/// Returns three mutable references in a tuple
/// ## panics:
/// * index out of bounds
/// * in debug: if indices are not unique
pub(crate) fn get_3_mut(&mut self, index0: usize, index1: usize, index2: usize) ->
(&mut A, &mut A, &mut A)
{
debug_assert!(
index0 < self.next_id &&
index1 < self.next_id &&
index2 < self.next_id
);
debug_assert!(
index0 != index1 &&
index1 != index2 &&
index2 != index0
);
let r1: &mut A;
let r2: &mut A;
let r3: &mut A;
let ptr = self.vertices.as_mut_ptr();
unsafe {
r1 = &mut *ptr.add(index0);
r2 = &mut *ptr.add(index1);
r3 = &mut *ptr.add(index2);
}
(r1, r2, r3)
}
/// Returns a reference to the element stored in the specified node or `None` if out of Bounds
pub fn get_contained(&self, index: usize) -> Option<&T>
{
self.vertices
.get(index)
.map(|v| v.contained())
}
/// Returns a mutable reference to the element stored in the specified node or `None` if out of Bounds
pub fn get_contained_mut(&mut self, index: usize) -> Option<&mut T>
{
self.vertices
.get_mut(index)
.map(|v| v.contained_mut())
}
/// Returns a reference to the element stored in the specified node
///
/// For a save alternative see [get_contained](`Self::get_contained`)
/// # Safety
/// Calling this method with an out-of-bounds index is [undefined behavior](https://doc.rust-lang.org/reference/behavior-considered-undefined.html) even if the resulting reference is not used.
pub unsafe fn get_contained_unchecked(&self, index: usize) -> &T
{
self.vertices
.get_unchecked(index)
.contained()
}
/// Returns a mutable reference to the element stored in the specified node
///
/// For a save alternative see [get_contained_mut](`Self::get_contained_mut`)
/// # Safety
/// Calling this method with an out-of-bounds index is [undefined behavior](https://doc.rust-lang.org/reference/behavior-considered-undefined.html) even if the resulting reference is not used.
pub unsafe fn get_contained_unchecked_mut(&mut self, index: usize) -> &mut T
{
self.vertices
.get_unchecked_mut(index)
.contained_mut()
}
/// Adds edge between nodes `index1` and `index2`
/// ## ErrorCases:
/// | Error | Reason |
/// | ---- | ---- |
/// | `GraphErrors::IndexOutOfRange` | `index1` or `index2` larger than `self.vertex_count()` |
/// | `GraphErrors::EdgeExists` | requested edge already exists! |
/// ## panics
/// * if indices out of bounds
/// * in debug: If `index0 == index1`
pub fn add_edge(&mut self, index1: usize, index2: usize) -> Result<(),GraphErrors> {
let (r1, r2) = self.get_2_mut(index1, index2);
unsafe{ r1.push(r2)?; }
self.edge_count += 1;
Ok(())
}
/// Removes edge between nodes *index1* and *index2*
/// ## ErrorCases:
/// | Error | Reason |
/// | ---- | ---- |
/// | `GraphErrors::IndexOutOfRange` | `index1` or `index2` larger than `self.vertex_count()` |
/// | `GraphErrors::EdgeDoesNotExist` | requested edge does not exists |
/// # panics
/// * if index out of bounds
/// * in debug: If `index0 == index1`
pub fn remove_edge(&mut self, index1: usize, index2: usize) -> Result<(),GraphErrors> {
let (r1, r2) = self.get_2_mut(index1, index2);
unsafe{ r1.remove(r2)?; }
self.edge_count -= 1;
Ok(())
}
/// returns total number of edges in graph
pub fn edge_count(&self) -> usize {
self.edge_count
}
/// * returns number of vertices adjacent to vertex `index`
/// * `None` if index out of bounds
pub fn degree(&self, index: usize) -> Option<usize> {
Some(
self
.vertices
.get(index)?
.degree()
)
}
/// # Iterator
/// Iterate over the degrees of each node (in the order of the indices)
pub fn degree_iter(&'_ self) -> impl Iterator<Item=usize> + '_
{
self.vertices
.iter()
.map(A::degree)
}
/// # get degree vector
/// * returns vector of length `self.vertex_count()`
/// where each entry corresponds to the degree of the vertex with the respective index
pub fn degree_vec(&self) -> Vec<usize>
{
let mut degree_vec = Vec::with_capacity(self.vertex_count());
degree_vec.extend(self.degree_iter());
degree_vec
}
/// # Get a histogram of the degrees
/// * You can look at your degree distribution with this
/// ## Safety
/// * Assumes you have at least one node, panics otherwise
pub fn degree_histogram(&self) -> HistUsizeFast
{
let mut iter = self.degree_iter();
let mut min = iter.next()
.expect("Expected at least one node");
let mut max = min;
iter.for_each(
|val|
{
if val < min {
min = val
} else if val > max {
max = val
}
}
);
let mut hist = match HistUsizeFast::new_inclusive(min, max)
{
Ok(hist) => hist,
Err(_) => unreachable!()
};
self.degree_iter()
.for_each(
|entry|
{
match hist.count_val(entry)
{
Ok(_) => (),
Err(_) => unreachable!(),
}
}
);
hist
}
/// # returns `Iterator`
///
/// * the iterator will iterate over the vertices in depth first search order,
/// beginning with vertex `index`.
/// * iterator returns what is contained at the corresponding vertices, i.e., `&T`
/// * iterator always returns None if index out of bounds
///
/// Order
///------------------------
/// Order is guaranteed to be in DFS order, however
/// if this order is not unambiguous
/// adding edges and especially removing edges will shuffle the order.
///
/// Note:
/// ----------------------
/// Will only iterate over vertices within the connected component that contains vertex `index`
pub fn dfs(&self, index: usize) -> Dfs<T, A> {
Dfs::new(self, index)
}
/// # returns `Iterator`
///
/// * the iterator will iterate over the vertices in depth first search order,
/// beginning with vertex `index`.
/// * iterator returns what is contained at the corresponding vertices, i.e., `&mut T`
/// * iterator always returns None if index out of bounds
///
/// Order
///------------------------
/// Order is guaranteed to be in DFS order, however
/// if this order is not unambiguous
/// adding edges and especially removing edges will shuffle the order.
///
/// Note:
/// ----------------------
/// Will only iterate over vertices within the connected component that contains vertex `index`
pub fn dfs_mut(&mut self, index: usize) -> DfsMut<T, A>
{
DfsMut::new(self, index)
}
/// # returns `Iterator`
///
/// * the iterator will iterate over the vertices in depth first search order,
/// beginning with vertex `index`.
/// * Iterator returns tuple `(index, node)`
/// * iterator always returns None if index out of bounds
///
/// Order
///------------------------
/// Order is guaranteed to be in DFS order, however
/// if this order is not unambigouse
/// adding edges and especially removing edges will shuffle the order.
///
/// Note:
/// ----------------------
/// Will only iterate over vertices within the connected component that contains vertex `index`
pub fn dfs_with_index(&self, index: usize) -> DfsWithIndex<T, A> {
DfsWithIndex::new(self, index)
}
/// # returns `Iterator`
///
/// * the iterator will iterate over the vertices in breadth first search order,
/// beginning with vertex `index`.
/// * Iterator returns tuple `(index, node, depth)` where `node` is what is contained at the
/// corresponding index, i.e., `&T`
///
/// ### depth
/// * starts at 0 (i.e. the first element in the iterator will have `depth = 0`)
/// * `depth` equals number of edges in the *shortest path* from the *current* vertex to the
/// *first* vertex (i.e. to the vertex with index `index`)
///
/// Order
///------------------------
/// Order is guaranteed to be in BFS order, however
/// if this order is not unambigouse
/// adding edges and especially removing edges will shuffle the order.
///
/// Note:
/// ----------------------
/// Will only iterate over vertices within the connected component that contains vertex `index`
pub fn bfs_index_depth(&self, index: usize) -> Bfs<T, A> {
Bfs::new(self, index)
}
/// # returns `Iterator`
///
/// * the iterator will iterate over the vertices in breadth first search order,
/// beginning with vertex `index`.
/// * Iterator returns tuple `(index, node, depth)` where `node` is what is contained at the
/// corresponding index, i.e., `&mut T`
///
/// ### depth
/// * starts at 0 (i.e. the first element in the iterator will have `depth = 0`)
/// * `depth` equals number of edges in the *shortest path* from the *current* vertex to the
/// *first* vertex (i.e. to the vertex with index `index`)
///
/// Order
///------------------------
/// Order is guaranteed to be in BFS order, however
/// if this order is not unambigouse
/// adding edges and especially removing edges will shuffle the order.
///
/// Note:
/// ----------------------
/// Will only iterate over vertices within the connected component that contains vertex `index`
pub fn bfs_index_depth_mut(&mut self, index: usize) -> BfsMut<T, A> {
BfsMut::new(self, index)
}
// # returns `Option<Iterator>`
/// * similar to self.bfs_index_depth, but allows for ignoring of vertices
///
/// * the iterator will iterate over the vertices in breadth first search order,
/// beginning with vertex `index`.
/// * the iterator will ignore all vertices, where `filter(vertex, index_of_vertex)` returns false
/// * returns `None`if index is out of bounds or `filter(vertex, index)`retruns false
/// * Iterator returns tuple `(index, vertex, depth)`
///
/// ### depth
/// * starts at 0 (i.e. the first element in the iterator will have `depth = 0`)
/// * `depth` equals number of edges in the *shortest path* from the *current* vertex to the
/// *first* vertex (i.e. to the vertex with index `index`), while ignoring paths that
/// go through filtered out vertices
///
/// Order
///------------------------
/// Order is guaranteed to be in BFS order, however
/// if this order is not unambigouse
/// adding edges and especially removing edges will shuffle the order.
///
/// Note:
/// ----------------------
/// Will only iterate over vertices within the connected component that contains vertex `index`
pub fn bfs_filtered<F>(&'_ self, index: usize, filter: F) -> Option<BfsFiltered<'_, T, A>>
where F: FnMut(&T, usize) -> bool,
{
BfsFiltered::new(self, index, filter)
}
/// | result | condition |
/// |--------------|----------------------------------------------------------|
/// | `None` | **if** graph does not contain any vertices |
/// | `Some(true)` | **else if** all vertices are connected by paths of edges |
/// | `Some(false)`| **otherwise** |
pub fn is_connected(&self) -> Option<bool> {
if self.vertex_count() == 0 {
None
} else {
Some(self.dfs(0).count() == self.vertex_count())
}
}
/// # definition
/// Calculates the size of the **q-core** (i.e. number of nodes in the biggest possible set of nodes,
/// where all nodes from the set are connected with at least `q` other nodes from the set)
///
/// returns `None` if impossible to calculate (e.g. `vertex_count == 0` or `q <= 1`)
/// # Example
/// ```
/// use net_ensembles::EmptyNode;
/// use net_ensembles::Graph;
///
/// let graph: Graph<EmptyNode> = Graph::new(0);
/// assert_eq!(graph.q_core(1), None);
/// assert_eq!(graph.q_core(2), None);
///
/// let graph2: Graph<EmptyNode> = Graph::new(1);
///
/// assert_eq!(graph2.q_core(1), None);
/// assert_eq!(graph2.q_core(2), Some(0));
///
///
/// // create complete graph
/// let mut graph3: Graph<EmptyNode> = Graph::new(20);
/// for i in 0..graph3.vertex_count() {
/// for j in i+1..graph3.vertex_count() {
/// graph3.add_edge(i, j).unwrap();
/// }
/// }
///
/// // since this is a complete graph, the q-core should always consist of 20 nodes
/// // as long as q < 20, as every node has 19 neighbors
/// for i in 2..20 {
/// assert_eq!(graph3.q_core(i), Some(20));
/// }
///
/// assert_eq!(graph3.q_core(20), Some(0));
/// ```
pub fn q_core(&self, q: usize) -> Option<usize> {
if q < 2 || self.vertex_count() == 0 {
return None;
}
let mut degree: Vec<_> = self.container_iter()
.map(|vertex| vertex.degree())
.collect();
// virtually: recursively remove all vertices with less then q neighbors
let mut something_changed = true;
while something_changed {
something_changed = false;
for i in 0..self.vertex_count() {
if degree[i] == 0 {
continue;
}
if degree[i] < q {
self.vertices[i]
.neighbors()
.for_each(|&n|
{
if degree[n] > 0 {
degree[n] -= 1;
}
}
);
degree[i] = 0;
something_changed = true;
}
}
}
// find biggest component
let mut result = 0;
// initiate stack
let mut stack: Vec<usize> = Vec::with_capacity(self.vertex_count());
for i in 0..self.vertex_count() {
// skip all nodes that are removed or in a known component
if degree[i] == 0 {
continue;
}
let mut counter = 0;
stack.push(i);
// i is in known component
degree[i] = 0;
while let Some(index) = stack.pop() {
counter += 1;
for &j in self
.container(index)
.neighbors() // iterate over neighbors
{
// skip if already handled
if degree[j] == 0 {
continue;
}
degree[j] = 0;
stack.push(j);
}
}
result = result.max(counter);
}
Some(result)
}
/// # compute connected component ids
/// * used for `self.connected_components()`
/// * each vertex gets an id, all vertices with the same id are in the same connected component
/// * returns (number of components, vector of ids)
pub fn connected_components_ids(&self) -> (usize, Vec<isize>)
{
let mut component_id : Vec<isize> = vec![-1; self.vertex_count()];
let mut current_id = 0;
for i in 0..self.vertex_count(){
// already in a component?
if component_id[i] != -1 {
continue;
}
// start depth first search over indices of vertices connected with vertex i
for (j, _) in self.dfs_with_index(i) {
component_id[j] = current_id;
}
current_id += 1;
}
// cast current_id as usize
let num_components = usize::try_from(current_id)
.expect("connected_components ERROR 0");
(num_components, component_id)
}
/// # compute sizes of all *connected components*
///
/// * the **number** of connected components is the **size** of the returned vector, i.e. `result.len()`
/// * returns **empty** vector, if graph does not contain vertices
/// * returns (reverse) **ordered vector of sizes** of the connected components,
/// i.e. the biggest component is of size `result[0]` and the smallest is of size `result[result.len() - 1]`
pub fn connected_components(&self) -> Vec<usize> {
let (num_components, component_id) = self.connected_components_ids();
let mut result = vec![0; num_components];
for i in component_id {
let as_usize = usize::try_from(i)
.expect("connected_components ERROR 1");
result[as_usize] += 1;
}
// sort by reverse
// unstable here means inplace and ordering of equal elements is not guaranteed
result.sort_unstable_by(
|a, b|
a.partial_cmp(b)
.unwrap()
.reverse()
);
result
}
/// # Connects connected components (CCs)
/// * returns vector of indices, where each corresponding node is in a different
/// connected component
pub(crate) fn suggest_connections(& self) -> Vec<usize>
{
let mut suggestions = Vec::new();
let mut component_id : Vec<i32> = vec![-1; self.vertex_count()];
let mut current_id = 0;
for i in 0..self.vertex_count(){
// already in a component?
if component_id[i] != -1 {
continue;
}
suggestions.push(i);
// start depth first search over indices of vertices connected with vertex i
for (j, _) in self.dfs_with_index(i) {
component_id[j] = current_id;
}
current_id += 1;
}
suggestions
}
/// Count number of leaves in the graph, i.e. vertices with exactly one neighbor
pub fn leaf_count(&self) -> usize {
self.vertices
.iter()
.filter(|a| a.degree() == 1)
.count()
}
/// * Creates String which contains the topology of the network in a format
/// that can be used by **circo** etc. to generate a pdf of the graph.
/// * **indices** are used as **labels**
/// * search for **graphviz** to learn about **.dot** format
#[deprecated(
since = "0.3.0",
note = "Please use any method of the `Dot` trait instead, e.g., `dot_with_indices`"
)]
pub fn to_dot(&self) -> String {
let mut s = "graph{\n\t".to_string();
for i in 0..self.vertex_count() {
let _ = std::fmt::Write::write_fmt(&mut s, format_args!("{i} "));
}
s += "\n";
for i in 0..self.vertex_count() {
for &j in self.container(i).neighbors() {
if i < j {
let _ = std::fmt::Write::write_fmt(&mut s, format_args!("\t{} -- {}\n", i, j));
}
}
}
s += "}";
s
}
/// # Example
/// ```
/// use std::fs::File;
/// use std::io::prelude::*;
/// use net_ensembles::{Graph, EmptyNode, dot_constants::EXAMPLE_DOT_OPTIONS};
///
/// let mut graph: Graph<EmptyNode> = Graph::new(3);
/// graph.add_edge(0, 1).unwrap();
/// graph.add_edge(0, 2).unwrap();
/// graph.add_edge(1, 2).unwrap();
///
/// // create string of dotfile
/// let s = graph.to_dot_with_labels_from_contained(
/// EXAMPLE_DOT_OPTIONS,
/// |_contained, index| format!("Hey {}!", index)
/// );
///
/// // write to file
/// let mut f = File::create("example.dot").expect("Unable to create file");
/// f.write_all(s.as_bytes()).expect("Unable to write data");
///
/// ```
/// In this example, `example.dot` now contains:
/// ```dot
/// graph G{
/// bgcolor="transparent";
/// fontsize=50;
/// node [shape=ellipse, penwidth=1, fontname="Courier", pin=true ];
/// splines=true;
/// 0 1 2 ;
/// "0" [label="Hey 0!"];
/// "1" [label="Hey 1!"];
/// "2" [label="Hey 2!"];
/// 0 -- 1
/// 0 -- 2
/// 1 -- 2
/// }
/// ```
///
/// Then you can use, e.g.,
/// ```console
/// foo@bar:~$ circo example.dot -Tpdf > example.pdf
/// ```
/// to create a **pdf** representation from it.
/// Search for **graphviz** to learn more.
#[deprecated(
since = "0.3.0",
note = "Please use any method of the `DotExtra` trait instead, e.g., `dot_from_contained_index`"
)]
pub fn to_dot_with_labels_from_contained<F, S1, S2>(&self, dot_options: S1, f: F ) -> String
where
S1: AsRef<str>,
S2: AsRef<str>,
F: Fn(&T, usize) -> S2
{
let mut writer = Vec::<u8>::with_capacity(40 * self.vertices.len());
self.dot_from_contained_index(
&mut writer,
dot_options,
|index, c|
f(c, index)
).unwrap();
String::from_utf8(writer)
.unwrap()
}
/// # Same as `to_dot_with_labels_from_contained` but with access to neighbor information
/// # Example
/// ```
/// use std::fs::File;
/// use std::io::prelude::*;
/// use net_ensembles::traits::*;
/// use net_ensembles::dot_constants::*;
/// use net_ensembles::{Graph,EmptyNode};
///
/// let mut graph: Graph<EmptyNode> = Graph::new(5);
/// graph.add_edge(0, 1).unwrap();
/// graph.add_edge(0, 2).unwrap();
/// graph.add_edge(1, 2).unwrap();
/// graph.add_edge(0, 3).unwrap();
/// graph.add_edge(3, 4).unwrap();
///
/// // create string of dotfile
/// let s = graph.to_dot_with_labels_from_container(
/// &[SPLINES, NO_OVERLAP].join("\n\t"),
/// |container, index|
/// {
/// container.contained(); // does nothing in this example, but you can still access
/// // contained, as you could in
/// // to_dot_with_labels_from_contained
/// format!("index {}, degree: {}", index, container.degree())
/// }
/// );
///
/// // write to file
/// let mut f = File::create("example_2.dot").expect("Unable to create file");
/// f.write_all(s.as_bytes()).expect("Unable to write data");
///
/// ```
/// In this example, `example_2.dot` now contains:
/// ```dot
/// graph G{
/// splines=true;
/// overlap=false;
/// 0 1 2 3 4 ;
/// "0" [label="index 0, degree: 3"];
/// "1" [label="index 1, degree: 2"];
/// "2" [label="index 2, degree: 2"];
/// "3" [label="index 3, degree: 2"];
/// "4" [label="index 4, degree: 1"];
/// 0 -- 1
/// 0 -- 2
/// 0 -- 3
/// 1 -- 2
/// 3 -- 4
/// }
/// ```
///
/// Then you can use, e.g.,
/// ```console
/// foo@bar:~$ circo example_2.dot -Tpdf > example_2.pdf
/// ```
/// to create a **pdf** representation from it.
/// Search for **graphviz** to learn more.
#[deprecated(
since = "0.3.0",
note = "Please use any method of the `DotExtra` trait instead, e.g., `dot_from_container_index`"
)]
pub fn to_dot_with_labels_from_container<F, S1, S2>(&self, dot_options: S1, f: F ) -> String
where
S1: AsRef<str>,
S2: AsRef<str>,
F: Fn(&A, usize) -> S2,
{
let mut writer = Vec::<u8>::with_capacity(40 * self.vertices.len());
self.dot_from_container_index(
&mut writer,
dot_options,
|index, c|
f(c, index)
).unwrap();
String::from_utf8(writer)
.unwrap()
}
/// * returns `None` **if** graph not connected **or** does not contain any vertices
/// * uses repeated breadth first search
pub fn diameter(&self) -> Option<usize> {
if !self.is_connected()? {
None
} else {
// well, then calculate from every node
// (except 1 node) and use maximum found
let mut max = 0;
let mut bfs = self.bfs_index_depth(0);
for index in 1..self.vertex_count() {
let mut depth = 0;
bfs.reuse(index);
for (.., d) in &mut bfs {
depth = d;
}
max = max.max(depth);
}
Some(max)
}
}
/// calculate the size of the longest shortest path **starting from** vertex with **index** `index`
/// using breadth first search
pub fn longest_shortest_path_from_index(&self, index: usize) -> Option<usize> {
let (.., depth) = self.bfs_index_depth(index)
.last()?;
Some(depth)
}
/// # calculate sizes of all binode connected components
/// * returns (reverse) **ordered vector of sizes**
/// i.e. the biggest component is of size `result[0]` and the smallest is of size `result[result.len() - 1]`
/// * destroys the underlying topology and therefore moves `self`
/// * if you still need your graph,
/// use `self.clone().vertex_biconnected_components(false/true)` for your calculations
/// # Definition: `vertex_biconnected_components(false)`
/// Here, the (vertex) biconnected component of a graph is defined as maximal subset of nodes,
/// where any one node could be removed and the remaining nodes would still be a connected component.
/// ## Note
/// Two vertices connected by an edge are considered to be biconnected, since after the
/// removal of one vertex (and the corresponding edge), only one vertex remains.
/// This vertex is in a connected component with itself.
/// # Alternative Definition: `vertex_biconnected_components(true)`
/// If you want to use the alternative definition:
/// > The biconnected component is defined as maximal subset of vertices, where each vertex can be
/// > reached by at least two node independent paths
///
/// The alternative definition just removes all 2s from the result vector.
/// # Citations
/// I used the algorithm described in this paper:
/// > J. Hobcroft and R. Tarjan, "Algorithm 447: Efficient Algorithms for Graph Manipulation"
/// > *Commun. ACM*, **16**:372-378, 1973, DOI: [10.1145/362248.362272](https://doi.org/10.1145/362248.362272)
///
/// You can also take a look at:
/// > M. E. J. Newman, "Networks: an Introduction" *Oxfort University Press*, 2010, ISBN: 978-0-19-920665-0.
pub fn vertex_biconnected_components(mut self, alternative_definition: bool) -> Vec<usize> {
let mut low: Vec<usize> = vec![0; self.vertex_count()];
let mut number: Vec<usize> = vec![0; self.vertex_count()];
let mut handled: Vec<bool> = vec![false; self.vertex_count()];
let mut edge_stack: Vec<(usize, usize)> = Vec::with_capacity(self.vertex_count());
let mut vertex_stack: Vec<usize> = Vec::with_capacity(self.vertex_count());
let mut biconnected_components: Vec<Vec<(usize, usize)>> = Vec::new();
let mut next_edge: (usize, usize);
for pivot in 0..self.vertex_count() {
if handled[pivot] {
continue;
}
low[pivot] = 0;
number[pivot] = 0;
handled[pivot] = true;
vertex_stack.push(pivot);
while let Some(&top_vertex) = vertex_stack.last() {
// if it has neighbors
// does the vertex have neighbors?
if self
.degree(top_vertex)
.unwrap() > 0
{
// remove one edge from graph, put it on stack
next_edge = (
top_vertex,
*self
.container(top_vertex)
.get_adj_first()
.unwrap()
);
edge_stack.push(next_edge);
let next_vertex = next_edge.1;
self.remove_edge(next_edge.0, next_edge.1).unwrap();
// check if next_vertex is not handled yet
if !handled[next_vertex] {
// number new point
number[next_vertex] = vertex_stack.len();
// add to stack of points
vertex_stack.push(next_edge.1);
// set LOWPOINT of the new point to NUMBER of current point
low[next_vertex] = number[top_vertex];
// now the point was visited once -> handled
handled[next_vertex] = true;
}
// Head of edge new point? NO -> Number of Head of edge lower than LOWPOINT of top point?
else if number[next_vertex] < low[top_vertex] {
// Set LOWPOINT of top Point to that number
low[top_vertex] = number[next_vertex];
}
}
// top point on stack has no edge
else {
vertex_stack.pop();
// at least one point in stack?
if let Some(&next_vertex) = vertex_stack.last() {
// LOWPOINT of top point equals NUMBER of next point on stack?
if low[top_vertex] == number[next_vertex]{
let mut tmp_component: Vec<(usize, usize)> = Vec::new();
while let Some(current_edge) = edge_stack.last() {
if number[current_edge.1] < number[next_vertex]
|| number[current_edge.0] < number[next_vertex]
{
break;
}
tmp_component.push(*current_edge);
edge_stack.pop();
}
// add to biconnected_components
if !tmp_component.is_empty(){
biconnected_components.push(tmp_component);
}
}
// LOWPOINT of top point equals NUMBER of next point on stack? NO
else if low[top_vertex] < low[next_vertex] {
// Set LOWPOINT of next point equal LOWPOINT of current point if less
low[next_vertex] = low[top_vertex]
}
}
// no more vertices in stack stack?
else {
// exit loop
break;
}
}
}
}
let mut result = Vec::with_capacity(biconnected_components.len());
for component in biconnected_components {
let mut size_set = HashSet::new();
for edge in component {
size_set.insert(edge.0);
size_set.insert(edge.1);
}
result.push(size_set.len());
}
if alternative_definition {
result.retain(|&val| val > 2);
}
// sort by reverse
// unstable here means inplace and ordering of equal elements is not guaranteed
result.sort_unstable_by(
|a, b|
a.partial_cmp(b)
.unwrap()
.reverse()
);
result
}
/// # Closely related (most of the time equal) to betweeness
/// ## calculates vertex_load of all vertices in O(edges * vertices)
/// * calculates the vertex_load for every vertex
/// * defined as how many shortest paths pass through each vertex
///
/// | variant | |
/// |---------------------|------------------------------------------------------------------------------------------------------------------------|
/// | `vertex_load(true)` | includes endpoints in calculation (for a complete graph with `N` vertices, every node will have vertex_load `N - 1`) |
/// | `vertex_load(false)` | excludes endpoints in calculation (for a complete graph with `N` vertices, every node will have vertex_load `0`) |
/// # Citations
/// I used the algorithm described in
/// > M. E. J. Newman, "Scientific collaboration networks. II. Shortest paths, weighted networks, and centrality",
/// > Phys. Rev. E **64**, 016132, 2001, DOI: [10.1103/PhysRevE.64.016132](https://doi.org/10.1103/PhysRevE.64.016132)
///
/// see also:
/// > M. E. J. Newman, "Erratum: Scientific collaboration networks. II. Shortest paths, weighted networks, and centrality",
/// > Phys. Rev. E **73**, 039906, 2006, DOI: [10.1103/PhysRevE.73.039906](https://doi.org/10.1103/PhysRevE.73.039906)
pub fn vertex_load(&self, include_endpoints: bool) -> Vec<f64> {
let mut queue0 = VecDeque::with_capacity(self.vertex_count());
let mut queue1 = VecDeque::with_capacity(self.vertex_count());
let mut ordering: Vec<usize> = Vec::with_capacity(self.vertex_count());
let mut b = vec![0.0; self.vertex_count()];
let mut b_k = vec![1f64; self.vertex_count()];
let mut distance: Vec<Option<usize>> = vec![None; self.vertex_count()];
let mut predecessor: Vec<Vec<usize>> = vec![Vec::new(); self.vertex_count()];
for i in 0..self.vertex_count() {
// initialize without allocation
if i > 0 {
for j in 0..self.vertex_count()
{
b_k[j] = 1.0;
distance[j] = None;
// clear predecessors, way more efficient then new allocation
predecessor[j].clear();
}
}
let mut depth = 0;
queue0.push_back(i);
distance[i] = Some(depth);
// build up predecessor and ordering information
while let Some(index) = queue0.pop_front() {
ordering.push(index); // to get indices in reverse order of distance
let container = self.container(index);
for &neighbor in container.neighbors() {
if let Some(d) = distance[neighbor] {
if d == depth + 1 {
predecessor[neighbor].push(index);
}
}
// None
else {
distance[neighbor] = Some(depth + 1);
queue1.push_back(neighbor);
predecessor[neighbor].push(index);
}
}
if queue0.is_empty() {
std::mem::swap(&mut queue0, &mut queue1);
depth += 1;
}
}
// calculate vertex_load resulting from the shortest paths starting at vertex i
while let Some(index) = ordering.pop() {
// skip last vertex
if ordering.is_empty(){
break;
}
// add number of shortest path to total count
b[index] += b_k[index];
if !include_endpoints {
b[index] -= 1.0;
}
let fraction = b_k[index] / predecessor[index].len() as f64;
for pred in predecessor[index].iter() {
b_k[*pred] += fraction;
}
}
}
b
}
pub fn closeness_centrality(&self) -> Vec<f64>
{
let mut count = vec![0; self.vertex_count()];
let mut bfs = Bfs::new(self, 0);
for i in 0..self.vertex_count()
{
bfs.reuse(i);
for (index, _, depth) in &mut bfs{
count[index] += depth;
}
}
let val = (self.vertex_count() - 1) as f64;
count.into_iter()
.map(|count| val / count as f64)
.collect()
}
/// # Calculates transitivity of graph
/// * related to cluster coefficient (Note: transitivity and cluster coefficient are similar,
/// but **not** necessarily equal)
/// * returns `NaN`, if there are no paths of length two in the graph
/// ## Definition
/// > transitivity = (number of closed paths of length two) / (number of paths of length two)
/// ## Citations
/// For the definition see for example:
/// > M. E. J. Newman, "Networks: an Introduction" *Oxfort University Press*, 2010, ISBN: 978-0-19-920665-0.
pub fn transitivity(&self) -> f64 {
let mut path_count: usize = 0;
let mut closed_path_count: usize = 0;
for source_index in 0..self.vertex_count() {
for neighbor_1 in self
.container(source_index)
.neighbors()
{
for neighbor_2 in self
.container(*neighbor_1)
.neighbors()
.filter(|&i| *i != source_index) // do not use edge we came from
{
if self
.container(*neighbor_2)
.is_adjacent(source_index)
{
closed_path_count += 1;
}
path_count += 1;
}
}
}
closed_path_count as f64 / path_count as f64
}
/// # Create a subgraph
/// Method to create a subgraph. `node_list` should contain all the indices
/// corresponding to the nodes you want to keep, the order of the indices is
/// irrelevant. Duplicate indices will be ignored.
/// If any index is out of bounds (or `node_list` is empty), `None` will be returned
///
/// All edges between nodes that are inside the `node_list` will be kept.
/// All other edges will be discarded.
/// The nodes in the subgraph will get new indices corresponding to their new position.
/// The contained information (`T`) will be cloned.
/// ## Note
/// The container used will be changed to a [NodeContainer](crate::graph::NodeContainer),
/// since I cannot guarantee that other container would make sense here.
/// E.g., if you used a [SwContainer](crate::sw_graph::SwContainer) the
/// information about the root edges might become invalid, because the corresponding
/// nodes might not be part of the subgraph
pub fn cloned_subgraph(&self, mut node_list: Vec<usize>) -> Option<Graph<T>>
where T: Clone
{
// get correct order
node_list.sort_unstable();
let last = *node_list.last()?;
// check if biggest node is valid
if last >= self.vertex_count() {
return None;
}
// remove duplicates
node_list.dedup();
let map: HashMap<usize, usize> = node_list.iter()
.copied()
.zip(0..)
.collect();
let mut vertices = Vec::with_capacity(node_list.len());
vertices.extend(
node_list.into_iter()
.enumerate()
.map(
|(i, node_index)|
{
let container = self.container(node_index);
let mut adj = Vec::with_capacity(container.degree());
adj.extend(
container.neighbors()
.filter_map(|n| map.get(n))
.copied()
);
NodeContainer{
id: i,
adj,
node: container.contained().clone()
}
}
)
);
let edge_count = vertices.iter()
.map(NodeContainer::degree)
.sum::<usize>() / 2;
Some(
Graph{
next_id: vertices.len(),
edge_count,
vertices,
phantom: PhantomData
}
)
}
}
impl<T, A> DotExtra<T, A> for GenericGraph<T, A>
where
A: AdjContainer<T>,
{
fn dot_from_container_index<F, S1, S2, W>(&self, mut writer: W, dot_options: S1, mut f: F)
-> Result<(), std::io::Error>
where
S1: AsRef<str>,
S2: AsRef<str>,
F: FnMut(usize, &A) -> S2,
W: Write
{
write!(writer, "graph G{{\n\t{}\n\t", dot_options.as_ref())?;
for i in 0..self.vertex_count() {
write!(writer, "{} ", i)?;
}
writeln!(writer, ";")?;
for (index, container) in self.container_iter().enumerate() {
let fun = f(index, container);
writeln!(writer, "\t\"{}\" [label=\"{}\"];", index, fun.as_ref())?;
}
for i in 0..self.vertex_count() {
for &j in self.container(i).neighbors() {
if i < j {
writeln!(writer, "\t{} -- {}", i, j)?;
}
}
}
write!(writer, "}}")
}
fn dot_from_contained_index<F, S1, S2, W>(&self, writer: W, dot_options: S1, mut f: F)
-> Result<(), std::io::Error>
where
W: Write,
S1: AsRef<str>,
S2: AsRef<str>,
F: FnMut(usize, &T) -> S2
{
self.dot_from_container_index(
writer,
dot_options,
|index, a| f(index, a.contained())
)
}
}
impl<T, A> Dot for GenericGraph<T, A>
where T: Node,
A: AdjContainer<T>
{
fn dot_from_indices<F, W, S1, S2>(&self, mut writer: W, dot_options: S1, mut f: F) -> Result<(), std::io::Error>
where
S1: AsRef<str>,
S2: AsRef<str>,
W: Write,
F: FnMut(usize) -> S2,
{
write!(writer, "graph G{{\n\t{}\n\t", dot_options.as_ref())?;
for i in 0..self.vertex_count() {
write!(writer, "{} ", i)?;
}
writeln!(writer, ";")?;
for index in 0..self.vertex_count() {
let fun = f(index);
writeln!(writer, "\t\"{}\" [label=\"{}\"];", index, fun.as_ref())?;
}
for i in 0..self.vertex_count() {
for &j in self.container(i).neighbors() {
if i < j {
writeln!(writer, "\t{} -- {}", i, j)?;
}
}
}
write!(writer, "}}")
}
}