How to change this Java code to accept character value instead of integer for edges u and v: import java.util.*; public class Main { privateintV;// number of vertices privateArrayList> adj;// adjacency list privateclassEdge{ int v;// vertex int bandwidth;// bandwidth publicEdge(int v,int bandwidth){ this.v = v; this.bandwidth = bandwidth; } } publicMain(intV){ this.V=V; adj = new ArrayList>(V); for(int i =0; i ()); } } publicvoidaddEdge(int u,int v,int bandwidth){ adj.get(u).add(new Edge(v, bandwidth)); adj.get(v).add(new Edge(u, bandwidth)); } publicintmaxBandwidth(int a,int b){ PriorityQueue pq =newPriorityQueue(V,Collections.reverseOrder()); HashMap visited =newHashMap(); HashMap parent =newHashMap(); pq.add(a); visited.put(a, Integer.MAX_VALUE); parent.put(a, -1); while(!pq.isEmpty()){ int u = pq.poll(); if(u == b){ break; } for(Edge e : adj.get(u)){ int v = e.v; int bandwidth = e.bandwidth; if(!visited.containsKey(v)){ int minBandwidth =Math.min(visited.get(u), bandwidth); visited.put(v, minBandwidth); parent.put(v, u); pq.add(v); } } } int maxBandwidth = visited.get(b); int curr = b; ArrayList path =newArrayList(); while(curr !=-1){ path.add(0, curr); curr = parent.get(curr); } System.out.println("Maximum bandwidth path from "+ a +" to "+ b +": "+ path); return maxBandwidth; } public static void main(String[] args){ Main g = new Main(5); g.addEdge(0, 1, 10); g.addEdge(0, 2, 5); g.addEdge(1, 2, 7); g.addEdge(1, 3, 3); g.addEdge(2, 3, 2); g.addEdge(2, 4, 6); System.out.println("Maximum bandwidth from 0 to 4: " + g.maxBandwidth(0, 4));
How to change this Java code to accept character value instead of integer for edges u and v: import java.util.*; public class Main { privateintV;// number of vertices privateArrayList> adj;// adjacency list privateclassEdge{ int v;// vertex int bandwidth;// bandwidth publicEdge(int v,int bandwidth){ this.v = v; this.bandwidth = bandwidth; } } publicMain(intV){ this.V=V; adj = new ArrayList>(V); for(int i =0; i ()); } } publicvoidaddEdge(int u,int v,int bandwidth){ adj.get(u).add(new Edge(v, bandwidth)); adj.get(v).add(new Edge(u, bandwidth)); } publicintmaxBandwidth(int a,int b){ PriorityQueue pq =newPriorityQueue(V,Collections.reverseOrder()); HashMap visited =newHashMap(); HashMap parent =newHashMap(); pq.add(a); visited.put(a, Integer.MAX_VALUE); parent.put(a, -1); while(!pq.isEmpty()){ int u = pq.poll(); if(u == b){ break; } for(Edge e : adj.get(u)){ int v = e.v; int bandwidth = e.bandwidth; if(!visited.containsKey(v)){ int minBandwidth =Math.min(visited.get(u), bandwidth); visited.put(v, minBandwidth); parent.put(v, u); pq.add(v); } } } int maxBandwidth = visited.get(b); int curr = b; ArrayList path =newArrayList(); while(curr !=-1){ path.add(0, curr); curr = parent.get(curr); } System.out.println("Maximum bandwidth path from "+ a +" to "+ b +": "+ path); return maxBandwidth; } public static void main(String[] args){ Main g = new Main(5); g.addEdge(0, 1, 10); g.addEdge(0, 2, 5); g.addEdge(1, 2, 7); g.addEdge(1, 3, 3); g.addEdge(2, 3, 2); g.addEdge(2, 4, 6); System.out.println("Maximum bandwidth from 0 to 4: " + g.maxBandwidth(0, 4));
Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
Related questions
Question
100%
How to change this Java code to accept character value instead of integer for edges u and v:
import java.util.*;
public class Main {
privateintV;// number of vertices
privateArrayList<ArrayList<Edge>> adj;// adjacency list
privateclassEdge{
int v;// vertex
int bandwidth;// bandwidth
publicEdge(int v,int bandwidth){
this.v = v;
this.bandwidth = bandwidth;
}
}
publicMain(intV){
this.V=V;
adj = new ArrayList<ArrayList<Edge>>(V);
for(int i =0; i <V; i++){
adj.add(new ArrayList<Edge>());
}
}
publicvoidaddEdge(int u,int v,int bandwidth){
adj.get(u).add(new Edge(v, bandwidth));
adj.get(v).add(new Edge(u, bandwidth));
}
publicintmaxBandwidth(int a,int b){
PriorityQueue<Integer> pq =newPriorityQueue<Integer>(V,Collections.reverseOrder());
HashMap<Integer, Integer> visited =newHashMap<Integer, Integer>();
HashMap<Integer, Integer> parent =newHashMap<Integer, Integer>();
pq.add(a);
visited.put(a, Integer.MAX_VALUE);
parent.put(a, -1);
while(!pq.isEmpty()){
int u = pq.poll();
if(u == b){
break;
}
for(Edge e : adj.get(u)){
int v = e.v;
int bandwidth = e.bandwidth;
if(!visited.containsKey(v)){
int minBandwidth =Math.min(visited.get(u), bandwidth);
visited.put(v, minBandwidth);
parent.put(v, u);
pq.add(v);
}
}
}
int maxBandwidth = visited.get(b);
int curr = b;
ArrayList<Integer> path =newArrayList<Integer>();
while(curr !=-1){
path.add(0, curr);
curr = parent.get(curr);
}
System.out.println("Maximum bandwidth path from "+ a +" to "+ b +": "+ path);
return maxBandwidth;
}
public static void main(String[] args){
g.addEdge(0, 1, 10);
g.addEdge(0, 2, 5);
g.addEdge(1, 2, 7);
g.addEdge(1, 3, 3);
g.addEdge(2, 3, 2);
g.addEdge(2, 4, 6);
System.out.println("Maximum bandwidth from 0 to 4: " + g.maxBandwidth(0, 4));
}
}
Expert Solution
This question has been solved!
Explore an expertly crafted, step-by-step solution for a thorough understanding of key concepts.
This is a popular solution!
Trending now
This is a popular solution!
Step by step
Solved in 7 steps with 3 images
Knowledge Booster
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.Recommended textbooks for you
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education