-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGraph.java
More file actions
231 lines (178 loc) · 6.44 KB
/
Graph.java
File metadata and controls
231 lines (178 loc) · 6.44 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
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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
import com.sun.jmx.remote.internal.ArrayQueue;
import java.util.*;
/*IMPORTANT NOTE:
* MODIFY METHODS HAVING TODO ONLY.
* DO NOT MODIFY OTHER METHODS IN THIS CLASS
* */
public class Graph<V> implements GraphInterface<V>
{
/** Construct an empty */
public Graph() {}
/** Create and returns an adjacency lists from edge arrays */
// Implemented by Jazmin Perez
public List<List<Edge>> createWeightedGraph(List<V> vertices, int[][] edges)
{
List<List<Edge>> neighbors = new ArrayList<>();
/*TODO ADD YOUR CODE IN THIS METHOD TO CREATE AN
* ADJACENCY LIST FOR A GRAPH (V,E) HAVING V=vertices and E=edges
*
* STORE THE ADJACENCY LIST IN neighbors LIST SUCH THAT neighbours.get(i)
* will give the list of all edges from vertex i (i.e. vertex with name vertices.get(i))
* AND RETURN neighbors LIST
*
* CHECK STRUCTURE OF EDGE CLASS BEFORE IMPLEMENTING YOUR CODE FOR ADJACENCY LIST
* * */
for(int i = 0; i < vertices.size(); i++){
List<Edge> edgeList = new ArrayList<>();
for(int j = 0; j < edges.length; j++){
if(edges[i][0] == vertices.indexOf(i)){
Edge e = new Edge(edges[i][0], edges[i][1], edges[i][2]);
edgeList.add(e);
}
}
neighbors.add(edgeList);
}
return neighbors;
}
/** Find single source shortest paths */
// Implemented by Jake Lyon
public Tree getShortestPath(
V sourceVertex, // The root vertex of the tree
List<V> vertices, // The list of vertices in the tree
List<List<Edge>> neighbors) // Adjacency list with edge data
{
/*TODO ADD YOUR CODE IN THIS METHOD TO CREATE A SHORTEST PATH TREE SUCH THAT
* THE ROOT OF TREE IS sourceVertex AND PATH FROM sourceVertex (i.e. root) TO ANY OTHER NODE
* IN THE TREE IS THE SHORTEST PATH FROM sourceVertex TO THAT NODE IN GRAPH defined BY ADJACENCY LIST neighbors
*
* RETURN THE TREE OBJECT
*
* CHECK STRUCTURE OF TREE CLASS BEFORE IMPLEMENTING YOUR CODE FOR SHORTEST PATH TREE FROM VERTEX sourceVertex
* * */
int numVertices = vertices.size(); // number of vertices in graph
int sourceIndex = vertices.indexOf(sourceVertex);
double weights[] = new double[numVertices]; // array of weights used to pick minimum cost path
int parents[] = new int[numVertices]; // each node has a parent node on its path
boolean marked[] = new boolean[numVertices]; // mark each vertex as it is visited
for (int i = 0; i < numVertices; i++)
{
weights[i] = Integer.MAX_VALUE; //infinity represented by the maximum value of an integer
marked[i] = false;
parents[i] = -1;
}
//step 1, visit source
marked[sourceIndex] = true;
weights[sourceIndex] = 0;
boolean exit = false;
// Traverse the graph
while(exit == false)
{
// Update parents
for (int i = 0; i < parents.length; i++)
for (int j = 0; j < neighbors.get(i).size(); j++)
if ((neighbors.get(i).get(j).u == sourceIndex))
parents[neighbors.get(i).get(j).v] = neighbors.get(i).get(j).u;
// Update weights
for (int i = 0; i < weights.length; i++)
for (int j = 0; j < neighbors.get(i).size(); j++)
if ((parents[i] == sourceIndex) &&
(weights[i] > weights[sourceIndex] + neighbors.get(i).get(j).weight))
{
weights[i] = weights[sourceIndex] + neighbors.get(i).get(j).weight;
}
double minWeight = Double.MAX_VALUE;
for (int i = 0; i < weights.length; i++)
{
if (weights[i] < minWeight && (!marked[i]))
minWeight = weights[i];
}
// begin visiting next node
List temp = Arrays.asList(weights);
sourceIndex = temp.indexOf(weights);
marked[sourceIndex] = true;
int t = 0;
for(int i = 0; i < marked.length; i++)
{
if (marked[i] == true)
t++;
}
if(t == marked.length)
exit = true;
}
Tree shortestPath = new Tree(vertices.indexOf(sourceVertex), parents, weights); //new Tree(sourceVertex, parents, weights)
return shortestPath;
}
/** Edge inner class inside the Graph class */
/*EDGE CLASS STORES THE EDGE SUCH THAT u AND v ARE THE TWO VERTEX CONNECTED BY THE EDGE AND
* weight IS THE WEIGHT OF THE EDGE
* */
public class Edge
{
public int u; // Starting vertex of the edge
public int v; // Ending vertex of the edge
public double weight; //Weight of the edge
/** Construct an edge for (u, v, weight) */
public Edge(int u, int v, double weight)
{
this.u = u; //Important
this.v = v;
this.weight=weight;
}
}
/** Tree inner class inside the Graph class */
/*TREE CLASS STORES THE TREE SUCH THAT root IS THE ROOT NODE OF TREE (i.e. sourceVertex FOR SHORTEST DISTANCE TREE)
* parent[i] STORES THE PARENT OF NODE i IN THE TREE
* NOTE: PARENT OF root is -1 (i.e. parent[root]=-1
* cost[i] is COST OF PATH FROM root (i.e. sourceVertex) to Node i
* */
public class Tree
{
private int root; // The root of the tree
private int[] parent; // Store the parent of each vertex, Parent of root node is -1
private double[] cost; // cost of each vertex from root i.e. source
/** Construct a tree with root, parent, cost */
public Tree(int root, int[] parent, double[] cost)
{
this.root = root;//Important
this.parent = parent;
this.parent[root] = -1;
this.cost = cost;
}
/** Return the root of the tree */
public int getRoot() {
return root;//Important
}
/** Return the path of vertices from a vertex to the root */
public List<V> getPath(int index, List<V> vertices)
{
ArrayList<V> path = new ArrayList<>();//Important
do
{
path.add(vertices.get(index));
index = parent[index];
}
while (index != -1);
return path;
}
/** Print a path from the root to vertex v */
public void printPath(int index,List<V> vertices)
{
List<V> path = getPath(index,vertices); //Important
System.out.print("A path from " + vertices.get(root) + " to " +
vertices.get(index) + ": ");
for (int i = path.size() - 1; i >= 0; i--)
System.out.print(path.get(i) + " ");
}
/** Print paths from all vertices to the source */
public void printAllPaths(List<V> vertices)
{
System.out.println("All shortest paths from " +
vertices.get(getRoot()) + " are:");//Important
for (int i = 0; i < cost.length; i++)
{
printPath(i,vertices); // Print a path from i to the source
System.out.println("(cost: " + cost[i] + ")"); // Path cost
}
}
}
}