1

The task is:

Find the shortest path between two cities. Cities are connected with one-way roads. Cities are present inside a .txt file in such manner:

// From // // To // // Distance //

e.g.:

London Glasgow 180

Manchester Liverpool 80

York Cambridge 415

York Manchester 260

Glasgow York 315

Glasgow Manchester 315

Given a source city S and target city T, find the shortest path. Calculate the distance and retrieve the taken path.

Problem is: I am not allowed to use any STL container at all. Cannot use containers such as std::list, std::vector, std::pair, std::set, etc. I am, however, able to use my own implemented dynamic data structures.

I'm clueless which data structures should I implement and how to implement them. Also, should I use priority queue, or a heap, or something else? Also wondering about adjacency list or adjacency matrix.

I don't really care if the solution is well-optimized or not.

I have been googling much, but all Dijkstras algorithms implementations I found use plenty of STL containers, that I'm not allowed to use.

Any tips, hints or links are appreciated. Thank you.

weno
  • 804
  • 8
  • 17
  • You can deduce what data structures you need from those examples using the standard library, even if you aren't allowed to use it directly. Seeing your assignment, I suspect you are expected to at least use a graph. – asu Dec 12 '18 at 22:07
  • https://stackoverflow.com/a/3448361/14065 – Martin York Dec 12 '18 at 22:53
  • 2
    To implement Dijkstra's: You need a priority queue (aka sorted list) and a set (which could be implemented as a list). The priority queue that keeps the current boundary and you pick the next item to be processed as the item in the queue with the shortest path (so the priority is distance to city). You need a set to keep track of the cities you have already reached. The algorithm needs a way of finding a set of connected cities and distances given a current city. So to implement Dijkstra's the minimal data structure you need is a list and an interface to your cities) – Martin York Dec 12 '18 at 23:09
  • 1
    *I am not allowed to use any STL container at all.* -- Yes, because if you used STL, the program will just magically write itself. (I hope you know that was sarcasm...). If you *were* to use STL, what would you use? – PaulMcKenzie Dec 12 '18 at 23:17
  • It amazes me how many instructors hand out assignments and ban the STL, and then we wonder why we keep interviewing people that don't know the STL. – Christopher Pisz Dec 12 '18 at 23:20
  • @ChristopherPisz: essentially all senior interviews I do have CVs claiming knowledge of STL. Reality is that hardly any of these people has even a cursory idea of what STL is about! I'd not be surprised if most instructors believed that the central theme of STL are containers. ... and do instructors really want to prohibit operating in terms of concepts for iteration which is what STL is actually about? – Dietmar Kühl Dec 12 '18 at 23:26
  • @OP Even if the STL was used, you still need to know what tools out of STL to use to create a program that implements Dijkstra's algorithm. The issue with your requirements is that you may know full and well how to implement the algorithm, but the stumbling block is getting the stupid dynamic array to work properly, or the priority queue to not have bugs. So the question is really -- is this an assignment to implement Dijkstra's algorithm, or an assignment in implementing home-made data structures? – PaulMcKenzie Dec 12 '18 at 23:38
  • @OP What I advise is to first implement the solution using STL -- make sure it works. Once it works, "reverse engineer" the STL parts of the code to your own data structures. – PaulMcKenzie Dec 12 '18 at 23:39
  • @PaulMcKenzie I have working implementation using STL. Problem is it's using std::list, vector, pair, set and, well... std::map. – weno Dec 13 '18 at 00:54
  • Then you need a home-made linked list class, vector, and map. Honestly, the work you've already done should be sufficient -- having you make home-made containers *should* have been separate exercises before doing this current homework example. Creating working vector, list and map classes are not trivial -- I have yet to see a student actual write, say, a bug-free, working linked list class. – PaulMcKenzie Dec 13 '18 at 01:39

1 Answers1

1

To code Dijkstra's shortest path algorithm you only need to implement a priority queue (min heap).

I recommend heap above all. The implementation of a set involves a (almost) balanced binary search tree like AVL or Red-Black Tree or Treap (Cartesian Tree), such codes are way harder by several orders of magnitude than implementing a heap. Also, all these solutions have the same running time O(log n) so easier to code solution come first.

Eloy Pérez Torres
  • 1,050
  • 4
  • 14