-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUndirectedWeightedGraph.java
158 lines (131 loc) · 4.33 KB
/
UndirectedWeightedGraph.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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
package graph;
import java.util.*;
public class UndirectedWeightedGraph implements WeightedGraph {
private Map<Integer, Set<Edge>> adjacencyList;
private final int noOfVertices;
public UndirectedWeightedGraph(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<Edge>> getAdjacencyList() {
return adjacencyList;
}
public int getNoOfVertices() {
return noOfVertices;
}
@Override
// Time Complexity: O(1)
public boolean addEdge(int source, int destination, int weight) {
if (!this.validateSourceAndDestination(source, destination)) {
return false;
}
return this.adjacencyList.get(source).add(new Edge(source, destination, weight)) &&
this.adjacencyList.get(destination).add(new Edge(destination, source, weight));
}
@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);
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<Edge> adjacentVertices = this.adjacencyList.get(source);
for (Edge i : adjacentVertices) {
if (destination == i.getDestination()) {
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<Edge> adjacent(int node) {
return this.adjacencyList.get(node).stream().toList();
}
@Override
// Time Complexity: O(V + E)
public List<Integer> bfs() {
List<Integer> result = new ArrayList<>();
boolean[] visited = new boolean[this.getVerticesCount()];
for (int i = 0; i < this.getVerticesCount(); i++) {
if (!visited[i]) {
bfs(i, visited, result);
}
}
return result;
}
private void bfs(int source, boolean[] visited, List<Integer> result) {
Queue<Integer> queue = new LinkedList<>();
queue.add(source);
visited[source] = true;
while (!queue.isEmpty()) {
int node = queue.poll();
result.add(node);
for (Edge adj : this.adjacent(node)) {
if (!visited[adj.getDestination()]) {
visited[adj.getDestination()] = true;
queue.add(adj.getDestination());
}
}
}
}
@Override
// Time Complexity: O(V + E)
public List<Integer> dfs() {
// TODO: similar to normal DFS traversal
return null;
}
@Override
public String toString() {
StringBuilder s = new StringBuilder();
for (int i = 0; i < noOfVertices; i++) {
s.append(i).append(": ");
for (Edge j : this.adjacencyList.get(i)) {
s.append(j.getDestination()).append(" ");
}
s.append("\n");
}
return s.toString();
}
@Override
public int getVerticesCount() {
return this.noOfVertices;
}
public static void main(String args[]) {
WeightedGraph g = new UndirectedWeightedGraph(4);
g.addEdge(0, 1, 9);
g.addEdge(0, 2, 10);
g.addEdge(0, 3, 5);
g.addEdge(1, 2, 8);
g.addEdge(2, 3, 7);
System.out.print(g);
System.out.println(g.adjacent(1));
System.out.println(g.degree(2));
}
}