Concept explainers
The below program contains error please help to solve it.
from collections import deque
def find_shortest_paths(graph):
paths = {}
vertices = graph.keys()
for start_vertex in vertices:
visited = set()
distances = {vertex: float('inf') for vertex in vertices}
distances[start_vertex] = 0
queue = deque([(start_vertex, [])])
while queue:
current_vertex, path = queue.popleft()
visited.add(current_vertex)
for neighbor in graph[current_vertex]:
if neighbor not in visited:
distances[neighbor] = min(distances[neighbor], distances[current_vertex] + 1)
queue.append((neighbor, path + [current_vertex]))
for end_vertex in vertices:
paths[(start_vertex, end_vertex)] = set(path + [end_vertex] for path in paths[(start_vertex, end_vertex)] + [path] if len(path) == distances[end_vertex])
return paths
def find_largest_set(paths):
largest_set = set()
for u, v in paths:
for w in paths:
if u != v and u != w and v != w and len(paths[u, v]) != len(paths[u, w]) + len(paths[w, v]):
break
else:
if len(paths[u, v]) > len(largest_set):
largest_set = paths[u, v]
return largest_set
# Example 1
graph1 = {
'A': ['B', 'C', 'D', 'F'],
'B': ['A', 'C', 'D', 'F'],
'C': ['A', 'B', 'D', 'F'],
'D': ['A', 'B', 'C', 'E', 'F'],
'E': ['D', 'F'],
'F': ['A', 'B', 'C', 'D', 'E']
}
paths1 = find_shortest_paths(graph1)
largest_set1 = find_largest_set(paths1)
print("The largest set is:")
print(largest_set1)
print("The cardinality is:", len(largest_set1))
print()
# Example 2
graph2 = {
'1': ['2', '5', '6'],
'2': ['1', '3', '7'],
'3': ['2', '4', '8'],
'4': ['3', '5', '9'],
'5': ['1', '4', '10'],
'6': ['1', '8', '9'],
'7': ['2', '9', '10'],
'8': ['3', '6', '10'],
'9': ['4', '6', '7'],
'10': ['5', '7', '8']
}
paths2 = find_shortest_paths(graph2)
largest_set2 = find_largest_set(paths2)
print("The largest set is:")
print(largest_set2)
print("The cardinality is:", len(largest_set2))
Step by stepSolved in 5 steps with 3 images
- My code does not produce the required output, can anyone let me know what seems to be off? Input: from Node import Node from LinkedList import LinkedList def print_linkedlist(list): print(f'Linked List ------ Nodes ----------------------------------') print(f'head: {list.head.name:<6}', end=' ') node = list.head while node != None: print(f"{node.name:<12}", end=' ') node = node.next print() print(f'tail: {list.tail.name:<6}', end=' ') node = list.head while node != None: print(f"data:{node.data:<7}", end=' ') node = node.next print() print(f' ', end=' ') node = list.head while node != None: if node.next != None: print(f"next:{node.next.name:<7}", end=' ') else: print("next:None") node = node.next print() print(f' ', end=' ') node = list.tail while node != None: if node.prev != None:…arrow_forwardIn python. Write a LinkedList class that has recursive implementations of the add and remove methods. It should also have recursive implementations of the contains, insert, and reverse methods. The reverse method should not change the data value each node holds - it must rearrange the order of the nodes in the linked list (by changing the next value each node holds). It should have a recursive method named to_plain_list that takes no parameters (unless they have default arguments) and returns a regular Python list that has the same values (from the data attribute of the Node objects), in the same order, as the current state of the linked list. The head data member of the LinkedList class must be private and have a get method defined (named get_head). It should return the first Node in the list (not the value inside it). As in the iterative LinkedList in the exploration, the data members of the Node class don't have to be private. The reason for that is because Node is a trivial class…arrow_forwardJAVA please Given main() in the ShoppingList class, define an insertAtEnd() method in the ItemNode class that adds an element to the end of a linked list. DO NOT print the dummy head node. Ex. if the input is: 4 Kale Lettuce Carrots Peanuts where 4 is the number of items to be inserted; Kale, Lettuce, Carrots, Peanuts are the names of the items to be added at the end of the list. The output is: Kale Lettuce Carrots Peanuts Code provided in the assignment ItemNode.java:arrow_forward
- If the following is a circular array based queue of size 99 43 54 76 93 77 18 If rear is at index 1 and front is at index 6, what is the size of the queue? Answer:arrow_forwardSimple JAVA linkedlist code implementation please help and complete any part you can - Without using the java collections interface (ie do not import java.util.List,LinkedList, Stack, Queue...)- Create an implementation of LinkedList interface- For the implementation create a tester to verify the implementation of thatdata structure performs as expected Build Bus Route – Linked List- Your task is to:o Implement the LinkedList interface (fill out the implementation shell)o Put your implementation through its paces by exercising each of themethods in the test harnesso Create a client (a class with a main) ‘BusClient’ which builds a busroute by performing the following operations on your linked list:o§ Create (insert) 4 stations§ List the stations§ Check if a station is in the list (print result)• Check for a station that exists, and onethat doesn’t§ Remove a station§ List the stations§ Add a station before another station§ List the stations§ Add a station after another station§ Print the…arrow_forwardDONT use any of the list methods (append, pop etc.). class mycircularqueue(): def __init__(self, m = 10): self.maxsize = m self.array = [None]*m self.FRONT = self.REAR = -1 def enqueue(self, item): # Inserts item at the rear of a non full queue and returns True. If unable to insert the item returns false ####################################################################### # Remove the pass statement and write your code ####################################################################### pass def dequeue(self): # Removes the item from the front of the queue and returns it. For unsuccessful dequeue return False ####################################################################### # Remove the pass statement and write your code ####################################################################### pass def peek(self): # Returns the…arrow_forward
- Database System ConceptsComputer ScienceISBN:9780078022159Author:Abraham Silberschatz Professor, Henry F. Korth, S. SudarshanPublisher:McGraw-Hill EducationStarting Out with Python (4th Edition)Computer ScienceISBN:9780134444321Author:Tony GaddisPublisher:PEARSONDigital Fundamentals (11th Edition)Computer ScienceISBN:9780132737968Author:Thomas L. FloydPublisher:PEARSON
- C How to Program (8th Edition)Computer ScienceISBN:9780133976892Author:Paul J. Deitel, Harvey DeitelPublisher:PEARSONDatabase Systems: Design, Implementation, & Manag...Computer ScienceISBN:9781337627900Author:Carlos Coronel, Steven MorrisPublisher:Cengage LearningProgrammable Logic ControllersComputer ScienceISBN:9780073373843Author:Frank D. PetruzellaPublisher:McGraw-Hill Education