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(); } }

Computer Networking: A Top-Down Approach (7th Edition)
7th Edition
ISBN:9780133594140
Author:James Kurose, Keith Ross
Publisher:James Kurose, Keith Ross
Chapter1: Computer Networks And The Internet
Section: Chapter Questions
Problem R1RQ: What is the difference between a host and an end system? List several different types of end...
icon
Related questions
Question

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<Integer, List<Integer>> adjList = new HashMap<Integer, List<Integer>>();

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<Integer>());
}

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

/* Indegree of each vertex as a Map<Vertex, IndegreeValue> */
public Map<Integer, Integer> inDegree() {
Map<Integer, Integer> result = new HashMap<Integer, Integer>();
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<Integer,Integer> outDegree () {
Map<Integer,Integer> result = new HashMap<Integer,Integer>();
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.
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.
Transcribed Image 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.
Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 2 steps

Blurred answer
Recommended textbooks for you
Computer Networking: A Top-Down Approach (7th Edi…
Computer Networking: A Top-Down Approach (7th Edi…
Computer Engineering
ISBN:
9780133594140
Author:
James Kurose, Keith Ross
Publisher:
PEARSON
Computer Organization and Design MIPS Edition, Fi…
Computer Organization and Design MIPS Edition, Fi…
Computer Engineering
ISBN:
9780124077263
Author:
David A. Patterson, John L. Hennessy
Publisher:
Elsevier Science
Network+ Guide to Networks (MindTap Course List)
Network+ Guide to Networks (MindTap Course List)
Computer Engineering
ISBN:
9781337569330
Author:
Jill West, Tamara Dean, Jean Andrews
Publisher:
Cengage Learning
Concepts of Database Management
Concepts of Database Management
Computer Engineering
ISBN:
9781337093422
Author:
Joy L. Starks, Philip J. Pratt, Mary Z. Last
Publisher:
Cengage Learning
Prelude to Programming
Prelude to Programming
Computer Engineering
ISBN:
9780133750423
Author:
VENIT, Stewart
Publisher:
Pearson Education
Sc Business Data Communications and Networking, T…
Sc Business Data Communications and Networking, T…
Computer Engineering
ISBN:
9781119368830
Author:
FITZGERALD
Publisher:
WILEY