RSS Amplifier

Recent Questions - Meta Stack Exchange · Feb 17, 2022

"Your post appears to contain code that is not properly formatted as code." I have no idea why

0
Sign in to vote or save

This page did not load. You can still read it on the original site — the toolbar below keeps your place in the directory.

I can’t seem to understand what is wrong with the following Markdown content. I keep getting the Your post appears to contain code that is not properly formatted as code. Please indent all code by 4 spaces using the code toolbar button or the CTRL+K keyboard shortcut. For more editing help, click the [?] toolbar icon. My Markdown content looks like this: I'm solving this IDDFS-based problem where…

I can’t seem to understand what is wrong with the following Markdown content. I keep getting the

Your post appears to contain code that is not properly formatted as code. Please indent all code by 4 spaces using the code toolbar button or the CTRL+K keyboard shortcut. For more editing help, click the [?] toolbar icon.

My Markdown content looks like this:

I'm solving this IDDFS-based problem where I am using a utility function that performs recursive DFS based on depth. I have written the Python code for it which works perfectly, but I have been asked to convert it to C++, wherein the problem occurs.

Let me explain with the help of an example graph that I am using where there are 7 nodes labeled from 0 to 6. Their adjacency list is as follows:\
0: [4],\
1: [4, 5],\
2: [3, 5],\
3: [2, 6],\
4: [0, 1, 5],\
5: [1, 2, 4],\
6: [3]\
And I am attempting to find the DFS between 4 and 2 with a depth of 2, whose answer should come out to [4,5,2].

Now, I will attach the working Python code along with its output:

def dfs_util(path,target,depth):
    # Returns path if it exists, None otherwise
    curr_node = path[-1]
    if curr_node == target:
        return path
    if depth<=0:
        return None
    for child in adj_list[curr_node]:
        print(child)
        new_path = list(path)
        new_path.append(child)
        result = dfs_util(new_path,target,depth-1)
        # Remove this print statement gurllllll
        print(new_path, result)
        if result is not None:
            return result
    return None

I have added the extra print statements in there to help debug and understand the flow. The output this gave for command ```dfs_util([4],2,2)``` is:\
0\
4\
[4, 0, 4] None\
[4, 0] None\
1\
4\
[4, 1, 4] None\
5\
[4, 1, 5] None\
[4, 1] None\
5\
1\
[4, 5, 1] None\
2\
[4, 5, 2] [4, 5, 2]\
[4, 5] [4, 5, 2]\
[4, 5, 2]

As we can see, this returns the correct output.

When I tried to convert this to C++, I wrote the following function:

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(NULL);
            return tmp;
        }
        for(auto child : adj_list[curr_node]){
            cout<<child<<endl;
            vector<int> new_path = path;
            new_path.push_back(child);
            vector<int> result = dfs_util(new_path, target, adj_list, --depth);
            cout<<"[";
            for(auto i : new_path)
                cout<<i<<" ";
            cout<<"]\t";
            cout<<"res=";
            for(auto i : result)
                cout<<i<<" ";
            cout<<"\n";
            if(result.back()!=NULL)
                return result;
        }
        vector<int> tmp;
        tmp.push_back(NULL);
        return tmp;        
    }

With the same input parameters, it should ideally return the same output as the Python code mentioned above. But instead, it prints the following:
0\
4\
[4 0 4 ]        res=0 \
[4 0 ]  res=0 \
1\
[4 1 ]  res=0 \
5\
[4 5 ]  res=0 

While chalking out the logic, I have come to understand that after entering the recursion loop where ```dfs_util([4,1],2,1)``` is called, it does not enter the for loop mentioned further, thus not exploring the edges connected to node 1.

I have been stuck on this for two days now and would really appreciate some help in figuring out what is going wrong here since I feel like it should technically work (at least it does when I try to go line by line and solve on paper).

What is the issue?

Read on meta.stackexchange.com

Comments

Nothing yet. Say the first thing.

    Sign in to join the conversation.