Commit 228406c7 authored by uwdkn's avatar uwdkn
Browse files

Merge branch 'master' into 'master'

two more exec test cases

See merge request !53
parents cd4b7618 03e21568
This diff is collapsed.
import lib.UnweightedGraph;
class AllToAllConnected {
public UnweightedGraph G;
public static void main(String[] args) throws Exception {
new AllToAllConnected().run();
}
public void run() throws Exception {
G = new UnweightedGraph();
G.loadFromStdin();
int u = 0;
while (u < G.numberOfNodes() / 256) { /* checking all nodes takes too long */
boolean connectedToAll = true;
int v = 0;
while (v < G.numberOfNodes()) {
connectedToAll = connectedToAll && isConnected(u, v);
v = v + 1;
}
if (connectedToAll) {
System.out.println(1);
} else {
System.out.println(0);
}
u = u + 1;
}
}
public boolean isConnected(int s, int t) {
buildConnectedArray(s);
return scanned[t];
}
public boolean[] scanned;
public void buildConnectedArray(int s) {
scanned = new boolean[G.numberOfNodes()];
int u = 0;
while (u < G.numberOfNodes()) {
scanned[u] = false;
u = u + 1;
}
connectionScan(s);
}
public void connectionScan(int u) {
scanned[u] = true;
int e = G.firstEdge(u);
while (e < G.firstInvalidEdge(u)) {
int v = G.edgeTarget(e);
if (!scanned[v] && u != v) {
connectionScan(v);
}
e = e + 1;
}
}
}
\ No newline at end of file
import lib.MinHeap;
class MinHeapTest {
public static void main(String[] args) {
MinHeap heap = new MinHeap();
/* populate it */
heap.insert(60);
System.out.println(heap.minKey());
heap.insert(10);
System.out.println(heap.minKey());
heap.insert(5);
System.out.println(heap.minKey());
heap.insert(20);
System.out.println(heap.minKey());
/* now make it empty again */
System.out.println(heap.deleteMin());
System.out.println(heap.deleteMin());
System.out.println(heap.deleteMin());
System.out.println(heap.deleteMin());
/* now do both */
heap.insert(100);
System.out.println(heap.deleteMin());
heap.insert(100);
heap.insert(90);
System.out.println(heap.deleteMin());
heap.insert(80);
heap.insert(70);
System.out.println(heap.deleteMin());
System.out.println(heap.deleteMin());
System.out.println(heap.deleteMin());
/* test decreaseKey */
int handle1 = heap.insert(100);
int handle2 = heap.insert(50);
heap.decreaseKey(handle2, 40);
heap.decreaseKey(handle1, 10);
System.out.println(heap.deleteMin());
System.out.println(heap.deleteMin());
}
}
\ No newline at end of file
package lib;
public class MinHeap {
public MinHeapEntry[] heap;
public int heapSize;
public int heapCapacity;
public int insert(int key) {
int handle = heapSize;
MinHeapEntry entry = new MinHeapEntry();
entry.key = key;
entry.handle = handle;
if (heapSize == heapCapacity) growHeap();
heap[handle] = entry;
heapSize = heapSize + 1;
siftDown(handle);
return handle;
}
public void decreaseKey(int handle, int key) {
/* find our element */
int i = 0;
boolean found = false;
while (i < heapSize && !found) {
if (heap[i].handle == handle) {
found = true;
}
i = i + 1;
}
i = i - 1;
/* then change the key and restore heap property */
heap[i].key = key;
siftUp(i);
}
public int minKey() {
return heap[0].key;
}
public int deleteMin() {
int key = heap[0].key;
int i = 1;
while (i < heapSize) {
heap[i - 1] = heap[i];
i = i + 1;
}
heapSize = heapSize - 1;
if (heapSize > 0) siftUp(0);
return key;
}
/* private methods */
public void growHeap() {
int newCapacity = 0;
if (heapCapacity == 0) {
newCapacity = 1;
} else {
newCapacity = heapCapacity * 2;
}
MinHeapEntry[] newHeap = new MinHeapEntry[newCapacity];
int i = 0;
while (i < heapCapacity) {
newHeap[i] = heap[i];
i = i + 1;
}
heapCapacity = newCapacity;
heap = newHeap;
}
public void siftUp(int pos) {
int currentKey = heap[pos].key;
int lhsChild = 2 * pos + 1;
int rhsChild = 2 * pos + 2;
if (rhsChild < heapSize) {
int lhsKey = heap[lhsChild].key;
int rhsKey = heap[rhsChild].key;
if (lhsKey < currentKey || rhsKey < currentKey) {
int swapPosition = rhsChild;
if (lhsKey < rhsKey) {
swapPosition = lhsChild;
}
swap(pos, swapPosition);
siftUp(swapPosition);
}
return;
}
if (lhsChild < heapSize) {
if (heap[pos].key > heap[lhsChild].key) {
swap(pos, lhsChild);
siftUp(lhsChild);
}
}
}
public void siftDown(int pos) {
if (pos <= 0) return;
int parentPos = (pos - 1) / 2;
if (heap[parentPos].key > heap[pos].key) {
swap(parentPos, pos);
siftDown(parentPos);
}
}
public void swap(int a, int b) {
MinHeapEntry tmp = heap[a];
heap[a] = heap[b];
heap[b] = tmp;
}
}
class MinHeapEntry {
public int handle;
public int key;
}
\ No newline at end of file
package lib;
public class UnweightedGraph {
public int firstEdge(int u) {
return nodes[u];
}
public int firstInvalidEdge(int u) {
return nodes[u + 1];
}
public int edgeTarget(int e) {
return edges[e];
}
public int numberOfNodes() {
return numberOfNodes;
}
public int numberOfEdges() {
return numberOfEdges;
}
/**
* Reads unweighted graphs in METIS format.
*/
public void loadFromStdin() throws Exception {
numberOfNodes = readIntFromStdin();
numberOfEdges = readIntFromStdin() * 2;
readIntFromStdin(); /* new line */
nodes = new int[numberOfNodes + 1];
edges = new int[numberOfEdges];
int u = 0;
int e = 0;
while (u < numberOfNodes) {
int v;
while ((v = readIntFromStdin()) != -1) {
edges[e] = v - 1;
e = e + 1;
}
u = u + 1;
nodes[u] = e;
}
}
/* private */
public int numberOfNodes;
public int[] nodes;
public int numberOfEdges;
public int[] edges;
public boolean endedOnNewLine;
/**
* @return the next integer or a negative integer if a new line character is read.
*/
public int readIntFromStdin() throws Exception {
if (endedOnNewLine) {
endedOnNewLine = false;
return -1;
}
int ch;
while (((ch = System.in.read()) < 48 || ch > 57) && ch != 10);
if (ch == 10) return -1;
int num = 0;
while (48 <= ch && ch <= 57) {
num = num * 10 + (ch - 48);
ch = System.in.read();
}
if (ch == 10) endedOnNewLine = true;
return num;
}
}
\ No newline at end of file
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment