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 namespace std; class SinglyLinkedListNode { // INSERT YOUR CODE HERE }; class SinglyLinkedList { public: SinglyLinkedListNode *head; SinglyLinkedListNode *tail; SinglyLinkedList() { this->head = nullptr; this->tail = nullptr; } voidinsert_node(intnode_data) { // INSERT YOUR CODE HERE } }; void free_singly_linked_list(SinglyLinkedListNode* node) { // INSERT YOUR CODE HERE } // Complete the has_cycle function below. /* * For your reference: * * SinglyLinkedListNode { * int data; * SinglyLinkedListNode* next; * }; * */ bool has_cycle(SinglyLinkedListNode* head) { SinglyLinkedListNode* temp = head; bool isCycle = false; while (temp != nullptr) { // INSERT YOUR CODE HERE } } int main() { // INSERT YOUR CODE HERE TO TEST YOUR CODE return0; }arrow_forwardusing namespace std; class SinglyLinkedListNode { // INSERT YOUR CODE HERE }; class SinglyLinkedList { public: SinglyLinkedListNode *head; SinglyLinkedListNode *tail; SinglyLinkedList() { this->head = nullptr; this->tail = nullptr; } voidinsert_node(intnode_data) { // INSERT YOUR CODE HERE } }; void free_singly_linked_list(SinglyLinkedListNode* node) { // INSERT YOUR CODE HERE } // Complete the has_cycle function below. /* * For your reference: * * SinglyLinkedListNode { * int data; * SinglyLinkedListNode* next; * }; * */ bool has_cycle(SinglyLinkedListNode* head) { SinglyLinkedListNode* temp = head; bool isCycle = false; while (temp != nullptr) { // INSERT YOUR CODE HERE } } int main() { // INSERT YOUR CODE HERE TO TEST YOUR CODE return0; }arrow_forwardLinkedList list = new LinkedList(); ListIterator s1 = list.listIterator(); System.out.println(s1.hasNext()); What is the output of the above print statement?arrow_forward
- dict_graph = {} # Read the data.txt file with open('data.txt', 'r') as f: for l in f: city_a, city_b, p_cost = l.split() if city_a not in dict_graph: dict_graph[city_a] = {} dict_graph[city_a][city_b] = int(p_cost) if city_b not in dict_graph: dict_graph[city_b] = {} dict_graph[city_b][city_a] = int(p_cost) # Breadth First Search Method def BreadthFirstSearch(graph, src, dst): q = [(src, [src], 0)] visited = {src} while q: (node, path, cost) = q.pop(0) for temp in graph[node].keys(): if temp == dst: return path + [temp], cost + graph[node][temp] else: if temp not in visited: visited.add(temp) q.append((temp, path + [temp], cost + graph[node][temp])) # Depth First Search Method def DepthFirstSearch(graph, src, dst): stack = [(src, [src], 0)] visited = {src} while stack:…arrow_forwarddata structures-java language quickly plsarrow_forwarddata structures in javaarrow_forward
- In an array based FIFO queue Q, which of the following is correct about the queue after Q.Dequeue( ) is executed? The size of the array is 9 as shown below: 5 (front) 9 8 7 3(rear) 4 front = 1; rear =8 front = 0; rear =7; front = 2; rear =3; front = 0; rear =8;arrow_forwardModify following delete() function so that it takes only two arguments, first and x without trail. void delete(listPointer *first, listPointer trail, listPointer x) { if (trail) trail -> link = x -> link; else *first = (*first) -> link; free(x); }arrow_forward//main.cpp #include "linkedListType.h"int main(){Node* head = NULL;orderedLinkedList oll = orderedLinkedList(head); oll.insert(40);oll.insert(100);oll.insert(50); oll.printList(); oll.insert(50);oll.printList(); return 0;} //linkedListType.h #include <stdio.h>#include <iostream>using namespace std; class Node{public:int data;Node* next;}; class orderedLinkedList {Node* head; public:orderedLinkedList(Node* h) {head = h;} void insert(int new_data){Node* head_ref = head; while (head_ref != NULL && (head_ref->next != NULL) && (head_ref->next)->data < new_data){cout << head_ref->data << "\n";head_ref = head_ref->next;} if (head_ref != NULL && (head_ref->next != NULL) && (head_ref->next)->data == new_data){cout << "ERROR: Item to be inserted is already in the list\n";return;} /* Insert new node */Node* new_node = new Node();new_node->data = new_data;if (head_ref != NULL) {new_node->next =…arrow_forward
- Missing Brackets Checking: Write a technique to find on the off chance that an articulation has missing sections or not utilizing StackLinkedList. The technique takes a String articulation as a boundary and return True on the off chance that the articulation's sections are right and Falsearrow_forwardGiven the following C code: struct Node int data: struct Node* prev: struct Node next: Which one of following statements delete the node pointed by X? (Assume that X does not point to head or tail of the linked list) a) X->prev->next-x->prev; X->next->prev-X->next: b) X->prev->prev-X->next; c) X->prev->next-X->next: d) X->prev->prev-X->prev: X->next->next-X->prev: X->next->prev-X->prev; X->next->next-X->next:arrow_forward29. Which of the following statements deletes the node to which cur points? b. cur->next - prev; d. prev->next = cur->next; a. prev->next = cur; c. cur->next = cur->next; 30. In all circular linked lists, a. every node points to a predecessor b. every node points to a successor c. the next pointer of the last node has the value NULL d. each node points to both its predecessor and its successor 31. Which of the following is NOT true about all circular linked lists? a. every node references a successor b. the last node references the first node c. the precede reference of each node references the node that precedes it d. no node contains NULL in its next reference 32. Which of the following is true about all doubly linked lists? a. each node has both a precede pointer and a next pointer b. the precede pointer of the last node has the value NULL c. the last node points to the first node d. the precede pointer of the first node points to the last node 33. Which of the following is true…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