Skip to content

Faster edge lookup #340

@nrkramer

Description

@nrkramer

Edge lookup is currently done by iterating over all edges:

template <typename T>
bool Graph<T>::findEdge(shared<const Node<T>> v1, shared<const Node<T>> v2,
                        unsigned long long &id) const {
  // This could be made faster by looking for the edge hash, assuming we hash
  // based on node data, instead of a unique integer
  for (auto e : this->edgeSet) {
    if ((e->getNodePair().first == v1) && (e->getNodePair().second == v2)) {
      id = e->getId();
      return true;
    }
    if (!e->isDirected() &&
        ((e->getNodePair().second == v1) && (e->getNodePair().first == v2))) {
      id = e->getId();
      return true;
    }
  }

  id = 0;
  return false;
}

This could probably be made a lot faster if we relied on two changes:

  1. Cache and update the Adjacency Matrix whenever a node or edge is added/removed/changed
  2. Utilize the cached Adjacency Matrix for all node and edge searches, using the superior hash lookup speed when the search space is larger

Or alternatively, we store a lookup table of edges with bucketing for repeat edges.

Metadata

Metadata

Assignees

Labels

Priority:HighPriority Label for high priority issuecoresomething about coreenhancementNew feature or request

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions