Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

*Please wirte in java 8* Directed Acyclic Graphs Given a directed graph as an adjacency list, you need to determine whether it is acyclic or

*Please wirte in java 8*

Directed Acyclic Graphs

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, s and d, that represents an edge from the node s to 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

0 1 0 2 0 3 1 2 1 3 2 3 2 4 4 5 5 6 

Sample Output 0

1 

Explanation 0

The example graph in this example is

image text in transcribed

Since there is no cycle in the graph, the result is 1

image text in transcribedimage text in transcribed

1 0 N 2 4 4. 5 6 6 3 3 13 14 1 import java.io.*; 2 import java.math.*; 3 import java.security.*; 4 import java.text.*; 5 import java.util.*; 6 import java.util.concurrent.*; 7 import java.util. function.*; 8 import java.util.regex.*; 9 import java.util.stream.*; 10 11 public class Solution { 12 public static class DirectedGraph { /* Adjacency List representation of the given graph */ 15 private Map> adjlist = new HashMap>(); 16 17 public String toString({ 18 StringBuffer s = new StringBuffer(); 19 for (Integer v: adjlist.keySet() 20 s.append(" " + V + " -> " + adjlist.get()); 21 return s.toString(); 22 } 23 24 public void add(Integer vertex) { 25 if (adj list.containsKey(vertex)) 26 return; 27 adjlist.put(vertex, new ArrayList(); 28 } 29 30 public void add(Integer source, Integer dest) { 31 add (source); 32 add(dest); 33 adjlist.get (source).add(dest); 34 } 35 36 /* Indegree of each vertex as a Map */ 37 public Map inDegree() { 38 Map result = new HashMap(); 39 for (Integer v : adjlist.keySet) 40 result.put(v, 0); 41 for (Integer from : adjlist.keySet() { 42 for (Integer to : adjlist.get(from)) { 43 result.put(to, result.get(to) + 1); 44 } 45 } 46 return result; 47 } 48 49 public Map outDegree () { 50 Map result = new HashMap(); for (Integer v: adjlist.keySet()) result.put(v, adjList.get(v).size()); return result; } nm m m m m m m m UNO NON public Map outDegree () { Map result = new HashMap(); for (Integer v: adj list.keySet()) result.put(v, adj list.get(v).size(); return result; } } // Complete the isDAG function below. public static boolean isDag (DirectedGraph digraph) { } 48 49 50 51 52 53 54 3 55 56 57 57 59 58 59 60 ou 61 62 62 63 63 64 64 65 65 66 66. 67 68. 69 70 71 72 72 73 72 74 74 75 75 76 77 78 79 80 81 82 } 83 public static void main(String[] args) throws IOException { BufferedWriter bufferredWriter = new Bufferedwriter(new FileWriter(System.getenv ("OUTPUT_PATH")); Buffered Reader bufferred Reader = new BufferedReader(new InputStreamReader(System.in); DirectedGraph digraph = new DirectedGraph(); String line; while ((line = bufferred Reader.readLine()) != null) { String[] v = line.split(" "); digraph.add(Integer.parseInt(v[0]), Integer.parseInt(v[1])); } bufferredWriter.write((isDag(digraph) ? "1" : "O")); bufferredReader.close(); bufferredwriter.close(); }

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_2

Step: 3

blur-text-image_step3

Ace Your Homework with AI

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

Get Started

Students also viewed these Databases questions

Question

How is triple-Des backward compatible?

Answered: 1 week ago

Question

Comment as you serve the customer.

Answered: 1 week ago

Question

Describe Balor method and give the chemical reaction.

Answered: 1 week ago