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)
- 6. Consider the following code segment: ArrayList list = new ArrayList(10) ; list.add( "Ali" ); list.add( "Ahmed" ); list.add( "Hassan" ); list.add( 0, "Qasim" ); Which of the following elements will be at index 1 of the list? K Ali Ahmed Hassan Qasimarrow_forwardAssume that the SampleQueue class, with the given code below, is used in the TestSampleQueue that is also shown below. Show the output of the TestSampleQueue. Do the exercise without using a computer (i.e. Make amanual trace of the program):// SampleQueue classimport java.lang.*;import java.util.*;public class SampleQueue<T> {private LinkedList<T> list;public SampleQueue() {list = new LinkedList<T>();}public void setList(LinkedList<T> list) {this.list = list;}public void clear() {list.clear();}public boolean isEmpty() {return list.isEmpty();}public T firsElement() {return list.getFirst();}public T dequeue() {return list.removeFirst();}public void enqueue(T element) {list.addLast(element);}public LinkedList<T> getList() {return list;}public String toString() {return list.toString();}} // end of SampleQueue classarrow_forwardusing System; using System.Collections.Generic; using System.Linq; using System.Text; namespace Testing { class Program { staticvoid Main(string[] args) { int number = 1; int sum = 0; while (number <= 1000) { Console.WriteLine(number); number = number + 1; if (number >802) { sum = sum + number; } if (number >19) number = number + 2; } Console.ReadKey(); } } } how can this program be fixed to show me the sum of all of the numbers after 802?arrow_forward
- Write a Python Code for the given constructor and conditions: Given Constructor: def __init__ (self, a) Pre-condition: Array cannot be empty. Post-condition: This is the default constructor of MyList class. This constructor creates a list from an array.arrow_forwardDirection: Continue the code below and add case 4, case 5, and case 6. Add 3 more functions aside from insert, getValue, and clear from List ADT import java.util.LinkedList; import java.util.Scanner; class SampleLL { } public static void main(String[] args) { LinkedList 11s = new LinkedList(); String msg = "Choose a function: \n [1] Insert, [2]Get Value, [3]Clear, [0] Exit"; System.out.println(msg); Scanner scan= new Scanner(System.in); int choice scan.nextInt (); while(true) { } if (choice =0) { } System.exit(0); switch(choice) { } case 1: System.out.println("Enter a word/symbol:"); break; case 2: System.out.println("Enter a number: "); break; 11s.add(scan.next()); break; case 3 11s.clear(); default: System.out.println("Invalid input!"); break; System.out.println(11s.get (scan.nextInt())); System.out.println(msg); choice scan.nextInt ();arrow_forwardJava // Complete the missing code in the main function in the Main class below import java.util.ArrayList; import java.util.Iterator; import java.util.List; class Student { private int studentId; private String studentName; private String course; public Student(int studentId, String studentName, String course) { super(); this.studentId = studentId; this.studentName = studentName; this.course = course; } @Override public String toString() { return "Student Details:\n" + "studentId:"+studentId + "\nstudentName:" + studentName + "\ncourse:" + course; } } public class Main { public static void main(String[] args) { List<Student> studentList =new ArrayList<>(); studentList.add(new Student(1,"Alex","Java with DS")); studentList.add(new Student(2,"Albert","Java with DS")); studentList.add(new Student(3,"Samson","Spring Api"));…arrow_forward
- Define: int findrirstEven (int num [1, int sise); Returns the index location of the first even number of the list; if no even number, returns -1arrow_forwardpublic LLNode secondHalf(LLNode head) { }arrow_forwardstarter code //Provided imports, feel free to use these if neededimport java.util.Collections;import java.util.ArrayList; /** * TODO: add class header */public class Sorts { /** * this helper finds the appropriate number of buckets you should allocate * based on the range of the values in the input list * @param list the input list to bucket sort * @return number of buckets */ private static int assignNumBuckets(ArrayList<Integer> list) { Integer max = Collections.max(list); Integer min = Collections.min(list); return (int) Math.sqrt(max - min) + 1; } /** * this helper finds the appropriate bucket index that a data should be * placed in * @param data a particular data from the input list if you are using * loop iteration * @param numBuckets number of buckets * @param listMin the smallest element of the input list * @return the index of the bucket for which the particular data should…arrow_forward
- starter code //Provided imports, feel free to use these if neededimport java.util.Collections;import java.util.ArrayList; /** * TODO: add class header */public class Sorts { /** * this helper finds the appropriate number of buckets you should allocate * based on the range of the values in the input list * @param list the input list to bucket sort * @return number of buckets */ private static int assignNumBuckets(ArrayList<Integer> list) { Integer max = Collections.max(list); Integer min = Collections.min(list); return (int) Math.sqrt(max - min) + 1; } /** * this helper finds the appropriate bucket index that a data should be * placed in * @param data a particular data from the input list if you are using * loop iteration * @param numBuckets number of buckets * @param listMin the smallest element of the input list * @return the index of the bucket for which the particular data should…arrow_forward5. The following function indexCounter aims to find and return all the indices of the element x in a list L. if x is not in the list L, return an empty list. However, the implementation has a flaw, so that many times it will return error message. Can you fix the flaw? def indexCounter(L, x): indexList [] startIndex = 0 while startIndex < len(L): indexList.append (L.index (x, startIndex)) startIndex = L.index (x, startIndex) + 1 return indexListarrow_forwardAnswer for.Write a Python Code for the given constructor and conditions: Given Constructor: def __init__ (self, a) Pre-condition: Array cannot be empty. Post-condition: This is the default constructor of MyList class. This constructor creates a list from an array..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