Thread: Need help to implement an algorithm

  1. #1
    Registered User
    Join Date
    Nov 2016
    Posts
    8

    Need help to implement an algorithm

    I have three different data structures:


    -int-array with 144 entries called trains, each entry is a different trainID


    -Station-array with 367 entries, each entry is a different Station-struct:


    Code:
        typedef struct
        {
          int id;
          char name[51];
        }Station;
    -Schedule-array with 2548 entries, each is a different Schedule-entry struct:


    Code:
        typedef struct
        {
          int trainId;
          int stationId;
          int arrival;     //in minutes, i.e. 2:15am = 135
          int departure;   //in minutes
        }Schedule;
    What I have to do, is to find all trains, which overtake an other train and ouput the trainId.
    I got a hint from the teacher:
    Code:
        For all trains B
           For all trains A
              Find station C, where A departs and B later departs than A (that one where A departs as earliest)
              If such exists:
                 For all stations D, where A arrives after C and B also arrives
                    If B is on station D earlier than A, then B is overtaker.


    I think that should be some for-loops and some if-statements, but i don't get it.


    I tried some different nested loops, but it never worked right. When i build it like the hint, how can i compare with the schedule, on which position do I need a loop which iterates over the schedule... Because the trains just have the id, also the station, I always have to look after entries in the schedule, I think this is my main problem.

    Here some code from Main, I tried some different ways, i.e. instead of train-loops with schedule-loops, etc.


    Code:
        int main (int argc, char *argv[])
        {    
          //lenght of all arrays
          int lengthOfStations = countLines(argv[1]);
          int lengthOfTrains = countLines(argv[2]);
          int lengthOfSchedule = countLines(argv[3]);
        
          int *B = readTrains(argv[2]);
          int *A = readTrains(argv[2]);
          Station *C = readStation(argv[1]);
          Station *D = readStation(argv[1]);
          Schedule *sched = readSchedule(argv[3]);
        
           //qsort(sched, lengthOfSchedule, sizeof(Schedule), compare);
        
          /* HERE I NEED THE NESTED LOOPS */ 
    
          return 0;
        }

    The arguments are like above descripted, when I call readSchedule, then i.e. Schedule B is a Schedule-array with all 2548 schedule-elements, each is in this shape: "trainId stationId arrival departure" (i.e. 28281 6228 392 393). The expected output should be a list from all trains which overtake another train.

    TY

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,663
    Shouldn't a schedule have two station IDs?
    But this still assumes that all stations in the network have point to point links.

    Have you tried a paper exercise where you have say just two stations, two trains, two schedules?
    You don't solve problems by writing random for loops in code.

    To write the code, you need to fully understand the problem on paper. So examine a few very small cases to make sure you understand when trains don't overtake and when trains do.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Nov 2016
    Posts
    8
    Thanks for your reply.
    No just one stationId in this case.

    My problem is, when I iterate about all trains A and then about all trains B, and then iterate about all stations C, how can I compare this all together the easiest way in the schedule?
    Because in the train-"data structure" I have just the trainId and in the station-structure just the stationId.

    Then I thought I could handle that when I use schedule-"objects" instead of train-arrays or station-arrays, but that also does not worked right.

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,663
    Presumably, trains are always inbound to the same stationID from the same (unspecified) source station, hence are all on the same piece of track.

    So the distance to travel is constant (but unspecified), and the velocity is simply a proportion of the end-start times.

    Like I said, time for pencil and paper and start sketching out how to solve the problem.

    Imagine two trains heading for station 1
    a starts at 08:00 and arrives at 12:00
    b starts at 09:00 and arrives at 11:00
    Do they pass?

    What about this?
    a starts at 08:00 and arrives at 12:00 at station 1
    b starts at 09:00 and arrives at 11:00 at station 2
    Do they pass?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Registered User
    Join Date
    Nov 2016
    Posts
    8
    a and b have to start at the same station and to arrive at the same station:
    Imagine two trains heading for station 1
    a starts at 08:00 and arrives at 12:00
    b starts at 09:00 and arrives at 11:00
    Do they pass?
    So a starts at 8 at station1 and arrives at 12 on station2, the same with b, then b overtakes a.

    What about this?
    a starts at 08:00 and arrives at 12:00 at station 1
    b starts at 09:00 and arrives at 11:00 at station 2
    Do they pass?
    Can't compare this, because they must have at least one same station where they start and one same station where both arrive.

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,663
    So can you write a logical statement that determines whether a passes b ?

    At least you understand that the stationID has to be the same before trying to compare two trains. This is a clue for sorting your data.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  7. #7
    Registered User
    Join Date
    Nov 2016
    Posts
    8
    Quote Originally Posted by Salem View Post
    So can you write a logical statement that determines whether a passes b ?
    What do you mean with passes and which example?

    So my problem is still that I do not know how I can compare all three data-structures the easiest way, so that I can build my for-loops and get the right solution.

  8. #8
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,663
    There is only one data structure you need, and that's the schedule array, at least as far as solving the basic problem is concerned.

    > What do you mean with passes and which example?
    I mean this
    Imagine two trains heading for station 1
    a starts at 08:00 and arrives at 12:00
    b starts at 09:00 and arrives at 11:00
    Do they pass?
    It's all very well eyeballing it and knowing from experience that b must pass a at some point.

    The point is, can you write the same thing in mathematical terms?

    Do you know the equations of motion for example?

    Imagine all the tracks are the same length, say 1Km.

    From that, you can assign a fixed velocity to each train, based on the end-start times.

    From that, you can work out where along the track each train is at any given moment.

    For any two trains, you end up with two lines on a graph of distance vs time.

    What you need is a bit of maths which tells you when two lines cross.

    See?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  9. #9
    Registered User
    Join Date
    Nov 2016
    Posts
    8
    Sorry, I'm to stupid for that. I don't know the equation of motion and it looks very complicated.

    B overtakes A, because b starts after a and arrives before a.

  10. #10
    Registered User
    Join Date
    Jun 2015
    Posts
    1,640
    Quote Originally Posted by birdbox View Post
    B overtakes A, because b starts after a and arrives before a.
    That's exactly correct. The equations of motion don't enter into it. It seems as if there's one giant loop of track with stations along it and all you have to do is determine which trains have "crossed paths" based on their departure and arrival times at two stations they both stopped at.

    The given algorithm unpacked a little:
    Code:
    for all trains T1:
        for all trains T2:
            for all stations SD:  // stations departed from
                if T1 and T2 depart from SD and T2 departs later than T1:
                    for all stations SA:  // stations arrived at
                        if T1 and T2 arrive at SA after SD
                               and T2 arrives at SA earlier than T1:
                            T2 must have passed T1
    In your code at the end of your first post, reading in the trains and and stations twice indicates a misunderstanding. You only need one copy of the data in memory. Train T1 and T2 are just indices into the array containing the train data. SD and SA are indices into the station array.

  11. #11
    Registered User
    Join Date
    Nov 2016
    Posts
    8
    Hey thanks!

    Quote Originally Posted by algorism View Post
    In your code at the end of your first post, reading in the trains and and stations twice indicates a misunderstanding. You only need one copy of the data in memory. Train T1 and T2 are just indices into the array containing the train data. SD and SA are indices into the station array.
    Yes absolutely.

    But when I try to implement it, then my first problem is on line 4:
    Code:
    if T1 and T2 depart from SD and T2 departs later than T1:
    How can I check that in the schedule?
    Maybe I need anywhere another loop which iterates over the schedule-array, so that I can check that if-statement somehow.

  12. #12
    Registered User
    Join Date
    Nov 2016
    Posts
    8
    no more hints?

  13. #13
    Registered User
    Join Date
    Jun 2015
    Posts
    1,640
    Write a schedule lookup function:
    Code:
    int x1 = lookup(sched, t1, sd);
    int x2 = lookup(sched, t2, sd);
    
    if (sched[x2].departure > sched[x1].departure)
        ...

  14. #14
    Registered User
    Join Date
    Nov 2016
    Posts
    8
    Ok thank you both so much, I think I've got it!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 09-30-2016, 09:53 AM
  2. Attempt to implement the minimax algorithm
    By gavra in forum C++ Programming
    Replies: 2
    Last Post: 08-17-2011, 11:27 PM
  3. Replies: 1
    Last Post: 04-05-2011, 04:15 AM
  4. help me implement Dijkstra's Algorithm
    By iamnew in forum C++ Programming
    Replies: 4
    Last Post: 05-07-2010, 07:02 PM
  5. Implement of a Fast Time Series Evaluation Algorithm
    By BiGreat in forum C Programming
    Replies: 7
    Last Post: 12-04-2007, 02:30 AM

Tags for this Thread