-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathGraph.java
215 lines (139 loc) · 4.25 KB
/
Graph.java
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedHashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Queue;
import java.util.Set;
import java.util.Stack;
public class Graph {
private Map<Vertex, List<Vertex>> graph = new HashMap<Vertex, List<Vertex>>();
public void addVertex(String nodeName) {
this.graph.putIfAbsent(new Vertex(nodeName), new ArrayList<Vertex>());
}
public void removeVertex(String nodeName) {
Vertex vertex = new Vertex(nodeName);
for (Map.Entry<Vertex, List<Vertex>> itr : this.graph.entrySet()) {
itr.getValue().remove(vertex);
}
this.graph.remove(vertex);
}
public void addEdge(String nodeName1, String nodeName2) {
Vertex vertex1 = new Vertex(nodeName1);
Vertex vertex2 = new Vertex(nodeName2);
this.graph.get(vertex1).add(vertex2);
}
public void removeEdge(String nodeName1, String nodeName2) {
Vertex vertex1 = new Vertex(nodeName1);
Vertex vertex2 = new Vertex(nodeName2);
if (this.graph.get(vertex1) != null) {
this.graph.get(vertex1).remove(vertex2);
}
}
public List<Vertex> getAdjVertices(String nodeName) {
return this.graph.get(new Vertex(nodeName));
}
public void bfs(String rootNode) {
Set<String> visitedNodes = new LinkedHashSet<String>();
Queue<Vertex> queue = new LinkedList<>();
Vertex root = new Vertex(rootNode);
queue.add(root);
while (!queue.isEmpty()) {
Vertex vertex = queue.poll();
visitedNodes.add(vertex.nodeName);
for (Vertex adjNode : getAdjVertices(vertex.nodeName)) {
if (!visitedNodes.contains(adjNode.nodeName)) {
queue.add(adjNode);
}
}
}
System.out.println("BFS : " + visitedNodes);
}
public void dfs(String rootNode) {
Set<String> visitedNodes = new LinkedHashSet<String>();
Stack<Vertex> stack = new Stack<>();
Vertex root = new Vertex(rootNode);
stack.push(root);
while (!stack.isEmpty()) {
Vertex ver = stack.pop();
visitedNodes.add(ver.nodeName);
for (Vertex node : getAdjVertices(ver.nodeName)) {
if (!visitedNodes.contains(node.nodeName)) {
stack.push(node);
}
}
}
System.out.println("DFS : " + visitedNodes);
}
public void seeGraph() {
System.out.println("Graph :");
for (Map.Entry<Vertex, List<Vertex>> itr : this.graph.entrySet()) {
System.out.print("Vertex : " + itr.getKey().nodeName);
System.out.print(" Edge : ");
for (Vertex vertex : itr.getValue()) {
System.out.print(vertex.nodeName + " ");
}
System.out.println();
}
}
public static void main(String[] args) {
Graph graph = new Graph();
System.out.println("Add Vertex : ");
graph.addVertex("A");
graph.addVertex("B");
graph.addVertex("C");
graph.addVertex("D");
graph.addVertex("E");
graph.addVertex("F");
graph.seeGraph();
System.out.println("Remove Vertex : ");
graph.removeVertex("C");
graph.removeVertex("A");
graph.seeGraph();
graph.addVertex("A");
graph.addVertex("C");
graph.addEdge("B", "F");
graph.addEdge("F", "B");
graph.addEdge("F", "A");
graph.addEdge("B", "E");
graph.addEdge("C", "E");
graph.addEdge("E", "B");
graph.addEdge("A", "B");
graph.addEdge("A", "D");
graph.addEdge("A", "C");
graph.addEdge("D", "F");
graph.addEdge("D", "E");
//graph.seeGraph();
//graph.removeEdge("B", "F");
graph.seeGraph();
graph.bfs("A");
graph.dfs("A");
}
}
class Vertex {
String nodeName;
public Vertex(String nodeName) {
this.nodeName = nodeName;
}
@Override
public boolean equals(Object obj) {
if (obj == null) {
return false;
}
if (obj.getClass() != this.getClass()) {
return false;
}
Vertex vertex = (Vertex) obj;
if (this.nodeName != vertex.nodeName) {
return false;
}
return true;
}
@Override
public int hashCode() {
int hashKey = 3;
hashKey = 53 * hashKey + (this.nodeName != null ? this.nodeName.hashCode() : 0);
return hashKey;
}
}