Concept explainers
Weighted Graph:
A graph is termed as weighted graph if each edge of the graph is assigned a weight. The weighted edges stored in the weighted graphs can be stored in adjacency lists.
Weighted edges can be represented using a two-dimensional array. An weighted edge can be represented as “WeightedEdge(u,v,w)”, where “u” and “v” are edges and “w” represents the weight between them.
Example of storing edge in a weighted graph:
Object[][] edges =
{ new Integer(0), new Integer(1), new SomeTypeForWeight(8) };
Program:
List<WeightedEdge> list = new ArrayList<>();
list.add(new WeightedEdge(1, 2, 3.5));
list.add(new WeightedEdge(2, 3, 4.5));
WeightedEdge e = java.util.Collections.max(list);
System.out.println(e.u);
System.out.println(e.v);
System.out.println(e.weight)
Want to see the full answer?
Check out a sample textbook solutionChapter 29 Solutions
Introduction to Java Programming and Data Structures, Comprehensive Version (11th Edition)
- Given two linked lists sorted in increasing order, write a method intersection( ) to return a new list representing the intersection of the two lists. The new list should be made with its own memory — the original lists should not be changed. Example: if list1 contains {3,4,7,9,11} and list contains {2,3,8,9,12,15} so the intersection list3 is {3,9}.arrow_forwardWrite a short IO function, runTest, which runs your tests using quickCheck. runTest = undefined --Consider the following type which allows non-homogenous lists to be built. data MkType a b = ElemA a (MkType a b) | ElemB b (MkType a b) | Emptyderiving Showarrow_forward. Each next problem is based on theprevious.1) Suppose we have a listlst = [1,2,3,4,9,0]What is a one-liner discussed in chapter 11 to get the length of said list?2) Assuming you are dealing with the same list, how can you mutate the list to be:lst = [1,2,3,4,9,6,0]3) Continuing with the new list, how do we find the position of 9?4) Now remove element at index 2 and return its value.5) We don’t like the first 4 numbers, delete them with a single command.6) We have a short list left, but the order is wrong, how do you reverse the list?arrow_forward
- How to get insert method to accept two values? Right now it accepts one, and ignores the other. class Node: """ Represents a node in a linked list """ def __init__(self, data): self.data = data self.next = None class LinkedList: """ A linked list implementation of the List ADT """ def __init__(self): self._head = None def add(self, val): """ Adds a node containing val to the linked list """ self._head = self.rec_add(self._head, val) def rec_add(self, a_node, val): """ Adds a node containing val to the linked list """ if a_node is None: return Node(val) else: a_node.next = self.rec_add(a_node.next, val) return a_node def insert(self, val, pos): """ Adds a node containing val to the linked list """ self._head = self.insert_add(self._head, val, pos) def insert_add(self, a_node, val, pos): """…arrow_forwardSuppose that you have a singly linked list with five nodes and with head reference. Then the statement head = head.next will remove the first node of the linked list? a) true b) falsearrow_forwardWhich methods from the List interface are not supported by ArrayList and LinkedList? Explain why you think such methods aren't a part of List.arrow_forward
- Can an unordered list include Comparable-implemented objects? Why do you consider this to be the case?arrow_forwardBubble Sort You will change the following method bubbleSort() by using generics and complete only one of three bubbleSort() methods. public int[] bubbleSort(int[] list) {//{2, 9, 5, 4, 8, 1, 6} int i, j, temp = 0; for (i = 0; i < list.length - 1; i++) { for (j = 0; j < list.length - 1 ; j++) { if (list[j] > list[j + 1]) { temp = list[j]; list[j] = list[j + 1]; list[j + 1] = temp; } } } return list; } OPTION 1 public static <E> void bubbleSort(E[] list){ //COMPLETE HERE } OPTION 2 public static<E extends Comparable<E>> void bubbleSort(E[] list){ //COMPLETE HERE } OPTION 3public static<E> void bubbleSort(E[] list, Comparator<? super E> comparator){ //COMPLETE HERE }arrow_forwardPlease explai The list ADT interface and its implementation remove the need for the original list interface and its two implementing classes (ElementNode & EmptyNode). Group of answer choices A. True B. False C. No answer text provided. D. No answer text provided. Q#2, What is the type of the argument to a sort operation on a list? Group of answer choices A. Function B. Predicate C. Comparator D. BiFunctionarrow_forward
- Write java code to do the following: Implement the Quicksort algorithm for both generic lists and generic arrays public static <T extends Comparable<T>> void quickSort(final List<T> list, final Comparator<T> comparator) {...} public static <T extends Comparable<T>> void quickSort(final T[] array, final Comparator<T> comparator) {...}arrow_forwardWrite a method called replaceAll that accepts two integer values as parameters and replaces all occurrences of the first value in the list with the second value. For example, if a variable called list stores [11, –7, 3, 42, 3, 0, 14, 3], the call of list.replaceAll(3, 999); should change the list to store [11, –7, 999, 42, 999, 0, 14, 999].arrow_forwardWrite java code to do the following: Implement the Quicksort algorithm for both generic lists and generic arrays: Please note you cannot make any additional FUNCTIONS OR METHODS, Everything must be housed within the functions I have provided below: public static <T extends Comparable<T>> void quickSort(final List<T> list, final Comparator<T> comparator) {...} public static <T extends Comparable<T>> void quickSort(final T[] array, final Comparator<T> comparator) {...}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