Skip to content

Shortest paths

Shortest paths

In this example, we will look at the simplified food web of the purple pitcher plant Sarracenia purpurea, as an excuse to see how the functions to handle shortest path analyses work.

The food web of the purple pitcher plan is surprisingly simple. In this demonstration, we will use the version presented in Ellison et al. (2021), itself an adaptation from Baiser et al. (2013).

using SpeciesInteractionNetworks

We will create the nodes first:

nodes = Unipartite([
    :Fletcherimyia,
    :Wyeomyia,
    :Protozoa,
    :Habrotrocha,
    :Bacteria,
    :Sarraceniopus,
    :POM,
    :Metriocnemus,
    :Detritus
])
Unipartite{Symbol}([:Fletcherimyia, :Wyeomyia, :Protozoa, :Habrotrocha, :Bacteria, :Sarraceniopus, :POM, :Metriocnemus, :Detritus])

In order to facilitate our work, we will start with an empty food web, and fill in interactions later:

edges = Binary(zeros(Bool, (richness(nodes), richness(nodes))))
N = SpeciesInteractionNetwork(nodes, edges)
A binary unipartite network
 → 0 interactions
 → 9 species

The interactions can be added one by one. Note that we represent interactions using the fromto syntax.

N[:Fletcherimyia, :Fletcherimyia] = true
N[:Fletcherimyia, :Protozoa] = true
N[:Fletcherimyia, :Habrotrocha] = true
N[:Fletcherimyia, :Wyeomyia] = true
N[:Fletcherimyia, :Bacteria] = true
N[:Fletcherimyia, :Detritus] = true
N[:Wyeomyia, :Protozoa] = true
N[:Wyeomyia, :Habrotrocha] = true
N[:Wyeomyia, :Bacteria] = true
N[:Habrotrocha, :Protozoa] = true
N[:Habrotrocha, :Bacteria] = true
N[:Habrotrocha, :POM] = true
N[:Protozoa, :Bacteria] = true
N[:Bacteria, :POM] = true
N[:Bacteria, :Detritus] = true
N[:Sarraceniopus, :Bacteria] = true
N[:Sarraceniopus, :POM] = true
N[:POM, :Metriocnemus] = true # This one is strange but let's roll with it
N[:Metriocnemus, :Detritus] = true

N
A binary unipartite network
 → 19 interactions
 → 9 species

With this network, we can start by measuring the length of the shortest path between Fletcherimyia and all other nodes of the food web:

shortestpath(N, :Fletcherimyia)
Dict{Symbol, Float64} with 7 entries:
  :Wyeomyia     => 1.0
  :Protozoa     => 1.0
  :Bacteria     => 1.0
  :POM          => 2.0
  :Habrotrocha  => 1.0
  :Detritus     => 1.0
  :Metriocnemus => 3.0

By default, the shortespath function will use the BellmanFord algorithm. It is theoretically slower than Dijkstra, but in practice, we use an early termination check which makes it compare very favorably.

We can get the path between Fletcherimyia and, for example, Metriocnemus, using pathbetween:

pathbetween(N, :Fletcherimyia, :Metriocnemus)
3-element Vector{Tuple{Symbol, Symbol, Bool}}:
 (:Fletcherimyia, :Habrotrocha, 1)
 (:Habrotrocha, :POM, 1)
 (:POM, :Metriocnemus, 1)

This returns a list of interactions. Note that the pathbetween method will return a single path, even though several may exist.