d2jsp
Log InRegister
d2jsp Forums > Off-Topic > General Chat > Homework Help > Traversing A Directed Graph
Add Reply New Topic New Poll
Member
Posts: 20,928
Joined: Mar 18 2009
Gold: 435,910.13
Apr 4 2013 02:45pm
i'm given an directed weighted graph like this one



i have to write a program that given a input of arbitrary points i.e 4, 1, 3, 6, find the shortest path that passes through each vertex once
can anyone tell me a algorithm that can solve this? Doing this in java btw, will don8 if i can understand/use your answer totally stumped atm

This post was edited by bakalolo on Apr 4 2013 02:46pm
Member
Posts: 10,812
Joined: Oct 15 2009
Gold: Locked
Warn: 20%
Apr 4 2013 03:42pm
i'd say use brute force, since you don't have very many choices/nodes.

basically i'm saying calculate every possible path though the system, from every possible starting point (and stop jumping once you have passed though every node, and put a cap on the number of allowed jumps, say 10). Then save the shortest one from each starting point. Then just return that path when queried with a starting point.

This post was edited by Azrad on Apr 4 2013 03:56pm
Member
Posts: 20,928
Joined: Mar 18 2009
Gold: 435,910.13
Apr 4 2013 04:00pm
Quote (Azrad @ Apr 4 2013 11:42am)
i'd say use brute force, since you don't have very many choices/nodes.

basically i'm saying calculate every possible path though the system, from every possible starting point (and stop jumping once you have passed though every node, and put a cap on the number of allowed jumps, say 10). Then save the shortest one from each starting point. Then just return that path when queried with a starting point.


yea i was considering that just wondering if there are any algorithms out there since i dont actually know how big the input might be but i know what ur saying
Member
Posts: 10,812
Joined: Oct 15 2009
Gold: Locked
Warn: 20%
Apr 4 2013 04:15pm
Quote (bakalolo @ Apr 4 2013 03:00pm)
since i dont actually know how big the input might be but i know what ur saying
yeah i'm hand waving and saying the solution space would be "small" but lets see.. the most exits any node has is 2, so lets go with the idea each node has 2 exits. If we assume all the good solutions (all of the shortest) will have less than 10 jumps in them, the total number of possible paths is 2^10 for any given starting point. That is only about a thousand, and some nodes only have 1 exit so that will cut it down a bit. Also when you find a solution of say 9 hops for a given starting point, you can limit all further investigation into that starting point to 9 hops (why look for solutions with 10 hops when you already have a 9 hop solution?), which should cut the space down even more.

I'll see if i can work something up later.

This post was edited by Azrad on Apr 4 2013 04:17pm
Member
Posts: 35,291
Joined: Aug 17 2004
Gold: 12,730.67
Apr 4 2013 05:57pm
Azrad's suggestion of brute forcing it is correct. It's very similar to the "Traveling Salesman Problem (with one less vertex looping back)." Anyway, you're looking at an NP-hard problem.
Member
Posts: 10,812
Joined: Oct 15 2009
Gold: Locked
Warn: 20%
Apr 4 2013 07:52pm
I've been fooling around with the code for this and noticed that you can't get to node 3 or 6 if you don't start there (at 3 or 6 ) because node 2 is kind of a road block.

This post was edited by Azrad on Apr 4 2013 08:04pm
Member
Posts: 54,265
Joined: Aug 22 2004
Gold: 6,069.00
Apr 4 2013 08:18pm
travelling salesman problem?

interesting twist with negative values

/e even for the non-negative case, it's well known the program runs in exponential time, so if your program is slow as hell, don't worry

Quote (thundercock @ Apr 4 2013 06:57pm)
Azrad's suggestion of brute forcing it is correct.  It's very similar to the "Traveling Salesman Problem (with one less vertex looping back)."  Anyway, you're looking at an NP-hard problem.


very good

This post was edited by Casey on Apr 4 2013 08:20pm
Member
Posts: 10,812
Joined: Oct 15 2009
Gold: Locked
Warn: 20%
Apr 4 2013 09:13pm
I wrote the following on a whim. I realize it does not do what you need (it creates a random path, then calculates the distance of that path); but maybe it will give you some ideas.

Code

from random import choice

#TEST PURPOSES, hardcoded user input
starting_node = '3'

#data about system
exits=[['0'],['5'],['1','4'],['2','6'],['1','5'],['2'],['2','3']]
distance = dict()
distance[1,5]=-1
distance[2,1]=1
distance[2,4]=2
distance[3,2]=2
distance[3,6]=-8
distance[4,1]=-4
distance[4,5]=3
distance[5,2]=7
distance[6,2]=5
distance[6,3]=10

#create random path of length 10
def Make_Move(path,current_hop,current_location):
    next_step = choice(exits[int(current_location)])
    path+=next_step
    current_location=next_step
    if current_hop == 10:
         return path
    else:
         current_hop+=1
         return Make_Move(path,current_hop,current_location)

#calculate distance of given path
def Calculate_Distance(path):
    sum_of_distance = 0
    for x in range(0,9):
         initial_location = int(path[x])
         final_location = int(path[x+1])
         sum_of_distance+=distance[initial_location,final_location]
    return sum_of_distance


path = Make_Move(starting_node,2,starting_node)
distance = Calculate_Distance(path)
print 'physical path = ' + path
print 'distance = ' + str(distance)


This post was edited by Azrad on Apr 4 2013 09:16pm
Member
Posts: 35,291
Joined: Aug 17 2004
Gold: 12,730.67
Apr 5 2013 03:54pm
In this specific case, there is only one path from two that works. Using that knowledge, you should be able to build an algorithm from there :)

If you need more help, let me know.
Go Back To Homework Help Topic List
Add Reply New Topic New Poll