-
Notifications
You must be signed in to change notification settings - Fork 0
Property Check
Since not all graphs are valid for all algorithms, we need to check whether a graph is an acceptable input first by verifying its properties.
The empty graph is always rejected.
Apart from that, there are two types of properties: required and incompatible.
The required properties must be present for the algorithm; for example, the graph must be connected for Kruskal since otherwise we can never find a minimal spanning tree. The incompatible properties prevent the algorithm from being run if they are present. For instance for Dijkstra no edge must possess a negative weight.
The property enum in properties.rs is:
pub enum Property {
UnweightedLinks,
PositiveWeightedLinks,
NonNegativeWeightedLinks,
WeightedLinks,
Connected,
Unconnected,
Empty,
Complete,
NotComplete
}Each algorithm declares two static enums in its implementation of the Algorithm trait:
const REQUIRED_PROPERTIES: &'static [Property] = &[Property::Connected, Property::WeightedLinks];
const INCOMPATIBLE_PROPERTIES: &'static [Property] = &[Property::Empty];If you are implementing a new algorithm and don't need any fancy new properties, it suffices to simply declare your prerequisites here.
On each algorithm page, once the user tries to run the algorithm or views the different algorithms available a check is run. This means verifyGraph() is called in each adapter.js.
Next, the properties are calculated dynamically for the current graph. This is done by graph_properties() in properties.rs.
It first sorts out the link weight properties. This is done by checking whether an the weight is greater than 0, in which case we get PositiveWeightedLinks, or 0, which results in NonNegativeWeightedLinks, or less than 0, which simply makes it a weighted link.
Afterwards, it is calculated whether the graph is empty or is connected.
Then properties_test() in private.rs checks the properties are requirements against each other. If inconsistencies are found, the first array entry is false and a string representations of each invalid property is returned. These are then used by the modal to show a readable error message.