Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DecimalToHexadecimal and BFS #1261

Open
wants to merge 3 commits into
base: Development
from
Open
Changes from all commits
Commits
File filter...
Filter file types
Jump to…
Jump to file
Failed to load files.

Always

Just for now

Binary file not shown.
@@ -0,0 +1,10 @@
/AnyBaseToDecimal.class
/BinaryToGray.class
/DecimalToAnyBase.class
/DecimalToHexadecimal.class
/DecimalToOctal.class
/AnyBaseToDecimalTest.class
/BinaryToGrayTest.class
/DecimalToAnyBaseTest.class
/DecimalToHexadecimalTest.class
/DecimalToOctalTest.class
@@ -0,0 +1,53 @@
package com.conversions;

import java.math.BigInteger;

public class DecimalToHexadecimal {
private static final char[] hexChars = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'};
private static final BigInteger valueHex = BigInteger.valueOf(16);
private static final int sizeOfIntInHalfBytes = 8;
private static final int numberOfBitsInAHalfByte = 4;
private static final int halfByte = 0x0F;
private static final char[] hexDigits = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E',
'F' };

/**
* This method converts and decimal number to a Hexadecimal number
*
* @param decimalStr
* @return hexadecimal number
*/
public String decimalToHex(String decimalStr) {
BigInteger decimal = new BigInteger(decimalStr);

int rem;
String hex = "";
while (decimal.compareTo(BigInteger.ZERO) > 0) {
rem = decimal.mod(valueHex).intValueExact();
hex = hexChars[rem] + hex;
decimal = decimal.divide(valueHex);
}
return hex;
}
public static String decToHex(int dec) {
StringBuilder hexBuilder = new StringBuilder(sizeOfIntInHalfBytes);
hexBuilder.setLength(sizeOfIntInHalfBytes);
for (int i = sizeOfIntInHalfBytes - 1; i >= 0; --i) {
int j = dec & halfByte;
hexBuilder.setCharAt(i, hexDigits[j]);
dec >>= numberOfBitsInAHalfByte;
}
return hexBuilder.toString().toLowerCase();
}

// Test above function.
public static void main(String[] args) {
System.out.println("Test...");
int dec = 70654444;
String libraryDecToHex = Integer.toHexString(dec);
String decToHex = decToHex(dec);
System.out.println("Result from the library : " + libraryDecToHex);
System.out.println("Result decToHex method : " + decToHex);
}
}

@@ -0,0 +1 @@
package com.conversions;
@@ -0,0 +1,2 @@
/Base64.class
/Base64Test.class
@@ -0,0 +1,2 @@
/Sha2.class
/Sha2Test.class
File renamed without changes.
@@ -0,0 +1,9 @@
/BinaryTree.class
/DisjointSet$Node.class
/DisjointSet.class
/Stack.class
/GeneralQueue.class
/BinaryTreeTest.class
/DisjointSetTest.class
/GeneralQueueTest.class
/StackTest.class
@@ -0,0 +1,11 @@
/AbstractShapeFactory.class
/Circle.class
/FactoryProvider.class
/FactoryType.class
/Line.class
/Shape.class
/ShapeType.class
/Sphere.class
/ThreeDShapeFactory.class
/TwoDShapeFactory.class
/AbstractShapeFactoryTest.class
@@ -0,0 +1,3 @@
/Desktop$DesktopBuilder.class
/Desktop.class
/DesktopBuilderTest.class
@@ -0,0 +1,6 @@
/Pentagon.class
/Polygon.class
/PolygonFactory.class
/Square.class
/Triangle.class
/PolygonFactoryTest.class
@@ -0,0 +1,6 @@
/BlackColor.class
/BlueColor.class
/ColorStore.class
/Color.class
/RedColor.class
/PrototypeTest.class
@@ -0,0 +1,2 @@
/Singleton.class
/SingletonTest.class
@@ -0,0 +1 @@
/DecoratorDemo.class
@@ -0,0 +1,5 @@
/BugattiVeyron.class
/Movable.class
/MovableAdapter.class
/MovableAdapterImpl.class
/MovableAdapterTest.class
@@ -0,0 +1,5 @@
/CompressingDecorator.class
/EmailSender.class
/EncodingDecorator.class
/Sender.class
/SenderDecorator.class
@@ -0,0 +1 @@
/Citizen.class
@@ -0,0 +1,2 @@
/President.class
/PresidentSecretary.class
@@ -0,0 +1,4 @@
/SimplexNoise.class
/SimplexNoiseOctave$Gradient.class
/SimplexNoiseOctave.class
/SimplexNoiseTest.class
@@ -0,0 +1,3 @@
/Dijkstra.class
/DijkstraTest.class
/Dijkstra$EntryComparator.class
@@ -0,0 +1,83 @@
package com.graphs;

import java.util.Comparator;
import java.util.Hashtable;
import java.util.PriorityQueue;

import org.jgrapht.graph.DefaultDirectedWeightedGraph;
import org.jgrapht.graph.DefaultEdge;

/**
* Hello world!
*
*/
public class Dijkstra {
DefaultDirectedWeightedGraph<Integer, DefaultEdge> dag;
Integer startVertex, endVertex;
Hashtable<Integer, Double> distance;
PriorityQueue<Integer> queue;

public Dijkstra(DefaultDirectedWeightedGraph<Integer, DefaultEdge> dag,
Integer startVertex, Integer endVertex) {
super();
this.dag = dag;
this.startVertex = startVertex;
this.endVertex = endVertex;
}

public double getPathLength() {
// init single source
initSingleSource();

// make priority queue
queue.addAll(dag.vertexSet());

// priority first search
while (!queue.isEmpty()) {
Integer vertexU = queue.poll();
if (distance.get(vertexU) == Double.MAX_VALUE)
break;

for (Integer vertexV : dag.vertexSet()) {
if (dag.containsEdge(vertexU, vertexV))
relax(vertexU, vertexV,
dag.getEdgeWeight(dag.getEdge(vertexU, vertexV)));
}

}
// return distance
return distance.get(endVertex);
}

private void relax(Integer vertexU, Integer vertexV, double edgeWeight) {

if (distance.get(vertexU) + edgeWeight < distance.get(vertexV))
distance.put(vertexV, distance.get(vertexU) + edgeWeight);

}

private void initSingleSource() {
distance = new Hashtable<Integer, Double>();
// init the distance table
for (Integer v : dag.vertexSet())
if (v == startVertex)
distance.put(v, 0.0);
else
distance.put(v, Double.MAX_VALUE);
EntryComparator compare = new EntryComparator();

queue = new PriorityQueue<Integer>(dag.vertexSet().size(), compare);
}

class EntryComparator implements Comparator<Integer> {

public int compare(Integer arg0, Integer arg1) {
if (distance.get(arg0) < distance.get(arg1))
return -1;
else
return 1;

}

}
}
@@ -0,0 +1,59 @@
package com.graphs;

import java.io.Serializable;
import java.util.Iterator;
import java.util.Random;
import java.util.Set;

import junit.framework.TestCase;

import org.jgrapht.alg.shortestpath.DijkstraShortestPath;
import org.jgrapht.graph.DefaultDirectedWeightedGraph;
import org.jgrapht.graph.DefaultEdge;
import org.jgrapht.graph.DefaultWeightedEdge;


public class DijkstraTest extends TestCase {

int numberOfEdges = 100;


public void testDijkstra() {
// Create the graph object; it is null at this point
DefaultDirectedWeightedGraph<Integer, DefaultEdge> dag = new DefaultDirectedWeightedGraph<Integer, DefaultEdge>(
DefaultEdge.class);

// add a number of vertices
for (int i = 0; i < this.numberOfEdges; i++) {
Integer nextV = new Integer(i);
dag.addVertex(nextV);
}

Set<Integer> vertices = dag.vertexSet();
// fill in edges
Random rand = new Random();
for (Integer one : vertices)
for (Integer other : vertices) {
if (other != one) {
Serializable edge = new DefaultWeightedEdge();
dag.addEdge(one, other);
dag.setEdgeWeight(edge, rand.nextDouble());
}
}

// get shortest path in two ways
Iterator<Integer> iter = vertices.iterator();
Integer startVertex = iter.next();
Integer endVertex = iter.next();

DijkstraShortestPath<Integer, DefaultEdge> path1 = new DijkstraShortestPath<Integer, DefaultEdge>(
dag, startVertex);

Dijkstra path2 = new Dijkstra(dag, startVertex, endVertex);

assertEquals(path1.getPathLength(), path2.getPathLength());

}
};


@@ -0,0 +1,2 @@
/GaleShapley.class
/GaleShapleyTest.class
@@ -0,0 +1,2 @@
/FastPower.class
/FastPowerTest.class
File renamed without changes.
@@ -0,0 +1,18 @@
/BFS.class
/BinarySearch.class
/BloomFilter$Builder.class
/BloomFilter.class
/ExponentialSearch.class
/FibonacciSearch.class
/InterpolationSearch.class
/JumpSearch.class
/LinearSearch.class
/vertex.class
/BFSTest.class
/BinarySearchTest.class
/BloomFilterTest.class
/ExponentialSearchTest.class
/FibonacciSearchTest.class
/InterpolationSearchTest.class
/JumpSearchTest.class
/LinearSearchTest.class
@@ -0,0 +1,70 @@
package com.search;
import java.awt.Color;
import java.io.*;
import java.util.*;

import java.util.Arrays;
import java.util.PriorityQueue;
import java.util.Set;

import org.jgrapht.graph.DefaultDirectedGraph;

/**
* Hello world!
*
*/
public class BFS<E extends Comparable<E>> {

private DefaultDirectedGraph<vertex, E> graph;
private Set<vertex> vertices;
vertex[] visitedVertices;

public BFS(DefaultDirectedGraph<vertex, E> graph) {
//
this.graph = graph;
this.vertices = graph.vertexSet();
}

public vertex[] breadthFirstTree(vertex source) {
// init
for (vertex v : vertices) {
v.color = Color.WHITE;
v.distance = Double.MAX_VALUE;
v.prevVertex = null;
}

source.color = Color.GRAY;
source.distance = 0;
source.prevVertex = null;

// start bfs
PriorityQueue<vertex> q = new PriorityQueue<vertex>();
q.offer(source);

while(!q.isEmpty()) {
vertex u = q.poll();
for (vertex adj : vertices)
if (graph.containsEdge(u,adj) && adj.color == Color.WHITE) {
adj.color = Color.GRAY;
adj.distance = u.distance + 1;
adj.prevVertex = u;
q.offer(adj);
}
u.color = Color.BLACK;
}

this.visitedVertices = new vertex[vertices.size()];
int i = 0;
for (vertex v : vertices)
if (i < vertices.size()) {
visitedVertices[i] = v;
i++;
}

Arrays.sort(visitedVertices);
return visitedVertices;

}

}

ProTip! Use n and p to navigate between commits in a pull request.
You can’t perform that action at this time.