Solving the River Crossing problem with SQL Graph

Solving the River Crossing problem with SQL Graph

Solving the River Crossing problem with SQL Graph. Let’s see if we can solve this classic problem, using T-SQL and the graph capabilities in Azure SQL / SQL Server!

Are you are familiar with the classic “river crossing” problem? If not, an easy 4 minutes introduction using Wildebeest and Lions as the actors in the problem, is presented in this TED-Ed talk. If you observe carefully, the video briefly mentions using an enumeration approach to list all the possible transitions, and for each transition, the end state. It so happens that this is the classic algorithmic approach as well to solve such problems – to create a graph of all possible states (represented by vertices / nodes in the graph) and the transitions (represented by edges) which result into those states. If you search online, there are plenty of references showing you how to use Breadth-First-Search (BFS) to find the shortest path to solve the problem. David Kopec’s Classic Computer Science Problems books also present implementations of this problem in Python and other languages.

Let’s see if we can solve this classic problem, using T-SQL and the graph capabilities in Azure SQL / SQL Server!

Governing Rules

Let’s present the main “rules” of this problem, as presented in the TED talk:

  1. There are 3 wildebeest and 3 lions, all located on one bank (for completeness, we have the West bank, and the East bank) of a river. They are trying to cross the other river bank.
  2. As it turns out, wildebeest are a natural prey for the lions. Therefore, there is a “rule” which states that if the number of wildebeest either equal, or outnumber the number of lions, on the same bank of the river, they are safe. In our context, we will call this a “safe” state.
  3. Conversely, If the number of wildebeest are lesser than the number of lions, they are unsafe. In our context, we will refer to this as an “unsafe” or “disallowed” state, as we don’t want the wildebeest to come to harm!
  4. Let’s also imagine, that these animals know how to use a raft, to cross the river However, the raft can only take 2 animals at any time, and obviously, the raft is not “autonomous”, and needs an animal or 2 animals on board to move from one bank of the river to the other.
  5. At any given point in time, rules 2 and 3 must continuously be applied, even when 2 animals are on the raft!

azure sql graph t-sql sql

What is Geek Coin

What is GeekCash, Geek Token

Best Visual Studio Code Themes of 2021

Bootstrap 5 Tutorial - Bootstrap 5 Crash Course for Beginners

Nest.JS Tutorial for Beginners

Hello Vue 3: A First Look at Vue 3 and the Composition API

Introduction to Structured Query Language SQL pdf

SQL stands for Structured Query Language. SQL is a scripting language expected to store, control, and inquiry information put away in social databases. The main manifestation of SQL showed up in 1974, when a gathering in IBM built up the principal model of a social database. The primary business social database was discharged by Relational Software later turning out to be Oracle.

How to set up Azure Data Sync between Azure SQL databases and on-premises SQL Server

In this article, you learn how to set up Azure Data Sync services. In addition, you will also learn how to create and set up a data sync group between Azure SQL database and on-premises SQL Server.

Creating and Cataloging SQL pools in Azure SQL Server

In this article, we will show how to create a new SQL Pool within an existing Azure SQL Server as well as catalog the same using the Azure Purview service

How to Create Elastic Jobs in Azure SQL Database Using T-SQL

How to Create Elastic Jobs in Azure SQL Database Using T-SQL. In this episode of Data Exposed, Kate Smith walks us through the process of how to create Elastic Jobs for Azure SQL using T-SQL.

Welcome Back the T-SQL Debugger with SQL Complete – SQL Debugger

Debug SQL stored procedures and develop your SQL database project with dbForge SQL Complete, a new add-in for Visual Studio and SSMS. When you develop large chunks of T-SQL code with the help of the SQL Server Management Studio tool, it is essential to test the “Live” behavior of your code by making sure that each small piece of code works fine and being able to allocate any error message that may cause a failure within that code.