-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathGraph.java
More file actions
132 lines (114 loc) · 3.05 KB
/
Graph.java
File metadata and controls
132 lines (114 loc) · 3.05 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
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
/**
*
* @author asus
*/
public class Graph {
// symbol table: key = string vertex, value = set of neighboring vertices
private ST<String, SET<String>> st;
// number of edges
private int E;
/**
* Create an empty graph with no vertices or edges.
*/
public Graph() {
st = new ST<String, SET<String>>();
}
/**
* Create an graph from given input stream using given delimiter.
*/
public Graph(In in, String delimiter) {
st = new ST<String, SET<String>>();
while (in.hasNextLine()) {
String line = in.readLine();
String[] names = line.split(delimiter);
for (int i = 1; i < names.length; i++) {
addEdge(names[0], names[i]);
}
}
}
/**
* Number of vertices.
*/
public int V() {
return st.size();
}
/**
* Number of edges.
*/
public int E() {
return E;
}
// throw an exception if v is not a vertex
private void validateVertex(String v) {
if (!hasVertex(v)) throw new IllegalArgumentException(v + " is not a vertex");
}
/**
* Degree of this vertex.
*/
public int degree(String v) {
validateVertex(v);
return st.get(v).size();
}
/**
* Add edge v-w to this graph (if it is not already an edge)
*/
public void addEdge(String v, String w) {
if (!hasVertex(v)) addVertex(v);
if (!hasVertex(w)) addVertex(w);
if (!hasEdge(v, w)) E++;
st.get(v).add(w);
st.get(w).add(v);
}
/**
* Add vertex v to this graph (if it is not already a vertex)
*/
public void addVertex(String v) {
if (!hasVertex(v)) st.put(v, new SET<String>());
}
/**
* Return the set of vertices as an Iterable.
*/
public Iterable<String> vertices() {
return st;
}
/**
* Return the set of neighbors of vertex v as in Iterable.
*/
public Iterable<String> adjacentTo(String v) {
validateVertex(v);
return st.get(v);
}
/**
* Is v a vertex in this graph?
*/
public boolean hasVertex(String v) {
return st.contains(v);
}
/**
* Is v-w an edge in this graph?
*/
public boolean hasEdge(String v, String w) {
validateVertex(v);
validateVertex(w);
return st.get(v).contains(w);
}
/**
* Return a string representation of the graph.
*/
public String toString() {
StringBuilder s = new StringBuilder();
for (String v : st) {
s.append(v + ": ");
for (String w : st.get(v)) {
s.append(w + " ");
}
s.append("\n");
}
return s.toString();
}
}