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

Add convenience functions for computing out neighbor nodes, inout neighbor nodes #310

Closed
nrkramer opened this issue May 25, 2023 · 4 comments · Fixed by #357
Closed

Add convenience functions for computing out neighbor nodes, inout neighbor nodes #310

nrkramer opened this issue May 25, 2023 · 4 comments · Fixed by #357
Labels
core something about core development Development of new Functionalities enhancement New feature or request good first issue Good for newcomers hacktoberfest hacktoberfest issue Priority:Medium Priority Label for medium priority issue

Comments

@nrkramer
Copy link
Collaborator

While almost all graph searching and neighbor lookups can be accomplished through the adjacency matrix, it is sometimes very useful for programmers to iteravely go through neighbors by providing a pointer to a node.

This can be done by simply using the adjacency matrix and determining the edge type, then returning the set of edges that satisfies the direction.

Formally, this feature request is to provide functions such as outEdges(Node<T>* node) and inOutEdges(Node<T>* node), which will return out edges from a node in a directed graph and all edges from a node in any graph, respectively.

@ZigRazor ZigRazor added enhancement New feature or request good first issue Good for newcomers development Development of new Functionalities core something about core Priority:Medium Priority Label for medium priority issue labels May 25, 2023
@sbaldu
Copy link
Collaborator

sbaldu commented May 30, 2023

Hey @nrkramer, is this what you had in mind?

template <typename T>
const std::set<const Node<T> *> Graph<T>::outEdges(const Node<T> *node) const {
  auto adj = getAdjMatrix();
  if (adj->find(node) == adj->end()) {
	return std::set<const Node<T> *>();
  }
  auto nodeEdgePairs = adj->at(node);

  std::set<const Node<T> *> outEdges;
  for (auto pair : nodeEdgePairs) {
	if (pair.second->isDirected().has_value() && pair.second->isDirected().value()) {
	  outEdges.insert(pair.first);
	}
  }

  return outEdges;
}

template <typename T>
const std::set<const Node<T> *> Graph<T>::inOutEdges(const Node<T> *node) const {
  auto adj = Graph<T>::getAdjMatrix();
  if (adj->find(node) == adj->end()) {
	return std::set<const Node<T> *>();
  }
  auto nodeEdgePairs = adj->at(node);
  
  std::set<const Node<T> *> inOutEdges;
  for (auto pair : nodeEdgePairs) {
	inOutEdges.insert(pair.first);
  }
  
  return inOutEdges;
}

@sbaldu
Copy link
Collaborator

sbaldu commented May 30, 2023

Maybe out/inOutNeighbors would be better names? Because we get a set of nodes, not edges

@nrkramer
Copy link
Collaborator Author

nrkramer commented Jun 4, 2023

I think there's convenience in both cases - however with just the set of nodes it's trivial to reconstruct the edges.

A separate issue can be filed to address the edges case. I'll rename this issue to address getting the nodes.

@nrkramer nrkramer changed the title Add convenience functions for computing out edges, inout edges Add convenience functions for computing out edges, inout nodes Jun 4, 2023
@nrkramer nrkramer changed the title Add convenience functions for computing out edges, inout nodes Add convenience functions for computing out neighbor nodes, inout neighbor nodes Jun 4, 2023
@ZigRazor
Copy link
Owner

ZigRazor commented Jun 5, 2023

Perfect

@ZigRazor ZigRazor added the hacktoberfest hacktoberfest issue label Sep 26, 2023
@ZigRazor ZigRazor linked a pull request Sep 28, 2023 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
core something about core development Development of new Functionalities enhancement New feature or request good first issue Good for newcomers hacktoberfest hacktoberfest issue Priority:Medium Priority Label for medium priority issue
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants