Dijkstra Algorithm's source node and graph for grid problem - java

My grid
Im trying to implement Dijkstra algorithm for grid-like graph attached above to find shortest path from source node to every other node. Having watched many tutorials I fail to understand how I am supposed to define my starting node and graph.
As far as I understand grid doesnt change how algorithm/implementation works. In every tutorial I read/watched source is always "0". For example I read this: Javatpoint article. This is their input: Javapoint graph, Javatpoint Driver class.
So I wonder do the order of rows in input 'graph' matter? Does first row always have to be the row of source node?
This is my attempt at implementing driver class for my grid:
int grph[][] = new int[][] {
//S A B C D E F G H I J K
{ 0,-1,-1, 1,-1,-1, 1, 1,-1,-1, 2,-1 }, // S
{-1, 0, 1,-1,-1, 4,-1,-1,-1,-1,-1,-1 }, // A
{-1, 1, 0, 4,-1,-1, 9,-1,-1,-1,-1,-1 }, // B
{ 1,-1, 4, 0, 3,-1,-1,-1,-1,-1,-1,-1 }, // C
{-1,-1,-1, 3, 0,-1,-1, 8,-1,-1,-1,-1 }, // D
{-1, 4,-1,-1,-1, 0, 2,-1, 2,-1,-1,-1 }, // E
{ 1,-1, 9,-1,-1, 2, 0,-1,-1, 3,-1,-1 }, // F
{ 1,-1,-1,-1, 8,-1,-1, 0,-1,-1,-1, 6 }, // G
{-1,-1,-1,-1,-1, 2,-1,-1, 0, 1,-1,-1 }, // H
{-1,-1,-1,-1,-1,-1, 3,-1, 1, 0, 2,-1 }, // I
{ 2,-1,-1,-1,-1,-1,-1,-1,-1, 2, 0, 2 }, // J
{-1,-1,-1,-1,-1,-1,-1, 6,-1,-1, 2, 0 }, // K
};
DijkstraExample obj = new DijkstraExample();
obj.dijkstra(grph, 0);
}
Is my graph and source correct?

do the order of rows in input 'graph' matter?
No.
Does first row always have to be the row of source node?
No.
For instance, you could reorganise the order, so that "S" is ordered after "F":
int grph[][] = new int[][] {
//A B C D E F S G H I J K
{ 0, 1,-1,-1, 4,-1,-1,-1,-1,-1,-1,-1 }, // A
{ 1, 0, 4,-1,-1, 9,-1,-1,-1,-1,-1,-1 }, // B
{-1, 4, 0, 3,-1,-1, 1,-1,-1,-1,-1,-1 }, // C
{-1,-1, 3, 0,-1,-1,-1, 8,-1,-1,-1,-1 }, // D
{ 4,-1,-1,-1, 0, 2,-1,-1, 2,-1,-1,-1 }, // E
{-1, 9,-1,-1, 2, 0, 1,-1,-1, 3,-1,-1 }, // F
{-1,-1, 1,-1,-1, 1, 0, 1,-1,-1, 2,-1 }, // S
{-1,-1,-1, 8,-1,-1, 1, 0,-1,-1,-1, 6 }, // G
{-1,-1,-1,-1, 2,-1,-1,-1, 0, 1,-1,-1 }, // H
{-1,-1,-1,-1,-1, 3,-1,-1, 1, 0, 2,-1 }, // I
{-1,-1,-1,-1,-1,-1, 2,-1,-1, 2, 0, 2 }, // J
{-1,-1,-1,-1,-1,-1,-1, 6,-1,-1, 2, 0 }, // K
};
This involves moving both the S-row and S-column.
You would then call the solver with the appropriate index:
DijkstraExample obj = new DijkstraExample();
obj.dijkstra(grph, 6);
Is my graph and source correct?
The matrix and the main code (2 statements) are correct, but much depends on what your class does.

Related

Find how many connected groups of nodes in a given adjacency matrix

I have a list of lists, each list is a node and contains the edges to other nodes. e.g
[[1, 1, 0], [1, 1, 0], [0, 0, 1]]
The node has a 1 when it refers to its own position, as well as when it has an edge to another node, and a 0 when no edge exists.
This means that node 0 ([1, 1, 0]) is connected to node 1, and node 2 ([0,0,1]) is not connected to any other nodes. Therefore this list of lists can be thought of as an adjacency matrix:
1 1 0 <- node 0
1 1 0 <- node 1
0 0 1 <- node 2
Adding on to this, whether a node is connected with another is transitive, meaning that if node 1 is connected to node 2 and node 2 is connected to node 3, nodes 1 and 3 are also connected (by transitivity).
Taking all this into account, I want to be able to know how many connected groups there are given a matrix. What algorithm should I use, recursive DFS? Can someone provide any hints or pseudocode as to how this problem can be approached?
If the input matrix is guaranteed to describe transitive connectivity, it has a peculiar form that allows for an algorithm probing only a subset of the matrix elements. Here is an example implementation in Python:
def count_connected_groups(adj):
n = len(adj)
nodes_to_check = set([i for i in range(n)]) # [] not needed in python 3
count = 0
while nodes_to_check:
count += 1
node = nodes_to_check.pop()
adjacent = adj[node]
other_group_members = set()
for i in nodes_to_check:
if adjacent[i]:
other_group_members.add(i)
nodes_to_check -= other_group_members
return count
# your example:
adj_0 = [[1, 1, 0], [1, 1, 0], [0, 0, 1]]
# same with tuples and booleans:
adj_1 = ((True, True, False), (True, True, False), (False, False, True))
# another connectivity matrix:
adj_2 = ((1, 1, 1, 0, 0),
(1, 1, 1, 0, 0),
(1, 1, 1, 0, 0),
(0, 0, 0, 1, 1),
(0, 0, 0, 1, 1))
# and yet another:
adj_3 = ((1, 0, 1, 0, 0),
(0, 1, 0, 1, 0),
(1, 0, 1, 0, 0),
(0, 1, 0, 1, 0),
(0, 0, 0, 0, 1))
for a in adj_0, adj_1, adj_2, adj_3:
print(a)
print(count_connected_groups(a))
# [[1, 1, 0], [1, 1, 0], [0, 0, 1]]
# 2
# ((True, True, False), (True, True, False), (False, False, True))
# 2
# ((1, 1, 1, 0, 0), (1, 1, 1, 0, 0), (1, 1, 1, 0, 0), (0, 0, 0, 1, 1), (0, 0, 0, 1, 1))
# 2
# ((1, 0, 1, 0, 0), (0, 1, 0, 1, 0), (1, 0, 1, 0, 0), (0, 1, 0, 1, 0), (0, 0, 0, 0, 1))
# 3
An optimized version of the same algorithm (less readable, but faster and more easily translatable into other languages) is the following:
def count_connected_groups(adj):
n = len(adj)
nodes_to_check = [i for i in range(n)] # [0, 1, ..., n-1]
count = 0
while n:
count += 1
n -= 1; node = nodes_to_check[n]
adjacent = adj[node]
i = 0
while i < n:
other_node = nodes_to_check[i]
if adjacent[other_node]:
n -= 1; nodes_to_check[i] = nodes_to_check[n]
else:
i += 1
return count
There are many approaches to do this. You can use DFS/BFS or disjoint sets to solve this problem. Here are some useful links:
https://www.geeksforgeeks.org/connected-components-in-an-undirected-graph/
https://www.geeksforgeeks.org/find-the-number-of-islands-set-2-using-disjoint-set/
Solution with Java syntax:
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.Stack;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
public class ConnectedGroups {
public static void main(String[] args) {
List<String> adj0 = Arrays.asList("110", "110", "001");
List<String> adj1 = Arrays.asList("10000","01000","00100","00010","00001");
List<String> adj2 = Arrays.asList("11100","11100","11100","00011","00011");
List<String> adj3 = Arrays.asList("10100","01010","10100","01010","00001");
for (List<String> related : Arrays.asList(adj0, adj1, adj2, adj3)) {
System.out.println(related);
System.out.println(count_connected_groups(related));
}
}
private static int count_connected_groups(List<String> adj) {
int count=0;
int n = adj.size();
Stack<Integer> nodesToCheck = new Stack<>();
nodesToCheck.addAll(IntStream.range(0,n).boxed().collect(Collectors.toList()));
while (!nodesToCheck.isEmpty()) {
count++;
Integer node = nodesToCheck.pop();
String adjacent = adj.get(node);
Set<Integer> otherGroupMembers = new HashSet<>();
for (Integer i : nodesToCheck) {
if (adjacent.charAt(i) == '1') otherGroupMembers.add(i);
}
nodesToCheck.removeAll(otherGroupMembers);
}
return count;
}
}

Iterate through a 2d array replacing values of columns (make code generic)

I have a CODE 2 that I would like to make generic. CODE 2 takes 2d array columns as values: x1, x2, x3, x4 and R. And generates probabilities. I would like to create a code that doest this generic for 2d arrays with lots of columns.
In my CODE 1, I've successfully printed my 2d array columns as a 1d array using the first part of my code.
Then, I would like to take each one of my columns and replace them with x1, for example, in the following line: double p1 = network.performQuery("P(+x1|+R)"). So, as I have 4 columns, I want a loop that iterates for x1, x2, x3, x4. Notice my last column is named R.
I thought about this code but it's not working:
CODE 1
public static void main(String[] args) {
double DATA[][] = {
// x1,x2,x3,x4,R
{ 1, 0, 0, 1, 1 }, // case 1
{ 1, 1, 1, 1, 0 }, // case 2
{ 0, 0, 1, 1, 1 }, // case 3
{ 1, 1, 1, 1, 1 }, // case 4
{ 0, 0, 0, 1, 0 }, // case 5
{ 0, 1, 1, 0, 1 }, // case 6
{ 1, 1, 1, 0, 1 }, // case 7
{ 0, 0, 0, 0, 1 }, // case 8
{ 1, 1, 1, 0, 1 }, // case 9
{ 0, 0, 0, 0, 1 }, // case 10
};
for (int j = 0; j < DATA[0].length; j++) {
double[] cols = new double[DATA.length];
for (int i = 0; i < DATA.length; i++) {
cols[i] = DATA[i][j];
}
System.out.println(Arrays.toString(cols)); //prints it right
MLDataSet data = new BasicMLDataSet(DATA,null);
BayesianNetwork network = new BayesianNetwork();
network.createEvent(cols); // in CODE 2, cols = x1 is written between quotation marks
//TRAINING PART
double p1 = network.performQuery("P(+" + cols +"|+R)");
//double p1 = network.performQuery("P(+x1|R)");
System.out.println("x1 probability : " + network.getEvent("" + cols + "").getTable().findLine(1, new int[] {1}));
System.out.println("Calculated P(+R|+" + cols+ "): " + p1);
//System.out.println("Calculated P(+R|+x1):" + p1);
}
}
CODE 2
double DATA[][] = {
// x1,x2,x3,x4,R
{ 1, 0, 0, 1, 1 }, // case 1
{ 1, 1, 1, 1, 0 }, // case 2
{ 0, 0, 1, 1, 1 }, // case 3
{ 1, 1, 1, 1, 1 }, // case 4
{ 0, 0, 0, 1, 0 }, // case 5
{ 0, 1, 1, 0, 1 }, // case 6
{ 1, 1, 1, 0, 1 }, // case 7
{ 0, 0, 0, 0, 1 }, // case 8
{ 1, 1, 1, 0, 1 }, // case 9
{ 0, 0, 0, 0, 1 }, // case 10
};
MLDataSet data = new BasicMLDataSet(DATA,null);
BayesianNetwork network = new BayesianNetwork();
network.createEvent("x1");
network.createEvent("x2");
network.createEvent("x3");
network.createEvent("x4");
network.createEvent("R");
network.finalizeStructure();
//TRAINING PART
TrainBayesian train = new TrainBayesian(network,data,10);
train.setInitNetwork(BayesianInit.InitEmpty);
while(!train.isTrainingDone()) {
train.iteration();
}
double p1 = network.performQuery("P(+x1|+R)");
double p2 = network.performQuery("P(+x2|+R)");
double p3 = network.performQuery("P(+x3|+R)");
double p4 = network.performQuery("P(+x4|+R)");
System.out.println("x1 probability : " + network.getEvent("x1").getTable().findLine(1, new int[] {1}));
System.out.println("x2 probability : " + network.getEvent("x2").getTable().findLine(1, new int[] {1}));
System.out.println("x3 probability : " + network.getEvent("x3").getTable().findLine(1, new int[] {1}));
System.out.println("x4 probability : " + network.getEvent("x4").getTable().findLine(1, new int[] {1}));
System.out.println("R probability : " + network.getEvent("R").getTable().findLine(1, new int[] {1}));
System.out.println("Calculated P(+R|+x1): " + p1);
System.out.println("Calculated P(+R|+x2): " + p3);
System.out.println("Calculated P(+R|+x3): " + p4);
System.out.println("Calculated P(+R|+x4): " + p5);
Could you please help me?

Java Linear Regression

I need to find the best fitting regression line for a set of points.
For example for this matrix:
int b [][] = { { 3, 1, 0, 0, 0, 0, 0, 0, 0 },
{ 1, 2, 3, 1, 0, 1, 0, 0, 0 },
{ 0, 1, 2, 1, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 3, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 1, 3, 0, 0 },
{ 0, 0, 0, 0, 0, 1, 2, 3, 1 },
{ 0, 0, 0, 0, 0, 1, 1, 1, 2 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 1 } };
Every number represents the amount of data points (weight I suppose) at that location (where rows are the X axis and Columns are for the Y).
I have attempted to use the SimpleRegression class from the apache mathematics library and am having some issues.
First, it doesn't appear to support weights. Second I believe that I'm doing something wrong, even for a matrix that is nothing but 1's on the main diagonal the slope/intercept results make no sense.
public static void main(String[] args) {
double a[][] = new double[9][9];
for (int i = 0; i < 9; i++)
a[i][i] = 1;
SimpleRegression r = new SimpleRegression(true);
r.addData(a);
System.out.println("Slope = " + r.getSlope());
System.out.println("Intercept = " + r.getIntercept());
}
This gives me results that are incorrect. I would assume that this matrix represents the function f(x) = x yet the slope I'm getting is -0.12499..
Could anyone point me at what I'm doing wrong?
I have a feeling I'm not only misusing the code but also the mathematics.
As the comments say, addData() expects a 2xN matrix of x y positions or individual x y positions. The following example returns a slope of 1 for a diagonal matrix as expected:
public static void main(String[] args) {
double a[][] = new double[9][9];
for (int i = 0; i < 9; i++)
a[i][i] = 1;
SimpleRegression r = new SimpleRegression(true);
addData(r, a);
System.out.println("Slope = " + r.getSlope());
System.out.println("Intercept = " + r.getIntercept());
}
public static void addData(SimpleRegression r, double[][] data) {
for(int x=0; x<data.length; x++) {
for(int y=0; y<data[0].length; y++) {
for(int i=0; i<data[x][y]; i++) {
r.addData(x, y);
}
}
}
}
The example assumes that index 0 corresponds to a position of 0, index 1 corresponds to a position of 1 and so on. If this is not the case you need to add a function to transform index to position.

tiled map in java from array

I am attempting to iterate through a 2D array of integers to generate a tiled map using Java's Graphics2D.
int[][] mapArray = {{1, 1, 1, 1, 1, 1, 1, 1},
{1, 0, 0, 0, 0, 0, 0, 1},
{1, 0, 0, 0, 0, 0, 0, 1},
{1, 0, 0, 0, 0, 0, 0, 1},
{1, 0, 0, 0, 0, 0, 0, 1},
{1, 1, 1, 1, 1, 1, 1, 1}};
public void draw(Graphics2D g2d){
for(int y = 0; y < mapArray.length; y++){
for(int x = 0; x < mapArray[0].length; x++){
if(mapArray[x][y] == 1){
ImageIcon ic = new ImageIcon("/Textures/stone.jpg");
g2d.drawImage(ic.getImage(), x, y, null);
}
else if(mapArray[x][y] == 0){
ImageIcon ic = new ImageIcon("/Textures/water.jpg");
g2d.drawImage(ic.getImage(), x, y, null);
}
I just can't seem to wrap my head around the logic of iterating a 2D array. Ideally, each 0 would represent a water tile while each 1 would represent a stone tile. Every time I run this I get a NullPointerException.
x and y are wrong way around
public void draw(Graphics2D g2d){
for(int y = 0; y < mapArray.length; y++){
for(int x = 0; x < mapArray[y].length; x++){ //you want to use y here not 0
if(mapArray[y][x] == 1){ //first box is outer array second is inner one
ImageIcon ic = new ImageIcon("/Textures/stone.jpg");
g2d.drawImage(ic.getImage(), x, y, null);
} else if(mapArray[y][x] == 0){
ImageIcon ic = new ImageIcon("/Textures/water.jpg");
g2d.drawImage(ic.getImage(), x, y, null);
}
}
}
}
I could see potentially two big issues in your code, in your code "y" represents rows and "x" represents columns but in your if statement you are picking [column][row] and while having a dry run you are probabily counting [row][column] and secondly you are always counting columns that are present in first row. if your data structure is always nXn in such case it will work but in any other case you would have different results and you might encounter ArrayIndexOutofBound exception.

Steganography program giving weird results

I am developing a steganography program for a computer programming class. It appears to gives random ascii symbols. The output is supposed to be BINARY. The encode message method was given to us by my teacher. We just have to program the decode part.
import java.awt.*;
class HideMessage {
public void encodeMessage(Picture stegoObject, int[] binaryArray) {
Pixel pixelTarget = new Pixel(stegoObject, 0, 0);
Pixel[] pixelArray = stegoObject.getPixels();
Color pixelColor = null;
int redValue = 0;
for (int x = 0; x < binaryArray.length; x++) {
redValue = binaryArray[x];
pixelTarget = pixelArray[x];
pixelTarget.setRed(redValue);
}
pixelTarget = pixelArray[binaryArray.length];
pixelTarget.setRed(255);
System.out.println("FinishedPic");
stegoObject.write("SecretMessage.bmp");
stegoObject.explore();
}
public void decodeMessage(Picture decodepic) {
int redValue = 0;
Pixel targetPixel = null;
Color pixelColor = null;
int sum = 0;
for (int x = 0; redValue < 2; x++) {
//inside nested loop to traverse the image from left to right
for (int count = 1; count < 9; count++) {
targetPixel =
decodepic.getPixel(count + (8 * x), 0);
//gets the x,y coordinate of the target pixel
pixelColor = targetPixel.getColor();
//gets the color of the target pixel
redValue = pixelColor.getRed();
if (redValue == 1) {
if (count == 1) {
sum = sum + 128;
}
if (count == 2) {
sum = sum + 64;
}
if (count == 3) {
sum = sum + 32;
}
if (count == 4) {
sum = sum + 16;
}
if (count == 5) {
sum = sum + 8;
}
if (count == 6) {
sum = sum + 4;
}
if (count == 7) {
sum = sum + 2;
}
if (count == 8) {
sum = sum + 1;
}
}
System.out.println(sum);
}
System.out.println((char)sum);
sum = 0;
} //end of the inner for loop
}
}
public class HideMessageTester {
public static void main(String[] args) {
int[] bitArray =
{ 0, 1, 1, 0, 0, 0, 1, 0, 0, 1, 1, 0, 1, 0, 0, 1, 0, 1, 1,
0, 1, 1, 1, 0, 0, 1, 1, 0, 0, 0, 0, 1, 0, 1, 1, 1, 0, 0, 1, 0, 0,
1, 1, 1, 1, 0, 0, 1 };
//int[] bitArray =
{ 0, 1, 0, 0, 1, 0, 0, 0, 0, 1, 1, 0, 1, 0, 0, 1, 0, 1, 1,
0, 0, 1, 0, 0, 0, 1, 1, 0, 0, 1, 0, 0, 0, 1,
1, 0, 0, 1, 0, 1, 0, 1, 1, 0, 1, 1, 1, 0, 0,
0, 1, 0, 0, 0, 0, 0, 0, 1, 1, 0, 1, 0, 0, 1,
0, 1, 1, 0, 1, 1, 1, 0, 0, 0, 1, 0, 0, 0, 0,
0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 1, 1, 0, 1, 1,
0, 0, 0, 1, 1, 0, 0, 0, 0, 1, 0, 1, 1, 0, 1,
0, 0, 1, 0, 1, 1, 0, 1, 1, 1, 0, 0, 0, 1, 0,
0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 1, 1, 0, 1, 1,
0, 1, 0, 0, 1, 0, 1, 1, 0, 0, 1, 1, 1, 0, 1,
1, 0, 1, 0, 0, 0, 0, 1, 1, 1, 0, 1, 0, 0, 0,
0, 1, 0, 0, 0, 0, 1};
Picture stegoObject = new Picture("Earth.bmp");
HideMessage stego = new HideMessage();
stego.encodeMessage(stegoObject, bitArray);
Picture decodeObject = new Picture("SecretMessage.bmp");
System.out.println("Now Decoding");
stego.decodeMessage(decodeObject);
}
}
First, some general pieces of advice: I think your program is overly complicated because the functions are commingling their responsibilities:
Picture stegoObject = new Picture("Earth.bmp");
HideMessage stego = new HideMessage();
stego.encodeMessage(stegoObject, bitArray);
Picture decodeObject = new Picture("SecretMessage.bmp");
System.out.println("Now Decoding");
stego.decodeMessage(decodeObject);
I was very surprised to see SecretMessage.bmp; it wasn't at all obvious that were trying to decode the object you had just created. Sure, upon reading the encodeMessage() method it was easy enough to determine where it came from, but I think this flow would have been easier:
/* encode */
Picture pic_to_steg = new Picture("foo.bmp");
HideMessage stego = new HideMessage();
Picture secret = stego.encodeMessage(pic_to_steg, bitArray);
secret.write("SecretMessage.bmp");
/* decode */
Picture pic_with_message = new Picture("SecretMessage.bmp");
int[] hidden = stego.decodeMessage(pic_with_message);
/* output `hidden` and compare against `bitArray` */
In other words: leave the file IO entirely up to the main flow of the program. Perhaps your routines will be called from a network server in the future, and the pictures will never be saved to disk. That modification will be far easier if the routines operate on Pictures and return amended Pictures and int[].
Can you test your encodeMessage() method in isolation? Perhaps look at the differences in what it does between an input file and an output file. This section looks troublesome:
public void encodeMessage(Picture stegoObject, int[] binaryArray) {
Pixel pixelTarget = new Pixel(stegoObject, 0, 0);
Pixel[] pixelArray = stegoObject.getPixels();
Color pixelColor = null;
int redValue = 0;
for (int x = 0; x < binaryArray.length; x++) {
redValue = binaryArray[x];
pixelTarget = pixelArray[x];
pixelTarget.setRed(redValue);
}
pixelTarget = pixelArray[binaryArray.length];
pixelTarget.setRed(255);
Is the pixelArray really a reference into the image that can be updated through simple assignment? I'd really expect the design to look more like this pseudo-code:
pixel p = image.getPixel(x, y);
p.setred(binaryArray[i]);
image.setPixel(x, y, p);
The decoding has some strange loops:
for (int x = 0; redValue < 2; x++) {
//inside nested loop to traverse the image from left to right
for (int count = 1; count < 9; count++) {
This loop might work exactly as you designed it, but upon a first reading, it feels very wrong: You start with x=0, you increment x each time through the loop, but you use redValue < 2 as your loop termination rule.
I would so much rather see the loop written like this:
int x = 0;
while (redValue < 2) {
/* stuff */
x++;
}
(It isn't identical; x is still valid outside the loop, which can be dangerous. However, I think this is much more clear.)
There are cases where the termination clause of a for loop isn't related to the setup or increment clauses -- in my experience, they are very rare.
In this case though, it feels like a mistake; the condition redValue < 2 a loop invariant, but the inner loop assumes it will only happen on pixels that are multiples of 8, which is an assumption that is not enforced in the encodeMessage() method.
Trying to compute an integer value from your redValues as you read them is needlessly complicating your decode routine. I suggest removing the inner loop and return an array exactly like the array passed into the encodeMessage() routine. This will be (a) easier (b) easier to debug (c) easier to test (d) a thousand times easier to handle writing bit arrays that aren't evenly divisible by 8.
Then write a second method that turns the bit array output into the sum, or ASCII characters, or EBCDIC characters, or RSA key parameters, or whatever it is that's being encoded. Don't try to do too much at once. Writing a separate method to decode the array of bits will be (a) easier (b) easier to debug (c) easier to test (d) thousand time easier to handle arbitrary output modifications.
I hope these hints help.

Categories

Resources