-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGraphDemo.java
More file actions
35 lines (31 loc) · 1.03 KB
/
Copy pathGraphDemo.java
File metadata and controls
35 lines (31 loc) · 1.03 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
import java.util.ArrayList;
public class GraphDemo {
// this function will connect 2 nodes
static void addEdge(ArrayList<ArrayList<Integer>> adj, int u, int v) {
// for undirected connect u and v and then v and u
adj.get(u).add(v);
adj.get(v).add(u);
}
static void printGraph(ArrayList<ArrayList<Integer>> adj) {
for(int i = 0; i < adj.size(); i++) {
System.out.print(i + " : ");
for(int j = 0; j < adj.get(i).size(); j++) {
System.out.print(adj.get(i).get(j) + ",");
}
System.out.println();
}
}
public static void main(String[] args) {
int V = 4;
ArrayList<ArrayList<Integer>> adj = new ArrayList<>();
for(int i = 0; i < V; i++) {
adj.add(new ArrayList<>());
}
addEdge(adj, 0, 1);
addEdge(adj, 0, 2);
addEdge(adj, 1, 2);
addEdge(adj, 2, 3);
// System.out.println(adj);
printGraph(adj);
}
}