Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Convert my python code to java and finish this problem. My code: import operator import math class PriorityQueue(object): def __init__(self, lt_comp=operator.lt): self.MinHeap = [] self.Sorted

Convert my python code to java and finish this problem.

image text in transcribed

My code:

import operator

import math class PriorityQueue(object): def __init__(self, lt_comp=operator.lt): self.MinHeap = [] self.Sorted = [] self.lt = lt_comp def empty(self): return not self.MinHeap # TODO: Fix this, not complete insert to preserve min heap. # OK for now, since it is not used to build the min heap in this example. def insert(self, x): self.MinHeap.append(x) def minimum(self): return self.MinHeap[0] # takes off the min from the end, after swapping it with the top def extract_min(self): self.MinHeap[0], self.MinHeap[-1] = self.MinHeap[-1], self.MinHeap[0] result = self.MinHeap.pop() self.min_heapify(0) return result def min_location(self, i, left, right): location = i if left = 3: left_child = 2 * i + 1 right_child = 2 * i + 2 min_location = self.min_location(i, left_child, right_child) if min_location != i: # switch parent and smaller child self.MinHeap[i], self.MinHeap[min_location] = \ self.MinHeap[min_location], self.MinHeap[i] # now, see if new parent is still smaller self.min_heapify(min_location) elif len(self.MinHeap) == 2: if lt(self.MinHeap[1], self.MinHeap[0]): self.MinHeap[0], self.MinHeap[1] = \ self.MinHeap[1], self.MinHeap[0] def build_min_heap(self, lst): self.MinHeap = lst for i in range(math.floor(len(self.MinHeap) / 2 - 1), -1, -1): self.min_heapify(i) class Vertex: def __init__(self, index): self.index = index self.d = 2000000 self.parent = None class Graph: def __init__(self, queue, adj, vertices): self.vertices = vertices self.queue = queue self.adj = adj def add_vertex(self, v): self.vertices[v.node] = v def relax(u, v, adj): edge_weight = adj[u.index][v.index] if v.d > u.d + edge_weight: v.d = u.d + edge_weight v.parent = u def dijkstra(graph): s = [] q = graph.queue while not q.empty(): u = q.extract_min() s.append(u) for v, i in enumerate(graph.adj[u.index]): relax(u, graph.vertices[v], graph.adj) if __name__ == '__main__': import sys source = 0 destination = 0 adj_matrix = [] queue = [] vertices = {} # with open('.\\input.txt', 'r') as f: with open(sys.argv[1], 'r') as f: hasFirst = False vertex_count = -1 for line in f: temp = line.rstrip().split(' ') if not hasFirst: hasFirst = True source = int(temp[0]) destination = int(temp[1]) else: vertex_count += 1 adj_matrix.append(list(map(int, temp))) queue.append(Vertex(vertex_count)) vertices[vertex_count] = queue[-1] vertices[source].d = 0 def lt(v1, v2): return v1.d Problem 3. (20 points) Implement Dijkstra's algorithm to find the shortest path to every other vertex. The input text file will contain the graph represented as an adjacency matrix. Values of 2 million will represent "infinity" edge weight (when there is no edge between the verticies). You can assume all shortest paths will be less than 2 million The first line in the input file is the source vertex, one space, then the destination vertex (with the first row of the adjacency matrix being vertex "O", the second row being vertex 1", and so on). Afterwards is the adjacency matrix (which will be square). Your output.txt should contain the shortest path length from this source to destination vertex followed by a .. After the ":" should be the actual list of vertexes you should travel through for this shortest path. Sample input file (fire line always source and destination vertex, afterwards is adjacency matrix): 0 1 0 2000000 4 2 0 7 2000000 30 Sample output.txt (7 is the shortest path length (before ."). O 2 1 is the actual path (after""): 7: 0 2 1 Problem 3. (20 points) Implement Dijkstra's algorithm to find the shortest path to every other vertex. The input text file will contain the graph represented as an adjacency matrix. Values of 2 million will represent "infinity" edge weight (when there is no edge between the verticies). You can assume all shortest paths will be less than 2 million The first line in the input file is the source vertex, one space, then the destination vertex (with the first row of the adjacency matrix being vertex "O", the second row being vertex 1", and so on). Afterwards is the adjacency matrix (which will be square). Your output.txt should contain the shortest path length from this source to destination vertex followed by a .. After the ":" should be the actual list of vertexes you should travel through for this shortest path. Sample input file (fire line always source and destination vertex, afterwards is adjacency matrix): 0 1 0 2000000 4 2 0 7 2000000 30 Sample output.txt (7 is the shortest path length (before ."). O 2 1 is the actual path (after""): 7: 0 2 1

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Spatial Database Systems Design Implementation And Project Management

Authors: Albert K.W. Yeung, G. Brent Hall

1st Edition

1402053932, 978-1402053931

More Books

Students also viewed these Databases questions

Question

Explain the function and purpose of the Job Level Table.

Answered: 1 week ago