I've implemented a BFS algorithm from my textbook and I am trying to modify it to throw an exception when it discovers a non-connected graph. My BFS using an array of boolean to store if a node has been reached or not. After running the BFS from the root node I thought I could iterate through the array and check if each node was reached. My code throws an exception every time and I cannot figure out why. Any guidance would be appreciated thanks!
Code:
private int bfs(Graph G, int s) {
int d = 0;
Queue<Integer> q = new Queue<>();
int distTo[] = new int[G.V()], max = 0;
boolean[] marked = new boolean[G.V()];
int[] edgeTo = new int[G.V()];
for(int v = 0; v < G.V(); v++) {
distTo[s] = Integer.MAX_VALUE;
marked[s] = true;
distTo[s] = 0;
q.enqueue(s);
}
while(!q.isEmpty()) {
d = q.dequeue();
for(int w : G.adj(d)) {
if(!marked[w]) {
edgeTo[w] = d;
distTo[w] = distTo[d] + 1;
marked[w] = true;
q.enqueue(w);
}
}
for(boolean x : marked) {
if(x == false) throw new RuntimeException("not a connected graph.");
}
}
return d;
}
You check for connectivity after processing each vertex. Only in the simplest graphs will the test succeed after the first vertex.
Instead you should seed the queue with one vertex and move the for loop testing for connectivity out of the while loop.
Related
So I'm working with the java implementation of Max Heaps. My Insert, bubbleUp and deleteMax (on its own) methods seem to work fine, but my heapsort method (which calls deleteMax) doesn't work as its supposed to (it doesn't cause an error message; it just doesn't sort them in the order it's supposed to). I've included the code below. Any help understanding the problem is greatly appreciated. Thanks!
The whole class can be found at: https://repl.it/repls/FrequentPartialBlockchain
'''
public int deleteMax(){
if(this.numNodes == 0)
throw new NoSuchElementException();
else if(this.numNodes == 1){
int elemToReturn = heapArr[0];
heapArr[0] = null;
return elemToReturn;
}
int elemToReturn = heapArr[0];
heapArr[0] = heapArr[numNodes-1];
heapArr[numNodes-1] = null;
this.numNodes--;
bubbleDown();
return elemToReturn;
}
private void bubbleDown(){
int n = 0;
int L = 2 * n + 1; // L will hold the index of the left child
while(L < this.numNodes - 1){
int max = L;
int R = L + 1; // R will hold the index of the right child
if(R < this.numNodes - 1){
if(heapArr[R] >= heapArr[L])
max++;
}
int temp;
if(heapArr[n] < heapArr[max]){
// swap
temp = heapArr[n];
heapArr[n] = heapArr[max];
heapArr[max] = temp;
n = max;
L = 2 * n + 1;
}
else{
break;
}
}
}
public static void heapsort(Integer[] arrayToSort){
MaxHeap tempHeap = new MaxHeap(arrayToSort);
for(int i = 0; i < tempHeap.numNodes; i++)
arrayToSort[i] = (Integer) tempHeap.deleteMax();
}
'''
This while statement seems wrong:
while(L < this.numNodes - 1){
If this.numNodes is the number of nodes in the heap, then this.numNodes - 1 is the last node. This condition, then, prevents the loop from being entered if L is the last node in the heap.
On a related note, your special case in deletMax is broken. You remove the only node in the heap, but you forgot to set numNodes to 0.
My task is to write a program to calculate shortest time it takes to go from node A(start) to node B(end) on a weighted graph(I'm using dijkstra algorithm).
Time requirement is 2 seconds on memory limit is 64 MB and I can assume that entered data is valid
First line has 4 whole numbers:
Amount of nodes M(2 ≤ M ≤ 20000)
Amount of edges(connections) N(0 ≤ N ≤ 50000)
Start node A(0 ≤ A < M) and end node O (0 ≤ O < M).
Next N lines has connection info represented with three whole numbers:
connected computers and the time between them in milliseconds(edge weight).
My code doesn't pass all the tests. If i test my code with a big graph then it fails as heap memory runs out(about 900MB on my machine).
My problem: How do i optimize my code so it fits into requirements?
Would using adjacency list be enough?
My code is this:
import java.util.ArrayList;
import java.util.Scanner;
public class Route3 {
public static int minDistance(ArrayList<Long> mindist, ArrayList<Boolean> visited){
long min = Long.MAX_VALUE;
int minindex = -1;
for (int i = 0; i < mindist.size(); i++) {
if(!visited.get(i) && (mindist.get(i) <= min)) {
min = mindist.get(i);
minindex = i;
}
}
return minindex;
}
public static long dijkstra(long[][] graph, int start, int end) {
int computers = graph.length;
ArrayList<Boolean> traversed = new ArrayList<>(); //Hold traversed nodes
for (int i = 0; i < computers; i++) {
traversed.add(i,false);
}
ArrayList<Long> mindist = new ArrayList<>(); //Holds mindistances to nodes based on index
for (int i = 0; i < computers; i++) {
mindist.add(i,Long.MAX_VALUE);
}
mindist.set(start,(long)0);
for (int i = 0; i < computers; i++) {
int min = minDistance(mindist,traversed);
if(min == -1) return mindist.get(end);
traversed.set(min,true);
if(min == end) return mindist.get(min) == Long.MAX_VALUE ? -1 : mindist.get(min); //Error check
for (int j = 0; j < computers; j++) {
if(!traversed.get(j) && graph[min][j] != 0 && mindist.get(min) + graph[min][j] < mindist.get(j)) {
mindist.set(j,(mindist.get(min) + graph[min][j]));
}
}
}
return mindist.get(end) == Long.MAX_VALUE ? -1 : mindist.get(end);
}
public static void main(String[] args){
Scanner in = new Scanner(System.in);
int computers = in.nextInt(); //nodes
int connections = in.nextInt(); //edges
int start = in.nextInt();
int end = in.nextInt();
long[][] graph = new long[computers+1][computers+1];
for (int i = 0; i < connections; i++) {
int x = in.nextInt();
int y = in.nextInt();
long t = in.nextLong();
graph[x][y] = t;
graph[y][x] = t;
}
if(connections == 0) {
System.out.println(-1);
System.exit(0);
}
long dist = dijkstra(graph,start,end);
if(dist != -1) System.out.println(dist);
else System.out.println(-1);
}
}
All help is appreciated!
I think that you'll have to find a better way to hold the graph information. This line:
long[][] graph = new long[computers+1][computers+1]
could take up to 20001*20001*8 bytes, that is 3GB!
As your net has few connections per node I'd suggest storing the graph as a HashMap of HashMaps of connections:
class Connection {
int nodeA, nodeB;
long time;
}
HashMap<Integer, HashMap<Integer, Connection>> graph;
It may sound less efficient, but you are saving all the blank edges. Then you add the Connection to the graph indexed by both nodes (nodeA and nodeB):
void addConnection(Connection c)
{
HashMap<Integer, Connection> subgraph = graph.get(c.nodeA);
if(subgraph == null)
subgraph = new HashMap<>();
subgraph.put(c.nodeB, c);
HashMap<Integer, Connection> subgraph = graph.get(c.nodeB);
if(subgraph == null)
subgraph = new HashMap<>();
subgraph.put(c.nodeA, c);
}
And retrieve the connection descriptor like:
long getConnection(int nodeA, int nodeB)
{
ArrayList<Connection> subgraph = graph.get(nodeA);
if(subgraph == null)
return 0L;
Connection c = subgraph.get(nodeB);
if(c == null)
return 0L;
return c.time;
}
This should make your program slower but much more memory efficient.
WARNING: The code is not tested, it's just for clarification.
I'm trying to learn Prim's algorithm and I'm using this website to do so, but I'm having trouble making the code part of it run. I'm confused with what goes in public static int Prims(Vector<Vector<node>> adjList) and how to get the code to compile and run. (New to java so excuse me if its a silly question).
edit: This is the code I'm trying to run:
class node implements Comparable<node> {
int weight, index;
public node(int weight, int index) {
this.weight = weight;
this.index = index;
}
public int compareTo(node e) {
return weight - e.weight;
}
}public static int Prims(Vector<Vector<node>> adjList) {
// Current cost of MST.
int cost = 0;
int n = adjList.size();
PriorityQueue<node> pq = new PriorityQueue<node>();
// Keep track if each node is visited.
boolean visited[] = new boolean[n];
for (int i = 0; i < n; i++) {
visited[i] = false;
}
// Number of nodes visited.
int inTree = 1;
// Mark starting node as visited.
visited[0] = true;
// Add all edges of starting node.
for (int i = 0; i < adjList.get(0).size(); i++) {
pq.add(adjList.get(0).get(i));
}
// Keep going until all nodes visited.
while (!pq.isEmpty() && inTree < n) {
// Get the edge with the smallest weight.
node cur = pq.poll();
// Skip if node already used.
if (visited[cur.index]) {
continue;
}
inTree++;
visited[cur.index] = true;
cost += cur.weight;
// Add all the edges of the new node to the priority queue.
for (int i = 0; i < adjList.get(cur.index).size(); i++) {
pq.add(adjList.get(cur.index).get(i));
}
}
// Graph not connected if number of nodes used is less than total nodes.
if (inTree < n) {
return -1;
}
return cost;
}
Your method public static int Prims(Vector<Vector<node>> adjList) does not appear to be a member of a class. It needs to be. The leading } on the line
}public static int Prims(Vector<Vector<node>> adjList) {
should probable be moved to the end of the file.
If you do not use an IDE to compile and run a code you need to issue the following commands:
javac MyCode.java
java MyCode
where I suppose your code resides in a file named MyCode.java and there is no package defined.
This question already has answers here:
What causes a java.lang.ArrayIndexOutOfBoundsException and how do I prevent it?
(26 answers)
Closed 1 year ago.
I know this is an amaetuer error, i understand what it means but i dont understand why i cant fix it. Ive been trying everything. Im trying to take an array of type T and switch its values around so it correctly corresponds to the rules of a heap, where the parent is always greater than the 2 children. The error is in my while loop
please dont be harsh if its something easily fixable. ive been struggling heavily and cant seem to find an answer.
public class myheap<T extends Comparable<T>> extends heap<T>
{
// constructors of the subclass should be written this way:
public myheap(int max) { super(max); }
public myheap(T[] A) {super(A);}
public void buildheap(T[] Arr){
int size = Arr.length;
int startsize = (size-1)/2;
for(int i=startsize;i>0;i--){
int l = left(i);
int r = right(i);
T temp = null;
while((Arr[r]!=null) && Arr[i].compareTo(Arr[r])<0){
if (Arr[l].compareTo(Arr[r])>0){
temp = Arr[l];
Arr[l] = Arr[i];
Arr[i] = temp;
}//if left is greater than right
else //then right must be greater than parent
temp = Arr[r];
Arr[r] = Arr[i];
Arr[i] = temp;
}//whileloop
if((Arr[r]==null) && Arr[i].compareTo(Arr[l])<0)
temp = Arr[l];
Arr[l] = Arr[i];
Arr[i] = temp;
}//for
}//buildheap
public static void main(String[] args){
String[] array = {"SH","AH","AB","YA","AY","AA","AB","LM","LL","LO"};
myheap<String> New = new myheap<String>(array.length);
for(int i=0;i<array.length;i++){
New.insert(array[i]);
}//insert
New.buildheap(array);
New.drawheap();
for(int i=0;i<array.length;i++){
System.out.println(New.deletemax() + " ");
}//for
System.out.println();
} //main
}
Heap superclass that myheap is extending
/*
Polymorphic priority heaps, largest value on top.
Heap axiom. The value at every node cannot be smaller than the values
at each of its children nodes.
Use internal array to implement heap "tree", with index 0 representing
the root. Given node index i, left(i)= 2*i+1 and right(i)=2*i+2, while
parent(i) = (i-1)/2.
*/
class heap<T extends Comparable<T>>
{
protected T[] H; // internal array representing heap.
protected int size; // size of current heap, not same as H.length!
public int size() { return size; } // size is read-only externally.
public int maxsize() { return H.length; }
public heap(T[] A) { H = A; size=0; } // preferred constructor
public heap(int m) // will cause compiler warning (ok to ignore)
{
H = (T[]) new Comparable[m]; // downcast from Object is OK.
size = 0;
}
protected int left(int i) { return 2*i+1; }
protected int right(int i) { return 2*i+2; }
protected int parent(int i) { return (i-1)/2; }
// protected is important!
// lookup heap, without delete
public T getmax()
{
if (size<1) return null;
return H[0];
}
// insert x into heap: place at end, then propagate upwards
// returns false on failure.
public boolean insert(T x)
{
if (size > H.length-1) return false;
H[size++] = x; // place at end, inc size
// propagate upwards
int cur = size-1; // current position
int p = parent(cur);
while (cur>0 && H[cur].compareTo(H[p])>0)
{ // propagate upwards
T temp = H[cur];
H[cur] = H[p]; H[p] = temp;
cur = p; // switch current to parent
p = parent(cur); // recalc parent
}//while
return true;
}//insert
// deletetop: take last element, move to top, propagate downwards:
public T deletemax()
{
if (size<0) return null;
T answer = H[0];
H[0] = H[--size]; // place at top:
// now propagate downwards.
boolean done = false;
int i = 0; // current position
int c = 0; // swap candidate
while (c != -1)
{
int l = left(i);
int r = right(i);
c = -1; // swap candidate
if (l<size && H[l].compareTo(H[i])>0) c = l; // set candidate to left
if (r<size && H[r].compareTo(H[i])>0 && H[r].compareTo(H[l])>0) c=r;
if (c!= -1)
{
T temp = H[i]; H[i] = H[c]; H[c] = temp;
i = c;
}
}//while
return answer;
}//deletemax
// but search is not log(n). Why?
public boolean search(T x)
{
for(int i=0;i<size;i++) {if (x.compareTo(H[i])==0) return true;}
return false;
}
public void drawheap() // use only with heapdisplay.java program
{
heapdisplay W = new heapdisplay(1024,768);
W.drawtree(H,size);
}
}//heap
public class heaps14
{
/**public static void main(String[] args){
heap<Integer> HI = new heap<Integer>(200);
for(int i=0;i<100;i++) HI.insert((int)(Math.random()*1000));
HI.drawheap();
for(int i=0;i<100;i++) System.out.print(HI.deletemax() + " ");
System.out.println();
}//main**/
}
You may check for null in your while loop, (Arr[r]!=null) but the problem is that you can't even get a value from the array to determine if it's null or not. You should check the index is within the range before trying to access the value from the array, using r < Arr.length or similar.
(If null) isnt the problem, arrayIndexOutofBounds means you are geting a value of an array that isnt there
Eg. Array.length =5; and you search Array[6]; - out of bounds....
The problem i think is your method right(i);
which is. i*2+2 and the array
So change the for loop to this
for(int i=startsize-2;i>0;i--)
comment if this helps.
I have implemented a one point crossover as follows;
public void onePointCrossover(Individual indi) {
if (SGA.rand.nextDouble() < pc) {
int xoverpoint = SGA.rand.nextInt(length);
int tmp;
for (int i=xoverpoint; i<length; i++){
tmp = chromosome[i];
chromosome[i] = indi.chromosome[i];
indi.chromosome[i] = tmp;
}
}
}
One point crossover - crossover point is selected, binary string from beginning of chromosome to the crossover point is copied from one parent, the rest is copied from the second parent.
Parent 1 = chromosome and Parent 2 = indi.
I am turning the parents into children inplace.
I now need to also do a two point crossover but having some trouble, this is what I have so far but I believe the bottom half of the code is doing the same thing as a one point crossover rather than swapping the middle sections.
public void twoPointCrossover(Individual indi) {
if (SGA.rand.nextDouble() < pc) {
int xoverpoint = SGA.rand.nextInt(length);
int xoverpoint2 = SGA.rand.nextInt(length);
int tmp;
if (xoverpoint > xoverpoint2){
tmp = xoverpoint;
xoverpoint = xoverpoint2;
xoverpoint2 = tmp;
}
for (int i=xoverpoint; i<xoverpoint2; i++){
tmp = chromosome[i];
chromosome[i] = indi.chromosome[i];
indi.chromosome[i] = tmp;
}
}
}
}
This does not seem right and any help will be appreciated so much! Thanks!
You should check for i < (or <=) xoverpoint2 rather than i<length in the loop.
I'm working on the same problem now. Here is my solution:
// Two-Point Crossover function
public Genome twoPtCrossover(Genome partner) {
Genome child = new Genome(partner.genome.length);
int crosspoint1 = xd.nextInt(genome.length);
int crosspoint2 = xd.nextInt(genome.length);
// Ensure crosspoints are different...
if (crosspoint1 == crosspoint2){
if(crosspoint1 == 0){
crosspoint2++;
} else {
crosspoint1--;
}
}
// .. and crosspoint1 is lower than crosspoint2
if (crosspoint2 < crosspoint1) {
int temp = crosspoint1;
crosspoint1 = crosspoint2;
crosspoint2 = temp;
}
for (int i = 0; i < genome.length; i++) {
if (i < crosspoint1 || i > crosspoint2)
child.genome[i] = genome[i];
else
child.genome[i] = partner.genome[i];
}
return child;
}