Here you can find the java code and photo of challenge: import java.io.*; import java.math.*; import java.security.*; import java.text.*; import java.util.*; import java.util.concurrent.*; import...


Here you can find the java code and photo of challenge:



import java.io.*;
import java.math.*;
import java.security.*;
import java.text.*;
import java.util.*;
import java.util.concurrent.*;
import java.util.function.*;
import java.util.regex.*;
import java.util.stream.*;


public class Solution {


public static class DirectedGraph {
/* Adjacency List representation of the given graph */
private Map> adjList = new HashMap>();


public String toString() {
StringBuffer s = new StringBuffer();
for (Integer v : adjList.keySet())
s.append("\n " + v + " -> " + adjList.get(v));
return s.toString();
}


public void add(Integer vertex) {
if (adjList.containsKey(vertex))
return;
adjList.put(vertex, new ArrayList());
}


public void add(Integer source, Integer dest) {
add(source);
add(dest);
adjList.get(source).add(dest);
}


/* Indegree of each vertex as a Map */
public Map inDegree() {
Map result = new HashMap();
for (Integer v : adjList.keySet())
result.put(v, 0);
for (Integer from : adjList.keySet()) {
for (Integer to : adjList.get(from)) {
result.put(to, result.get(to) + 1);
}
}
return result;
}


public Map outDegree () {
Map result = new HashMap();
for (Integer v: adjList.keySet()) result.put(v, adjList.get(v).size());
return result;
}


}


// Complete the isDAG function below.
public static boolean isDag(DirectedGraph digraph) {


}




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


BufferedWriter bufferredWriter = new BufferedWriter(new FileWriter(System.getenv("OUTPUT_PATH")));


BufferedReader bufferredReader = new BufferedReader(new InputStreamReader(System.in));


DirectedGraph digraph = new DirectedGraph();


String line;
while ((line = bufferredReader.readLine()) != null) {
String[] v = line.split(" ");
digraph.add(Integer.parseInt(v[0]), Integer.parseInt(v[1]));
}


bufferredWriter.write((isDag(digraph) ? "1" : "0"));


bufferredReader.close();
bufferredWriter.close();
}
}


Given a directed graph as an adjacency list, you need to determine whether it is acyclic or not.<br>Input Format<br>• Each of the lines in the input contains two space-separated integers, sand d, that represents an edge from<br>the node sto d.<br>Constraints<br>Node numbers are 0 based, i.e. if there n nodes, the nodes are numnered as 0,1,2.,n-1.<br>Output Format<br>The output of the program is 0 or 1, depending on the result of the isDag() function. If it returns TRUE, output<br>is 1, otherwise it is 0.<br>Sample Input 0<br>1 2<br>13<br>2 3<br>2 4<br>4 5<br>5 6<br>Sample Output 0<br>1<br>Explanation 0<br>The example graph in this example is<br>(1<br>3<br>Since there is no cycle in the graph, the result is 1<br>4.<br>2.<br>

Extracted text: Given a directed graph as an adjacency list, you need to determine whether it is acyclic or not. Input Format • Each of the lines in the input contains two space-separated integers, sand d, that represents an edge from the node sto d. Constraints Node numbers are 0 based, i.e. if there n nodes, the nodes are numnered as 0,1,2.,n-1. Output Format The output of the program is 0 or 1, depending on the result of the isDag() function. If it returns TRUE, output is 1, otherwise it is 0. Sample Input 0 1 2 13 2 3 2 4 4 5 5 6 Sample Output 0 1 Explanation 0 The example graph in this example is (1 3 Since there is no cycle in the graph, the result is 1 4. 2.
Jun 11, 2022
SOLUTION.PDF

Get Answer To This Question

Related Questions & Answers

More Questions »

Submit New Assignment

Copy and Paste Your Assignment Here