Skip to content

Instantly share code, notes, and snippets.

@ignat
ignat / node_distance.md
Last active August 11, 2023 15:10
Calculating distance between two nodes in SurrealDB

Calculating distance between two nodes in SurrealDB

Current version of SurrealDB doesn't have yet graph::shortest_path(node1, node2) function. It sure will be added later to a stable release. However if the task is simplified a little bit we can calculate the distance with the available functions already.

Assume we have a graph of users that resembles a social network where users can connect (make friends). And we want to label the connection distance between two users similar to LinkedIn:

  • direct connections are labeled as 1st
  • connections with the shortest path of 2 are labeled as 2nd
  • connections with the shortest path of 3 are labeled as 3rd
  • connections with the shortest path of 4 and more are labeled as nth
DDEClient client;
Conversation bids = null;
client = DDEClient.getInstance();
// Add event handler
client.addDDEEventListener(new DDEEventListener() {
public void onAsyncCompleted(AsyncCompletedEvent e) {}
public void onDisconnect(DDEEvent e) {
System.out.println("Server has closed connection");
}
public void onItemChanged(ItemChangedEvent e) {