473,395 Members | 1,583 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Floyd's algorithm in SQL

I am re-doing the graphs section for the third edition of SQL FOR
SMARTIES and would like some feedback and help.

Here is a version of Floyd's algorithm to find the transitive closure
of a graph. You can also look up Warshall's algorithm, which is an
earlier and more specialized version of Floyd's algorithm. Given a
directed graph G represented by an adjacency matrix A[i, j], WHERE
A[i, j] = 1 if the edge (i, j) is in the graph, compute the matrix P
of paths with their lengths. Now put it into SQL:

CREATE TABLE Paths
(source CHAR(1)NOT NULL,
destination CHAR(1) NOT NULL,
lgth INTEGER DEFAULT 0 NOT NULL);

CREATE TABLE Edges
(source CHAR(1)NOT NULL,
destination CHAR(1)NOT NULL,
PRIMARY KEY (source, destination),
lgth INTEGER DEFAULT 1 NOT NULL);

INSERT INTO Edges (source, destination) VALUES ('a', 'b');
INSERT INTO Edges (source, destination) VALUES ('b', 'c');
INSERT INTO Edges (source, destination) VALUES ('c', 'd');
INSERT INTO Edges (source, destination) VALUES ('d', 'a');
INSERT INTO Edges (source, destination) VALUES ('a', 'e');

The algorithm extends each known path by one edge at a time. Since no
path can be longer than the number of nodes in the graph, we have an
upper bound on iterations. But we can also stop when the INSERT
statement adds no new paths.

CREATE PROCEDURE Floyd()
LANGUAGE SQL
DETERMINISTIC
BEGIN
DECLARE old_path_tally INTEGER;
SET old_path_tally = 0;
DELETE FROM Paths; -- clean out working table
INSERT INTO Paths SELECT * FROM Edges; -- load the edges

-- add one edge to each path
WHILE old_path_tally < (SELECT COUNT(*) FROM Paths)
DO INSERT INTO Paths (source, destination, lgth)
SELECT E1.source, P1.destination, (E1.lgth + P1.lgth)
FROM Edges AS E1, Paths AS P1
WHERE E1.destination = P1.source
AND NOT EXISTS -- path is not here already
(SELECT *
FROM Paths AS P2
WHERE E1.source = P2.source
AND P1.destination = P2.destination);
SET old_path_tally = (SELECT COUNT(*) FROM Paths);
END WHILE;
END;

You could also have used a SQLSTATE code to detect any empty
insertion. I am not sure if that would actually be faster or not.

SELECT * FROM Paths;
source destination lgth
=========================
a b 1
a e 1
b c 1
c d 1
d a 1
a c 2
b d 2
c a 2
d b 2
d e 2
a d 3
b a 3
c b 3
c e 3
d c 3
a a 4
b b 4
b e 4
c c 4
d d 4

What I'd like to know is:

1) Can I put Paths in a VIEW defined by a recursive WITH operator? I
don't have a copy of a product that implements this SQL-99 feature.

2) Can I modify my SQL-92 code or the SQL-99 to handle the lowest cost
problem? That is, each edge has a positive cost; what is the CHEAPEST
path from x to y, not the SHORTEST path?

There is fame, glory and virtual beer in this puzzle.
Nov 12 '05 #1
0 3289

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Jack Smith | last post by:
Hello, any help appreciated with following problem. I figured out the algorithm (I think), just having trouble proving it is optimal. Suppose we are given n tasks each of which takes 1 unit...
1
by: Dave | last post by:
Hello, I'm looking for a Floyd-Warshall Python implementation. The only implementation I have seen (searching this newsgroup) used a Python library called "Numeric". This implementation...
2
by: Julio C. Hernandez Castro | last post by:
Dear all, We have just developped a new block cipher called Raiden, following a Feistel Network structure by means of genetic programming. Our intention now consists on getting as much feedback...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.