473,396 Members | 2,089 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

How to get the second shortest path or k-shortest path

Hello All,

I very need the code of the second shortest path or k-shortest paths that paths are separate(only same as source and destination) and find them almost parallel(not find the shortest path, remove it then find the second path...).

Could you please send me soon.

Thank you very much!
Nguyen
Jul 28 '10 #1
3 7413
jkmyoung
2,057 Expert 2GB
I recommend an altered Dijkstra's algorithm. In this, keep two sets of values, one the best, and the other the second best.

You can similarly extend to k-shortest paths, although this will require much more memory and exponentially more processing time.
Jul 29 '10 #2
@jkmyoung
Hi jkmyoung,

Could you tell me how to choose the second best set?

Thanks so much.
Jul 30 '10 #3
jkmyoung
2,057 Expert 2GB
Doing this with the example on wikipedia on the right:
http://en.wikipedia.org/wiki/Dijkstra%27s_algorithm

start with distance values to each of the nodes as
d2: inf, inf
d3: inf, inf
d4: inf, inf
d5: inf, inf
d6: inf, inf

We have 6 nodes with following edge weights from 1:
(12) - 7, (13) - 9, (16) - 14
So we set
d2 := 7, inf
d3 := 9, inf
d6 := 14, inf

Next round:
(23) = 10, (24) = 15. Here is where it gets a little tricky. Do the incoming edges to 2 first, to find the second edge.
d2:= 7, 19 (19 is 9 + edge of 10)
then set the other vertices:
d3:= 9, 17
d4:= 22, 34

Total:
*d2: 7, 19
d3: 9, 17
d4: 22, 34
d5: inf, inf
d6: 14, inf

Next round:36 = 2, 34 = 11
d3:= 9, 16
There might be some more backtracking, so we double check, 16 + (32) = 16 + 10 = 26 > 19. No more back tracking needed
d6:= 11, 14 (quick back check of edges from 6 reveals no updates needed)
d4, comparing, 20, 27 with 22, 34, we get
d4:= 20, 22

Total:
*d2: 7, 19
*d3: 9, 16
d4: 20, 22
d5: inf, inf
d6: 11, 14

Next is 6: (65) = 9
*d2: 7, 19
*d3: 9, 16
d4: 20, 22
d5: 20, 23
*d6: 11, 14

Finally, we have 4: (45) = 6
d4 + 45 = 26, 28. comparing to 20, 23 nothing changes.

End result:
*d2: 7, 19
*d3: 9, 16
*d4: 20, 22
*d5: 20, 23
*d6: 11, 14

Finding the path back:
You could also store where the last 2 edges came from, to do this quickly. However, it requires more space.
the 2nd furthest path to d5 is 23.

23 - (54) = 17, doesn't match
23 - (56) = 14. Match
14 - (61) = 0 Match (since node 1 implicitly contains values (0, inf).
The second furthest path to node 5 from 1 is 1 - 6 - 5
Aug 4 '10 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

9
by: lawrence | last post by:
Is there an easy way to sort a 2 dimensional array alphabetically by the second field in each row? Also, when I use sort() on a two dimensional array, it seems to work a lot like...
0
by: Frederick Noronha \(FN\) | last post by:
---------- Forwarded message ---------- Solutions to Everyday User Interface and Programming Problems O'Reilly Releases "Access Cookbook, Second Edition" Sebastopol, CA--Neither reference book...
21
by: Jaspreet | last post by:
I was working on some database application and had this small task of getting the second highes marks in a class. I was able to do that using subqueries. Just thinking what is a good way of...
2
by: bryhhh | last post by:
I have am still learning C#, so please bear with me. I have an application that needs to open a second form, but I need the second form to process message loops, whilst the original form also...
4
by: tneufeld | last post by:
I am using a ListView control to display 2 columns of data that I populate manually. I can get the data in the first column easily enough. its when i try to get the data in the second column...
16
by: liking C lang. | last post by:
How to write a C programming that it may output the char one after one every other second? For example: char a="hello world!" output h,next second output e,next second output l...
3
by: Grizlyk | last post by:
Hello. I am thinking: why if i will comment lines marked with '#' in the following example, all will be compiled, else will not: // *********************** typedef unsigned uint; ...
8
by: Ishaaq Datay | last post by:
Hi there I am a novice access user using Access 2003 in a Windows XP Home platform. I have a table containing 2 colums, the first containing a code (CODE) and the second containing the...
1
by: thirunavukarasukm | last post by:
I have a link wich opens a the second page with links wich call a javascript function. I have some problem with javascript. i am two aspx page.. in my first aspx page contain the second...
1
by: =?Utf-8?B?Qm9iQWNoZ2lsbA==?= | last post by:
I am using Windows Media Player to play my half second audio files one after the other using the code below... My problem comes when I play a second audio file immediately after the first one...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.