I am using an implementation of the IDDFS graph algorithm in a project and I noticed that for each node, the function takes around 8 - 10 seconds to compute. And, with a very large dataset with over 7000 nodes, it is proving to be the most cumbersome. So, I am trying to optimize my code so that it will run faster but all I have found so far is to change my function call by value parameters to being called by reference which has made a decent difference.
Are there any other ways I could fasten my code? I am compiling using c++11.
// Utility DFS function -- returns DFS Path if it exists, -1 if not exists
vector<int> dfs_util(vector<int> path, int target, vector<vector<int>> &adj_list, int depth)
{
int curr_node = path.back();
if (curr_node == target)
return path;
if (depth <= 0)
{
vector<int> tmp;
tmp.push_back(CUST_NULL);
return tmp;
}
for (auto child : adj_list[curr_node])
{
vector<int> new_path = path;
new_path.push_back(child);
vector<int> result = dfs_util(new_path, target, adj_list, depth - 1);
if (result.back() != CUST_NULL)
{
return result;
}
}
vector<int> tmp;
tmp.push_back(CUST_NULL);
return tmp;
}
// IDDFS Function -- returns IDDFS Path if it exists, -1 if not
vector<int> iddfs(vector<vector<int>> &adj_list, int src, int target, int max_depth = 2)
{
vector<int> result;
max_depth++;
for (int depth = 0; depth < max_depth; depth++)
{
vector<int> path;
path.push_back(src);
result = dfs_util(path, target, adj_list, depth);
if (result.back() == CUST_NULL || result.size() == 0)
continue;
int final_index = 0;
int idx_count = 0;
for (auto item : result)
{
if (item == src)
final_index = max(final_index, idx_count);
idx_count++;
}
result = vector<int>(result.begin() + final_index, result.end());
}
return result;
}