-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBFSTest.java
More file actions
202 lines (149 loc) · 4.59 KB
/
BFSTest.java
File metadata and controls
202 lines (149 loc) · 4.59 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
/**
* CSE 373, Winter 2011, Jessica Miller
* The BinaryHeap is an -generic- implementation of the PriorityQueue interface.
* This is a binary heap implementation of the priority queue ADT.
*/
import java.util.Arrays;
import java.util.Scanner;
import java.io.*;
import java.lang.*;
import java.util.*;
class BFSTest{
static final Integer infinity = Integer.MAX_VALUE;
public static void main(String[] args){
BFSTest searcher = new BFSTest();
Scanner in = new Scanner(new InputStreamReader(System.in));
int vertices = in.nextInt();
int edges = in.nextInt();
HashMap<Integer,HashMap<Integer,Integer>> graph =
new HashMap<Integer,HashMap<Integer,Integer>>();
Integer[] distance = new Integer[vertices+1];
int[] previous = new int[vertices+1];
Integer v1;
Integer v2;
Integer weight;
//Create directed graph, distances to infinity, &
for(int i = 0; i < edges; i++){
v1 = new Integer(in.nextInt());
v2 = new Integer(in.nextInt());
weight = new Integer(in.nextInt());
distance[v1.intValue()] = infinity;
distance[v2.intValue()] = infinity;
previous[v1.intValue()] = -1;
previous[v2.intValue()] = -1;
HashMap<Integer,Integer> edgeList = new HashMap<Integer,Integer>();
if(graph.containsKey(v1)){
edgeList = graph.get(v1);
edgeList.put(v2, weight);
graph.put(v1,edgeList);
}
if(!graph.containsKey(v1)){
edgeList.put(v2, weight);
graph.put(v1,edgeList);
}
}
}
}
class BinaryHeap<T extends Comparable<T>> implements AbstractQueue<T> {
private static final int DEFAULT_CAPACITY = 10;
private T[] array;
private int size;
@SuppressWarnings("unchecked")
private BinaryHeap () {
// Java doesn't allow construction of arrays of placeholder data types
array = (T[])new Comparable[DEFAULT_CAPACITY];
size = 0;
}
private void add(T value) {
// grow array if needed
if (size >= array.length - 1) {
array = this.resize();
}
// place element into heap at bottom
size++;
int index = size;
array[index] = value;
bubbleUp();
}
private boolean isEmpty() {
return size == 0;
}
private T peek() {
if (this.isEmpty()) {
throw new IllegalStateException();
}
return array[1];
}
private T remove() {
// what do want return?
T result = peek();
// get rid of the last leaf/decrement
array[1] = array[size];
array[size] = null;
size--;
bubbleDown();
return result;
}
public String toString() {
return Arrays.toString(array);
}
private void bubbleDown() {
int index = 1;
// bubble down
while (hasLeftChild(index)) {
// which of my children is smaller?
int smallerChild = leftIndex(index);
// bubble with the smaller child, if I have a smaller child
if (hasRightChild(index)
&& array[leftIndex(index)].compareTo(array[rightIndex(index)]) > 0) {
smallerChild = rightIndex(index);
}
if (array[index].compareTo(array[smallerChild]) > 0) {
swap(index, smallerChild);
} else {
// otherwise, get outta here!
break;
}
// make sure to update loop counter/index of where last el is put
index = smallerChild;
}
}
private void bubbleUp() {
int index = this.size;
while (hasParent(index)
&& (parent(index).compareTo(array[index]) > 0)) {
// parent/child are out of order; swap them
swap(index, parentIndex(index));
index = parentIndex(index);
}
}
private boolean hasParent(int i) {
return i > 1;
}
private int leftIndex(int i) {
return i * 2;
}
private int rightIndex(int i) {
return i * 2 + 1;
}
private boolean hasLeftChild(int i) {
return leftIndex(i) <= size;
}
private boolean hasRightChild(int i) {
return rightIndex(i) <= size;
}
private T parent(int i) {
return array[parentIndex(i)];
}
private int parentIndex(int i) {
return i / 2;
}
private T[] resize() {
return Arrays.copyOf(array, array.length * 2);
}
private void swap(int index1, int index2) {
T tmp = array[index1];
array[index1] = array[index2];
array[index2] = tmp;
}
}