-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGraph.java
More file actions
73 lines (54 loc) · 1.66 KB
/
Graph.java
File metadata and controls
73 lines (54 loc) · 1.66 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
package graph_algorithms;
import data_structures.LinkedList;
import java.util.function.BiConsumer;
public abstract class Graph {
protected final int vertexCount;
protected final boolean directed;
protected int edgeCount = 0;
public Graph(int n, boolean directed) {
vertexCount = n;
this.directed = directed;
}
protected static void constructExampleGraph(Graph graph) {
graph.addEdge(0, 1, 1);
graph.addEdge(1, 2, 2);
graph.addEdge(2, 3, 3);
graph.addEdge(3, 1, 4);
graph.addEdge(3, 0, 5);
}
public int getVertexCount() {
return vertexCount;
}
public int getEdgeCount() {
return edgeCount;
}
public void addEdge(int from, int to) {
addEdge(from, to, 1);
}
public void addEdge(int from, int to, double cost) {
_addEdge(from, to, cost);
if (!directed) {
_addEdge(to, from, cost);
}
edgeCount++;
}
protected abstract void _addEdge(int from, int to, double cost);
public abstract boolean containsEdge(int from, int to);
public abstract LinkedList<Integer> outEdges(int vertex); // TODO: Use Dictionary once AVLTree is finished
public void forEachEdge(BiConsumer<Integer, Integer> action) {
for (int u = 0; u < vertexCount; ++u) {
for (int v : this.outEdges(u)) {
action.accept(u, v);
}
}
}
public abstract double cost(int from, int to);
public static class Edge {
int from;
int to;
public Edge(int from, int to) {
this.from = from;
this.to = to;
}
}
}