-
Notifications
You must be signed in to change notification settings - Fork 0
/
UndirectedGraphAdjacencyList.java
136 lines (114 loc) · 3.7 KB
/
UndirectedGraphAdjacencyList.java
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
package graph;
import java.util.*;
public class UndirectedGraphAdjacencyList implements Graph {
private Map<Integer, Set<Integer>> adjacencyList;
private final int noOfVertices;
public UndirectedGraphAdjacencyList(int noOfVertices) {
this.noOfVertices = noOfVertices;
this.initAdjacencyList(noOfVertices);
}
private void initAdjacencyList(int noOfVertices) {
this.adjacencyList = new HashMap<>();
for (int i = 0; i < noOfVertices; i++) {
this.adjacencyList.put(i, new HashSet<>());
}
}
public Map<Integer, Set<Integer>> getAdjacencyList() {
return adjacencyList;
}
public int getNoOfVertices() {
return noOfVertices;
}
@Override
// Time Complexity: O(1)
public boolean addEdge(int source, int destination) {
if (!this.validateSourceAndDestination(source, destination)) {
return false;
}
return this.adjacencyList.get(source).add(destination) && this.adjacencyList.get(destination).add(source);
}
@Override
// Time Complexity: O(V)
public boolean removeEdge(int source, int destination) {
if (!this.validateSourceAndDestination(source, destination)) {
return false;
}
this.adjacencyList.get(source).remove(destination);
this.adjacencyList.get(destination).remove(source);
return true;
}
private boolean validateSourceAndDestination(int source, int destination) {
if (source < 0 || source >= noOfVertices || destination < 0 || destination >= noOfVertices) {
System.out.println("source or destination is invalid");
return false;
}
return true;
}
@Override
// Time Complexity: O(V)
public boolean hasEdge(int source, int destination) {
if (!validateSourceAndDestination(source, destination)) {
return false;
}
Set<Integer> adjacentVertices = this.adjacencyList.get(source);
for (int i : adjacentVertices) {
if (destination == i) {
return true;
}
}
return false;
}
@Override
// Time Complexity: O(1)
public int degree(int node) {
return this.adjacencyList.get(node).size();
}
@Override
// Time Complexity: O(V)
public List<Integer> adjacent(int node) {
return this.adjacencyList.get(node).stream().toList();
}
@Override
// Time Complexity: O(V + E)
public List<Integer> bfs() {
Traversal t = new BfsTraversal(this);
return t.traverse();
}
@Override
// Time Complexity: O(V + E)
public List<Integer> dfs() {
// Traversal t = new DfsIterativeTraversal(this);
// return t.traverse();
Traversal t = new DfsRecursiveTraversal(this);
return t.traverse();
}
@Override
public String toString() {
StringBuilder s = new StringBuilder();
for (int i = 0; i < noOfVertices; i++) {
s.append(i).append(": ");
for (int j : this.adjacencyList.get(i)) {
s.append(j).append(" ");
}
s.append("\n");
}
return s.toString();
}
@Override
public int getVerticesCount() {
return this.noOfVertices;
}
public static void main(String args[]) {
Graph g = new UndirectedGraphAdjacencyList(4);
g.addEdge(0, 1);
g.addEdge(0, 2);
g.addEdge(1, 2);
g.addEdge(2, 0);
g.addEdge(2, 3);
System.out.print(g);
System.out.println(g.adjacent(2));
System.out.println(g.degree(2));
System.out.println("Bfs :" + g.bfs());
System.out.println("Dfs :" + g.dfs());
}
}