Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Fix this Java code Code:- import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.nio.charset.StandardCharsets; import java.util.*; public class Main { private static void checkCycle_getMaxRating(ArrayList inputLines) {

Fix this Java code

Code:-

import java.io.BufferedReader;

import java.io.IOException;

import java.io.InputStreamReader;

import java.nio.charset.StandardCharsets;

import java.util.*;

public class Main

{

private static void checkCycle_getMaxRating(ArrayList inputLines)

{

HashMap idToIndex = new HashMap();

int nodeNum = 0;

for(String line : inputLines)

{

line = line.trim();

String[] nodes = line.split("|");

String src = nodes[0].trim();

String dest = nodes[1].trim();

if(!idToIndex.containsKey(src))

{

idToIndex.put(src, nodeNum);

nodeNum++;

}

if(!idToIndex.containsKey(dest))

{

idToIndex.put(dest, nodeNum);

nodeNum++;

}

}

int n = idToIndex.size();

int[][] edges = new int[n][2];

String[] rates = new String[n];

int k = 0;

for(String line : inputLines)

{

//System.out.println(convert);

line = line.trim();

if(line.contains(" ")) continue; //??

String[] nodes = line.split("|");

String src = nodes[0].trim();

String dest = nodes[1].trim();

int start = idToIndex.get(src);

int end = idToIndex.get(dest);

edges[k] = new int[]{start, end};

rates[k] = nodes[2].trim();

k++;

}

boolean isCyclic = checkCyclic(n, edges);

if(isCyclic)

{

System.out.println("cyclic");

}

else

{

System.out.println("noncyclic");

}

}

private static boolean checkCyclic(int n, int[][] edges)

{

if(edges.length != n-1) return false;

List> graph = new ArrayList();

for(int i=0; i

{

graph.add(new ArrayList());

}

for(int[] e : edges)

{

graph.get(e[0]).add(e[1]);

}

Queue q = new LinkedList();

Set seen = new HashSet();

q.add(0);

seen.add(0);

while(!q.isEmpty())

{

int node = q.poll();

for(int nei : graph.get(node))

{

if(seen.contains(nei))

{

continue;

}

seen.add(nei);

q.add(nei);

}

}

return seen.size()

}

public static void main(String[] args) throws IOException

{

InputStreamReader reader = new InputStreamReader(System.in, StandardCharsets.UTF_8);

BufferedReader in = new BufferedReader(reader);

String line;

ArrayList inputLines = new ArrayList();

while ((line = in.readLine()) != null)

{

//System.out.println(line);

inputLines.add(line);

}

Main.checkCycle_getMaxRating(inputLines);

}

}

QUESTION

image text in transcribed

image text in transcribed

image text in transcribed

image text in transcribed

image text in transcribed

ESG Issuer Graph Programming challenge description: ESG Issuer Data is of the form Issuer | Parent | ESG Rating A54365 B34454 | AA B34454 | C34563 A D45747 | B34454 I B E36547 045747 | AAA G34657 | 045747 | CCC H84464 | 034563 | BB 176474 | H84464 | AA C345631 | BBB F346541 | BB J74576 K46565 C K46565 I CC L54334 | 176474 | AA H84464 L54334 | BB Assumptions that can be made: 1. If asked to find a min or max rating, given an issuer, consider all the issuers in the path from the given issuer to the ultimate parent 2. Rating order AAA > AA > A > BBB > BB > B> CCC > CC > C Your assignment: Come up with a Data Structure to hold this type of data. Write an algorithm to check if the relations above are cyclic in nature. (Convert the Data Structure to a directed one assuming the direction is from the Issuer to its parent) Find the issuer with max rating Input: The issuer table, with respective columns: Issuer Parent Esgrating, separated by 0 Output: If relations from the input table are cyclic in nature cyclic or noncyclic issuer with max rating, return None if invalidot applicable max rating, return None if invalidot applicable Test 1 Test Input A54365|B34454 |AA B34454C34563A D45747|B34454|B E36547|D45747|AAA G34657 | D45747 CCC H84464C34563 | BB 176474 | H84464 |AA C34563||BBB F34654||BB J74576|K46565|C K46565||CC L54334|176474|AA H84464|L54334 | BB Expected Output cyclic A54365 AA Test 2 Test Input A54365|B34454CCC B34454C34563|A D45747|B34454|B E36547|D45747|AAA G34657|D45747|CCC H84464C34563 | BB 176474|H84464 |AA C345631 BBB F346541 BB J74576|K46565|C K465651 L54334|176474|AA H84464| | BB ICC Expected Output noncyclic C34563 BBB i import java.io.BufferedReader; 2 import java.io.IOException; 3 import java.io.InputStreamReader; 4 import java.nio.charset. StandardCharsets; 5 /** 7 * The Main class implements an application that reads lines from the standard input * and prints them to the standard output. 6 B 9 14 10 public class Main { 11 /** 12 * Iterate through each line of input. 13 public static void main(String[] args) throws IOException { 15 InputStreamReader reader = new InputStreamReader(System.in, StandardCharsets.UTF_8); 16 BufferedReader in = new BufferedReader (reader); 17 String line; 18 while ((line = in.readLine()) != null) { 19 System.out.println(line); } 21 } 22 } 23 1 20 NN<>

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Oracle 11G SQL

Authors: Joan Casteel

2nd Edition

1133947360, 978-1133947363

More Books

Students also viewed these Databases questions

Question

=+4. Are there areas where you are seeing healing take root?

Answered: 1 week ago

Question

=+2. What are some of your racialized triggers?

Answered: 1 week ago