Question

In: Computer Science

Language: Python Create a graph and then set initial and goal states such that the number...

Language: Python

Create a graph and then set initial and goal states such that the number of nodes visited for BFS is smaller than that in DFS. Now modify the initial and goal state such that the number of nodes visited for BFS is larger than that in DFS.

Solutions

Expert Solution

Consider the following graph:

Two searches are done

0 to 1 and 0 to 4

PYTHON CODE:

def BFS(graph,start,goal):
       n=len(graph[0])
       list=[]
       list.append(start)
       visited=[]
       for i in range(0,n):
              visited.append(False)
       while True:
              count=0
              for i in range(0,n):
                     if visited[i]==True:
                            count=count+1
              if count==n:
                     break
              for i in range(0,n):
                     if visited[i]==False and graph[list[0]][i]==1:
                            list.append(i)
              visited[list[0]]=True
              if list[0]==goal:
                     print("Founded ",goal)
                     return
              else:
                     print("Visited ",list[0])
              list.pop(0)

def DFS(graph,start,goal):
       n=len(graph[0])
       list=[]
       list.append(start)
       visited=[]
       for i in range(0,n):
              visited.append(False)
       while True:
              count=0
              for i in range(0,n):
                     if visited[i]==True:
                            count=count+1
              if count==n:
                     break
              p=list.pop()
              for i in range(0,n):
                     if visited[i]==False and graph[p][i]==1:
                            list.append(i)
              visited[p]=True
              if p==goal:
                     print("Founded ",goal)
                     return
              else:
                     print("Visited ",p)

#Adjacency representation of graph
graph=[[0,1,1,0,0],
       [1,0,0,0,0],
       [1,0,0,1,1],
       [0,0,1,0,0],
       [0,0,1,0,0]]
print("No. of nodes visited in BFS smaller than DFS")
print("BFS")
BFS(graph,0,1)
print("DFS")
DFS(graph,0,1)
print("No. of nodes visited in BFS larger than DFS")
print("BFS")
BFS(graph,0,4)
print("DFS")
DFS(graph,0,4)

OUTPUT:


Related Solutions

PYTHON LANGUAGE PLEASE DO NUMBER 5 ONLY """ # 1. Based on Textbook R6.28 # Create...
PYTHON LANGUAGE PLEASE DO NUMBER 5 ONLY """ # 1. Based on Textbook R6.28 # Create a table of m rows and n cols and initialize with 0 m = 3 n = 4 # The long way: table = [] for row in range(m) : table.append([0]*n)    # The short way: # 2. write a function to print the table in row, column format, # then call the function ''' # using index def printTable(t): for i in range(len(t))...
Question 1: Using Python 3 Create an algorithm The goal is to create an algorithm that...
Question 1: Using Python 3 Create an algorithm The goal is to create an algorithm that can sort a singly-linked-list with Merge-sort. The program should read integers from file (hw-extra.txt) and create an unsorted singly-linked list. Then, the list should be sorted using merge sort algorithm. The merge-sort function should take the head of a linked list, and the size of the linked list as parameters. hw-extra.txt provided: 37 32 96 2 25 71 432 132 76 243 6 32...
The programming language is Python Instructions: Create a function that will delete a node in a...
The programming language is Python Instructions: Create a function that will delete a node in a Linked List based on position number. On below example, if you want to delete position #2, it will remove the Banana (arrangement of nodes below is Apple, Banana, Cherry, Grapes, Orange). myLinkedList = LinkedList() myLinkedList.append("Banana") myLinkedList.append("Cherry") myLinkedList.append("Grapes") myLinkedList.append("Orange") myLinkedList.prepend("Apple") myLinkedList.deleteByPositionNum(2) node = myLinkedList.head while node: print(node.value, " ") node = node.next_node You may start with the function head: def deleteByPositionNum(self, positionNum):
Python Language Only! Python Language Only! Python Language Only! Python 3 is used. When doing this...
Python Language Only! Python Language Only! Python Language Only! Python 3 is used. When doing this try to use a level one skill python, like as if you just learned this don't use anything advanced if you can please. If not then do it as you can. Assume you have a file on your disk named floatnumbers.txt containing float numbers. Write a Python code that reads all the numbers in the file and display their average. Your code must handle...
This program (in python) will display a set of authors and the number of novels written...
This program (in python) will display a set of authors and the number of novels written by each author in both a table and a histogram. You will ask the user for all of the information. Using what you learned about incremental development, use the following approach to create your program: Prompt the user for the information about the table. First, ask for the title of this data set by prompting the user for a title for data. Output the...
Please solve this problem with Python Language. P#2. A Pythagorean triplet is a set of three...
Please solve this problem with Python Language. P#2. A Pythagorean triplet is a set of three natural numbers, a < b < c, for which a2 + b2 = c2 . For example, 32 + 42 = 52. And a + b + c = 3 + 4 + 5 = 12. There exists exactly two Pythagorean triplet for which a + b + c = 300. Find a, b, c. Hints: Level-0 Algorithm: 1. Find the possible ranges of...
(Programming Language: Python) It's trivial that the value of a number remains the same no matter...
(Programming Language: Python) It's trivial that the value of a number remains the same no matter how many zeros precede it. However, adding the zeros at the end of the number increases the value * 10. Jasmine is tired of seeing \001/100" on her tests (well yes, no one really writes 001, but Jasmine's teacher finds it funny to do so). So, she managed to login to her teacher's computer and now wants to design a function that can move...
(Programming Language: Python) Complete the function remove number such that given a list of integers and...
(Programming Language: Python) Complete the function remove number such that given a list of integers and an integer n, the function removes every instance of n from the list. Remember that this function needs to modify the list, not return a new list. # DO NOT ADD ANY OTHER IMPORTS from typing import List def remove_number(lst: List[int], number: int) -> None: """ Remove every instance of number in lst. Do this *in-place*, i.e. *modify* the list. Do NOT return a...
Draw a single graph in Python showing the performance of both algorithms against the number of...
Draw a single graph in Python showing the performance of both algorithms against the number of elements (100,000). Consider the worst case scenario. In the graph we want to see: labeling of the axes, a legend, a title and the graphs for both linear and binary search, again correctly labeled. Explain what you see.
Python Language: Similar to Project 3, write a program that loops a number from 1 to...
Python Language: Similar to Project 3, write a program that loops a number from 1 to 10 thousand and keeps updating a count variable according to these rules: if the number is divisible by n1, increase count by 1 if the number is divisible by n2, increase count by 2 if the number is divisible by n3, increase count by 3 if none of the above conditions match for the number, increase count by the number. Before the loop begins,...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT