Skip to content

Commit 2f1181b

Browse files
committed
fixed some stuff
1 parent 60953b6 commit 2f1181b

File tree

5 files changed

+71
-1
lines changed

5 files changed

+71
-1
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -24,3 +24,4 @@ hs_err_pid*
2424
/.metadata/
2525

2626
.DS_STORE
27+
.metadata

CCSPiJ/src/chapter4/Graph.java

+17
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,19 @@
1+
// Graph.java
2+
// From Classic Computer Science Problems in Java Chapter 4
3+
// Copyright 2020 David Kopec
4+
//
5+
// Licensed under the Apache License, Version 2.0 (the "License");
6+
// you may not use this file except in compliance with the License.
7+
// You may obtain a copy of the License at
8+
//
9+
// http://www.apache.org/licenses/LICENSE-2.0
10+
//
11+
// Unless required by applicable law or agreed to in writing, software
12+
// distributed under the License is distributed on an "AS IS" BASIS,
13+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
// See the License for the specific language governing permissions and
15+
// limitations under the License.
16+
117
package chapter4;
218

319
import java.util.ArrayList;
@@ -6,6 +22,7 @@
622
import java.util.stream.Collectors;
723

824
// V is the type of the vertices in the Graph
25+
// E is the type of the edges
926
public abstract class Graph<V, E extends Edge> {
1027

1128
private ArrayList<V> vertices;

CCSPiJ/src/chapter4/UnweightedGraph.java

+16
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,19 @@
1+
// UnweightedGraph.java
2+
// From Classic Computer Science Problems in Java Chapter 4
3+
// Copyright 2020 David Kopec
4+
//
5+
// Licensed under the Apache License, Version 2.0 (the "License");
6+
// you may not use this file except in compliance with the License.
7+
// You may obtain a copy of the License at
8+
//
9+
// http://www.apache.org/licenses/LICENSE-2.0
10+
//
11+
// Unless required by applicable law or agreed to in writing, software
12+
// distributed under the License is distributed on an "AS IS" BASIS,
13+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
// See the License for the specific language governing permissions and
15+
// limitations under the License.
16+
117
package chapter4;
218

319
import java.util.List;

CCSPiJ/src/chapter4/WeightedEdge.java

+16
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,19 @@
1+
// WeightedEdge.java
2+
// From Classic Computer Science Problems in Java Chapter 4
3+
// Copyright 2020 David Kopec
4+
//
5+
// Licensed under the Apache License, Version 2.0 (the "License");
6+
// you may not use this file except in compliance with the License.
7+
// You may obtain a copy of the License at
8+
//
9+
// http://www.apache.org/licenses/LICENSE-2.0
10+
//
11+
// Unless required by applicable law or agreed to in writing, software
12+
// distributed under the License is distributed on an "AS IS" BASIS,
13+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
// See the License for the specific language governing permissions and
15+
// limitations under the License.
16+
117
package chapter4;
218

319
public class WeightedEdge extends Edge implements Comparable<WeightedEdge> {

CCSPiJ/src/chapter4/WeightedGraph.java

+21-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,19 @@
1+
// WeightedGraph.java
2+
// From Classic Computer Science Problems in Java Chapter 4
3+
// Copyright 2020 David Kopec
4+
//
5+
// Licensed under the Apache License, Version 2.0 (the "License");
6+
// you may not use this file except in compliance with the License.
7+
// You may obtain a copy of the License at
8+
//
9+
// http://www.apache.org/licenses/LICENSE-2.0
10+
//
11+
// Unless required by applicable law or agreed to in writing, software
12+
// distributed under the License is distributed on an "AS IS" BASIS,
13+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
// See the License for the specific language governing permissions and
15+
// limitations under the License.
16+
117
package chapter4;
218

319
import java.util.Arrays;
@@ -52,7 +68,10 @@ public static double totalWeight(List<WeightedEdge> path) {
5268
// *start* is the vertex index to start the search at
5369
public List<WeightedEdge> mst(int start) {
5470
LinkedList<WeightedEdge> result = new LinkedList<>(); // final mst
55-
PriorityQueue<WeightedEdge> pq = new PriorityQueue();
71+
if (start < 0 || start > (getVertexCount() - 1)) {
72+
return result;
73+
}
74+
PriorityQueue<WeightedEdge> pq = new PriorityQueue<>();
5675
boolean[] visited = new boolean[getVertexCount()]; // where we've been
5776

5877
// this is like a "visit" inner function
@@ -133,6 +152,7 @@ public DijkstraResult dijkstra(V root) {
133152
for (WeightedEdge we : edgesOf(u)) {
134153
// the old distance to this vertex
135154
double distV = distances[we.v];
155+
// the new distance to this vertex
136156
double pathWeight = we.weight + distU;
137157
// new vertex or found shorter path?
138158
if (!visited[we.v] || (distV > pathWeight)) {

0 commit comments

Comments
 (0)