-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGraph.java
More file actions
152 lines (119 loc) · 3.99 KB
/
Graph.java
File metadata and controls
152 lines (119 loc) · 3.99 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
//Modified from Graph.java from the package EDU.colorado.graphs
import java.util.LinkedList;
import java.util.Queue;
public class Graph implements Cloneable {
/* Adjacency matrix:
* - for edges[i][j]: i is source vertex, j is target vertex
* - each element in edges is the path weight between the current i and j
* - if edges[i][j] = -1, then no path exists between i and j
*/
private double[][] edges;
//Labels array: each source vertex i has a label contained in labels[i]
private Object[] labels;
public Graph(double[][] adjacencies){
edges = adjacencies;
labels = new Object[adjacencies.length]; //all values initially null
}
// **********EDGES**********
public boolean isEdge(int source, int target){
return (edges[source][target] != -1);
}
// **********WEIGHTS**********
public double getWeight(int source, int target){
return edges[source][target];
}
// **********LABELS**********
public void setLabel(int vertex, Object newLabel){
labels[vertex] = newLabel;
}
public Object getLabel(int vertex){
return labels[vertex];
}
public int size(){
return labels.length;
}
// **********CLONING**********
public Object clone() {
Graph answer;
try {
answer = (Graph)super.clone();
}
catch(CloneNotSupportedException e) {
// This would indicate an internal error in the Java runtime system
// because super.clone always works for an Object.
throw new InternalError(e.toString());
}
answer.edges = (double[][]) edges.clone();
answer.labels = (Object[]) labels.clone();
return answer;
}
// **********DEPTH FIRST SEARCHING**********
public static void depthFirstPrint(Graph g, int start) {
// Boolean array for whether or not vertex has been marked (traversed once)
boolean[] marked = new boolean [g.size()];
depthFirstRecurse(g, start, marked);
}
public static void depthFirstRecurse(Graph g, int v, boolean[] marked){
// Starting vertex:
marked[v] = true; // Mark
System.out.println(g.getLabel(v)); // Process
int[] connections = g.neighbors(v);
// Traverse all reighboring vertices
for (int i = 0; i < connections.length; i++){
int nextNeighbor = connections[i];
// Check if reighbor vertex is marked
if (!marked[nextNeighbor]){
depthFirstRecurse(g, nextNeighbor, marked);
}
}
}
// **********BREADTH FIRST SEARCHING**********
public static void breadthFirstPrint(Graph g, int start){
// Boolean array for whether or not vertex has been marked (traversed once)
boolean[] marked = new boolean[g.size()];
breadthFirst(g, start, marked);
}
public static void breadthFirst(Graph g, int v, boolean[] marked){
// Queue to store unmarked neighbor vertices to process
Queue<Integer> q = new LinkedList<Integer>();
// Starting vertex:
System.out.println(g.getLabel(v)); // Process
marked[v] = true; // Mark
q.add(v); // Place in queue
// Run until queue is empty (no more unmarked vertices in scope)
while(!q.isEmpty()){
// Remove vertex from front
// Traverse all neighboring vertices
int[] connections = g.neighbors(q.remove());
for (int i = 0; i < connections.length; i++) {
int nextNeighbor = connections[i];
if (!marked[nextNeighbor]) { // Check if neighbor vertex is marked
System.out.println(g.getLabel(nextNeighbor));// Process
marked[nextNeighbor] = true; // Mark
q.add(nextNeighbor); // Place in queue
}
}
}
}
// **********NEIGHBORING VERTICES**********
public int[] neighbors(int vertex){
int i;
int count;
int[] answer;
// First count how many edges have the vertex as their source
count = 0;
for (i = 0; i < labels.length; i++){
if (edges[vertex][i] != -1)
count++;
}
// Allocate the array for the answer
answer = new int[count];
// Fill the array for the answer
count = 0;
for (i = 0; i < labels.length; i++){
if (edges[vertex][i] != -1)
answer[count++] = i;
}
return answer;
}
}