So, this is what ive got.
Im trying to create a depth first search algorithm, but the problem is ive never been taught this - i dont do programming at school or uni, i try to teach myself
but i need help sometimes.
Im using a fairly simple iterated method of a DFS. Also, sorry about the lack of comments - this is all an informatics solution,so the code is throwaway and doesnt really need comments when im coding it, so i leave them out.
There are a few of them in there though.
#include <cstdio>
#include <vector>
#include <cstdlib>
#include <list>
using namespace std;
int main () {
FILE* in = fopen("sculptin.txt", "r");
FILE* out = fopen("sculptout.txt", "w");
int n; //number of apples.
fscanf(in,"%d", &n);
int a, x, b, y;
int z;
z = n*4;
vector<int> applearray(z);
int f;
f = 0;
for (;f<applearray.size();f = f+4){
fscanf(in, "%d %d %d %d", &applearray[f], &applearray[f+1], &applearray[f+2], &applearray[f+3]);}
int c, d, g, h;
int t, u; // NOT USED
vector<int> treesize(n);
list<int> dfs_unvisited;
list<int> dfs_visited;
g = 0;
c = 0;
f = 0;
// this loop is for the top half of the tree (Tracking stage)
while (applearray[f] != 0){
d = applearray[f];
dfs_unvisited.push_back(d);
f = applearray[f]*3 - (4 - d); }
if (applearray[f] == 0){
while (applearray[f] !=0) {
f = d;
f += 2;
d = f;
dfs_unvisited.push_back(d);
f = applearray[f]*3 - (4 - d);}
}
f = 2;
// this loop is for the second half of the tree.
while (applearray[f] != 0){
d = applearray[f];
dfs_unvisited.push_back(d);
f = applearray[f]*3 - (4 - d);}
if (applearray[f] == 0){
while (applearray[f] !=0) {
f = d;
f += 2;
d = f;
dfs_unvisited.push_back(d);
f = applearray[f]*3 - (4 - d);}}
printf("%d", dfs_unvisited.size());
system("pause");
// I NEED TO ADD STUFF TO MY LIST.
// this loop is after tracking - its supposed to total up the length to the end of the graph from each tree
for (f = 1; f<=applearray.size(); f +=2 ){
if (f == 1||3){
treesize[g] = applearray[f]; }
c = treesize[g];
d = applearray[f];
f--;
f = applearray[f]*3 - (4 - d);
f++;
treesize[g] = c + applearray[f];
g++;
} //end of the for loop
// ADD EVERY NODE ON EACH LINE TO A LIST AND GO THROUGH EACH NODE.
return 0;
}
and if anyone was wondering, the input file is in the format
n - there are going to be n lines proceeding this, each describing the links to and from a node. the first line below describes the first node, the second describes the second, etc.
a x b y - a is the node it links to with a length of x. It also links to b with a length of y.
The graph is directed and weighted, for further information.
I have managed to implement a BFS in this, but i know that it is not optimal for this sort of search, and so want to learn how to do a DFS (iterated). I guess i would like to learn the recursive version, too, but i currently dont even know the difference between the iterated and recursive versions.
And please dont tell me that you refuse to do my homework for me.
because its not my homework.
Many thanks in advance for any help,
Ben