Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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
image text in transcribed

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Professional SQL Server 2000 Database Design

Authors: Louis Davidson

1st Edition

1861004761, 978-1861004765

More Books

Students also viewed these Databases questions

Question

Demonstrate three aspects of assessing group performance?

Answered: 1 week ago