recursive function

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • costantinos@gmail.com

    #1

    recursive function

    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,i nt start_vertex,
    unsigned int graph_size, ofstream &outputfile)
    {
    outputfile << "Starting from node: " << start_vertex << endl;

    map<int, float>::const_i terator iter_mB;
    map <int, vector<int::con st_iterator iter_mP;
    vector <int>::const_it erator iter_i_mP;
    vector <intv_temp, v_temp2, v_temp3, v_temp4;
    vector <intv_p2;
    int counter1 = 0;
    int node;
    unsigned int iii;

    for (iii = 1; iii < graph_size; iii++)
    {
    map<int, floatm_betweenn es;

    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.be gin(); iter_mB != m_betweennes.en d();
    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

  • mlimber

    #2
    Re: recursive function

    costantinos@gma il.com wrote:
    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.programmin g 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

    Comment

    • Alf P. Steinbach

      #3
      Re: recursive function

      * costantinos@gma il.com:
      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,i nt 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.

      --
      A: Because it messes up the order in which people normally read text.
      Q: Why is it such a bad thing?
      A: Top-posting.
      Q: What is the most annoying thing on usenet and in e-mail?

      Comment

      Working...