def depthfirst(node): #depth first search function if node not in visited: visited.append(node) for neighbour in graph[node]: depthfirst(neighbour) def findpath(): # function to find the path path = [] for i in range(len(visited)-1): if is adjacent(visited[i], visited[i+1]): path.append(visited[i]) else: path.append(visited[i]) for j in range(len(path)-1, -2, -1): path.append(path[j-1]) if is_adjacent(path[j-1], break path.append(visited[-1]) return path def is_adjacent(node1, node2): #funnction to check if 2 nodes are adjacent if nodel in graph[node2]: return True else: return False graph={ 1: [2,3], 2: [1,3], 3: [1,2,5, 4], 4: [6,11,3,5], 5: [7,4,3], 6: [8,4], 7: [5,9], 8: [6], 9: [7,10], 10: [9,12], 11: [12,4], 12: [10,11] } visited[i+1]): visited = 0 start = int(input("Enter starting node: ")) depthfirst(start) path = findpath() #print path print(*path, sep= '->')

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
icon
Related questions
Question

Write this code exactly but in a different and easier way 

 

Question 3
def depthfirst(node):
#depth first search function
if node not in visited:
visited.append(node)
for neighbour in graph[node]:
depthfirst(neighbour)
def findpath():
# function to find the path
path = []
for i in range(len(visited)-1):
if is_adjacent(visited[i], visited[i+1]):
path.append(visited[i])
path.append(visited[i])
for j in range(len(path)-1, -2, -1):
path.append(path[j-1])
if is_adjacent(path[j-1], visited[i+1]):
else:
break
path.append(visited[-1])
return path
def is_adjacent(node1, node2):
#funnction to check if 2 nodes are adjacent
if nodel in graph[node2]:
return True
else:
return False
graph={
1: [2,3],
2: [1,3],
3: [1,2,5, 4],
4: [6,11,3,5],
5: [7,4,3],
6: [8,4],
7: [5,9],
8: [6],
9: [7,10],
10: [9,12],
11: [12,4],
12: [10,11]
visited = []
start = int(input("Enter starting node: "))
depthfirst(start)
path = findpath()
#print path
print(*path, sep = '->')
Transcribed Image Text:Question 3 def depthfirst(node): #depth first search function if node not in visited: visited.append(node) for neighbour in graph[node]: depthfirst(neighbour) def findpath(): # function to find the path path = [] for i in range(len(visited)-1): if is_adjacent(visited[i], visited[i+1]): path.append(visited[i]) path.append(visited[i]) for j in range(len(path)-1, -2, -1): path.append(path[j-1]) if is_adjacent(path[j-1], visited[i+1]): else: break path.append(visited[-1]) return path def is_adjacent(node1, node2): #funnction to check if 2 nodes are adjacent if nodel in graph[node2]: return True else: return False graph={ 1: [2,3], 2: [1,3], 3: [1,2,5, 4], 4: [6,11,3,5], 5: [7,4,3], 6: [8,4], 7: [5,9], 8: [6], 9: [7,10], 10: [9,12], 11: [12,4], 12: [10,11] visited = [] start = int(input("Enter starting node: ")) depthfirst(start) path = findpath() #print path print(*path, sep = '->')
Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 2 steps

Blurred answer
Knowledge Booster
Binomial Heap
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education