How I started

Growing up without a mother since the age of 9 affected my emotions to a very great extent. I always felt a sense of regret for not being able to spend enough time with her. She left too soon, as…

Smartphone

独家优惠奖金 100% 高达 1 BTC + 180 免费旋转




Graph Storage Techniques

In this Graph storage techniques blog, I’ll take you through the basics of graph technology assuming you have little (or no) background in the space. we’ll be discussing different graph search algorithms and how they’re used, including Dijkstra’s algorithm and the A* algorithm. Our discussion will focus on what graph search algorithms do for you (and your business) without diving too deep into the mathematics of graph theory.

there are two basic types of graph search algorithms: depth-first and breadth-first.

The former type of algorithm travels from a starting node to some end node before repeating the search down a different path from the same start node until the query is answered. Generally, depth-first search is a good choice when trying to discover discrete pieces of information. They’re also a good strategic choice for general graph traversals.

The most classic or basic level of depth-first is an uninformed search, where the algorithm searches a path until it reaches the end of the graph, then backtracks to the start node and tries a different path.

On the contrary, dealing with semantically rich graph databases allows for informed searches, which conduct an early termination of a search if nodes with no compatible outgoing relationships are found. As a result, informed searches also have lower execution times.

(For the record, Cypher queries and Java graph traversals generally perform informed searches.)

Breadth-first search algorithms conduct searches by exploring the graph one layer at a time. They begin with nodes one level deep away from the start node, followed by nodes at depth two, then depth three, and so on until the entire graph has been traversed. Like so:

As you see above, a breadth-first search starts at a given node (marked 0) and then travels to every node that’s only one hop away (marked 1) before it travels to each node that’s two hops away (2). Then, only after it’s traveled to all of the 2 nodes would it explore one more hop to the nodes marked 3.

Like his super-nerd predecessors — al-Khwarizmi and Euler — Edsger Wybe Dijkstra was super into algorithms and super into graphs. That’s probably why he invented Dijkstra’s algorithm in twenty minutes. Like I said, super-nerd.

The goal of Dijkstra’s algorithm is to conduct a breadth-first search with a higher level of analysis in order to find the shortest path between two nodes in a graph. Here’s how it works:

Dijkstra’s algorithm is efficient because it only works with a smaller subset of the possible paths through a graph (i.e., it doesn’t have to read through all of your data). After each node is solved, the shortest path from the start node is known and all subsequent paths build upon that knowledge.

Dijkstra’s algorithm is often used to find real-world shortest paths, like for navigation and logistics. Let’s look at an example.

After all their hard work, al-Khwarizmi and Euler are going to indulge themselves a bit with an item on both of their bucket lists: A good, old-fashioned road trip across Australia — from Sydney to Perth. But, being super-nerd mathematicians, they want to take the shortest route possible, and they’re going to use Dijkstra’s algorithm to find it.

Sydney is the start node and is immediately solved with a value of 0 as our famous duo are already there.

Moving in a breadth-first manner, they look at the next cities one hop away from Sydney: Brisbane, Canberra and Melbourne.

Canberra is the shortest path at 4 hours, so they count that as solved. They continue looking at the rest of the graph from their solved nodes (from Sydney and from Canberra), considering other nodes and selecting the shortest path. From there, Brisbane at 9 hours is the next solved node.

They move on, choosing between Melbourne, Cairns and Alice Springs. Melbourne is the shortest (total) path at 10 hours from Sydney via Canberra, so it becomes their next solved node.

Their next options of neighboring nodes (from already solved ones) are Adelaide, Cairns and Alice Springs. At 18 hours from Sydney via Canberra and Melbourne, Adelaide is the next shortest path.

The next options are Perth (their final destination), Alice Springs and Cairns. While in reality we know that they should next head directly to Perth, al-Khwarizmi and Euler are still sort of new to this whole “Australia even exists” concept. So, they decide to strictly follow Dijkstra’s algorithm no matter what (this is what a computer would do, after all).

According to Dijkstra’s algorithm, Alice Springs is chosen because it has the current shortest path (19 total hours vs. 50 total hours).

Note that because this is a breadth-first search, Dijkstra’s algorithm must first search all still-possible paths, not just the first solution that it happens across. This principle is why Perth isn’t immediately ruled out as the shortest path.

From Alice Springs, their two options are Darwin and Cairns. The latter is 31 hours to the former’s 34 hours, so Cairns is the next solved node.

The only unsolved node left from Cairns is Darwin.

From Darwin, they make the final stretch and end up at Perth, with a cost of 82 hours for this given explored path.

Now that al-Khwarizmi and Euler have used Dijkstra’s algorithm to solve for all possible paths, they can rightly compare the two routes to Perth:

Accordingly, Dijkstra’s algorithm would choose the route via Adelaide and consider Perth from Sydney solved at a shortest path of 50 hours. Let the road trip of the centuries begin.

In the end, we see that the algorithm uses an undirected exploration to find its results. Occasionally, this causes us to explore more of the graph than is intuitively necessary, as Dijkstra’s algorithm looks at each node in relative isolation and may end up following paths that do not contribute to the overall shortest path.

The example above could have been improved if the search had been guided by some heuristic like in a best-first search. To apply that in the road trip example, they could have chosen to prefer heading west over east and south over north, which would have helped avoid the unnecessary (mental) side trips.

The A* algorithm improves upon Dijkstra’s algorithm by combining some of its elements with elements of a best-first search.

Pronounced “A-star”, the A* is based on the observation that some searches are informed, which helps us make better choices over which paths to take through the graph. Like Dijkstra’s algorithm, A* can search a large area of a graph, but like a best-first search, A* uses a heuristic to guide its search.

Additionally, while Dijkstra’s algorithm prefers to search nodes close to the current starting point, a best-first search prefers nodes closer to the destination. A* balances the two approaches to ensure that at each level, it chooses the node with the lowest overall cost of the traversal.

As demonstrated by the example in the previous section, it is possible for Dijkstra’s algorithm to overlook a better route while trying to complete its search.

For more information on how the A* algorithm is used in practice, consult Chapter 7 of Graph Databases from O’Reilly Media.

Graph search algorithms help you (or your friendly computer sidekick) traverse a graph dataset in the most efficient means possible. But “most efficient” depends on the results you’re looking for — a breadth-first search isn’t the most efficient if your results are better suited to depth-first queries (and vice versa).

This is where your super-nerd friends (or even a helpful ebook, if you don’t have friends) come in handy. If they’re smart, they’ll match the right graph algorithm with the type of results you’re looking for.

But now when they start talking breadth-first vs. depth-first vs. best-first you can tell them you know your A* from your Dijkstra and this isn’t your first rodeo. Go make al-Khwarizmi proud.

Add a comment

Related posts:

Earn Rewards by Staking ODE

ODE is proud to announce the launch of our first staking service. Starting Thursday, November 19, you can leverage Proof of Digital Commodity (PoDC) holdings to obtain financial rewards.

Professional Black Silence

Many Afro-American writers bloggers and speakers like to point out a flaw within American Racial Integration. The flaw they point out tends to lean on business and economic ventures, such as black…

Types of Roommate You Meet In A PG Or Shared Space

Moving away from home may be profoundly stressful because it is often difficult to adjust to a new place and environment.Initially, many people fail to adapt to the unlikely environment, but slowly…