-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDigraph.java
More file actions
executable file
·149 lines (121 loc) · 4.14 KB
/
Digraph.java
File metadata and controls
executable file
·149 lines (121 loc) · 4.14 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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
import java.util.InputMismatchException;
import java.util.NoSuchElementException;
import java.util.Scanner;
import java.io.BufferedInputStream;
import java.io.File;
import java.io.IOException;
public class Digraph {
// assume Unicode UTF-8 encoding
private static final String CHARSET_NAME = "UTF-8";
private Scanner scanner;
private final int V;
private int E;
private Bag<Integer>[] adj;
public Digraph() {
scanner = new Scanner(new BufferedInputStream(System.in), CHARSET_NAME);
this.V = scanner.nextInt(); //Non static final variable must assigned a value in a constructor or
// or with declaration.
if (V < 0) throw new IllegalArgumentException("Number of vertices must be nonnegative");
constructGraph();
}
public Digraph(int V) {
if (V < 0) throw new IllegalArgumentException("Number of vertices in a Digraph must be nonnegative");
this.V = V;
this.E = 0;
adj = (Bag<Integer>[]) new Bag[V];
for (int v = 0; v < V; v++) {
adj[v] = new Bag<Integer>();
}
}
public Digraph(File file) {
try {
scanner = new Scanner(file, CHARSET_NAME);
}
catch (IOException ioe) {
System.err.println("Could not open " + file);
}
this.V = scanner.nextInt(); // read nr of vertices. Must do it here since V is declared final!
if (V < 0) throw new IllegalArgumentException("Number of vertices must be nonnegative");
constructGraph();
}
public Digraph(Digraph G) {
this(G.V());
this.E = G.E();
for (int v = 0; v < G.V(); v++) {
// reverse so that adjacency list is in same order as original
Stack<Integer> reverse = new Stack<Integer>();
for (int w : G.adj[v]) {
reverse.push(w);
}
for (int w : reverse) {
adj[v].add(w);
}
}
}
private void constructGraph() {
this.E = scanner.nextInt(); // read nr of edges.
if (E < 0) throw new IllegalArgumentException("Number of edges must be nonnegative");
adj = (Bag<Integer>[]) new Bag[V]; // Create array of lists.
for (int v = 0; v < V; v++) { // Initialize all lists
adj[v] = new Bag<Integer>(); // to empty.
}
int nrEdges = E();
for (int i = 0; i < nrEdges; i++) {
int v = scanner.nextInt(); // Read a vertex,
int w = scanner.nextInt(); // read another vertex,
addEdge(v, w); // and add edge connecting them.
}
}
public int V() {
return V;
}
public int E() {
return E;
}
// throw an IndexOutOfBoundsException unless 0 <= v < V
private void validateVertex(int v) {
if (v < 0 || v >= V)
throw new IndexOutOfBoundsException("vertex " + v + " is not between 0 and " + (V-1));
}
public void addEdge(int v, int w) {
validateVertex(v);
validateVertex(w);
adj[v].add(w);
E++;
}
public Iterable<Integer> adj(int v) {
validateVertex(v);
return adj[v];
}
public int outdegree(int v) {
validateVertex(v);
return adj[v].size();
}
public Digraph reverse() {
Digraph R = new Digraph(V);
for (int v = 0; v < V; v++) {
for (int w : adj(v)) {
R.addEdge(w, v);
}
}
return R;
}
public String toString() {
StringBuilder s = new StringBuilder();
String NEWLINE = System.getProperty("line.separator");
s.append(V + " vertices, " + E + " edges " + NEWLINE);
for (int v = 0; v < V; v++) {
s.append(String.format("%d: ", v));
for (int w : adj[v]) {
s.append(String.format("%d ", w));
}
s.append(NEWLINE);
}
return s.toString();
}
public static void main(String[] args) {
File file = new File(args[0]);
Digraph G = new Digraph(file);
System.out.println(G);
}
}