How can I create the code and algorithm in C++ under this circumstances?
- There are limited (at most 6) cities(nodes) which can be read from a file.
- Connections and distance between cities can be assigned manually or read from a file.
- There are cars that have routes can be assigned manually and the starting points are the cities(nodes).
Questions,
- How can I create nodes and assign distance between nodes?
- How can I make connections between nodes?
- How can I calculate the initial points of the cars a while after the cars start moving?
I tried connection between cities like:
A B C D
A 0 20 0 20
B 20 0 20 20
C 20 20 0 15
D 0 8 9 0
If there is a connection between 2 cities, it has a value. If not, the value is “0”.
Thanks,
One way to do this is by building a graph of the connections. What you’re looking at here is fairly basic graph building. The most obvious method is usually to have a node type with a vector of connections to other nodes:
With this, building a connection from one town to another is a matter of creating a
connectionobject holding the distance, destination city, and (probably) the name of the road, then pushing that connection onto theconnectionsvector for the starting city. For the moment, this assumes everyconnectionis “simple” — that you get on one road, which goes directly to a destination (which sounds like it’s sufficient for your needs). In most cases you’ll also want these to be symmetrical, so when/if you add a connection from X to Y, you’ll also want to add a similar connection from Y to X.In real life, a connection might require switching roads along the way, so you might have some nameless nodes that just represent (for example) the junction of highway X with highway Y, (though it might not be nameless either — “junction of X and Y” might be a perfectly good name) so you have to travel through several nodes to get from some point A to some point B.
Unless you’re doing this as an exercise, you might want to look at the Boost Graph Library. It’ll help not only with building a basic graph like this, but also with things like finding a route from one town to another even though the two aren’t directly connected.
As far as positions of cars go, a lot depends on whether you’re looking at just calculating a specific distance, or whether (for example) you want a full-blown simulation where you can do things like find a route from point A to point B, find that traffic is above some threshold on a particular road, possibly find alternate routing if traffic is too heavy, etc. For the first, it’s a simple matter of computing a distance based on time and speed, then walking through nodes on their route until you’ve traveled that distance. For the latter, you’d probably add a
carclass, where each car knows its own current position and destination. You’d then walk through the list of cars at intervals and have each update its current position (e.g., once per simulated minute).If you do a bit of research, you’ll quickly find there are quite a few algorithms for things like finding routes in a graph (e.g., given graph X, find the shortest route from A to B), finding whether all the nodes are connected, etc.