Question: Please help, Im not sure why my dfs algorithm is not passing test cases and producing inccor#dfs ( a ) performs a depth first search

Please help, Im not sure why my dfs algorithm is not passing test cases and producing inccor#dfs(a) performs a depth first search starting at node 0 and returns a list of nodes in the order in which they were seen, with start and stop times. Press shift enter to test your code. Ensure that your code has been saved first by pressing shift+enter on the previous cell.
from IPython.core.display import display, HTML
def dfs_test():test_cases =[([[1,3],[0],[1,3],[2]],[(0,(1,8)),(1,(2,3)),(3,(4,7)),(2,(5,6))]),([[],[0,3],[1],[]],[(0,(1,2)),(1,(4,7)),(3,(5,6)),(2,(9,10))]),for (test_graph, solution) in test_cases: if (solution != output): s2=''+ str(solution)+' Your code output: '+ str(output)+"" failed = True display(HTML('')) display(HTML(''))
dfs_test()/tmp/ipykernel_126/474394405.py:3: DeprecationWarning: Importing display from IPython.core.display is deprecated since IPython 7.14, please import from IPython display
from IPython.core.display import display, HTML
Failed - test case: Inputs: graph =[],[0,2],[3],[1]]
Expected Output: (0,(1,2)),(1,(4,9)),(2,(5,8)),(3,(6,7)) Your code output: (0,(1,2)),(1,(3,8)),(2,(4,7)),(3,(5,6))
Failed - test case: Inputs: graph =[??031???]
Expected Output: [(0,(1,2)),(1,(4,7)),(3,(5,6)),(2,(9,10))] Your code output: (0,(1,2)),(1,(3,6)),(3,(4,5)),(2,(7,8))
One or more tests failed.
#INPUT: [[1],[2],[0]](a 3 node cycle)
#OUTPUT: [(0,(1,6)),(1,(2,5)),(2,(3,4))]
def dfs_visit(a, u, color, d, f, time, result):d[u]= time[0]for v in a[u]: dfs_visit(a, v, color, d, f, time, result)time[0]+=1result.append((u,(d[u], f[u])))
def dfs(a):color =['WHITE']* nf =[0]*nresult =[] if color[u]== 'WHITE':result.sort(key=lambda x: x[1][0]) # Sort based on discovery timesect output
 Please help, Im not sure why my dfs algorithm is not

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!