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:
PriorityQueue<WeightedEdge> q = new PriorityQueue<>();
q.offer(new WeightedEdge(1, 2, 3.5));
q.offer(new WeightedEdge(1, 6, 6.5));
q.offer(new WeightedEdge(1, 7, 1.5));
System.out.println(q.poll().weight);
System.out.println(q.poll().weight);
System.out.println(q.poll().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)
- Using a doubly linked list class and node class, implement the following methods: Node* lastNode(void); void pushBack(string argData); void popBack(void); void pushFront(string argData); void print(void); void printReverse(void); void insertAfter(Node* argPtr); void deallocateAll(void); Node* searchFor(string argData); int size(void); Hard code a linked list (no UI) in the main to demonstrate your functions. Print forward and backward to show that the links are well-formed.arrow_forwardIn a doubly linked list of five nodes, if n points at the first node and p points at the second node, which of the following expressions does not refer to the third node in the list? a. p.next b. n.next.next.prev.next c. p.prev.next d. p.next.prev.next e. n.next.nextarrow_forward(defmacro mac (start end) `(dfs ,start ,end nil nil)) (defun dfs (current-state goal-state path &optional (solution-found nil)) (cond ((and solution-found (equal current-state goal-state)) (format t "Solution: ~A~%" (reverse path))) ((member current-state path) nil) (t (let ((new-paths (generate-next-states current-state))) (dolist (new-state new-paths) (dfs new-state goal-state (cons current-state path) t)))))) (defun generate-next-states (state) (let* ((m (car state)) (c (cadr state)) (b (caddr state)) (next-states '())) (loop for m-move from 0 to m do (loop for c-move from 0 to c do (when (valid-move m-move c-move m c b) (let* ((new-state (update-state state m-move c-move)) (missionaries-on-left (- m m-move)) (cannibals-on-left (- c c-move))…arrow_forward
- Given a linked list of N nodes such that it may contain a loop. A loop here means that the last node of the link list is connected to the node at position X(1-based index). If the link list does not have any loop, X=0. Remove the loop from the linked list, if it is present, i.e. unlink the last node which is forming the loop. Example 1: Input: N = 3 value[] = {1,3,4} X = 2 Output: 1 Explanation: The link li. st looks like 1 -> 3 -> 4 ^ | |____|arrow_forwardWrite a program (in main.cpp) that: Prompts the user for a filename containing node data. Outputs the minimal spanning tree for a given graph. You will need to implement the createSpanningGraph method in minimalSpanTreeType.h to create the graph and the weight matrix. Note: Files Ch20_Ex21Data.txt and Ch20_Ex4Data.txt contain node data that you may test your program with. minimalSpanTreeType.h : #ifndef H_msTree #define H_msTree #include <iostream> #include <fstream> #include <iomanip> #include <cfloat> #include "graphType.h" using namespace std; class msTreeType: public graphType { public: void createSpanningGraph(); //Function to create the graph and the weight matrix. //Postcondition: The graph using adjacency lists and // its weight matrix is created. void minimalSpanning(int sVertex); //Function to create a minimal spanning tree with //root as sVertex. // Postcondition: A minimal spanning…arrow_forwardGiven main.py and a Node class in Node.py, complete the LinkedList class (a linked list of nodes) in LinkedList.py by writing the insert_in_ascending_order() method that inserts a new Node into the LinkedList in ascending order. Ex: If the input is: 8 3 6 2 5 9 4 1 7 the output is: 1 2 3 4 5 6 7 8 9 Code givenarrow_forward
- Given main() in the Inventory class, define an insertAtFront() method in the InventoryNode class that inserts items at the front of a linked list (after the dummy head node). Ex. If the input is: 4 plates 100 spoons 200 cups 150 forks 200 the output is: 200 forks 150 cups 200 spoons 100 plates public class InventoryNode { private String item; private int numberOfItems; private InventoryNode nextNodeRef; // Reference to the next node public InventoryNode() { item = ""; numberOfItems = 0; nextNodeRef = null; } // Constructor public InventoryNode(String itemInit, int numberOfItemsInit) { this.item = itemInit; this.numberOfItems = numberOfItemsInit; this.nextNodeRef = null; } // Constructor public InventoryNode(String itemInit, int…arrow_forwardLet INITIALISE (Q) be an operation that initializes a linear queue Q to beempty. Let ENQUEUE (Q, ITEM) insert an ITEM into Q and DEQUEUE (Q,ITEM) delete an element from Q through ITEM. EMPTY_QUEUE (Q) is a Booleanfunction that is true if Q is empty and false otherwise, and PRINT (ITEM) is afunction that displays the value of ITEM.What is the output of the following pseudo-code?1. X = Y = Z = 0;2. INITIALISE (Q)3. ENQUEUE (Q,10)4. ENQUEUE (Q, 70)5. ENQUEUE (Q, 88)6. DEQUEUE (Q, X)7. DEQUEUE (Q, Z)8. ENQUEUE (Q, X)9. ENQUEUE (Q, Y+18)10. DEQUEUE (Q, X)11. DEQUEUE (Q, Y)12. while not EMPTY_QUEUE (Q) do13. DEQUEUE (Q, X)14. PRINT (X)15. endarrow_forwardA client can add instances of DirectedEdge class to a List<Edge>, providedthat class Edge extends DirectedEdge. Group of answer choices True Falsearrow_forward
- The code of the class Linkedlist is provided. Test your method from the main. Constraints. For full credit, your solution must meet the following restrictions. A violating solution will be penalized accordingly. points if violated. Your code must run in no worse than O(n), where n is the length of the linked list. B)ts if violated. Do not leak memoryarrow_forwardIN JAVA This triple-ended queue exercise can be found at https://open.kattis.com/problems/teque. The requirements for this program are compatible with those of the Kattis problem, so a solution should produce correct results when submitted on Kattis. The Triple-Ended QUEue supports four operations: push_back x: inserts the element x at the end of the TEQUE push_front x: inserts the element x at the front of the TEQUE push_middle x: inserts the element x so that the insertion index for x is (k + 1)/2 for a 0-based indexed TEQUE. get i: prints out the ith index element (0-based) of the TEQUE. The TEQUE will be implemented as a linked list. The usual implementation requirements apply along with a screenshot of the results from Kattis. Sample input 10 push_back 1 push_front 2 push_back 3 push_front 4 push_middle 5 push_back 6 get 3 push_middle 7 push_middle 8 get 5 Sample Output 1 1 Subject: Java Programmingarrow_forwardConsider the following functions are called for an integer queue in order. What will be the final state of the queue after these operations?CreateQueue(Q);Enqueue(9, Q);Enqueue(1, Q);Dequeue(&x, Q);Enqueue(4, Q);ClearQueue(Q);Enqueue(1, Q);QueueGetFront(&x, Q);Enqueue(7, Q);Dequeue(&x, Q);Enqueue(9, Q);Enqueue(2, Q);Dequeue(&x, Q);Enqueue(3,Q);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