Skip to content

Commit a8af67d

Browse files
committed
added weighted graph
1 parent 12665dc commit a8af67d

File tree

4 files changed

+191
-76
lines changed

4 files changed

+191
-76
lines changed

CCSPiJ/src/chapter4/Graph.java

Lines changed: 8 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,11 @@
55
import java.util.List;
66
import java.util.stream.Collectors;
77

8-
import chapter2.GenericSearch;
9-
import chapter2.GenericSearch.Node;
10-
118
// V is the type of the vertices in the Graph
12-
public class Graph<V> {
9+
public abstract class Graph<V, E extends Edge> {
1310

1411
private ArrayList<V> vertices;
15-
private ArrayList<ArrayList<Edge>> edges;
12+
protected ArrayList<ArrayList<E>> edges;
1613

1714
public Graph() {
1815
vertices = new ArrayList<>();
@@ -22,8 +19,8 @@ public Graph() {
2219
public Graph(List<V> vertices) {
2320
this.vertices = new ArrayList<>(vertices);
2421
edges = new ArrayList<>();
25-
for (V element : this.vertices) {
26-
edges.add(new ArrayList<Edge>());
22+
for (V vertice : vertices) {
23+
edges.add(new ArrayList<>());
2724
}
2825
}
2926

@@ -34,7 +31,7 @@ public int getVertexCount() {
3431

3532
// Number of edges
3633
public int getEdgeCount() {
37-
return edges.stream().mapToInt(al -> al.size()).sum();
34+
return edges.stream().mapToInt(ArrayList::size).sum();
3835
}
3936

4037
// Add a vertex to the graph and return its index
@@ -44,23 +41,6 @@ public int addVertex(V vertex) {
4441
return getVertexCount() - 1;
4542
}
4643

47-
// This is an undirected graph, so we always add
48-
// edges in both directions
49-
public void addEdge(Edge edge) {
50-
edges.get(edge.u).add(edge);
51-
edges.get(edge.v).add(edge.reversed());
52-
}
53-
54-
// Add an edge using vertex indices (convenience method)
55-
public void addEdge(int u, int v) {
56-
addEdge(new Edge(u, v));
57-
}
58-
59-
// Add an edge by looking up vertex indices (convenience method)
60-
public void addEdge(V first, V second) {
61-
addEdge(new Edge(indexOf(first), indexOf(second)));
62-
}
63-
6444
// Find the vertex at a specific index
6545
public V vertexAt(int index) {
6646
return vertices.get(index);
@@ -84,73 +64,25 @@ public List<V> neighborsOf(V vertex) {
8464
}
8565

8666
// Return all of the edges associated with a vertex at some index
87-
public List<Edge> edgesOf(int index) {
67+
public List<E> edgesOf(int index) {
8868
return edges.get(index);
8969
}
9070

9171
// Look up the index of a vertex and return its edges (convenience method)
92-
public List<Edge> edgesOf(V vertex) {
72+
public List<E> edgesOf(V vertex) {
9373
return edgesOf(indexOf(vertex));
9474
}
9575

9676
// Make it easy to pretty-print a Graph
9777
@Override
9878
public String toString() {
9979
StringBuilder sb = new StringBuilder();
100-
for (int i = 0; i < vertices.size(); i++) {
80+
for (int i = 0; i < getVertexCount(); i++) {
10181
sb.append(vertexAt(i).toString());
10282
sb.append(" -> ");
10383
sb.append(Arrays.toString(neighborsOf(i).toArray()));
10484
sb.append(System.lineSeparator());
10585
}
10686
return sb.toString();
10787
}
108-
109-
// Test basic Graph construction
110-
public static void main(String[] args) {
111-
// Represents the 15 largest MSAs in the United States
112-
Graph<String> cityGraph = new Graph<>(
113-
List.of("Seattle", "San Francisco", "Los Angeles", "Riverside", "Phoenix", "Chicago", "Boston",
114-
"New York", "Atlanta", "Miami", "Dallas", "Houston", "Detroit", "Philadelphia", "Washington"));
115-
116-
cityGraph.addEdge("Seattle", "Chicago");
117-
cityGraph.addEdge("Seattle", "San Francisco");
118-
cityGraph.addEdge("San Francisco", "Riverside");
119-
cityGraph.addEdge("San Francisco", "Los Angeles");
120-
cityGraph.addEdge("Los Angeles", "Riverside");
121-
cityGraph.addEdge("Los Angeles", "Phoenix");
122-
cityGraph.addEdge("Riverside", "Phoenix");
123-
cityGraph.addEdge("Riverside", "Chicago");
124-
cityGraph.addEdge("Phoenix", "Dallas");
125-
cityGraph.addEdge("Phoenix", "Houston");
126-
cityGraph.addEdge("Dallas", "Chicago");
127-
cityGraph.addEdge("Dallas", "Atlanta");
128-
cityGraph.addEdge("Dallas", "Houston");
129-
cityGraph.addEdge("Houston", "Atlanta");
130-
cityGraph.addEdge("Houston", "Miami");
131-
cityGraph.addEdge("Atlanta", "Chicago");
132-
cityGraph.addEdge("Atlanta", "Washington");
133-
cityGraph.addEdge("Atlanta", "Miami");
134-
cityGraph.addEdge("Miami", "Washington");
135-
cityGraph.addEdge("Chicago", "Detroit");
136-
cityGraph.addEdge("Detroit", "Boston");
137-
cityGraph.addEdge("Detroit", "Washington");
138-
cityGraph.addEdge("Detroit", "New York");
139-
cityGraph.addEdge("Boston", "New York");
140-
cityGraph.addEdge("New York", "Philadelphia");
141-
cityGraph.addEdge("Philadelphia", "Washington");
142-
System.out.println(cityGraph.toString());
143-
144-
Node<String> bfsResult = GenericSearch.bfs("Boston",
145-
v -> v.equals("Miami"),
146-
cityGraph::neighborsOf);
147-
if (bfsResult == null) {
148-
System.out.println("No solution found using breadth-first search!");
149-
} else {
150-
List<String> path = GenericSearch.nodeToPath(bfsResult);
151-
System.out.println("Path from Boston to Miami:");
152-
System.out.println(path);
153-
}
154-
}
155-
15688
}
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
package chapter4;
2+
3+
import java.util.List;
4+
5+
import chapter2.GenericSearch;
6+
import chapter2.GenericSearch.Node;
7+
8+
public class UnweightedGraph<V> extends Graph<V, Edge> {
9+
10+
public UnweightedGraph(List<V> vertices) {
11+
super(vertices);
12+
}
13+
14+
// This is an undirected graph, so we always add
15+
// edges in both directions
16+
public void addEdge(Edge edge) {
17+
edges.get(edge.u).add(edge);
18+
edges.get(edge.v).add(edge.reversed());
19+
}
20+
21+
// Add an edge using vertex indices (convenience method)
22+
public void addEdge(int u, int v) {
23+
addEdge(new Edge(u, v));
24+
}
25+
26+
// Add an edge by looking up vertex indices (convenience method)
27+
public void addEdge(V first, V second) {
28+
addEdge(new Edge(indexOf(first), indexOf(second)));
29+
}
30+
31+
// Test basic Graph construction
32+
public static void main(String[] args) {
33+
// Represents the 15 largest MSAs in the United States
34+
UnweightedGraph<String> cityGraph = new UnweightedGraph<>(
35+
List.of("Seattle", "San Francisco", "Los Angeles", "Riverside", "Phoenix", "Chicago", "Boston",
36+
"New York", "Atlanta", "Miami", "Dallas", "Houston", "Detroit", "Philadelphia", "Washington"));
37+
38+
cityGraph.addEdge("Seattle", "Chicago");
39+
cityGraph.addEdge("Seattle", "San Francisco");
40+
cityGraph.addEdge("San Francisco", "Riverside");
41+
cityGraph.addEdge("San Francisco", "Los Angeles");
42+
cityGraph.addEdge("Los Angeles", "Riverside");
43+
cityGraph.addEdge("Los Angeles", "Phoenix");
44+
cityGraph.addEdge("Riverside", "Phoenix");
45+
cityGraph.addEdge("Riverside", "Chicago");
46+
cityGraph.addEdge("Phoenix", "Dallas");
47+
cityGraph.addEdge("Phoenix", "Houston");
48+
cityGraph.addEdge("Dallas", "Chicago");
49+
cityGraph.addEdge("Dallas", "Atlanta");
50+
cityGraph.addEdge("Dallas", "Houston");
51+
cityGraph.addEdge("Houston", "Atlanta");
52+
cityGraph.addEdge("Houston", "Miami");
53+
cityGraph.addEdge("Atlanta", "Chicago");
54+
cityGraph.addEdge("Atlanta", "Washington");
55+
cityGraph.addEdge("Atlanta", "Miami");
56+
cityGraph.addEdge("Miami", "Washington");
57+
cityGraph.addEdge("Chicago", "Detroit");
58+
cityGraph.addEdge("Detroit", "Boston");
59+
cityGraph.addEdge("Detroit", "Washington");
60+
cityGraph.addEdge("Detroit", "New York");
61+
cityGraph.addEdge("Boston", "New York");
62+
cityGraph.addEdge("New York", "Philadelphia");
63+
cityGraph.addEdge("Philadelphia", "Washington");
64+
System.out.println(cityGraph.toString());
65+
66+
Node<String> bfsResult = GenericSearch.bfs("Boston",
67+
v -> v.equals("Miami"),
68+
cityGraph::neighborsOf);
69+
if (bfsResult == null) {
70+
System.out.println("No solution found using breadth-first search!");
71+
} else {
72+
List<String> path = GenericSearch.nodeToPath(bfsResult);
73+
System.out.println("Path from Boston to Miami:");
74+
System.out.println(path);
75+
}
76+
}
77+
}

CCSPiJ/src/chapter4/WeightedEdge.java

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package chapter4;
2+
3+
public class WeightedEdge extends Edge implements Comparable<WeightedEdge> {
4+
public final double weight;
5+
6+
public WeightedEdge(int u, int v, double weight) {
7+
super(u, v);
8+
this.weight = weight;
9+
}
10+
11+
@Override
12+
public WeightedEdge reversed() {
13+
return new WeightedEdge(v, u, weight);
14+
}
15+
16+
// so that we can order edges by weight to find the minimum weight edge
17+
@Override
18+
public int compareTo(WeightedEdge other) {
19+
Double mine = weight;
20+
Double theirs = other.weight;
21+
return mine.compareTo(theirs);
22+
}
23+
24+
@Override
25+
public String toString() {
26+
return u + " " + weight + "> " + v;
27+
}
28+
}
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
package chapter4;
2+
3+
import java.util.Arrays;
4+
import java.util.List;
5+
6+
public class WeightedGraph<V> extends Graph<V, WeightedEdge> {
7+
8+
public WeightedGraph(List<V> vertices) {
9+
super(vertices);
10+
}
11+
12+
// This is an undirected graph, so we always add
13+
// edges in both directions
14+
public void addEdge(WeightedEdge edge) {
15+
edges.get(edge.u).add(edge);
16+
edges.get(edge.v).add(edge.reversed());
17+
}
18+
19+
public void addEdge(int u, int v, float weight) {
20+
addEdge(new WeightedEdge(u, v, weight));
21+
}
22+
23+
public void addEdge(V first, V second, float weight) {
24+
addEdge(indexOf(first), indexOf(second), weight);
25+
}
26+
27+
// Make it easy to pretty-print a Graph
28+
@Override
29+
public String toString() {
30+
StringBuilder sb = new StringBuilder();
31+
for (int i = 0; i < getVertexCount(); i++) {
32+
sb.append(vertexAt(i).toString());
33+
sb.append(" -> ");
34+
sb.append(Arrays.toString(edgesOf(i).stream()
35+
.map(we -> "(" + vertexAt(we.v) + ", " + we.weight + ")").toArray()));
36+
sb.append(System.lineSeparator());
37+
}
38+
return sb.toString();
39+
}
40+
41+
// Test basic Graph construction
42+
public static void main(String[] args) {
43+
// Represents the 15 largest MSAs in the United States
44+
WeightedGraph<String> cityGraph2 = new WeightedGraph<>(
45+
List.of("Seattle", "San Francisco", "Los Angeles", "Riverside", "Phoenix", "Chicago", "Boston",
46+
"New York", "Atlanta", "Miami", "Dallas", "Houston", "Detroit", "Philadelphia", "Washington"));
47+
48+
cityGraph2.addEdge("Seattle", "Chicago", 1737);
49+
cityGraph2.addEdge("Seattle", "San Francisco", 678);
50+
cityGraph2.addEdge("San Francisco", "Riverside", 386);
51+
cityGraph2.addEdge("San Francisco", "Los Angeles", 348);
52+
cityGraph2.addEdge("Los Angeles", "Riverside", 50);
53+
cityGraph2.addEdge("Los Angeles", "Phoenix", 357);
54+
cityGraph2.addEdge("Riverside", "Phoenix", 307);
55+
cityGraph2.addEdge("Riverside", "Chicago", 1704);
56+
cityGraph2.addEdge("Phoenix", "Dallas", 887);
57+
cityGraph2.addEdge("Phoenix", "Houston", 1015);
58+
cityGraph2.addEdge("Dallas", "Chicago", 805);
59+
cityGraph2.addEdge("Dallas", "Atlanta", 721);
60+
cityGraph2.addEdge("Dallas", "Houston", 225);
61+
cityGraph2.addEdge("Houston", "Atlanta", 702);
62+
cityGraph2.addEdge("Houston", "Miami", 968);
63+
cityGraph2.addEdge("Atlanta", "Chicago", 588);
64+
cityGraph2.addEdge("Atlanta", "Washington", 543);
65+
cityGraph2.addEdge("Atlanta", "Miami", 604);
66+
cityGraph2.addEdge("Miami", "Washington", 923);
67+
cityGraph2.addEdge("Chicago", "Detroit", 238);
68+
cityGraph2.addEdge("Detroit", "Boston", 613);
69+
cityGraph2.addEdge("Detroit", "Washington", 396);
70+
cityGraph2.addEdge("Detroit", "New York", 482);
71+
cityGraph2.addEdge("Boston", "New York", 190);
72+
cityGraph2.addEdge("New York", "Philadelphia", 81);
73+
cityGraph2.addEdge("Philadelphia", "Washington", 123);
74+
75+
System.out.println(cityGraph2);
76+
}
77+
78+
}

0 commit comments

Comments
 (0)