We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
defbfs(n,m,edges,s):adj=defaultdict(list)foru,vinedges:adj[u].append(v)adj[v].append(u)d=0dists=[-1]*nq=deque([s])whileq:for_inrange(len(q)):node=q.popleft()ifdists[node-1]==-1:// check node not visiteddists[node-1]=d*6forchinadj[node]:q.append(ch)deladj[node]// remove node from graphd+=1pruned=[]// get rid of the 0 value entryfordindists:ifd!=0:pruned.append(d)returnpruned
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Breadth First Search: Shortest Reach
You are viewing a single comment's thread. Return to all comments →