Last post Jul 30, 2016 03:53 PM by mgebhard
Jul 25, 2016 11:12 AM|Outlooker|LINK
I've two tables in my database in order to generating a graph. One table for nodes list (NodeID Name) and another for edges (To From Weight).
Now, I want to find cycles in graph with a web-form asp.net app.
May you please tell me is there any procedure or function to do that?
Jul 25, 2016 12:02 PM|PatriceSc|LINK
AFAIK not out of the box. A possible approach would be to navigate in the graph (using a
https://technet.microsoft.com/en-us/library/ms186243(v=sql.105).aspx for example) and count how many times each node is reached.
As soon as the value is 2 it means you have a cycle.
Mayve too late but if not already when you have to build this kind of structure you could also consider using the
https://msdn.microsoft.com/en-us/library/bb677290.aspx data type.
Jul 26, 2016 07:34 AM|Yohann Lu|LINK
As far as I know, we generate a graph by the following steps.
1: Get the data from Database/services (Your two tables need to have a certain relationship)
Tutorial: Data Binding a Chart to a Database:
2: Analytical data and generate a graph in our applications.
You can use Chart to show data.
Creating Line Chart For WebForms Using C#:
Drawing Line Chart in ASP.NET:
You can also use some third party libraries to implement your needs.
If you have some code cause an error, you could also share us more relevant code to help us reproduce the problem.
Jul 30, 2016 08:50 AM|Outlooker|LINK
I've two tables Edge and Node which store edges list and nodes list.
Nodes Table is as follows:
Also, edges Table is as follows:
Now, I want to find all cycles in graph with SQL.
For example there is a cycle from 10 to 10 via the path: 10>13>15>10
How can I find all possible cycles in graph?
Jul 30, 2016 11:20 AM|K_Pramod|LINK
Your question is not clear. Can you give brief description what exactly you want. How the cycles are mapping done, give some examples.
Jul 30, 2016 03:53 PM|mgebhard|LINK
A recursive query and a bit of filtering will work.
However, the data structure can easily cause the recursion to loop forever. For example, adding 15-14 to the edge table. I'm sure data constraints are need if you go with recursion. What the constraints look like? I have no idea. I'd have to study graph
theory to make sure what schema will work. You might find a different approach is required. Since, graph theory is beyond the scope of these forums, so you're on your own in that regard.
IF OBJECT_ID('tempdb..#Edge') IS NOT NULL
DROP TABLE #Edge
CREATE TABLE #Edge (
DECLARE @node INT = 10;
INSERT INTO #Edge (FromNode, ToNode)
VALUES (10, 11),
SELECT FromNode, ToNode FROM #Edge
;WITH edgePath (FromNode, ToNode)
SELECT FromNode, ToNode
WHERE FromNode = @node
SELECT e.FromNode, e.ToNode
FROM #Edge AS e
INNER JOIN edgePath AS ep ON e.FromNode = ep.ToNode
WHERE e.ToNode <> @node -- need to do this otherwise the recursion loops forever
--Results from the CTE
SELECT FromNode AS [Path]
UNION -- Distinct FromNode which filters the 10-11
--Add the last connection
SELECT FromNode AS [Path]
WHERE ToNode = @node
-- add 10 to the end
SELECT @node AS [Path]