Thread: shortest path problem

  1. #1
    Registered User
    Join Date
    May 2011
    Posts
    4

    shortest path problem

    Hi everbody!

    so here is the problem i got :

    i would like to implement some sort of map as a weighted graph. the weight is always 1. im just interested in the "jumps" from vertex to vertex.
    it will be probably an Adjacency list.
    but know i would like to search for a specific path between two vertices, and i want to determine wich patch is the shortest.
    dijkstra i think is a bit to much because as i said i only got weights of 1.
    also i would like to abort searching further if the jumps hit a certain amount.

    is there a nice and easy way to do this?

    thank you!
    Last edited by bananorama; 05-24-2011 at 05:09 AM.

  2. #2
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    You can certainly abort within the Dijkstra algorithm if you just have a total weight boundary in your problem. I would start of with the Dijkstra algorithm implementation and modify it to suit my needs. Then I would run some performance analysis and see how I can speed it up for certain trivial cases.
    1. Get rid of gets(). Never ever ever use it again. Replace it with fgets() and use that instead.
    2. Get rid of void main and replace it with int main(void) and return 0 at the end of the function.
    3. Get rid of conio.h and other antiquated DOS crap headers.
    4. Don't cast the return value of malloc, even if you always always always make sure that stdlib.h is included.

  3. #3
    Registered User
    Join Date
    May 2011
    Posts
    4
    ah kk. thank you. god my brain is melting right now. dont know where to start and how to setup all this...

  4. #4
    Registered User
    Join Date
    Jul 2010
    Location
    Oklahoma
    Posts
    107
    Bananorama,

    Do you have a copy of Dijkstra's algorithm? It should lead the implementation rather well. Have you got a list data structure handy for implementing the adjacency lists? Have you considered an adjacency matrix? Initializing your graph is probably the most time consuming activity involved in an implementation. Once you get an implementation working you can optimize for special cases....

    Best Regards,
    Kept the text books....
    Went interdisciplinary after college....
    Still looking for a real job since 2005....

    During the interim, I may be reached at ELance, vWorker, FreeLancer, oDesk and WyzAnt.

  5. #5
    Registered User
    Join Date
    May 2011
    Posts
    4
    actually i would like to implement it myself. just to practice a little bit.
    on paper Dijkstra is so easy but when it comes to an implementation im quite lost.
    so maybe lets get more specific. im not that great in abstract thinking

    so thats how i would like to do it:

    i got a list of structs which contains an ID, a next pointer and a list of structs which contains the edges the current vector has. so it would look something like this

    Code:
    struct vector
    int id
    struct vector *next
    struct vector edges []
    now in my first iteration i check the edges array for the edges.
    then iŽll post my result into another list of structs.

    Code:
    struct result
    int fromID
    int toID
    int jumps (0 at the beginning)
    int flag
    struct result *next
    now iŽll take the first toID out of my result list and search for it in my list of vector structs.
    after that iŽll check for the edges again and save/update my result list and set the flag that i visited this vector.

    so in pseudocode it may look like this

    Code:
    method(int start){
    int jumps2 = 1;
    while( something){
    
    
    struct vector *ptr = pointer to struct which contains start as id;
    
    k=0;
    while(ptr->edges[k] !=NULL){
    
    struct result *ptr2->fromID = ptr->ID;
    struct result *ptr2->toID = ptr->edges[k]->ID;
    ptr2->jumps = jumps2;
    k++;
    }
    jumps2++;
    ptr = the struct which contains the id of the next struct in ptr2;
    ptr2=ptr2->next;
    }
    }
    hope you get the idea how i mean to implement this.
    would this work?

    sorry for some stupid syntax misstakes.

    thanks and cya!

  6. #6
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Don't know why you have a *next element in your graph, unless this graph is also a list.

    I also am not sure what labeling you're doing. In your labeling, you need to label each struct with a number (representing the # of hops) and a node (representing where you came from). There's no next (except maybe in the context of "these labeled but unflagged nodes need to go into a queue"), and there's no toID.

  7. #7
    Registered User
    Join Date
    May 2011
    Posts
    4
    could you perhaps post some code to make it a bit more clear?

    im just a bit lost on how to access these structs after i created them without having any of these nextpointers. i have to save them somewhere...
    just some code would definetly help me to understand this.

    thanks

  8. #8
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    I'm assuming it's a connected graph. You will need a "start" node, but from there you should be able to get to anywhere else just by following the edges. (If you wanted an adjacency matrix, then you would need to put an ordering on your nodes, but otherwise not really.)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. single source shortest path problem
    By 4eFFWNW in forum C Programming
    Replies: 8
    Last Post: 07-10-2010, 12:41 PM
  2. Shortest path
    By anirban in forum Tech Board
    Replies: 3
    Last Post: 06-10-2008, 11:42 PM
  3. Shortest path problem
    By Digitalxero in forum C++ Programming
    Replies: 0
    Last Post: 10-25-2005, 05:32 PM
  4. shortest path problems
    By talz13 in forum C++ Programming
    Replies: 7
    Last Post: 05-08-2004, 06:13 AM
  5. Help!!! Shortest path graph
    By hansy32 in forum C Programming
    Replies: 5
    Last Post: 03-01-2002, 06:39 PM