You've got way too much stuff swimming around there.
First off, you need to back-off from the code a bit first. (Don't feel like I'm talking down to you. This is the very same technique I use both before beginning an algorithm and whenever I get confused writing it. People never believe me that pulling out the construction paper and crayons and drawing stuff makes a difference, but they are always amazed at my results...)
I recommend drawing your graph. You'll notice it is a weighted, directed, acyclic, unconnected graph. (That is, a forest with one-way edges and no loops.)
Next, you might want to take a look at the Wikipedia article:
|
http://en.wikipedia.org/wiki/Depth_first_search
|
(it has both recursive and iterative versions) and review your own texts on graph theory.
When writing your algorithm, start with the basic structure. Decide what data structures best represent each kind of data. For example, a directed tree might best be represented as a
list of edges, where an edge is a pair of (
source node, target node, length). So far you've got a
std::vector or
std::list to represent the graph and a
struct to represent an edge.
Once you have a solid data structure, make your input file match. Currently, your input file attempts to represent one node per line, with two edges coming from each node. Since you don't actually need to represent individual
nodes there actually is no need to worry about line numbers. Just make your input a list of edges:
1 2 3 4 5 6 7 8 9 10
|
9 nodes (starting at 1)
edges as (source, target, length):
1 2 3
1 3 4
2 4 1
2 5 2
3 6 3
3 7 2
6 8 1
6 9 1
|
This structure makes it very easy to read the list of edges directly in from file. It also removes limitations on the number of edges leaving a given node.
Penultimately, make yourself the necessary functions to manipulate the graph (list of edges) easily: add, find, remove, etc. This uncomplicates the main algorithm, which should be your final effort.
Some comments on your code and commentary:
Your algorithm looks to be an iterative attempt, but you have confused searching for and tracking edges.
Also, watch your indentation. Your outer loop does not appear to be there (to the eye). I know you know it is there, but it will help you to make it obvious what is inside and outside the loop. Also, you need to only document what it is that the code is doing, and what individual variables represent.
For example, your code might have comments along the following lines:
1 2 3 4 5 6 7 8 9 10
|
// 'applearray' represents the data in the nodes of the weighted graph.
// Every four elements are a x b y, where a and b are node numbers and x and y are distances from the indexed node.
// (Remember: node number - 1 = index into vector of node)
vector< int > applearray( n*4 );
// 'f' iterates through all the nodes in the list
// Each iteration we are doing <explain what is happening>
for (int f = 0; f < applearray.size(); f += 4) {
...
}
|
Lastly, forget about the "standard streams are slow" crap. Whoever told you that doesn't know what he is talking about. There are
very few applications where the difference in speed between C and C++ I/O methods is significant. For a program like this, you won't even notice. Using the standard streams makes it easier to code, codes in fewer lines, and improves type safety, error checking, and security.
Hope this helps. Post back once you get further along.