-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGraphRepresentations.java
More file actions
65 lines (56 loc) · 1.76 KB
/
GraphRepresentations.java
File metadata and controls
65 lines (56 loc) · 1.76 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
import java.util.ArrayList;
public class GraphRepresentations {
public static void main(String[] args) {
// 1. Edge list
// from list
int[] v1 = {0, 0, 1, 1, 2, 3};
int[] v2 = {1, 2, 2, 4, 3, 4};
for (int i = 0; i < v1.length; i++) {
// System.out.println(v1[i] + " to " + v2[i]);
}
ArrayList<Edge> edgeList = new ArrayList<>();
edgeList.add(new Edge(0, 1));
edgeList.add(new Edge(0, 2));
edgeList.add(new Edge(1, 2));
edgeList.add(new Edge(1, 4));
edgeList.add(new Edge(2, 3));
edgeList.add(new Edge(3, 4));
for (Edge edge : edgeList) {
if (edge.v1 == 2) {
// System.out.println(edge.v1 + " to " + edge.v2);
}
}
//2. Adjacency Matrix
int[][] adjMatrix = new int[5][5];
adjMatrix[0][1] = 1;
adjMatrix[1][0] = 1;
adjMatrix[0][2] = 1;
adjMatrix[1][2] = 1;
adjMatrix[1][2] = 1;
adjMatrix[2][3] = 1;
adjMatrix[3][4] = 1;
//3. Adjacency Lists
ArrayList<ArrayList<Integer>> adjList = new ArrayList<>();
for (int i = 0; i < 5; i++)
adjList.add(new ArrayList<>());
System.out.println(adjList);
adjList.get(0).add(1);
adjList.get(0).add(2);
adjList.get(1).add(2);
adjList.get(1).add(4);
adjList.get(2).add(3);
adjList.get(3).add(4);
System.out.println(adjList);
for (int i = 0; i <= 4; i++) {
System.out.println(i + " is connected to: " + adjList.get(i));
}
}
static class Edge {
int weight;
int v1, v2;
public Edge(int from, int to) {
v1 = from;
v2 = to;
}
}
}