|
| 1 | +package gvisual; |
| 2 | + |
| 3 | +import edu.uci.ics.jung.graph.Graph; |
| 4 | +import java.util.*; |
| 5 | + |
| 6 | +/** |
| 7 | + * Shared graph traversal and adjacency utilities used by multiple analyzers. |
| 8 | + * |
| 9 | + * <p>Centralizes common operations that were previously duplicated across |
| 10 | + * {@link CommunityDetector}, {@link GraphDiameterAnalyzer}, |
| 11 | + * {@link NodeCentralityAnalyzer}, {@link PageRankAnalyzer}, |
| 12 | + * {@link ShortestPathFinder}, {@link LinkPredictionAnalyzer}, and |
| 13 | + * {@link GraphIsomorphismAnalyzer}.</p> |
| 14 | + * |
| 15 | + * <p>All methods are static and stateless — safe for concurrent use.</p> |
| 16 | + * |
| 17 | + * @author zalenix |
| 18 | + */ |
| 19 | +public final class GraphUtils { |
| 20 | + |
| 21 | + private GraphUtils() { /* utility class */ } |
| 22 | + |
| 23 | + /** |
| 24 | + * Returns the vertex at the other end of an edge from the given vertex. |
| 25 | + * |
| 26 | + * @param e the edge |
| 27 | + * @param current the vertex we're "standing on" |
| 28 | + * @return the other endpoint, or {@code null} if {@code current} is not |
| 29 | + * an endpoint of the edge |
| 30 | + */ |
| 31 | + public static String getOtherEnd(edge e, String current) { |
| 32 | + String v1 = e.getVertex1(); |
| 33 | + String v2 = e.getVertex2(); |
| 34 | + if (current.equals(v1)) return v2; |
| 35 | + if (current.equals(v2)) return v1; |
| 36 | + return null; |
| 37 | + } |
| 38 | + |
| 39 | + /** |
| 40 | + * Builds an adjacency set map for all vertices in the graph. |
| 41 | + * |
| 42 | + * @param graph the JUNG graph |
| 43 | + * @return map from each vertex to its set of neighbor vertex IDs |
| 44 | + */ |
| 45 | + public static Map<String, Set<String>> buildAdjacencyMap( |
| 46 | + Graph<String, edge> graph) { |
| 47 | + Map<String, Set<String>> adj = new HashMap<String, Set<String>>(); |
| 48 | + for (String v : graph.getVertices()) { |
| 49 | + Set<String> neighbors = new HashSet<String>(); |
| 50 | + Collection<String> graphNeighbors = graph.getNeighbors(v); |
| 51 | + if (graphNeighbors != null) { |
| 52 | + neighbors.addAll(graphNeighbors); |
| 53 | + } |
| 54 | + adj.put(v, neighbors); |
| 55 | + } |
| 56 | + return adj; |
| 57 | + } |
| 58 | + |
| 59 | + /** |
| 60 | + * BFS from a source vertex, returning distances (hop counts) to all |
| 61 | + * reachable vertices. |
| 62 | + * |
| 63 | + * @param graph the JUNG graph |
| 64 | + * @param source the starting vertex |
| 65 | + * @return map from vertex ID to its BFS distance from source |
| 66 | + */ |
| 67 | + public static Map<String, Integer> bfsDistances( |
| 68 | + Graph<String, edge> graph, String source) { |
| 69 | + Map<String, Integer> distances = new HashMap<String, Integer>(); |
| 70 | + Queue<String> queue = new LinkedList<String>(); |
| 71 | + distances.put(source, 0); |
| 72 | + queue.add(source); |
| 73 | + |
| 74 | + while (!queue.isEmpty()) { |
| 75 | + String current = queue.poll(); |
| 76 | + int currentDist = distances.get(current); |
| 77 | + for (edge e : graph.getIncidentEdges(current)) { |
| 78 | + String neighbor = getOtherEnd(e, current); |
| 79 | + if (neighbor != null && !distances.containsKey(neighbor)) { |
| 80 | + distances.put(neighbor, currentDist + 1); |
| 81 | + queue.add(neighbor); |
| 82 | + } |
| 83 | + } |
| 84 | + } |
| 85 | + return distances; |
| 86 | + } |
| 87 | + |
| 88 | + /** |
| 89 | + * BFS to discover the connected component containing a source vertex. |
| 90 | + * |
| 91 | + * @param graph the JUNG graph |
| 92 | + * @param source the starting vertex |
| 93 | + * @return set of all vertices reachable from source (including source) |
| 94 | + */ |
| 95 | + public static Set<String> bfsComponent( |
| 96 | + Graph<String, edge> graph, String source) { |
| 97 | + Set<String> component = new LinkedHashSet<String>(); |
| 98 | + Queue<String> queue = new LinkedList<String>(); |
| 99 | + component.add(source); |
| 100 | + queue.add(source); |
| 101 | + |
| 102 | + while (!queue.isEmpty()) { |
| 103 | + String current = queue.poll(); |
| 104 | + for (edge e : graph.getIncidentEdges(current)) { |
| 105 | + String neighbor = getOtherEnd(e, current); |
| 106 | + if (neighbor != null && !component.contains(neighbor)) { |
| 107 | + component.add(neighbor); |
| 108 | + queue.add(neighbor); |
| 109 | + } |
| 110 | + } |
| 111 | + } |
| 112 | + return component; |
| 113 | + } |
| 114 | + |
| 115 | + /** |
| 116 | + * Finds all connected components of the graph. |
| 117 | + * |
| 118 | + * @param graph the JUNG graph |
| 119 | + * @return list of components, each a set of vertex IDs, sorted largest-first |
| 120 | + */ |
| 121 | + public static List<Set<String>> findComponents( |
| 122 | + Graph<String, edge> graph) { |
| 123 | + Set<String> visited = new HashSet<String>(); |
| 124 | + List<Set<String>> components = new ArrayList<Set<String>>(); |
| 125 | + |
| 126 | + for (String vertex : graph.getVertices()) { |
| 127 | + if (!visited.contains(vertex)) { |
| 128 | + Set<String> component = bfsComponent(graph, vertex); |
| 129 | + visited.addAll(component); |
| 130 | + components.add(component); |
| 131 | + } |
| 132 | + } |
| 133 | + |
| 134 | + // Sort largest-first |
| 135 | + Collections.sort(components, new Comparator<Set<String>>() { |
| 136 | + public int compare(Set<String> a, Set<String> b) { |
| 137 | + return Integer.compare(b.size(), a.size()); |
| 138 | + } |
| 139 | + }); |
| 140 | + return components; |
| 141 | + } |
| 142 | + |
| 143 | + /** |
| 144 | + * Finds the largest connected component. |
| 145 | + * |
| 146 | + * @param graph the JUNG graph |
| 147 | + * @return set of vertices in the largest component, or empty set if graph is empty |
| 148 | + */ |
| 149 | + public static Set<String> findLargestComponent( |
| 150 | + Graph<String, edge> graph) { |
| 151 | + List<Set<String>> components = findComponents(graph); |
| 152 | + return components.isEmpty() ? Collections.<String>emptySet() : components.get(0); |
| 153 | + } |
| 154 | + |
| 155 | + /** |
| 156 | + * Returns the set of common neighbors between two vertices. |
| 157 | + * |
| 158 | + * @param adjacency precomputed adjacency map |
| 159 | + * @param u first vertex |
| 160 | + * @param v second vertex |
| 161 | + * @return set of vertices adjacent to both u and v |
| 162 | + */ |
| 163 | + public static Set<String> getCommonNeighbors( |
| 164 | + Map<String, Set<String>> adjacency, String u, String v) { |
| 165 | + Set<String> common = new HashSet<String>(adjacency.get(u)); |
| 166 | + common.retainAll(adjacency.get(v)); |
| 167 | + return common; |
| 168 | + } |
| 169 | +} |
0 commit comments