Best Way to Store/Access a Directed Graph

后端 未结 6 621
清酒与你
清酒与你 2021-02-01 09:14

I have around 3500 flood control facilities that I would like to represent as a network to determine flow paths (essentially a directed graph). I\'m currently using SqlServer an

相关标签:
6条回答
  • 2021-02-01 09:33

    Yes (maybe). Your data set sounds relatively small, you could load the graph to memory as an adjacency matrix or adjacency list and query the graph directly - assuming you program.

    As far as on-disk format, DOT is fairly portable/popular among others. It also seems pretty common to store a list of edges in a flat file format like:

    vertex1 vertex2 {edge_label1}+
    

    Where the first line of the file contains the number of vertices in the graph, and every line after that describes edges. Whether the edges are directed or undirected is up to the implementor. If you want explicit directed edges, then describe them using directed edges like:

    vertex1 vertex2
    vertex2 vertex1
    
    0 讨论(0)
  • 2021-02-01 09:35

    Traditionally graphs are either represented by a matrix or a vector. The matrix takes more space, but is easier to process(3500x3500 entries in your case); the vector takes less space(3500 entries, each have a list of who they connect to).

    Does that help you?

    0 讨论(0)
  • 2021-02-01 09:37

    The best way to store graphs is of course to use a native graph db :-)

    Take a look at neo4j. It's implemented in Java and has Python and Ruby bindings as well.

    I wrote up two wiki pages with simple examples of domain models represented as graphs using neo4j: assembly and roles. More examples are found on the domain modeling gallery page.

    0 讨论(0)
  • 2021-02-01 09:52

    My experiences with storing something like you described in a SQL Server database:

    I was storing a distance matrix, telling how long does it take to travel from point A to point B. I have done the naive representation and stored them directly into a table called distances with columns A,B,distance,time.

    This is very slow on simple retreival. I found it is lot better to store my whole matrix as text. Then retreive it into memory before the computations, create an matrix struxture in memory and work with it there.

    I could provide with some code, but it would be C#.

    0 讨论(0)
  • 2021-02-01 09:53

    I know nothing about flood control facilities. But I would take the first facility. And use a temp table and a while loop to generate the path.

    -- Pseudo Code
    TempTable (LastNode, CurrentNode, N)

    DECLARE @intN INT SET @intN = 1

    INSERT INTO TempTable(LastNode, CurrentNode, N) -- Insert first item in list with no up stream items...call this initial condition SELECT LastNode, CurrentNode, @intN FROM your table WHERE node has nothing upstream

    WHILE @intN <= 3500 BEGIN SEt @intN = @intN + 1 INSERT INTO TempTable(LastNode, CurrentNode, N) SELECT LastNode, CurrentNode, @intN FROM your table WHERE LastNode IN (SELECT CurrentNode FROM TempTable WHERE N = @intN-1)

    IF @@ROWCOUNT = 0
         BREAK
    

    END

    If we assume that every node points to one child. Then this should take no longer than 3500 iterations. If multiple nodes have the same upstream provider then it will take less. But more importantly, this lets you do this...

    SELECT LastNode, CurrentNode, N FROM TempTable ORDER BY N

    And that will let you see if there are any loops or any other issues with your provider. Incidentally 3500 rows is not that much so even in the worst case of each provider pointing to a different upstream provider, this should not take that long.

    0 讨论(0)
  • 2021-02-01 09:55

    i think your data structure is fine (for SQL Server) but a CTE may not be the most efficient solution for your queries. You might try making a stored procedure that traverses the graph using a temp table as a queue instead, this should be more efficient.

    the temp table can also be used to eliminate cycles in the graph, though there shouldn't be any

    0 讨论(0)
提交回复
热议问题