Find paths between two nodes using SQL sort by cost

Hi gurus,

I want to find all paths from the source node to the target node that will be sorted by the cost. I don't know if it was possible with the SQL statement. How to start with this query? The script to create the tables and data are given below:

------------------------------------------------------------------------------------------------------------------
create table of nodes (nodeId int Primary Key, nodeName varchar (50));

create paths of table (pathId int Primary Key, int fromNodeId, int toNodeId, int cost);

insert into a values (1, 'ISL') nodes;
insert into a values (2, "LHR") nodes;
insert into a values (3, 'HYD') nodes;
insert into a values (4, 'FLS') nodes;
insert into a values (5, 'MUL') nodes;
insert into a values (6, "KHI") nodes;
insert into a values (7, 'QT') nodes;

insert into values of paths (1,1,3,20);
insert into values of paths (2,1,5,10);
insert into values of paths (3,1,7,80);
insert into values of paths (4,2,4,10);
insert into values of paths (5,3,4,40);
insert into values of paths (6,3,5,20);
insert into values of paths (7,3,6,10);
insert into values of paths (8,6,7,30);
insert into values of paths (9,6,5,30);
insert into values of paths (10,6,3,10);
insert into values of paths (11,7,2,20);
insert into values of paths (12,5,4,40);
insert into values of paths (13,5,7,40);

----------------------------------------------------------------------------------------------------------------------------------
Assume that the source = ISL and QT= target, there are different paths to QT ISL:

Path relative cost DSB #.
==== ================================= =================
1 ISL-> Multimedia-> QT 50
2 ISL->->-> QT 60 KHI HYD
3 ISL-> QT 80
4 ISL-> HYD-> Multimedia-> QT 80
5 ISL-> HYD at KHI-> Multimedia-> QT 100

This gives us all possible paths, sorted by price.

No indication or help will be appreciated.

Thanks in advance and best regards

Bilal

Published by: naive2Oracle on February 11, 2011 09:59
select  ltrim(sys_connect_by_path(nodeName,'->'),'->') path,
        extractvalue(dbms_xmlgen.getXMLtype('select '|| sys_connect_by_path(cost,'+') ||' cost from dual'),'/ROWSET/ROW/COST') - cost cost
  from  paths p,
        nodes n
  where p.fromNodeId = n.nodeId
    and nodeName = 'QT'
  start with nodeName = 'ISL'
  connect by nocycle fromNodeId = prior toNodeId
                 and prior nodeName != 'QT'
    order by cost
/

PATH                                                              COST
-------------------------------------------------- -------------------
ISL->MUL->QT                                                        50
ISL->HYD->KHI->QT                                                   60
ISL->HYD->MUL->QT                                                   80
ISL->QT                                                             80
ISL->HYD->KHI->HYD->MUL->QT                                        100
ISL->HYD->KHI->MUL->QT                                             100

6 rows selected.

SQL> 

SY.

Tags: Database

Similar Questions

  • Calculate the fastest path between 2 nodes with the data model for the Oracle network

    Hi all,

    I have Oracle 10 g 2.

    My problem is the following:

    I created a network named ITALIA_NET in the data model for the Oracle network.
    The table of links of this network is named: ITALIA_NET_LINK$.
    The table of the nodes of this network is named: ITALIA_NET_NODE$.

    The table ITALIA_NET_LINK$ contains a field named COST that contains links (in meters) lengths.

    I've already calculated the SHORTEST PATH between two nodes of the network, by using the method of shortestPath() (using the Java API) as shown on "Pro Oracle Spatial for Oracle Database 11 g" manual. Infact, this method makes reference to the COST field for $ ITALIA_NET_LINK to make this calculation.

    Now, I want to calculate the FASTER PATH between two nodes of the network. I have the links (in hours) travel time to make this calculation.

    My idea is to create a new field in ITALIA_NET_LINK$ named Cost2 containing the travel time from the links and then do the math by using the shorthestPath() method, referring to the Cost2 field to $ ITALIA_NET_LINK COST field instead.
    By default, I know that the shorthestPath() method returns the COST field for $ ITALIA_NET_LINK. Is possible to change this setting and do that this method refers to the Cost2 field?

    In the alternative, is another way for the calculation of the fastest way?
    I want to leave the creation of another network as last solution, because I will have other costs of field (Cost3, cos4t,...)

    Thank you much in advance.

    Your approach is good. You will have two networks and you can read them in memory and analyze of shortest path. The shortestPath method is static for the class of NetworkManager. You can use the same method for both networks, once they are read into memory.

    ...
    read the network with time as cost of link
    NetTime network = NetworkManager.readNetwork (dbConnection, 'ITALIA_NET_TIME');
    read the network length as cost of link
    Network netLength = NetworkManager.readNetwork (dbConnection, 'ITALIA_NET_LENGTH');

    calculate the quickest way
    PathTime path = NetworkManager.shortestPath (netTime, startNodeID, endNodeID);
    calculate the shortest path
    PathLength path = NetworkManager.shortestPath (netLength, startNodeID, endNodeID);
    ...

    In the future, if you upgrade to 11g, network data model provides a load on demand (LOD) API that loads only the scores of necessary network in memory during the analysis. This command removes the restriction of the memory of the 10g (in memory API you use) API. API of LOD can handle very large networks and offers more features analysis and modeling capabilities.

    The following link contains the tutorial of NDM LOD API ready for download. Just for your information.
    https://spatial.SampleCode.Oracle.com/servlets/ProjectProcess?PageID=0Zl7oV

    Kind regards
    Jack

  • How to find directions between two locations other than my current position. I finally gave up and use Google Maps.

    In the latest version of cards, there is not apparent until you find directions between two places when or is your current position. In earlier versions, we enter in two places of departure and destination. In the new version, you enter your destination into a search window, but there is no place to enter a starting location. The application assumes that your current place of residence is always your place of departure. It is a remarkable oversight by designers. Am I missing something?

    If you go to maps, you can search for a location. Once you find the place, press the route button. It is from your current location, but if you type on my position, next: you can change the location that you want the management of.

  • Connectivity between two nodes of Virtual Box

    For a few days, I try to establish the network connectivity between two nodes OEL (Oracle Enterprise Linux) in Oracle Virtualbox. I tried everything I have found Google, but still nothing works, everytime I try to ping from one node to another node Hostname/IP is showing unknown host. All the advice everyone here will be great for me.

    Published by: 918868 on October 30, 2012 02:30

    When you create a virtual machine, by default VirtualBox allows virtual network card and selects the "Network Address Translation" (NAT) mode for it. In this way the guest can connect to the outside world using the networking of the host and the outside world can connect to services on the feedback that you choose to make visible outside of the virtual machine.

    This default configuration is good probably 95% of users of VirtualBox. VirtualBox is, however, extremely flexible in how it can virtualize networks. It supports multiple virtual NICs per virtual machine, the first four that can be configured in detail in the Manager window. Additional network cards can be configured on the command-line with VBoxManage.

    Source: http://www.virtualbox.org/manual/ch03.html#settings-network

    For more details, please see:

    Virtual networks
    http://www.VirtualBox.org/manual/CH06.html

    Concerning
    Girish Sharma

  • How to remove a path between two anchor points

    Hello

    everything is in the title,

    could you tell me please how to remove a path between two anchor points without using the tool Eraser path?

    When I select these two folders and press to remove all disappeared.

    PS: I want to keep the anchor after the removal

    Thank you

    with the Selection tool direct (white pointer), click the path segment (no points) or select slide above without going on something else and tap on delete.

  • It is possible to select a segment of a path between two points outside their clipping mask?

    I have a way with 3D rotation effect tends in a clipping mask. In the Preview mode (Ctrl + Y), most of the road is not the clipping mask, but most of the visible inside the mask in normal view because of the 3D effect turns. I want to select and remove a segment of the path between two points outside the clipping path, but Illustrator does not select the components of access outside their clipping mask path.

    Anyway is to do without for as much free mask?

    ALWAYS indicate the version you are using. Regulars here using one of the six different versions of Illustrator. Assuming that the current version:

    White pointer: marquee select around a portion of the original route which is located in the clipping mask.

    OR

    Black hand: select the object. Make a right click the object. Select isolate selected in the popup clipping mask.

    JET

  • Photoshop CS4 - is - does anyone know how to find out if two people use the same file?

    Photoshop CS4 - is - does anyone know how to find out if two people use the same file?

    Yes, I assumed that was the case, and I thought that this link addressed to her.  I would have thought it was more a function of your network and the OS than Photoshop.

  • All paths between 2 nodes

    Hello

    Can you get it someone please let me know how can I achieve this?

    I want to know all the paths between A Source node and the node of Destination B.

    I donot want the shortest way but all paths between the 2 nodes.

    Also I donot have no weight/cost by any path. (The graph is a directed graph)

    Kind regards
    Pantxo

    Hello

    Maybe this:

    Scott@my11g SQL>l
      1  with links(n1,n2) as (
      2  select 'A','B' from dual
      3  union all select 'A','C' from dual
      4  union all select 'B','C' from dual
      5  union all select 'B','D' from dual
      6  union all select 'D','G' from dual
      7  union all select 'C','G' from dual
      8  union all select 'D','I' from dual
      9  union all select 'C','E' from dual
     10  union all select 'E','F' from dual
     11  union all select 'F','G' from dual
     12  union all select 'F','H' from dual
     13  )
     14  select pth
     15  from (
     16  select connect_by_root(n1) || sys_connect_by_path(n2,'>') pth ,n2
     17  from links
     18  start with n1='A'
     19  connect by nocycle prior n2=n1
     20  )
     21* where n2='G'
    Scott@my11g SQL>/
    
    PTH
    ----------------------------------------
    A>B>C>E>F>G
    A>B>C>G
    A>B>D>G
    A>C>E>F>G
    A>C>G
    
  • Is it possible to synchronize a directory between two computers using ReadyCloud Desktop Client?

    Hi all

    Is it possible to use the ReadyCloud Desktop Client to synchronize a directory between two computers, i.e. of office and computer laptop?

    I use OS 6.4.2 and the latest version of the Desktop Client at ReadyCloud.

    In addition, both computers are running Windows 10 Pro.

    Thanks in advance

    Kevin

    Hello knocwans,

    Sorry, but ReadyCloud app is designed to allow you to synchronize folders computer device ReadyNAS and not from computer to computer. There is no option on the client where you select as the source computer and another computer as the destination.

    Kind regards

  • Length of the different interval between two dates in SQL/PLSQL

    Dear gurus, here are three questions to ask:
    1. Why is the result queries are different?
    2 - is a way to measure the length of the interval in seconds (L) between two dates?
    2. why the different result is generated in SQL and PL/SQL only to subtract dates from the 28th day of the month?

    declare
    number of l_offset;
    Start
    l_offset: = (to_date('09-nov-2009') - to_date ('28.10.2009 10:40:03 ', ' dd.mm.yyyy hh24:mi:ss')) * 86400;))
    dbms_output.put_line (l_offset);
    l_offset: = (to_date('09-nov-2009') - to_date ('27.10.2009 10:40:03 ', ' dd.mm.yyyy hh24:mi:ss')) * 86400;))
    dbms_output.put_line (l_offset);
    end;
    998396.9999999999999999999999999999999997
    1084797


    Select (to_date('09-nov-2009') - to_date ('28.10.2009 10:40:03 ', ' dd.mm.yyyy hh24:mi:ss')) * double 86400))
    Union of all the
    Select (to_date('09-nov-2009') - to_date ('27.10.2009 10:40:03 ', ' dd.mm.yyyy hh24:mi:ss')) * double 86400))
    (TO_DATE('09-NOV-2009')-TO_DAT
    998397
    1084797


    Oracle9i Enterprise Edition Release 9.2.0.7.0 - Production
    PL/SQL Release 9.2.0.7.0 - Production
    CORE 9.2.0.7.0 Production
    AMT for Linux: release 9.2.0.7.0 - Production
    NLSRTL Version 9.2.0.7.0 - Production

    Hello

    Sorry, I'm still not sure you understand the question.

    You wonder why the rounding error is apparent to 6 digits to the left of the decimal separator in numbers, but not in number 7 digits to the left of the decomal? The answer is itself a rounding error.
    When Oracle is the arithmetic mean of the dates, the result is a fraction that cannot be stored in a binary number, such as preciesly
    998396.9999999999999999999999999999999996857... it it rounds to the nearest value it can store, such as
    998396.9999999999999999999999999999999997. in other words, it can store 40 digits. In this case, 6 figures to the left of the decimal separator and 34 of the figures to the right. The number is rounded to the nearest multiple of 10 ^-34 ^
    What happens if we add 86400 this number?

    
     998396.9999999999999999999999999999999997
    + 86400.0000000000000000000000000000000000
    ------------------------------------------
    1084796.9999999999999999999999999999999997
    

    Now we need 7 digits before the comma, then round the number to the nearest multiple of 10 ^-33 ^, which is
    1084797.000000000000000000000000000000000

  • How to find days between two dates in days

    Hi, I want to get the days between two dates, expressed in days, how can I do this?  For example, I date i.e. 1 October 10, 2013 (Thursday) and 2 as October 13, 2013 (Sunday).  How can I get a production of:

    Thursday, Friday, Saturday and Sunday, which are the days between Oct. 10-13, 2013.  I hope you understand my point.  Thank you

    You can try something like this

    CURSOR LOAD_PROF is

    SELECT load_prof1, v_time_profile_day

    OF load_profile_test

    WHERE profile_day IN (select TO_CHAR(:day1+level-1,'Day')

    of the double

    connect by level<>

    );

    Kind regards

    S Pax

  • XPath to capture nodes between two nodes

    Hi all

    I met a problem a seemingly simple challenge of mine.

    I have the following xml code.

    Basically, I would like to capture the text between the begin-end blocks

    <? XML version = "1.0" encoding = "UTF-8"? >

    < document >

    < body >

    < paragraph >

    < run >

    < fld type = 'start' / >

    < / run >

    < run >

    < text > SAMPLE < / text >

    < / run >

    < run >

    < text > < / text >

    < / run >

    < run >

    < text > FIELD < / text >

    < / run >

    < run >

    < fld type = 'end' / >

    < / run >

    < run >

    < text > TEXT UNWANTED SOME < / text >

    < / run >

    < / paragraph >

    < paragraph >

    < run >

    < fld type = 'start' / >

    < / run >

    < run >

    < text > SAMPLE < / text >

    < / run >

    < run >

    < text > < / text >

    < / run >

    < run >

    < text > Field2 < / text >

    < / run >

    < run >

    < fld type = 'end' / >

    < / run >

    < run >

    < text > TEXT UNWANTED SOME < / text >

    < / run >

    < run >

    < fld type = 'start' / >

    < / run >

    < run >

    < text > SAMPLE < / text >

    < / run >

    < run >

    < text > < / text >

    < / run >

    < run >

    < text > FIELD3 < / text >

    < / run >

    < run >

    < fld type = 'end' / >

    < / run >

    < / paragraph >

    < / body >

    < / document >

    So far, what I came up with is:

    WITH temp AS (
    SELECT 
           XMLTYPE ('<?xml version="1.0" encoding="UTF-8"?>
                      <document>
                         <body>
                            <paragraph>
                               <run>
                                  <fld type="begin" />
                               </run>
                               <run>
                                  <text>SAMPLE</text>
                               </run>
                               <run>
                                  <text> </text>
                               </run>
                               <run>
                                  <text>FIELD</text>
                               </run>
                               <run>
                                  <fld type="end" />
                               </run>
                               <run>
                                  <text>SOME UNWANTED TEXT</text>
                               </run>
                            </paragraph>
                            <paragraph>
                               <run>
                                  <fld type="begin" />
                               </run>
                               <run>
                                  <text>SAMPLE</text>
                               </run>
                               <run>
                                  <text> </text>
                               </run>
                               <run>
                                  <text>FIELD2</text>
                               </run>
                               <run>
                                  <fld type="end" />
                               </run>
                               <run>
                                  <text>SOME UNWANTED TEXT</text>
                               </run>
                               <run>
                                  <fld type="begin" />
                               </run>
                               <run>
                                  <text>SAMPLE</text>
                               </run>
                               <run>
                                  <text> </text>
                               </run>
                               <run>
                                  <text>FIELD3</text>
                               </run>
                               <run>
                                  <fld type="end" />
                               </run>
                            </paragraph>
                         </body>
                      </document>') AS xml_template
    FROM dual
    )
    SELECT x.*
      FROM temp
          ,XMLTABLE('//*/paragraph/run[fld/@type="begin"]/following-sibling::run[following-sibling::run[fld/@type="end"]]/text'
                    PASSING temp.xml_template
                    COLUMNS res varchar2(100) PATH '.'
                   )x
    

    Result:

    RES

    SAMPLE

    FIELD

    SAMPLE

    FIELD2

    SOME UNWANTED TEXT

    SAMPLE

    FIELD3

    Unfortunately what I wrote XPath is not good enough, because with regard to the text between the two blocs of start-end it still captures the unwanted text.

    My XPath knowledgege dept unfortunately not so I would appriciate annything you can help me with even an idea could get through on the edge.

    Thank you

    Steve

    Steve,

    XQuery can help in this case:

    SELECT x.*
    FROM temp
       , XMLTABLE('for $begin in /document/body/paragraph/run[fld/@type="begin"]
                   let $end := $begin/following-sibling::run[fld/@type="end"][1]
                   return $begin/following-sibling::run[. << $end]'
           PASSING temp.xml_template
           COLUMNS res varchar2(100) PATH 'text'
         ) x  ;
    

    Explanation:

    This FLWOR expression traverses all nodes 'start' in the document, finds the corresponding node of the 'end' and returns the sequence of "run" nodes follows 'begin' and above 'end '.

    If you want to concatenate all the fragments of text in a block, you can also do it directly, like this:

    for $begin in /document/body/paragraph/run[fld/@type="begin"]
    let $end := $begin/following-sibling::run[fld/@type="end"][1]
    return string-join($begin/following-sibling::run[. << $end]/text, "")
    

    She's going back 3 ranks:

    RES

    -------------

    EXAMPLE OF A FIELD

    SAMPLE FIELD2

    SAMPLE FIELD3

  • I looked through the video tutorials for CS6 and can't find any help for PHP using SQL.

    Where is a good place I can go to learn more about the use of different languages as html and dreamweaver.  Is there a service that adobe offers that I could buy that would take me from recruit all the way to professional with a good structure, easy to use, such as adobe tv but more advanced and deeper?  I am building a Web site with a log in page and check.  I have the HTML part down pretty well, but need help to write php scripts and the use of SQL for storing user information

    I moved this to the main Dreamweaver support forum.

    In response to your question, you should be aware that PHP in CS6 Dreamweaver server behaviors use original MySQL functions that are should be removed from a future release of PHP. Server behaviors have been deleted from the CC of Dreamweaver. If you plan to create a website using PHP and MySQL, do not rely on Dreamweaver server behaviors. You must instead use MySQLi (improved MySQL) or PDO (PHP Data Objects) .

    If you are looking for video tutorials, you might be interested in the class I created for lynda.com. As a beginner, a good starting point would be Introducing PHP or PHP for Web designers (there are several videos of the sample that you can watch for free on my site). Both courses have been recorded on Dreamweaver (PHP for Dreamweaver CS6 Web designers), presenting the PHP on Dreamweaver CS5.5. PHP for Web designers shows how to connect to MySQL with MySQLi. You need a subscription to lynda.com to watch full courses, but you can get a free seven day trial by following the links on my site.

    If you don't want to commit to a subscription service, I also wrote a book called PHP Solutions, which covers MySQLi and PDO in depth. It also shows how to build a login system. At the moment, the second edition is available, but a third revised edition is due to the on sale in December.

    There are also a lot of free resources on the web you can find. The important thing to beware of is that a lot of old documents is based on the original functions of MySQL. Regardless of the resource you use, make sure it shows how to use MySQLi or PDO.

  • Share pst file between two users using the same Windows 7 Comp.

    A Windows 7 computer with two user accounts how can I share the pst files & psb between users?

    DONMS wrote:
    > With a computer Windows 7 and two user accounts how can I share the pst & psb
    > files between users?

    Re-ask this question in a forum of Outlook to receive the best advice.

    --

    Bruce Chambers

    Help us help you:
    http://www.CatB.org/~ESR/FAQs/smart-questions.html

    http://support.Microsoft.com/default.aspx/KB/555375

    They who can give up liberty for a little temporary
    safety deserve neither liberty nor safety. ~ Benjamin Franklin

    A lot of people could die rather that thinking; in fact, most do. ~ Bertrand Russell

    The philosopher never killed the priests, while the priest was
    has killed a large number of philosophers.
    ~ Denis Diderot

  • How to find values between two months (from January to the month prompted)?

    Hello

    I need to create a filter on the name of the month. That is to say of January of the year for the 'guest' month of guest of dashboard.

    Ex: If I ask year and month of year in 2013 and March respectively. I should get values for Jan 2013, 2013 Feb and March 2013.

    I can't try one between because that it only works on numeric values.

    I can't have a number of months in prompt because this isn't the condition and I am not allowed to.

    I tried the below query report filter

    «Sun - times» "" Fiscal month "< = (select"Dim - time". "" Fiscal month "where"Dim - time. " ("' Name of the fiscal month ' = @{PMonth}) PMonth where is the Variable of presentation" for months ".

    But it's not working

    Help, please

    Thank you

    If it is 11g then try to use the column twice.

    Thank you

    http://cool-bi.com

Maybe you are looking for

  • Regestiration problem

    Dear Sir I just bought a new laptop A300 1NO and after I registered on your site, it gives me that my A300-148. Really, I put t know that I have, should I contact my dealer? Or what?Please help me because the specifications in the A300-148, is not wh

  • image monitoring event

    Hello Ideally, I like to have the situation to display a number of pictures of feather flags 2D picture on my front. Then during one of the indicators photo click/double click on an event is raised and handled accordingly. As an indicator image has n

  • Analysis of XML files

    I have a problem, the analysis of this XML file. I swear it worked but now the sections DC_Source and DC_Load return null.

  • How the * fact pick from Microsoft that they deal with the problems of software?

    OK, so I just got a phone call from people "Microsoft" Software, saying that there is crazy with my computer problems. The person who I understand only thanks to a focus then directed me to look at the event viewer, so I could confirm a problem with

  • TouchSmart HP Envy 15-j020eb: SSD installation?

    Hello I am looking for a new laptop and the Touchsmart from HP Envy 15-j020eb looks like the best buy for me, except there is no SSD... Is it possible to build in an SSD (ssd or msata ssd)? Kind regards