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
/* * Copyright (c) 2017, 2018 Frank Fischer <frank-fischer@shadow-soft.de> * * This program is free software: you can redistribute it and/or * modify it under the terms of the GNU General Public License as * published by the Free Software Foundation, either version 3 of the * License, or (at your option) any later version. * * This program is distributed in the hope that it will be useful, but * WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see <http://www.gnu.org/licenses/> */ //! Traits for graph data structures. //! //! The traits for graph data structures provide an additional level //! of information about (the edges of) the graph. There are three //! levels: //! //! 1. `Graph`: an undirected graph, edges have no defined source or //! sink. //! 2. `Digraph`: a directed graph, each edge has a designated source //! and a designated sink node. Furthermore, there is the concept //! of "outgoing" and "incoming" edges. A `Digraph` is also a //! `Graph`, which basically means ignoring the direction //! information of the edges. //! 3. `Network`: a network is a directed graph, but each edge is //! actually a pair of edges: the normally directed edge and its //! reverse edge. Edge and reverse edge are considered equal for //! all purposes of a digraph (e.g. source and sink node are always //! source and sink of the forward edge), but the additional //! "reverse" information can be obtained by the methods of //! `Network`. Furthermore, if the network is an `IndexNetwork`, a //! `BiEdgeVec` can be used to store different values for edges and //! the reverse edges (in contrast, an `EdgeVec` always contains //! the same value for an edge and its reverse edge). use crate::adjacencies::{InEdges, Neighbors, OutEdges}; pub use crate::vec::{IndexBiEdgeSlice, IndexEdgeSlice, IndexNodeSlice}; pub use crate::vec::{IndexBiEdgeVec, IndexEdgeVec, IndexNodeVec}; /// Base information of a graph. pub trait GraphType<'a> { /// Type of a node. type Node: 'a + Copy + Eq; /// Type of an edge. type Edge: 'a + Copy + Eq; } /// A (finite) graph with a known number of nodes and edges. /// /// Finite graphs also provide access to the list of all nodes and edges. pub trait GraphSize<'a>: GraphType<'a> { /// Type of an iterator over all nodes. type NodeIter: 'a + Iterator<Item = Self::Node>; /// Type of an iterator over all edges. type EdgeIter: 'a + Iterator<Item = Self::Edge>; /// Return the number of nodes in the graph. fn num_nodes(&self) -> usize; /// Return the number of edges in the graph. fn num_edges(&self) -> usize; /// Return an iterator over all nodes. fn nodes(&'a self) -> Self::NodeIter; /// Return an iterator over all edges. /// /// This iterator traverses only the forward edges. fn edges(&'a self) -> Self::EdgeIter; } /// A graph with list access to undirected incident edges. pub trait Undirected<'a>: GraphType<'a> { /// Type of an iterator over all incident edges. type NeighIter: 'a + Iterator<Item = (Self::Edge, Self::Node)>; /// Return the nodes connected by an edge. /// /// The order of the nodes is undefined. fn enodes(&'a self, e: Self::Edge) -> (Self::Node, Self::Node); /// Return an iterator over the edges adjacent to some node. /// /// This iterator traverses only the forward edges. fn neighs(&'a self, u: Self::Node) -> Self::NeighIter; /// Return access to the neighbors via an `Adjacencies` trait. /// /// This is the same as calling `Neighbors(&g)` on the graph. fn neighbors(&'a self) -> Neighbors<'a, Self> where Self: Sized, { Neighbors(self) } } /// A graph with list access to directed incident edges. /// /// Note that each directed graph is also an undirected graph /// by simply ignoring the direction of each edge. Hence, each /// type implementing `Directed` must also implement `Undirected`. /// /// This trait adds a few additional methods to explicitely access the /// direction information of an edge. In particular, the direction /// information can be used in the following ways: /// /// - The `src` and `snk` methods return the source and sink nodes of /// an edge. /// - The iterators `outedges` and `inedges` iterate only over edges /// leaving or entering a certain node, respectively. pub trait Directed<'a>: Undirected<'a> { /// Type of an iterator over the forward edges leaving a node. type OutEdgeIter: 'a + Iterator<Item = (Self::Edge, Self::Node)>; /// Type of an iterator over the backward edges entering a node. type InEdgeIter: 'a + Iterator<Item = (Self::Edge, Self::Node)>; /// Return the source node of an edge. fn src(&'a self, e: Self::Edge) -> Self::Node; /// Return the sink node of an edge. fn snk(&'a self, e: Self::Edge) -> Self::Node; /// Return an iterator over the outgoing edges of a node. /// /// The iterator returns only forward edges. fn outedges(&'a self, u: Self::Node) -> Self::OutEdgeIter; /// Return an iterator over the incoming edges of a node. /// /// The iterator returns only backward edges. fn inedges(&'a self, u: Self::Node) -> Self::InEdgeIter; /// Return access to the outgoing arcs via an `Adjacencies` trait. /// /// This is the same as calling `OutEdges(&g)` on the graph. fn outgoing(&'a self) -> OutEdges<'a, Self> where Self: Sized, { OutEdges(self) } /// Return access to the incoming arcs via an `Adjacencies` trait. /// /// This is the same as calling `InEdges(&g)` on the graph. fn incoming(&'a self) -> InEdges<'a, Self> where Self: Sized, { InEdges(self) } } /// A network with list access to the incident edges. /// /// A network is a digraph with an additional property: each edge is /// represented by a pair of edges, the edge and its reverse edge. The /// methods of this trait provide access to the information, if an /// edge is a forward or backward edge: `is_reverse`, `is_forward`, /// `is_backward`. The reverse edge can be obtained by `reverse`. Note /// that an edge is equal to its reverse edge, i.e. `e == /// g.reverse(e)`, so they can only be distinguished by the above /// methods. In particular, `src` and `snk` always refer to the source /// and sink node of the *forward* edge. Therefore, a forward edge /// leaves its source node whereas its reverse edge virtually enters /// its source node. In order to get the "virtual" source and sink, /// use `bisrc` and `bisnk` methods. /// /// As an additional requirement, the iterators must satisfy the following rules: /// /// - `edges` iterates over forward edges, /// - `outedges` iterates over forward edges, /// - `inedges` iterates over backward edges, /// - `neighs` iterates over outgoing forward and incoming backward edges. pub trait BiDirected<'a>: Directed<'a> { /// Return true if e is the reverse edge of f. fn is_reverse(&self, e: Self::Edge, f: Self::Edge) -> bool { e == f && self.is_forward(e) != self.is_forward(f) } /// Return the reverse edge of e. fn reverse(&'a self, e: Self::Edge) -> Self::Edge; /// Return true if e is a forward edge. fn is_forward(&self, e: Self::Edge) -> bool; /// Return the forward edge of e. /// /// This method returns e if e is already a forward edge, /// otherwise it returns the reverse edge of e. fn forward(&'a self, e: Self::Edge) -> Self::Edge { if self.is_forward(e) { e } else { self.reverse(e) } } /// Return true if e is a backward edge. fn is_backward(&self, e: Self::Edge) -> bool { !self.is_forward(e) } /// Return the backward edge of e. /// /// This method returns e if e is already a backward edge, /// otherwise it returns the reverse edge of e. fn backward(&'a self, e: Self::Edge) -> Self::Edge { if self.is_backward(e) { e } else { self.reverse(e) } } /// Return the source of the directed edge e. /// /// If e is a forward edge, this is the same as `src` otherwise /// it is `snk`. fn bisrc(&'a self, e: Self::Edge) -> Self::Node { if self.is_forward(e) { self.src(e) } else { self.snk(e) } } /// Return the sink of the directed edge e. /// /// If e is a forward edge, this is the same as `snk` otherwise /// it is `src`. fn bisnk(&'a self, e: Self::Edge) -> Self::Node { if self.is_forward(e) { self.snk(e) } else { self.src(e) } } } /// A trait for general undirected, sized graphs. pub trait Graph<'a>: GraphSize<'a> + Undirected<'a> {} impl<'a, G> Graph<'a> for G where G: GraphSize<'a> + Undirected<'a> {} /// A trait for general directed, sized graphs. pub trait Digraph<'a>: Graph<'a> + Directed<'a> {} impl<'a, G> Digraph<'a> for G where G: GraphSize<'a> + Directed<'a> {} /// A trait for general sized networks. pub trait Network<'a>: Digraph<'a> + BiDirected<'a> {} impl<'a, G> Network<'a> for G where G: GraphSize<'a> + BiDirected<'a> {} /// An item that has an index. pub trait Indexable { fn index(&self) -> usize; } /// An item that has a second bi-index. /// /// This trait is only used for networks, which edges can have two indices. pub trait BiIndexable { fn biindex(&self) -> usize; } /// Associates nodes and edges with unique ids. pub trait IndexGraph<'a>: Graph<'a> { /// Return a unique id associated with a node. fn node_id(&self, u: Self::Node) -> usize; /// Return the node associated with the given id. /// /// The method panics if the id is invalid. fn id2node(&'a self, id: usize) -> Self::Node; /// Return a unique id associated with an edge. /// /// The returned id is the same for the edge and its reverse edge. fn edge_id(&self, e: Self::Edge) -> usize; /// Return the edge associated with the given id. /// /// The method returns the forward edge. /// /// The method panics if the id is invalid. fn id2edge(&'a self, id: usize) -> Self::Edge; } /// A `Digraph` that is also an `IndexGraph`. pub trait IndexDigraph<'a>: IndexGraph<'a> + Digraph<'a> {} impl<'a, T> IndexDigraph<'a> for T where T: IndexGraph<'a> + Digraph<'a> {} /// Associates edges with unique ids for forward and backward edge. /// /// There are no guarantees on the relation between edge and node ids. pub trait IndexNetwork<'a>: IndexGraph<'a> + Network<'a> { /// Return a unique id associated with a directed edge. /// /// The returned id must be different for the edge and its reverse /// edge. fn biedge_id(&self, e: Self::Edge) -> usize; /// Return the edge associated with the given id. /// /// The method panics if the id is invalid. fn id2biedge(&'a self, id: usize) -> Self::Edge; } /// Marker trait for graphs with directly numbered nodes and edges. pub trait NumberedGraph<'a>: Graph<'a> where <Self as GraphType<'a>>::Node: Indexable, <Self as GraphType<'a>>::Edge: Indexable, { } impl<'a, G> NumberedGraph<'a> for G where G: Graph<'a>, G::Node: Indexable, G::Edge: Indexable, { } /// Marker trait for digraphs with directly numbered nodes and edges. pub trait NumberedDigraph<'a>: NumberedGraph<'a> + Digraph<'a> where <Self as GraphType<'a>>::Node: Indexable, <Self as GraphType<'a>>::Edge: Indexable, { } impl<'a, G> NumberedDigraph<'a> for G where G: Digraph<'a> + NumberedGraph<'a>, G::Node: Indexable, G::Edge: Indexable, { } /// Marker trait for networks with directly numbered nodes and edges. pub trait NumberedNetwork<'a>: NumberedDigraph<'a> + Network<'a> where <Self as GraphType<'a>>::Node: Indexable, <Self as GraphType<'a>>::Edge: Indexable + BiIndexable, { } impl<'a, G> NumberedNetwork<'a> for G where G: Network<'a> + NumberedDigraph<'a>, G::Node: Indexable, G::Edge: Indexable + BiIndexable, { } impl<'a, G> GraphType<'a> for &'a G where G: GraphType<'a>, { type Node = G::Node; type Edge = G::Edge; } impl<'a, G> GraphSize<'a> for &'a G where G: GraphSize<'a>, { type NodeIter = G::NodeIter; type EdgeIter = G::EdgeIter; fn num_nodes(&self) -> usize { (*self).num_nodes() } fn num_edges(&self) -> usize { (*self).num_edges() } fn nodes(&'a self) -> Self::NodeIter { (*self).nodes() } fn edges(&'a self) -> Self::EdgeIter { (*self).edges() } } impl<'a, G> Undirected<'a> for &'a G where G: Undirected<'a>, { type NeighIter = G::NeighIter; fn enodes(&'a self, e: Self::Edge) -> (Self::Node, Self::Node) { (*self).enodes(e) } fn neighs(&'a self, u: Self::Node) -> Self::NeighIter { (*self).neighs(u) } } impl<'a, G> IndexGraph<'a> for &'a G where G: IndexGraph<'a>, { fn node_id(&self, u: Self::Node) -> usize { (*self).node_id(u) } fn id2node(&'a self, id: usize) -> Self::Node { (*self).id2node(id) } fn edge_id(&self, e: Self::Edge) -> usize { (*self).edge_id(e) } fn id2edge(&'a self, id: usize) -> Self::Edge { (*self).id2edge(id) } } impl<'a, G> Directed<'a> for &'a G where G: Directed<'a>, { type OutEdgeIter = G::OutEdgeIter; type InEdgeIter = G::InEdgeIter; fn src(&'a self, e: Self::Edge) -> Self::Node { (*self).src(e) } fn snk(&'a self, e: Self::Edge) -> Self::Node { (*self).snk(e) } fn outedges(&'a self, u: Self::Node) -> Self::OutEdgeIter { (*self).outedges(u) } fn inedges(&'a self, u: Self::Node) -> Self::InEdgeIter { (*self).inedges(u) } } impl<'a, G> BiDirected<'a> for &'a G where G: BiDirected<'a>, { fn is_reverse(&self, e: Self::Edge, f: Self::Edge) -> bool { (*self).is_reverse(e, f) } fn reverse(&'a self, e: Self::Edge) -> Self::Edge { (*self).reverse(e) } fn is_forward(&self, e: Self::Edge) -> bool { (*self).is_forward(e) } fn forward(&'a self, e: Self::Edge) -> Self::Edge { (*self).forward(e) } fn is_backward(&self, e: Self::Edge) -> bool { (*self).is_backward(e) } fn backward(&'a self, e: Self::Edge) -> Self::Edge { (*self).backward(e) } fn bisrc(&'a self, e: Self::Edge) -> Self::Node { (*self).bisnk(e) } fn bisnk(&'a self, e: Self::Edge) -> Self::Node { (*self).bisrc(e) } } impl<'a, G> IndexNetwork<'a> for &'a G where G: IndexNetwork<'a>, { fn biedge_id(&self, e: Self::Edge) -> usize { (*self).biedge_id(e) } fn id2biedge(&'a self, id: usize) -> Self::Edge { (*self).id2biedge(id) } }