Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Inclusion of "sink nodes" in adjacency map #373

Open
sbaldu opened this issue Dec 5, 2023 · 1 comment
Open

Inclusion of "sink nodes" in adjacency map #373

sbaldu opened this issue Dec 5, 2023 · 1 comment
Labels
core something about core enhancement New feature or request Priority:Low Priority Label for low priority issue

Comments

@sbaldu
Copy link
Collaborator

sbaldu commented Dec 5, 2023

This issue is related to the discussion in issue #367.
When we build the adjacency matrix for a directed graph we only add an element for the source node.

template <typename T>
shared<AdjacencyMatrix<T>> Graph<T>::getAdjMatrix() const {
  auto adj = std::make_shared<AdjacencyMatrix<T>>();
  auto addElementToAdjMatrix = [&adj](shared<const Node<T>> nodeFrom,
                                      shared<const Node<T>> nodeTo,
                                      shared<const Edge<T>> edge) {
    std::pair<shared<const Node<T>>, shared<const Edge<T>>> elem = {nodeTo,
                                                                    edge};
    (*adj)[nodeFrom].push_back(std::move(elem));
  };
  for (const auto &edgeSetIt : edgeSet) {
    if (edgeSetIt->isDirected().has_value() &&
        edgeSetIt->isDirected().value()) {
      shared<const DirectedEdge<T>> d_edge =
          std::static_pointer_cast<const DirectedEdge<T>>(edgeSetIt);
      addElementToAdjMatrix(d_edge->getNodePair().first,
                            d_edge->getNodePair().second, d_edge);
    } else if (edgeSetIt->isDirected().has_value() &&
               !edgeSetIt->isDirected().value()) {
      shared<const UndirectedEdge<T>> ud_edge =
          std::static_pointer_cast<const UndirectedEdge<T>>(edgeSetIt);
      ;
      addElementToAdjMatrix(ud_edge->getNodePair().first,
                            ud_edge->getNodePair().second, ud_edge);
      addElementToAdjMatrix(ud_edge->getNodePair().second,
                            ud_edge->getNodePair().first, ud_edge);
    } else {  // is a simple edge we cannot create adj matrix
      return adj;
    }
  }
  return adj;
}

This means that nodes that are only destination and never source of any edge are difficult to find using the adjacency matrix, because they are not in the set of keys.
We could maybe add empty vectors for destination nodes of directed edges, using something like try_emplace to avoid re-assignments. This would cause a small overhead when building the adjacency matrix, but would make populating the nodeSet as in issue #367 quicker and easier.

Wdyt @nrkramer @ZigRazor ?

@ZigRazor
Copy link
Owner

ZigRazor commented Jan 8, 2024

This could be a good enhancement for me

@ZigRazor ZigRazor added enhancement New feature or request core something about core Priority:Low Priority Label for low priority issue labels Jan 8, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
core something about core enhancement New feature or request Priority:Low Priority Label for low priority issue
Projects
None yet
Development

No branches or pull requests

2 participants