-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathGraph.java
More file actions
63 lines (51 loc) · 1.88 KB
/
Graph.java
File metadata and controls
63 lines (51 loc) · 1.88 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
// Java program to print breadthFirstSearch traversal from a given source
// vertex. breadthFirstSearch(int s) traverses vertices reachable from s.
import java.io.*;
import java.util.*;
import java.util.HashMap;
// This class represents a directed graph using adjacency
// list representation
class Graph {
private int V; // No. of vertices
private LinkedList<Integer> adj[]; // Adjacency Lists
// Constructor
Graph(int v)
{
V = v;
adj = new LinkedList[v];
for (int i = 0; i < v; ++i)
adj[i] = new LinkedList<Integer>();
}
// Function to add an edge into the graph
void addEdge(int v, int w) { adj[v].add(w); }
int[] getReachableNodes(int start, int distance){
return null;
}
// Driver method to
public static void main(String args[])
{
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));
try {
String[] params = bufferedReader.readLine().replaceAll("\\s+$", "").split(" ");
String[] firstMultipleInput = bufferedReader.readLine().replaceAll("\\s+$", "").split(" ");
int start = Integer.parseInt(params[0]);
int distance = Integer.parseInt(params[1]);
int n = Integer.parseInt(firstMultipleInput[0]);
int m = Integer.parseInt(firstMultipleInput[1]);
Graph graph = new Graph(n);
List<List<Integer>> edges = new ArrayList<>();
for (int i = 0; i < m; i++){
try {
String[] edge = bufferedReader.readLine().replaceAll("\\s+$", "").split(" ");
graph.addEdge(Integer.parseInt(edge[0]), Integer.parseInt(edge[1]));
} catch (IOException ex) {
throw new RuntimeException(ex);
}
}
int[] reachableNodes = graph.getReachableNodes(start, distance);
}
catch (IOException ex) {
throw new RuntimeException(ex);
}
}
}