C Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesC Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old November 22nd, 2012, 07:35 PM
paths000 paths000 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2012
Posts: 1 paths000 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 20 m 36 sec
Reputation Power: 0
All paths between two nodes

Hi everyone, I’m trying to solve problem in which I have to calculate the number of paths from one node to all of other nodes in the directed graph and I have a predetermined length of paths. The problem can be solved by simple recursive function, but the size of the input data precludes this option. Connections between two nodes which appear at the input, can appear more then once and we treat them as a different connections.

Code:
N, M, K (1<=N<=40, 1<=M<=100000, 1<K=10^1000), where n is number of nodes, m is number of connections and k is length of paths.


Example:
Code:
Input:
4 6 2
1 2
2 1
1 1
1 2
2 4
1 3
Output:
3
2
1
2

Explanation:
All possible paths with 2 hops are: (1,1,1), (1,2,1), (1,2,1), (1,1,2), (1,1,2), (1,1,3), (1,2,4), (1,2,4).


The fact is that the number of vertices is very small relative to the number of connections, so in the graph will be many hundreds of cycles. I wonder if somehow can use this fact.
Please, help me.

Reply With Quote
  #2  
Old November 23rd, 2012, 04:44 PM
b49P23TIvg's Avatar
b49P23TIvg b49P23TIvg is online now
Contributing User
Dev Shed Loyal (3000 - 3499 posts)
 
Join Date: Aug 2011
Posts: 3,347 b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level) 
Time spent in forums: 1 Month 2 Weeks 3 Days 6 h 53 m 17 sec
Reputation Power: 383
4 6 2 are your m n k
The pairs are directed paths.

All possible paths with 2 hops starting from node 1 are:
(1,1,1), (1,2,1), (1,2,1), (1,1,2), (1,1,2), (1,1,3), (1,2,4), (1,2,4)

Let G be the matrix connectivity matrix, where the connections are the count of like paths. (I've named my "G" noun as "GRAPH.)
Code:
   ('G';'  TO'),:(,.'FROM');GRAPH
┌─┬───────┐
│G│  TO   │
├─┼───────┤
│F│1 2 1 0│
│R│1 0 0 1│
│O│0 0 0 0│
│M│0 0 0 0│
└─┴───────┘

I assert that the number routes from one node to another given a path length N is G to the power of N. It works for path length 1 and 2!
Code:
   +/ .*~GRAPH     NB. Path length 2, G dot G
3 2 1 2
1 2 1 0
0 0 0 0
0 0 0 0

The first row gives the number of paths from node 1 to nodes 1 2 3 and 4. You see that 3 2 1 2 is the expected result. Also you can't get anywhere starting from nodes 3 and 4, that checks, and the paths length 2 from node 2 to the other nodes is verified.

You don't need to explicitly find each path. You'll need to add and multiply numbers that have more base 10 digits than there are atoms in the universe (assuming your caret in 10^1000 meant power instead of exclusive or).
__________________
[code]Code tags[/code] are essential for python code!

Last edited by b49P23TIvg : November 23rd, 2012 at 04:47 PM.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesC Programming > All paths between two nodes

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap