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= '->')
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
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 = '->')](/v2/_next/image?url=https%3A%2F%2Fcontent.bartleby.com%2Fqna-images%2Fquestion%2F6ba29638-0243-441c-918b-c80e4a8cbce6%2F20bf9c10-318e-4427-881b-f3af6bdf4de5%2Fmjrzuwo_processed.jpeg&w=3840&q=75)
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

This question has been solved!
Explore an expertly crafted, step-by-step solution for a thorough understanding of key concepts.
This is a popular solution!
Trending now
This is a popular solution!
Step by step
Solved in 2 steps

Knowledge Booster
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.Recommended textbooks for you

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)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON

Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON

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)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON

Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON

C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON

Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning

Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education