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
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