Undirected graph in Oracle SQL -
i'm trying represent undirected graph in oracle sql, example, have stations graph:
create table station ( station_id integer not null ); create table station_link ( from_station integer not null, to_station integer not null );
this directed graph, have no idea, how make undirected.
point: i need vertices, have path current vertex , information level (how many vertices on path).
for directed graph pretty easy:
select sl.to_station, level station_link sl start sl.from_station = :curvertex connect nocycle prior sl.to_station = sl.from_station
but one-way verticies.
question: do problem have solution, except adding additional links (2 -> 1 1 -> 2)?
there sql fiddle tests: http://sqlfiddle.com/#!4/6c09e/24
for "fast win" can use structure "as is", every edge shold have 2 records in station_link
table.
if want "not_so_fast_but_without_doble_edge_records_please win", can use weirdo-trick:
select x.to_station, x.lvl ( select sl.to_station, level lvl station_link sl start sl.from_station = :curvertex connect nocycle prior sl.to_station = sl.from_station union select sl.from_station to_station , level lvl station_link sl start sl.to_station = :curvertex connect nocycle prior sl.from_station = sl.to_station ) x
it work. actually, combines 2 traversal directions.
but if i'll want implement serious algorithms on graphs in plsql, sdo_geometry data type , oracle spatial , graphs datasheets.
Comments
Post a Comment