\n","children":[{"type":"text","text":""}]},{"type":"p","children":[{"type":"text","text":"About a decade ago, Marko Rodrigez wrote a blog post on Loopy Lattices (see "},{"type":"text","text":"https://dzone.com/articles/big-data-graphs-loopy-lattices","code":true},{"type":"text","text":"). It became infamous amongst graph database practitioners as it taught them a very important lesson: Never give in to the temptation to ask for all potential paths between two nodes. Never give in, never, never, never, never -- in graphs great or small, large or petty, never give in to the temptation to ask for all paths, except to intentionally crash the server."}]},{"type":"p","children":[{"type":"text","text":"Let me explain. No, there is too much. Let me "},{"type":"a","url":"https://www.youtube.com/watch?v=yokQ0_8__ts","title":null,"children":[{"type":"text","text":"sum up"}]},{"type":"text","text":": Imagine you take a graph, stretch it out and iron it, and when you do it turns out to look like a grid. This is a "},{"type":"a","url":"https://en.wikipedia.org/wiki/Lattice_graph","title":null,"children":[{"type":"text","text":"lattice"}]},{"type":"text","text":"."}]},{"type":"mdxJsxFlowElement","name":"ImgFig","children":[{"type":"text","text":""}],"props":{"src":"/blog/loopy-lattices/lattice-1.png","alt":"connected-dots"}},{"type":"p","children":[{"type":"text","text":"The grid-like graph above has 21 nodes across and 21 nodes down. There are 20 edges across and 20 down as well. This 20x20 lattice has a total of 441 nodes and 840 edges. By almost all measures, it is a tiny graph. Let’s imagine for a second this grid "},{"type":"a","url":"https://appliednetsci.springeropen.com/articles/10.1007/s41109-019-0189-1/figures/5","title":null,"children":[{"type":"text","text":"represents"}]},{"type":"text","text":" roads and intersections. Think about getting around in Manhattan or Chicago, in Beijing or Kyoto. What if we were driving from the top-left node and needed to get to the bottom-right node? If you’ve seen gas prices lately, you probably understand the general importance of finding the shortest paths. Let’s see what happens when we ask just for the count of all the possible shortest paths from those two nodes. Maybe let’s start small with a 2x2 lattice."}]},{"type":"mdxJsxFlowElement","name":"ImgFig","children":[{"type":"text","text":""}],"props":{"src":"/blog/loopy-lattices/lattice-2.png","alt":"connected-dots"}},{"type":"p","children":[{"type":"text","text":"We need to get from Node 1 to Node 9. If you do this by hand, you will see a total of six possible shortest paths. All of the paths are four hops. The length is the width of the lattice plus the height of the lattice. Project Euler has an easy to follow picture for this "},{"type":"a","url":"https://projecteuler.net/problem=15","title":null,"children":[{"type":"text","text":"problem"}]},{"type":"text","text":":"}]},{"type":"mdxJsxFlowElement","name":"ImgFig","children":[{"type":"text","text":""}],"props":{"src":"/blog/loopy-lattices/lattice-3.png","alt":"connected-dots"}},{"type":"p","children":[{"type":"text","text":"Let’s keep going and try bigger graphs:"}]},{"type":"mdxJsxFlowElement","name":"ImgFig","children":[{"type":"text","text":""}],"props":{"src":"/blog/loopy-lattices/lattice-4.png","alt":"connected-dots"}},{"type":"p","children":[{"type":"text","text":"A 3x3 lattice has 20 shortest paths from the top-left node to the bottom-right node. There are 70 in a 4x4 lattice and 924 in a 6x6 lattice. At this level, it is too much to try to do this by hand. It quickly becomes too much for a traditional graph database. In the "},{"type":"a","url":"https://dzone.com/articles/loopy-lattices-redux","title":null,"children":[{"type":"text","text":"follow up post"}]},{"type":"text","text":", Marko tells us that it took 10 minutes to calculate the answer up to 28 hops, 2.3 hours for 29 hops and almost 10 hours for 30 hops. He concluded that “"},{"type":"text","text":"the number of 40-length paths could not be reasonably calculated with titan/gremlin","bold":true},{"type":"text","text":"”."}]},{"type":"p","children":[{"type":"text","text":"Instead he turns from graph databases to the graph processing engine “Faunus” running on a hadoop cluster. It stores the count of the number of neighbors for each node and is able to brute force the answer much faster without as much traversing. Faunus is able to compute the total number of unique 40-length paths in the lattice in "},{"type":"text","text":"10.53 minutes","bold":true},{"type":"text","text":" – 137,846,528,820. That is 137 billion paths in 10 minutes, almost 11… keep that in the back of your head."}]},{"type":"p","children":[{"type":"text","text":"Can we do better in Rel, RelationalAI’s declarative modeling language? Of course we can."}]},{"type":"p","children":[{"type":"text","text":"We get the correct answer: 137,846,528,820."}]},{"type":"p","children":[{"type":"text","text":"In how long? "},{"type":"text","text":"0.41 seconds","bold":true},{"type":"text","text":"."}]},{"type":"p","children":[{"type":"text","text":"Do you want to know how? I’ll show you. Let’s start by building our graph. First, let’s define the size of our lattice and create the nodes:"}]},{"type":"p","children":[{"type":"text","text":"Now we have 441 nodes, numbered 1 to 441. Let’s create the edges between them. Every node connects to the one to the right of it, except for those along the right side:"}]},{"type":"code_block","lang":"rel","value":"// means we have a 20x20 lattice. This is the\n// number of edges per side.\ndef lattice_size = 20\n\n// The number of nodes wide times the number of nodes high\n// is equal to (20 + 1) * (20 + 1) = 441. For a 20x20 lattice,\n// we need to create 441 nodes in the graph.\ndef number_of_nodes = (lattice_size + 1) * (lattice_size + 1)\n\n// let's go ahead and create 441 nodes from 1 to 441\n// incrementing by 1.\ndef node = range[1, number_of_nodes, 1]","children":[{"type":"text","text":""}]},{"type":"code_block","lang":"rel","value":"// The node to the right is itself + 1. Node 8 connects\n// to Node 9 and so on as long as we are not on the right\n// side wall, which we are if we have a remainder from\n// modulo[node_number, lattice_size + 1].\n\n// For example 9 % 21 = 9. We're good.\n// But 42 % 21 = 0, which means we are at the wall\n// and we don't want to connect to the right.\n\ndef edge(node_number, right_node) = node(node_number) and\n right_node = node_number + 1 and\n modulo[node_number, lattice_size + 1] > 0","children":[{"type":"text","text":""}]},{"type":"p","children":[{"type":"text","text":"Every node connects to the one down from it, except those along the bottom:"}]},{"type":"code_block","lang":"rel","value":"// For example, Node 8 connects to (8 + 20 + 1) = Node 29.\n// But Node 434 can't connect down since it would connect\n// to a node greater than 441.\n\ndef edge(node_number, down_node) = node(node_number) and\n down_node = node_number + lattice_size + 1 and\n down_node <= number_of_nodes","children":[{"type":"text","text":""}]},{"type":"p","children":[{"type":"text","text":"Notice how we have “def edge = ” twice? The second time is not overwriting it, it is saying that this is also equal to edge."}]},{"type":"p","children":[{"type":"text","text":"Now we have our graph. But how can we tell Rel what we want to do? How do we tell it we want to count the number of unique paths from node 1 to node 441?"}]},{"type":"p","children":[{"type":"text","text":"In Rel, one of the things you have to get used to is recursive queries, which have these multiple “def statements”. "},{"type":"text","text":"Once you are able to think recursively, many problems become a lot easier","bold":true},{"type":"text","text":". We start with the “base” case, which is usually the least exciting. Node 1 has a single path to itself of zero length:"}]},{"type":"code_block","lang":"rel","value":"def number_of_paths_of_length(node_number, path_length, path_count) =\n\tnode_number=1, path_length=0, path_count=1","children":[{"type":"text","text":""}]},{"type":"p","children":[{"type":"text","text":"Now the exciting part… we define it again, this time saying that the number of paths of some length is also equal to the sum of the number of paths of (some length minus 1) of its neighbors."}]},{"type":"code_block","lang":"rel","value":"def number_of_paths_of_length[node_number, path_length] =\n sum[other_node, paths_of_length : paths_of_length =\n \tnumber_of_paths_of_length[other_node, path_length - 1]\n \tand edge(other_node, node_number)]","children":[{"type":"text","text":""}]},{"type":"p","children":[{"type":"text","text":"It makes more sense with an example. How many paths of "},{"type":"text","text":"length 2","bold":true},{"type":"text","text":" are there from Node 1 to Node 4 in this picture? The answer is the "},{"type":"text","text":"sum of the number of paths of length 1","bold":true},{"type":"text","text":" from Node 1 to Node 2, plus the number of paths from Node 1 to Node 3. Okay, so how many of those are there?"}]},{"type":"mdxJsxFlowElement","name":"ImgFig","children":[{"type":"text","text":""}],"props":{"src":"/blog/loopy-lattices/lattice-5.png","alt":"connected-dots"}},{"type":"p","children":[{"type":"text","text":"Well, we know there is one path of length 0 from Node 1 to itself. We also know an edge exists between Node 1 to Node 2 and another from Node 1 to Node 3, so if take the sum of these two formulas:"}]},{"type":"code_block","lang":"rel","value":"number_of_paths_of_length[node_one, path_length - 1] and\n edge(node_1, node_2)]\n\nnumber_of_paths_of_length[node_one, path_length - 1] and\n edge(node_1, node_3)]","children":[{"type":"text","text":""}]},{"type":"p","children":[{"type":"text","text":"We get 1 + 1, which is our answer: 2 paths."}]},{"type":"p","children":[{"type":"text","text":"Let’s see it on a 2x2 lattice. From Node 1, there are three paths of length 3 to Node 6 and three paths of length 3 to Node 8. If we add them up, we get six, which is the number of paths of length 4 from Node 1 to Node 9."}]},{"type":"mdxJsxFlowElement","name":"ImgFig","children":[{"type":"text","text":""}],"props":{"src":"/blog/loopy-lattices/lattice-6.png","alt":"connected-dots"}},{"type":"p","children":[{"type":"text","text":"But wait, how do we know there are three paths of length 3 to Node 6? Well, there is just one path from Node 1 to Node 3 of length 2, from Node 1 to Node 2 to Node 3… and there are two paths of length 2 from Node 1 to Node 5. One goes by Node 2 and the other by Node 4."}]},{"type":"p","children":[{"type":"text","text":"How do we know Node 3 only has one path? Well, because it only connects to Node 2 which has only one path. How do we know Node 2 only has one path of length 1? Because it only connects to Node 1 and we already know that Node 1 has just one path to itself. So then how do we know Node 5 has two paths? It connects to Node 2, which we already established has one path from Node 1, and Node 4 only has one way to get to Node 1 which we already know is one. If we add them up, we get two paths."}]},{"type":"p","children":[{"type":"text","text":"If we keep going all the way to 20x20 and output our results:"}]},{"type":"code_block","lang":"rel","value":"def output = number_of_paths_of_length[number_of_nodes, 2 * lattice_size]","children":[{"type":"text","text":""}]},{"type":"p","children":[{"type":"text","text":"What do we get?"}]},{"type":"code_block","lang":"rel","value":"def lattice_size = 20\ndef number_of_nodes = (lattice_size + 1) * (lattice_size + 1)\ndef node = range[1, number_of_nodes, 1]\n\ndef edge(node_number, right_node) = node(node_number) and right_node =\n node_number + 1 and modulo[node_number, lattice_size + 1] > 0\n\ndef edge(node_number, down_node) = node(node_number) and down_node =\n node_number + lattice_size + 1 and down_node <= number_of_nodes\n\ndef number_of_paths_of_length(node_number, path_length, path_count) =\n node_number=1, path_length=0, path_count=1\n\ndef number_of_paths_of_length[node_number, path_length] =\n sum[other_node, paths_of_length : paths_of_length =\n number_of_paths_of_length[other_node, path_length - 1] and edge(other_node, node_number)]\n\ndef output = number_of_paths_of_length[number_of_nodes, 2 * lattice_size]","children":[{"type":"text","text":""}]},{"type":"html","value":"","children":[{"type":"text","text":""}]},{"type":"p","children":[{"type":"text","text":"The correct answer: 137,846,528,820 (on the bottom left) in "},{"type":"text","text":"0.41","bold":true},{"type":"text","text":" seconds (on the bottom right). It took a fraction of a second to calculate the 137 billion shortest paths from the top-left node to the bottom-right node in our lattice. That time blows 10 minutes out of the water. It is… I don’t even know how many orders of magnitude faster. So how did we do it? How were we able to compute the right answer so fast? To answer this question, "},{"type":"a","url":"/blog/loopy-lattices-and-recursion","title":null,"children":[{"type":"text","text":"we will talk about the semantic optimizer next time"}]},{"type":"text","text":". "},{"type":"a","url":"/#subscribe","title":null,"children":[{"type":"text","text":"Subscribe"}]},{"type":"text","text":" to our newsletter to be notified!"}]},{"type":"p","children":[{"type":"text","text":"Do you want learn more about RelationalAI? Watch "},{"type":"a","url":"https://www.youtube.com/watch?v=WRHy7M30mM4","title":null,"children":[{"type":"text","text":"this video"}]},{"type":"text","text":" from "},{"type":"a","url":"https://twitter.com/mbravenboer","title":null,"children":[{"type":"text","text":"Martin Bravenboer"}]},{"type":"text","text":". You can also take a look at all the code mentioned above "},{"type":"a","url":"https://gist.github.com/maxdemarzi/6fc2920296af7684cf678f386d0aba4c","title":null,"children":[{"type":"text","text":"at this link"}]},{"type":"text","text":"."}]}],"_content_source":{"queryId":"src/content/resources/loopy-lattices.mdx","path":["resource","body"]}},"_content_source":{"queryId":"src/content/resources/loopy-lattices.mdx","path":["resource"]}}},"errors":null,"query":"\n query resource($relativePath: String!) {\n resource(relativePath: $relativePath) {\n ... on Document {\n _sys {\n filename\n basename\n breadcrumbs\n path\n relativePath\n extension\n }\n id\n }\n ...ResourceParts\n }\n}\n \n fragment ResourceParts on Resource {\n __typename\n title\n description\n date\n image\n categories\n authors {\n __typename\n name\n link\n }\n seo {\n __typename\n keywords\n description\n image\n image_alt\n canonical_url\n author\n published\n modified\n language\n robots\n site_name\n content_type\n }\n body\n}\n ","variables":{"relativePath":"loopy-lattices.mdx"}},"src/content/meta/meta.md":{"data":{"meta":{"_sys":{"filename":"meta","basename":"meta.md","breadcrumbs":["meta"],"path":"src/content/meta/meta.md","relativePath":"meta.md","extension":".md"},"id":"src/content/meta/meta.md","__typename":"Meta","banner":{"__typename":"MetaBanner","enabled":true,"content":{"type":"root","children":[{"type":"p","children":[{"type":"text","text":"Check out "},{"type":"a","url":"/resources/highlights-of-relationalai-at-snowflake-data-cloud-summit-2024","title":"SF summit highlights","children":[{"type":"text","text":"highlights"}]},{"type":"text","text":" of RelationalAI at "},{"type":"text","text":"Snowflake's Data Cloud Summit 2024!","bold":true}]}],"_content_source":{"queryId":"src/content/meta/meta.md","path":["meta","banner","content"]}},"_content_source":{"queryId":"src/content/meta/meta.md","path":["meta","banner"]}},"header":{"__typename":"MetaHeader","links":[{"__typename":"MetaHeaderLinks","text":"Product","url":"/product","style":"default","_content_source":{"queryId":"src/content/meta/meta.md","path":["meta","header","links",0]}},{"__typename":"MetaHeaderLinks","text":"Company","url":"/company","style":"default","_content_source":{"queryId":"src/content/meta/meta.md","path":["meta","header","links",1]}},{"__typename":"MetaHeaderLinks","text":"Docs","url":"/docs","style":"default","_content_source":{"queryId":"src/content/meta/meta.md","path":["meta","header","links",2]}},{"__typename":"MetaHeaderLinks","text":"Resources","url":"/resources/all/1","style":"default","_content_source":{"queryId":"src/content/meta/meta.md","path":["meta","header","links",3]}},{"__typename":"MetaHeaderLinks","text":"Get Started","url":"/get-started","style":"cta","_content_source":{"queryId":"src/content/meta/meta.md","path":["meta","header","links",4]}}],"_content_source":{"queryId":"src/content/meta/meta.md","path":["meta","header"]}},"footer":{"__typename":"MetaFooter","sections":[{"__typename":"MetaFooterSections","name":"Product","links":[{"__typename":"MetaFooterSectionsLinks","text":"Overview","url":"/product","_content_source":{"queryId":"src/content/meta/meta.md","path":["meta","footer","sections",0,"links",0]}},{"__typename":"MetaFooterSectionsLinks","text":"Use Cases","url":"/product#for-problems-that-matter","_content_source":{"queryId":"src/content/meta/meta.md","path":["meta","footer","sections",0,"links",1]}},{"__typename":"MetaFooterSectionsLinks","text":"Capabilities","url":"/product#a-new-toolset","_content_source":{"queryId":"src/content/meta/meta.md","path":["meta","footer","sections",0,"links",2]}}],"_content_source":{"queryId":"src/content/meta/meta.md","path":["meta","footer","sections",0]}},{"__typename":"MetaFooterSections","name":"Resources","links":[{"__typename":"MetaFooterSectionsLinks","text":"Documentation","url":"/docs/getting_started","_content_source":{"queryId":"src/content/meta/meta.md","path":["meta","footer","sections",1,"links",0]}},{"__typename":"MetaFooterSectionsLinks","text":"News","url":"/resources/news/1","_content_source":{"queryId":"src/content/meta/meta.md","path":["meta","footer","sections",1,"links",1]}},{"__typename":"MetaFooterSectionsLinks","text":"Research","url":"/resources/research/1","_content_source":{"queryId":"src/content/meta/meta.md","path":["meta","footer","sections",1,"links",2]}},{"__typename":"MetaFooterSectionsLinks","text":"Releases","url":"/resources/releases/1","_content_source":{"queryId":"src/content/meta/meta.md","path":["meta","footer","sections",1,"links",3]}}],"_content_source":{"queryId":"src/content/meta/meta.md","path":["meta","footer","sections",1]}},{"__typename":"MetaFooterSections","name":"About Us","links":[{"__typename":"MetaFooterSectionsLinks","text":"Our Company","url":"/company","_content_source":{"queryId":"src/content/meta/meta.md","path":["meta","footer","sections",2,"links",0]}},{"__typename":"MetaFooterSectionsLinks","text":"Contact Us","url":"/get-started","_content_source":{"queryId":"src/content/meta/meta.md","path":["meta","footer","sections",2,"links",1]}},{"__typename":"MetaFooterSectionsLinks","text":"Careers","url":"/careers","_content_source":{"queryId":"src/content/meta/meta.md","path":["meta","footer","sections",2,"links",2]}},{"__typename":"MetaFooterSectionsLinks","text":"Legal","url":"/legal","_content_source":{"queryId":"src/content/meta/meta.md","path":["meta","footer","sections",2,"links",3]}},{"__typename":"MetaFooterSectionsLinks","text":"GDPR","url":"/gdpr","_content_source":{"queryId":"src/content/meta/meta.md","path":["meta","footer","sections",2,"links",4]}},{"__typename":"MetaFooterSectionsLinks","text":"Security & Trust","url":"https://trust.relational.ai/","_content_source":{"queryId":"src/content/meta/meta.md","path":["meta","footer","sections",2,"links",5]}}],"_content_source":{"queryId":"src/content/meta/meta.md","path":["meta","footer","sections",2]}}],"socials":[{"__typename":"MetaFooterSocials","text":"GitHub","url":"https://github.com/RelationalAI","icon":"https://assets.tina.io/91d76337-e55d-4722-acb5-3106adb895b6/img/logos/github.png","_content_source":{"queryId":"src/content/meta/meta.md","path":["meta","footer","socials",0]}},{"__typename":"MetaFooterSocials","text":"LinkedIn","url":"https://www.linkedin.com/company/relationalai/about","icon":"https://assets.tina.io/91d76337-e55d-4722-acb5-3106adb895b6/img/logos/linkedin.png","_content_source":{"queryId":"src/content/meta/meta.md","path":["meta","footer","socials",1]}},{"__typename":"MetaFooterSocials","text":"Twitter","url":"https://twitter.com/relationalai","icon":"https://assets.tina.io/91d76337-e55d-4722-acb5-3106adb895b6/img/logos/twitter.png","_content_source":{"queryId":"src/content/meta/meta.md","path":["meta","footer","socials",2]}}],"_content_source":{"queryId":"src/content/meta/meta.md","path":["meta","footer"]}},"_content_source":{"queryId":"src/content/meta/meta.md","path":["meta"]}}},"errors":null,"query":"\n query meta($relativePath: String!) {\n meta(relativePath: $relativePath) {\n ... on Document {\n _sys {\n filename\n basename\n breadcrumbs\n path\n relativePath\n extension\n }\n id\n }\n ...MetaParts\n }\n}\n \n fragment MetaParts on Meta {\n __typename\n banner {\n __typename\n enabled\n content\n }\n header {\n __typename\n links {\n __typename\n text\n url\n style\n }\n }\n footer {\n __typename\n sections {\n __typename\n name\n links {\n __typename\n text\n url\n }\n }\n socials {\n __typename\n text\n url\n icon\n }\n }\n}\n ","variables":{"relativePath":"./meta.md"}}};
globalThis.tina_info = tina;
})();
Loopy Lattices · RelationalAI
Check out highlights of RelationalAI at Snowflake's Data Cloud Summit 2024!
About a decade ago, Marko Rodrigez wrote a blog post on Loopy Lattices (see https://dzone.com/articles/big-data-graphs-loopy-lattices). It became infamous amongst graph database practitioners as it taught them a very important lesson: Never give in to the temptation to ask for all potential paths between two nodes. Never give in, never, never, never, never -- in graphs great or small, large or petty, never give in to the temptation to ask for all paths, except to intentionally crash the server.
Let me explain. No, there is too much. Let me sum up: Imagine you take a graph, stretch it out and iron it, and when you do it turns out to look like a grid. This is a lattice.
The grid-like graph above has 21 nodes across and 21 nodes down. There are 20 edges across and 20 down as well. This 20x20 lattice has a total of 441 nodes and 840 edges. By almost all measures, it is a tiny graph. Let’s imagine for a second this grid represents roads and intersections. Think about getting around in Manhattan or Chicago, in Beijing or Kyoto. What if we were driving from the top-left node and needed to get to the bottom-right node? If you’ve seen gas prices lately, you probably understand the general importance of finding the shortest paths. Let’s see what happens when we ask just for the count of all the possible shortest paths from those two nodes. Maybe let’s start small with a 2x2 lattice.
We need to get from Node 1 to Node 9. If you do this by hand, you will see a total of six possible shortest paths. All of the paths are four hops. The length is the width of the lattice plus the height of the lattice. Project Euler has an easy to follow picture for this problem:
Let’s keep going and try bigger graphs:
A 3x3 lattice has 20 shortest paths from the top-left node to the bottom-right node. There are 70 in a 4x4 lattice and 924 in a 6x6 lattice. At this level, it is too much to try to do this by hand. It quickly becomes too much for a traditional graph database. In the follow up post, Marko tells us that it took 10 minutes to calculate the answer up to 28 hops, 2.3 hours for 29 hops and almost 10 hours for 30 hops. He concluded that “the number of 40-length paths could not be reasonably calculated with titan/gremlin”.
Instead he turns from graph databases to the graph processing engine “Faunus” running on a hadoop cluster. It stores the count of the number of neighbors for each node and is able to brute force the answer much faster without as much traversing. Faunus is able to compute the total number of unique 40-length paths in the lattice in 10.53 minutes – 137,846,528,820. That is 137 billion paths in 10 minutes, almost 11… keep that in the back of your head.
Can we do better in Rel, RelationalAI’s declarative modeling language? Of course we can.
We get the correct answer: 137,846,528,820.
In how long? 0.41 seconds.
Do you want to know how? I’ll show you. Let’s start by building our graph. First, let’s define the size of our lattice and create the nodes:
Now we have 441 nodes, numbered 1 to 441. Let’s create the edges between them. Every node connects to the one to the right of it, except for those along the right side:
// means we have a 20x20 lattice. This is the
// number of edges per side.
def lattice_size = 20
// The number of nodes wide times the number of nodes high
// is equal to (20 + 1) * (20 + 1) = 441. For a 20x20 lattice,
// we need to create 441 nodes in the graph.
def number_of_nodes = (lattice_size + 1) * (lattice_size + 1)
// let's go ahead and create 441 nodes from 1 to 441
// incrementing by 1.
def node = range[1, number_of_nodes, 1]
// The node to the right is itself + 1. Node 8 connects
// to Node 9 and so on as long as we are not on the right
// side wall, which we are if we have a remainder from
// modulo[node_number, lattice_size + 1].
// For example 9 % 21 = 9. We're good.
// But 42 % 21 = 0, which means we are at the wall
// and we don't want to connect to the right.
def edge(node_number, right_node) = node(node_number) and
right_node = node_number + 1 and
modulo[node_number, lattice_size + 1] > 0
Every node connects to the one down from it, except those along the bottom:
// For example, Node 8 connects to (8 + 20 + 1) = Node 29.
// But Node 434 can't connect down since it would connect
// to a node greater than 441.
def edge(node_number, down_node) = node(node_number) and
down_node = node_number + lattice_size + 1 and
down_node <= number_of_nodes
Notice how we have “def edge = ” twice? The second time is not overwriting it, it is saying that this is also equal to edge.
Now we have our graph. But how can we tell Rel what we want to do? How do we tell it we want to count the number of unique paths from node 1 to node 441?
In Rel, one of the things you have to get used to is recursive queries, which have these multiple “def statements”. Once you are able to think recursively, many problems become a lot easier. We start with the “base” case, which is usually the least exciting. Node 1 has a single path to itself of zero length:
Now the exciting part… we define it again, this time saying that the number of paths of some length is also equal to the sum of the number of paths of (some length minus 1) of its neighbors.
It makes more sense with an example. How many paths of length 2 are there from Node 1 to Node 4 in this picture? The answer is the sum of the number of paths of length 1 from Node 1 to Node 2, plus the number of paths from Node 1 to Node 3. Okay, so how many of those are there?
Well, we know there is one path of length 0 from Node 1 to itself. We also know an edge exists between Node 1 to Node 2 and another from Node 1 to Node 3, so if take the sum of these two formulas:
number_of_paths_of_length[node_one, path_length - 1] and
edge(node_1, node_2)]
number_of_paths_of_length[node_one, path_length - 1] and
edge(node_1, node_3)]
We get 1 + 1, which is our answer: 2 paths.
Let’s see it on a 2x2 lattice. From Node 1, there are three paths of length 3 to Node 6 and three paths of length 3 to Node 8. If we add them up, we get six, which is the number of paths of length 4 from Node 1 to Node 9.
But wait, how do we know there are three paths of length 3 to Node 6? Well, there is just one path from Node 1 to Node 3 of length 2, from Node 1 to Node 2 to Node 3… and there are two paths of length 2 from Node 1 to Node 5. One goes by Node 2 and the other by Node 4.
How do we know Node 3 only has one path? Well, because it only connects to Node 2 which has only one path. How do we know Node 2 only has one path of length 1? Because it only connects to Node 1 and we already know that Node 1 has just one path to itself. So then how do we know Node 5 has two paths? It connects to Node 2, which we already established has one path from Node 1, and Node 4 only has one way to get to Node 1 which we already know is one. If we add them up, we get two paths.
If we keep going all the way to 20x20 and output our results:
The correct answer: 137,846,528,820 (on the bottom left) in 0.41 seconds (on the bottom right). It took a fraction of a second to calculate the 137 billion shortest paths from the top-left node to the bottom-right node in our lattice. That time blows 10 minutes out of the water. It is… I don’t even know how many orders of magnitude faster. So how did we do it? How were we able to compute the right answer so fast? To answer this question, we will talk about the semantic optimizer next time. Subscribe to our newsletter to be notified!