当有多个交换机时,如何找到最短的总线路由?

时间:2014-07-26 10:33:42

标签: c# asp.net sql

我正在开发一个项目,帮助一个人找到公共汽车路线,公共汽车没有和公交车之间的切换细节。我能够找到直到单开关的路线,但更多的是我无法这样做。请帮忙。

现在我的任务是如何从“Cantt”中 GO“SARAI”?使用相同的表格。

列Bus_Stop_Up使总线路由向上,Bus_Stop_Down具有向下的总线路由。

结果应该像“Cantt(781) - > Dwarka(764) - > Nehruplace(456) - > SARAI”

表格详情如下:

CREATE TABLE [dbo].[bustable]
(
   [Sr] [int] NULL
   [bus_no] [varchar](50) NULL,
   [Bus_Stop_Up] [varchar](50) NULL,
   [Bus_Stop_Up_Id] [int] NULL,
   [Bus_Stop_Down] [varchar](50) NULL,
   [Bus_Stop_Down_Id] [int] NULL,
)

表格数据

||Sr    | bus_no | Bus_Stop_Up |    Bus_Stop_Down | Bus_Stop_Up_Id | Bus_Stop_Down_Id||              
-------------------------------------------------------------------------------------------------------------
||1     | 781    | DWARKA      |  NEW DELHI       |  1             |    1            ||
||2     | 781    | Airport     |  Cantt           |  2             |    2            ||
||3     | 781    | Cantt       |  Airport         |  3             |    3            ||
||4     | 781    | NEW DELHI   |  DWARKA          |  4             |    4            ||
||5     | 764    | DWARKA      |  NEHRU PLACE     |  1             |    1            ||
||6     | 764    | NEHRUPLACE  |  DWARKA          |  2             |    2            ||
||7     | 456    | NEHRU PLACE |  SARAI           |  1             |    1            ||
||8     | 456    | SARAI       |  NEHRU PLACE     |  2             |    2            ||

1 个答案:

答案 0 :(得分:0)

我刚才发布过这样的话,在这里: Graph problems: connect by NOCYCLE prior replacement in SQL server?

你会在这里找到进一步的提示,我在那里交叉发布了这个问题:
http://social.msdn.microsoft.com/Forums/sqlserver/en-US/32069da7-4820-490a-a8b7-09900ea1de69/is-there-a-nocycle-prior-replacement-in-sql-server?forum=transactsql

Graph

CREATE TABLE [dbo].[T_Hops](
    [UID] [uniqueidentifier] NULL,
    [From] [nvarchar](1000) NULL,
    [To] [nvarchar](1000) NULL,
    [Distance] [decimal](18, 5) NULL
) ON [PRIMARY]

GO




      INSERT INTO [dbo].[T_Hops]             ([UID]             ,[From]             ,[To]             ,[Distance])       VALUES             (newid()              ,'A'              ,'E'              ,10.00000              );   
      INSERT INTO [dbo].[T_Hops]             ([UID]             ,[From]             ,[To]             ,[Distance])       VALUES             (newid()              ,'E'              ,'D'              ,20.00000              );   
      INSERT INTO [dbo].[T_Hops]             ([UID]             ,[From]             ,[To]             ,[Distance])       VALUES             (newid()              ,'A'              ,'B'              ,5.00000              );   
      INSERT INTO [dbo].[T_Hops]             ([UID]             ,[From]             ,[To]             ,[Distance])       VALUES             (newid()              ,'B'              ,'C'              ,10.00000              );   
      INSERT INTO [dbo].[T_Hops]             ([UID]             ,[From]             ,[To]             ,[Distance])       VALUES             (newid()              ,'C'              ,'D'              ,5.00000              );   
      INSERT INTO [dbo].[T_Hops]             ([UID]             ,[From]             ,[To]             ,[Distance])       VALUES             (newid()              ,'A'              ,'F'              ,2.00000              );   
      INSERT INTO [dbo].[T_Hops]             ([UID]             ,[From]             ,[To]             ,[Distance])       VALUES             (newid()              ,'F'              ,'G'              ,6.00000              );   
      INSERT INTO [dbo].[T_Hops]             ([UID]             ,[From]             ,[To]             ,[Distance])       VALUES             (newid()              ,'G'              ,'H'              ,3.00000              );   
      INSERT INTO [dbo].[T_Hops]             ([UID]             ,[From]             ,[To]             ,[Distance])       VALUES             (newid()              ,'H'              ,'D'              ,1.00000              );   

现在我可以查询从点x到点y的最佳连接,如下所示:

WITH AllRoutes 
(
     [UID]
    ,[FROM]
    ,[To]
    ,[Distance]
    ,[Path]
    ,[Hops]
)
AS
(
    SELECT 
         [UID]
        ,[FROM]
        ,[To]
        ,[Distance]
        ,CAST(([dbo].[T_Hops].[FROM] + [dbo].[T_Hops].[To]) AS varchar(MAX)) AS [Path]
        ,1 AS [Hops]
      FROM [dbo].[T_Hops]
      WHERE [FROM] = 'A'

    UNION ALL


    SELECT 
         [dbo].[T_Hops].[UID]
        --,[dbo].[T_Hops].[FROM]
        ,Parent.[FROM]
        ,[dbo].[T_Hops].[To]
        ,CAST((Parent.[Distance] + [dbo].[T_Hops].[Distance]) AS [decimal](18, 5)) AS distance
        ,CAST((Parent.[Path] + '/' + [dbo].[T_Hops].[FROM] + [dbo].[T_Hops].[To]) AS varchar(MAX)) AS [Path]
        ,(Parent.[Hops] + 1) AS [Hops]
     FROM [dbo].[T_Hops]
     INNER JOIN AllRoutes AS Parent 
            ON Parent.[To] = [dbo].[T_Hops].[FROM] 

)

SELECT TOP 100 PERCENT * FROM AllRoutes


/*
WHERE [FROM] = 'A' 
AND [To] = 'D'
AND CHARINDEX('F', [Path]) != 0 -- via F
ORDER BY Hops, Distance ASC
*/

GO