1

Hi i wanted to solve "shortest cycle through a given edge " problem that is on this website http://rosalind.info/problems/cte/ . suppose our specific edge (that is our first edge in this problem) be 'E'. i wrote a program to solve this problem and my algorithm is to use DFS on end_node of 'E' and it goes till encounter start_node of 'E'. it works fine for sample given on that website but when i used a large data it goes to a loop. i tried many examples of simple directed graphs and i didn't find why it goes to a loop. can anyone show me if there is anyway that it goes to loop?

here is my program :

#include <iostream>
#include <vector>
#include <cmath>
#include <algorithm>
#include <map>
#include <set>
#include <string>
#include <queue>
#include <list>
#include <sstream>
#include <iomanip>
#include <fstream>
#include <stack>


using namespace std;

#define ll long long int
#define pb push_back
#define mk make_pair

const ll maxn = 1e5 + 3;
const ll inf = 1e12 + 10;

vector <ll> cost;

void is_cyclic(int i, bool * recstack,ll cnt,vector <pair<ll,ll>> * vec,ll ind) {
    recstack[i] = true;
    vector <pair<ll, ll>>::iterator it = vec[i].begin();
    for (; it != vec[i].end(); it++) {
        if (it->first == ind) {
            cnt += it->second;
            cost.pb(cnt);
        }
        else {
            if (recstack[it->first] == false) {
                is_cyclic(it->first, recstack, cnt + it->second, vec, ind);
            }
        }
    }
    recstack[i] = false;
}

int main() {
    ll k;
    //file >> k;
    //while (k--) {
        ll n, m;
        cin >> n >> m;
        ll s;
        vector <pair<ll, ll>> * vec;
        vec = new vector<pair<ll, ll>>[n];
        for (int j = 0; j < m; j++) {
            ll a, b, c;
            cin >> a >> b >> c;
            if (j == 0) {
                s = a - 1;
            }
            vec[a - 1].pb(mk(b - 1, c));
        }
        bool * recstack = new bool[n];
        for (int j = 0; j < n; j++) {
            recstack[j] = false;
        }
        ll cnt = 0;
        pair <ll, ll> p;
        p = vec[s][0];
        cnt += p.second;
        recstack[s] = true;
        is_cyclic(p.first, recstack, cnt, vec,s);
        if (cost.size() == 0) {
            cout << -1 << " ";
        }
        else {
            sort(cost.begin(), cost.end());
            cout  << cost[0] << " ";
        }
        cost.clear();
    //}
    return 0;
}
Lukas Barth
  • 2,734
  • 18
  • 43
Sia
  • 11
  • 1

1 Answers1

0

First, you should be using BFS, not DFS, to compute shortest paths in unweighted graphs.

Second, it's almost impossible to figure out exactly how your code works. If you want to get meaningful answers, I'd recommend cleaning up your code. Here are some pointers:

  • Please use variable names that describe what the variable does / means. Variable names like std::vector<…> vec make your code very hard to comprehend.
  • Add a few comments to tell us what your code does where.
  • Please don't use thinks like #define pb push_back. This makes it almost impossible for everyone who is not you to read your code.
  • You use a lot of unnecessary pointers / heap allocations (and you don't delete them.) Examples are vec or recstack (make that a std::vector<bool>…)
  • Use std::numeric_limits instead of defining your own maxn etc.
  • Instead of .push_back(std::make_pair(a,b)), use .emplace_back(a,b)
acraig5075
  • 10,588
  • 3
  • 31
  • 50
Lukas Barth
  • 2,734
  • 18
  • 43