-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGraph.java
More file actions
140 lines (113 loc) · 3.97 KB
/
Graph.java
File metadata and controls
140 lines (113 loc) · 3.97 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
import java.util.Scanner;
import java.io.BufferedInputStream;
import java.io.File;
import java.io.IOException;
public class Graph {
private static final String CHARSET_NAME = "UTF-8"; // assume Unicode UTF-8 encoding
private Scanner scanner;
private final int V;
private int E;
private Bag<Integer>[] adj;
public Graph() {
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 Graph(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 Graph(int V) {
if (V < 0) throw new IllegalArgumentException("Number of vertices must be nonnegative");
this.V = V;
this.E = 0;
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.
}
}
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.
}
}
/**
* Initializes A new graph that is a deep copy of <tt>G</tt>.
* @param G the graph to copy
*/
public Graph(Graph 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);
}
}
}
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);
E++;
adj[v].add(w);
adj[w].add(v);
}
public Iterable<Integer> adj(int v) {
validateVertex(v);
return adj[v];
}
public int degree(int v) {
validateVertex(v);
return adj[v].size();
}
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(v + ": ");
for (int w : adj[v]) {
s.append(w + " ");
}
s.append(NEWLINE);
}
return s.toString();
}
public static void main(String[] args) {
Graph g = new Graph();
System.out.println(g);
}
}