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
Instructor Solutions Manual For Introduction To Java Programming And Data Structures, Comprehensive Version, 11th Edition
- //give the output of the following program and draw the link list. struct node{ int info; struct node *link;};typedef struct node *nodePTR;void insort(nodePTR*,struct Info);nodePTR getnode();int main(void){nodePTR p=NULL,head=NULL,save;inti; for(i=0;i<3;i++){ insort (&head,i+5);}save=head;do{ printf("%d",save->info); save=save->link;}while(save!=NULL); return0;}nodePTR getnode(){nodePTR q;q=(nodePTR)malloc(sizeof(struct node));return q;}void insort(NODEPTR *head , int x){ NODEPTR p,q,r; q=NULL; for(p=*head; p!=NULL &&x>p->info; p=p->link) q=p; if(q=NULL) { p=getnode(); p->info=x; p->link=head; head=p; }else{ r=getnode(); q->link=r; r->info=x; r->link=p; }}arrow_forwardAssume that nodeType struct was defined as below. struct nodeType { int info; }; And nodeType *link; nodeType *n, *F, *L, *p, *q; Given initially the following linked list and new node n. n F F What are the steps to be executed to get the following linked list? O L=n; O n->link=nullptr; L->link=n; -0-0-0-6. n->link=nullptr; L->link=n: O It is not possible n=nullptr; L->link=n; L=n; DOC O n->link=nullptr; L=n; L->link=n; O L=n; ? O n->link=nullptr; L->link=n; L=n; n=nullptr; L->link=n; محمد D-0 n Larrow_forward7. What is output by the following code section? QueueInterface aQueue = new QueueReference Based(); int numl, num2; for (int i = 1; i <= 5; i++) { aQueue.enqueue(i); } // end for for (int i = 1; i <= 5; i++) { numl = (Integer) aQueue.dequeue (); num2 (Integer) aQueue.dequeue (); aQueue.enqueue (numl + num2); aQueue.enqueue (num2 - numl); } // end for while (!aQueue.isEmpty()) { System.out.print (aQueue.dequeue () + } // end forarrow_forward
- Parking (car Cars[], car Park[], int Area) { Car Cr, int CrNb; CrNb <-- 0; Park <-- { }; Cr <-- Select (Cars) I/ Cr = / if the Cars = {} while (Feasible (Cr)) do { Remove (Cr, Cars); Add (Cr, Park); CrNb <-- CrNb +1; Cr <-- Select (Cars) } if exist-Solution (Park) Then Success else return Error } Trace the above Greedy algorithm with the following data: Area = 1000, Cars [100, 50, 90, 70. 200, 300, 400, 80, 200] the cost for each parked car is 3JD. The objective function is: %3D k Max (ΣCis 1000) K |=1arrow_forwardQuestion 9: Draw internal representation of the queue q for each step of the following code: ArrayBoundedQueue<Integer> q = new ArrayBoundedQueue<Integer>(6); q.enqueue(37); q.enqueue(86); q.enqueue(54); q.enqueue(71); q.dequeue(); q.enqueue(97); q.dequeue();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_forward
- 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_forwardAssume that nodeType struct was defined as below. struct nodeType { int info; nodeType *link; }; And nodeType *n, *F, *L, *p, *q; Given initially the following linked list and new node n. n F р ‚-o-do-d F What are the steps to be executed to get the following linked list? O n->link=nullptr; p->link=n; L=n; On->link=nullptr; L->link=n; L=n; On=L; -ooooo n->link=nullptr; O L=n; n->link=nullptr; L->link=n; O L=n; ? L->link=n; n->link=nullptr; O n->link=nullptr; n=L; هم O It is not possible р n Larrow_forwardQueue q= new LinkedQueue (8); q.enqueue ("A"); q.enqueue ("B"); q.enqueue ("C"); q.enqueue ("D"); q.enqueue ("E"); q.enqueue ("F"); Stack s= new LinkedStack (8); for (int i=0; i<3; i++) { s.push (q.de queue () ); After all of this code executes, the size of the queue g is -----and the size of the stack is ------ 1, 5 3, 3 4. 2 5, 1arrow_forward
- LinkedList list = new LinkedList(); ListIterator s1 = list.listIterator(); System.out.println(s1.hasNext()); What is the output of the above print statement?arrow_forwarddict_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_forwardQueue abcQ = new Queue (); Queue out = new Queue (); //statements to insert value 1, 2, 3, 4, 5 into abcQ for (int i = 0; i < 5; i++) { int a = Integer.parseInt (abcQ.dequeue ().tostring ()); int b = Integer.parseInt (abcQ.dequeue ().tostring ()); abcQ.enqueue (a); abcQ.enqueue (b); out.enqueue (a + b); for (int i = 0; i < 5; i++) System.out.print (abcQ.dequeue () + " "); System.out.println ("\n") ; for (int i = 0; i < 5; i++) System.out.print (out.dequeue () + " "); (- ks)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