recursive function

C

costantinos

Can someone help me on how I can do this function recursive.
I have done it staticly as depth 4 but i want it to perform for any
depth.
The algorithm is an extension for Dijkstra algorithm to find all
possible shortest paths.


Code:

int betweennes(map <int, vector<int> > m_predecessor,int start_vertex,
unsigned int graph_size, ofstream &outputfile)
{
outputfile << "Starting from node: " << start_vertex << endl;

map<int, float>::const_iterator iter_mB;
map <int, vector<int> >::const_iterator iter_mP;
vector <int>::const_iterator iter_i_mP;
vector <int> v_temp, v_temp2, v_temp3, v_temp4;
vector <int> v_p2;
int counter1 = 0;
int node;
unsigned int iii;

for (iii = 1; iii < graph_size; iii++)
{
map<int, float> m_betweennes;

v_temp = m_predecessor[iii]; //it starts from zero.

for( iter_i_mP = v_temp.begin(); iter_i_mP != v_temp.end();
iter_i_mP++)
{
node = *iter_i_mP;
if (v_temp.size()>1)
{
if (node != start_vertex)
{
v_temp2 = m_predecessor[node];
int size2 = v_temp2.size();
if ((size2 == 1) && (v_temp2[size2 - 1] == start_vertex))
{
outputfile << iii << "-";
outputfile << node << "-";
outputfile << v_temp2[size2 - 1] << " done" << endl;
m_betweennes[node]++;
counter1++;
}
else
{
while (v_temp2[size2 - 1] != start_vertex)
{
v_temp3 = m_predecessor[v_temp2[size2 - 1]];

int size3 = v_temp3.size();
if ((size3 == 1) && (v_temp3[size3 - 1] == start_vertex))
{
outputfile << iii << "-";
outputfile << node << "-";
outputfile << v_temp2[size2-1] << "-";
outputfile << v_temp3[size3 - 1] << " done" << endl;
m_betweennes[node]++;
m_betweennes[v_temp2[size2-1]]++;
counter1++;
}
else
{
while (v_temp3[size3 - 1] != start_vertex)
{
outputfile << iii << "-";
outputfile << node << "-";
outputfile << v_temp2[size2-1] << "-";
outputfile << v_temp3[size3-1] << "-";
v_temp4 = m_predecessor[v_temp3[size3 - 1]];
m_betweennes[node]++;
m_betweennes[v_temp2[size2-1]]++;
m_betweennes[v_temp3[size3-1]]++;
counter1++;

int size4 = v_temp4.size();
if ((size4 == 1) && (v_temp4[size4 - 1] == start_vertex))
{
outputfile << v_temp4[size4 - 1] << " done" << endl;
}
size3--;
}// end of while
} //end of else
size2--;
}//end of while
} // end of else
}//end of if
}//end of if

else if (v_temp[0]!= -1)
{
outputfile << iii << "-";
outputfile << node << " done" << endl;
}
else ;
}//end of for

outputfile << endl;
outputfile << "------------" << endl;
for (iter_mB = m_betweennes.begin(); iter_mB != m_betweennes.end();
iter_mB++)
{
outputfile << iter_mB->first << " : " << iter_mB->second;
if (counter1 != 0)
outputfile << "/" << counter1;

outputfile << endl;
}
outputfile << "------------" << endl;
counter1 = 0;
}//end of for

outputfile << endl;

return 0;
}





cheers
costantinos
 
M

mlimber

Can someone help me on how I can do this function recursive.
I have done it staticly as depth 4 but i want it to perform for any
depth.
The algorithm is an extension for Dijkstra algorithm to find all
possible shortest paths.
[snip code]

Is there a C++ *language* question here? We're not here to write
algorithms for you (cf. FAQ 5.2 and visit comp.programming for more
help); we're here to discuss the C++ language and libraries proper (cf.
FAQ 5.9). If you can rephrase the question so it fits within the topic
of this group, we'll try to help.

Cheers! --M
 
A

Alf P. Steinbach

* (e-mail address removed):
Can someone help me on how I can do this function recursive.
I have done it staticly as depth 4 but i want it to perform for any
depth.
The algorithm is an extension for Dijkstra algorithm to find all
possible shortest paths.

As MLimber has already noted, your actual question is off-topic, except
if you're wondering how to call a function recursively in C++:

void foo()
{
foo(); // Recursive call.
}

Code:

int betweennes(map <int, vector<int> > m_predecessor,int start_vertex,
unsigned int graph_size, ofstream &outputfile)
{
outputfile << "Starting from node: " << start_vertex << endl;

It's generally not a good idea to pass large data structures by value.

You can use 'const' to prevent any changes.

Second, it's generally not a good idea to mix different concerns such as
i/o and a search algorithm; instead try to separate concerns.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,968
Messages
2,570,149
Members
46,695
Latest member
StanleyDri

Latest Threads

Top