766
|
1 |
/*
|
|
2 |
* Copyright 2008 Sun Microsystems, Inc. All Rights Reserved.
|
|
3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
|
4 |
*
|
|
5 |
* This code is free software; you can redistribute it and/or modify it
|
|
6 |
* under the terms of the GNU General Public License version 2 only, as
|
|
7 |
* published by the Free Software Foundation.
|
|
8 |
*
|
|
9 |
* This code is distributed in the hope that it will be useful, but WITHOUT
|
|
10 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
11 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
|
12 |
* version 2 for more details (a copy is included in the LICENSE file that
|
|
13 |
* accompanied this code).
|
|
14 |
*
|
|
15 |
* You should have received a copy of the GNU General Public License version
|
|
16 |
* 2 along with this work; if not, write to the Free Software Foundation,
|
|
17 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
18 |
*
|
|
19 |
* Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
|
|
20 |
* CA 95054 USA or visit www.sun.com if you need additional information or
|
|
21 |
* have any questions.
|
|
22 |
*
|
|
23 |
*/
|
|
24 |
package com.sun.hotspot.igv.hierarchicallayout;
|
|
25 |
|
|
26 |
import java.util.ArrayList;
|
|
27 |
import java.util.Collection;
|
|
28 |
import java.util.Collections;
|
1497
|
29 |
import java.util.HashMap;
|
766
|
30 |
import java.util.LinkedList;
|
|
31 |
import java.util.List;
|
|
32 |
import java.util.Queue;
|
|
33 |
|
|
34 |
/**
|
|
35 |
*
|
|
36 |
* @author Thomas Wuerthinger
|
|
37 |
*/
|
|
38 |
public class Graph<N, E> {
|
|
39 |
|
1497
|
40 |
private HashMap<Object, Node<N, E>> nodes;
|
|
41 |
private HashMap<Object, Edge<N, E>> edges;
|
766
|
42 |
private List<Node<N, E>> nodeList;
|
|
43 |
|
|
44 |
public Graph() {
|
1497
|
45 |
nodes = new HashMap<Object, Node<N, E>>();
|
|
46 |
edges = new HashMap<Object, Edge<N, E>>();
|
766
|
47 |
nodeList = new ArrayList<Node<N, E>>();
|
|
48 |
}
|
|
49 |
|
|
50 |
public Node<N, E> createNode(N data, Object key) {
|
|
51 |
Node<N, E> n = new Node<N, E>(this, data);
|
|
52 |
assert key == null || !nodes.containsKey(key);
|
|
53 |
if (key != null) {
|
|
54 |
nodes.put(key, n);
|
|
55 |
}
|
|
56 |
nodeList.add(n);
|
|
57 |
return n;
|
|
58 |
}
|
|
59 |
|
|
60 |
public Edge<N, E> createEdge(Node<N, E> source, Node<N, E> dest, E data, Object key) {
|
|
61 |
Edge<N, E> e = new Edge<N, E>(this, source, dest, data);
|
|
62 |
source.addOutEdge(e);
|
|
63 |
dest.addInEdge(e);
|
|
64 |
if (key != null) {
|
|
65 |
edges.put(key, e);
|
|
66 |
}
|
|
67 |
return e;
|
|
68 |
}
|
|
69 |
|
|
70 |
public Node<N, E> getNode(Object key) {
|
|
71 |
return nodes.get(key);
|
|
72 |
}
|
|
73 |
|
|
74 |
public Edge<N, E> getEdge(Object key) {
|
|
75 |
return edges.get(key);
|
|
76 |
}
|
|
77 |
|
|
78 |
public Collection<Edge<N, E>> getEdges() {
|
|
79 |
return Collections.unmodifiableCollection(edges.values());
|
|
80 |
}
|
|
81 |
|
|
82 |
public Collection<Node<N, E>> getNodes() {
|
|
83 |
return Collections.unmodifiableList(nodeList);
|
|
84 |
}
|
|
85 |
|
|
86 |
public void removeEdge(Edge<N, E> e, Object key) {
|
|
87 |
assert key == null || edges.containsKey(key);
|
|
88 |
if (key != null) {
|
|
89 |
edges.remove(key);
|
|
90 |
}
|
|
91 |
e.getSource().removeOutEdge(e);
|
|
92 |
e.getDest().removeInEdge(e);
|
|
93 |
}
|
|
94 |
|
|
95 |
public class DFSTraversalVisitor {
|
|
96 |
|
|
97 |
public void visitNode(Node<N, E> n) {
|
|
98 |
}
|
|
99 |
|
|
100 |
public boolean visitEdge(Edge<N, E> e, boolean backEdge) {
|
|
101 |
return true;
|
|
102 |
}
|
|
103 |
}
|
|
104 |
|
|
105 |
public class BFSTraversalVisitor {
|
|
106 |
|
|
107 |
public void visitNode(Node<N, E> n, int depth) {
|
|
108 |
}
|
|
109 |
}
|
|
110 |
|
|
111 |
public List<Node<N, E>> getNodesWithInDegree(int x) {
|
|
112 |
return getNodesWithInDegree(x, true);
|
|
113 |
}
|
|
114 |
|
|
115 |
public List<Node<N, E>> getNodesWithInDegree(int x, boolean countSelfLoops) {
|
|
116 |
|
|
117 |
List<Node<N, E>> result = new ArrayList<Node<N, E>>();
|
|
118 |
for (Node<N, E> n : getNodes()) {
|
|
119 |
if (n.getInDegree(countSelfLoops) == x) {
|
|
120 |
result.add(n);
|
|
121 |
}
|
|
122 |
}
|
|
123 |
|
|
124 |
return result;
|
|
125 |
|
|
126 |
}
|
|
127 |
|
|
128 |
private void markReachable(Node<N, E> startingNode) {
|
|
129 |
ArrayList<Node<N, E>> arr = new ArrayList<Node<N, E>>();
|
|
130 |
arr.add(startingNode);
|
|
131 |
for (Node<N, E> n : getNodes()) {
|
|
132 |
n.setReachable(false);
|
|
133 |
}
|
|
134 |
traverseDFS(arr, new DFSTraversalVisitor() {
|
|
135 |
|
|
136 |
@Override
|
|
137 |
public void visitNode(Node<N, E> n) {
|
|
138 |
n.setReachable(true);
|
|
139 |
}
|
|
140 |
});
|
|
141 |
}
|
|
142 |
|
|
143 |
public void traverseBFS(Node<N, E> startingNode, BFSTraversalVisitor tv, boolean longestPath) {
|
|
144 |
|
|
145 |
if (longestPath) {
|
|
146 |
markReachable(startingNode);
|
|
147 |
}
|
|
148 |
|
|
149 |
for (Node<N, E> n : getNodes()) {
|
|
150 |
n.setVisited(false);
|
|
151 |
n.setActive(false);
|
|
152 |
}
|
|
153 |
|
|
154 |
Queue<Node<N, E>> queue = new LinkedList<Node<N, E>>();
|
|
155 |
queue.add(startingNode);
|
|
156 |
startingNode.setVisited(true);
|
|
157 |
int layer = 0;
|
|
158 |
Node<N, E> lastOfLayer = startingNode;
|
|
159 |
Node<N, E> lastAdded = null;
|
|
160 |
|
|
161 |
while (!queue.isEmpty()) {
|
|
162 |
|
|
163 |
Node<N, E> current = queue.poll();
|
|
164 |
tv.visitNode(current, layer);
|
|
165 |
current.setActive(false);
|
|
166 |
|
|
167 |
|
|
168 |
for (Edge<N, E> e : current.getOutEdges()) {
|
|
169 |
if (!e.getDest().isVisited()) {
|
|
170 |
|
|
171 |
boolean allow = true;
|
|
172 |
if (longestPath) {
|
|
173 |
for (Node<N, E> pred : e.getDest().getPredecessors()) {
|
|
174 |
if ((!pred.isVisited() || pred.isActive()) && pred.isReachable()) {
|
|
175 |
allow = false;
|
|
176 |
break;
|
|
177 |
}
|
|
178 |
}
|
|
179 |
}
|
|
180 |
|
|
181 |
if (allow) {
|
|
182 |
queue.offer(e.getDest());
|
|
183 |
lastAdded = e.getDest();
|
|
184 |
e.getDest().setVisited(true);
|
|
185 |
e.getDest().setActive(true);
|
|
186 |
}
|
|
187 |
}
|
|
188 |
}
|
|
189 |
|
|
190 |
if (current == lastOfLayer && !queue.isEmpty()) {
|
|
191 |
lastOfLayer = lastAdded;
|
|
192 |
layer++;
|
|
193 |
}
|
|
194 |
}
|
|
195 |
}
|
|
196 |
|
|
197 |
public void traverseDFS(DFSTraversalVisitor tv) {
|
|
198 |
traverseDFS(getNodes(), tv);
|
|
199 |
}
|
|
200 |
|
|
201 |
public void traverseDFS(Collection<Node<N, E>> startingNodes, DFSTraversalVisitor tv) {
|
|
202 |
|
|
203 |
for (Node<N, E> n : getNodes()) {
|
|
204 |
n.setVisited(false);
|
|
205 |
n.setActive(false);
|
|
206 |
}
|
|
207 |
|
|
208 |
boolean result = false;
|
|
209 |
for (Node<N, E> n : startingNodes) {
|
|
210 |
traverse(tv, n);
|
|
211 |
}
|
|
212 |
}
|
|
213 |
|
|
214 |
private void traverse(DFSTraversalVisitor tv, Node<N, E> n) {
|
|
215 |
|
|
216 |
if (!n.isVisited()) {
|
|
217 |
n.setVisited(true);
|
|
218 |
n.setActive(true);
|
|
219 |
tv.visitNode(n);
|
|
220 |
|
|
221 |
for (Edge<N, E> e : n.getOutEdges()) {
|
|
222 |
|
|
223 |
Node<N, E> next = e.getDest();
|
|
224 |
if (next.isActive()) {
|
|
225 |
tv.visitEdge(e, true);
|
|
226 |
} else {
|
|
227 |
if (tv.visitEdge(e, false)) {
|
|
228 |
traverse(tv, next);
|
|
229 |
}
|
|
230 |
}
|
|
231 |
}
|
|
232 |
|
|
233 |
n.setActive(false);
|
|
234 |
}
|
|
235 |
|
|
236 |
}
|
|
237 |
|
|
238 |
public boolean hasCycles() {
|
|
239 |
|
|
240 |
for (Node<N, E> n : getNodes()) {
|
|
241 |
n.setVisited(false);
|
|
242 |
n.setActive(false);
|
|
243 |
}
|
|
244 |
|
|
245 |
boolean result = false;
|
|
246 |
for (Node<N, E> n : getNodes()) {
|
|
247 |
result |= checkCycles(n);
|
|
248 |
if (result) {
|
|
249 |
break;
|
|
250 |
}
|
|
251 |
}
|
|
252 |
return result;
|
|
253 |
}
|
|
254 |
|
|
255 |
private boolean checkCycles(Node<N, E> n) {
|
|
256 |
|
|
257 |
if (n.isActive()) {
|
|
258 |
return true;
|
|
259 |
}
|
|
260 |
|
|
261 |
if (!n.isVisited()) {
|
|
262 |
|
|
263 |
n.setVisited(true);
|
|
264 |
n.setActive(true);
|
|
265 |
|
|
266 |
for (Node<N, E> succ : n.getSuccessors()) {
|
|
267 |
if (checkCycles(succ)) {
|
|
268 |
return true;
|
|
269 |
}
|
|
270 |
}
|
|
271 |
|
|
272 |
n.setActive(false);
|
|
273 |
|
|
274 |
}
|
|
275 |
|
|
276 |
return false;
|
|
277 |
}
|
|
278 |
|
|
279 |
@Override
|
|
280 |
public String toString() {
|
|
281 |
|
|
282 |
StringBuilder s = new StringBuilder();
|
|
283 |
s.append("Nodes: ");
|
|
284 |
for (Node<N, E> n : getNodes()) {
|
|
285 |
s.append(n.toString());
|
|
286 |
s.append("\n");
|
|
287 |
}
|
|
288 |
|
|
289 |
s.append("Edges: ");
|
|
290 |
|
|
291 |
for (Edge<N, E> e : getEdges()) {
|
|
292 |
s.append(e.toString());
|
|
293 |
s.append("\n");
|
|
294 |
}
|
|
295 |
|
|
296 |
return s.toString();
|
|
297 |
}
|
|
298 |
}
|