Repository navigation
Expand file tree
/
Copy pathGraphTemplate.java
More file actions
76 lines (62 loc) · 1.72 KB
/
Copy pathGraphTemplate.java
File metadata and controls
76 lines (62 loc) · 1.72 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
package Graph;
import java.util.*;
/**
* Algorithm: Basic Graph Template (Unweighted)
* Time Complexity: O(V + E) for most operations
* Space Complexity: O(V + E)
* Use case: Foundation for all graph algorithms using adjacency list
*/
public class GraphTemplate {
// Constants
static final int MAXN = 100005;
// Data structures
List<Integer>[] adj;
int n; // number of nodes
boolean directed;
// Constructor
@SuppressWarnings("unchecked")
public GraphTemplate(int n, boolean directed) {
this.n = n;
this.directed = directed;
adj = new ArrayList[n + 1];
for (int i = 0; i <= n; i++) {
adj[i] = new ArrayList<>();
}
}
// Add edge method
void addEdge(int u, int v) {
adj[u].add(v);
if (!directed) {
adj[v].add(u);
}
}
// Get neighbors
List<Integer> getNeighbors(int u) {
return adj[u];
}
// Print graph
void printGraph() {
for (int i = 1; i <= n; i++) {
System.out.print(i + " -> ");
for (int v : adj[i]) {
System.out.print(v + " ");
}
System.out.println();
}
}
// Main method with test case
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
// TODO: Implement your test case here
int n = sc.nextInt(); // number of nodes
int m = sc.nextInt(); // number of edges
GraphTemplate graph = new GraphTemplate(n, false);
for (int i = 0; i < m; i++) {
int u = sc.nextInt();
int v = sc.nextInt();
graph.addEdge(u, v);
}
graph.printGraph();
sc.close();
}
}