5
5
import java .util .List ;
6
6
import java .util .stream .Collectors ;
7
7
8
- import chapter2 .GenericSearch ;
9
- import chapter2 .GenericSearch .Node ;
10
-
11
8
// V is the type of the vertices in the Graph
12
- public class Graph <V > {
9
+ public abstract class Graph <V , E extends Edge > {
13
10
14
11
private ArrayList <V > vertices ;
15
- private ArrayList <ArrayList <Edge >> edges ;
12
+ protected ArrayList <ArrayList <E >> edges ;
16
13
17
14
public Graph () {
18
15
vertices = new ArrayList <>();
@@ -22,8 +19,8 @@ public Graph() {
22
19
public Graph (List <V > vertices ) {
23
20
this .vertices = new ArrayList <>(vertices );
24
21
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 <>());
27
24
}
28
25
}
29
26
@@ -34,7 +31,7 @@ public int getVertexCount() {
34
31
35
32
// Number of edges
36
33
public int getEdgeCount () {
37
- return edges .stream ().mapToInt (al -> al . size () ).sum ();
34
+ return edges .stream ().mapToInt (ArrayList :: size ).sum ();
38
35
}
39
36
40
37
// Add a vertex to the graph and return its index
@@ -44,23 +41,6 @@ public int addVertex(V vertex) {
44
41
return getVertexCount () - 1 ;
45
42
}
46
43
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
-
64
44
// Find the vertex at a specific index
65
45
public V vertexAt (int index ) {
66
46
return vertices .get (index );
@@ -84,73 +64,25 @@ public List<V> neighborsOf(V vertex) {
84
64
}
85
65
86
66
// 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 ) {
88
68
return edges .get (index );
89
69
}
90
70
91
71
// 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 ) {
93
73
return edgesOf (indexOf (vertex ));
94
74
}
95
75
96
76
// Make it easy to pretty-print a Graph
97
77
@ Override
98
78
public String toString () {
99
79
StringBuilder sb = new StringBuilder ();
100
- for (int i = 0 ; i < vertices . size (); i ++) {
80
+ for (int i = 0 ; i < getVertexCount (); i ++) {
101
81
sb .append (vertexAt (i ).toString ());
102
82
sb .append (" -> " );
103
83
sb .append (Arrays .toString (neighborsOf (i ).toArray ()));
104
84
sb .append (System .lineSeparator ());
105
85
}
106
86
return sb .toString ();
107
87
}
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
-
156
88
}
0 commit comments