How can I implement extendible hashing in java? - java

extendible hashing is one of the best hashing method,I want to create program in java, for extenidble hashing. is there any api available for doing that? i dont get the clear algorithm for doing that myself, so if there is no api, .if possible post algoirhtm

I'm just curious, why do you need to implement such an algorithm? Are the standard Java Map implementations not performing for you? If you are suffering from an issue of buckets becoming too heavily loaded you may want to take a look at the hashCode() method before opting for non-standard routes. An alternative could also be to look at some of the options provided by GNU Trove.
Finally - a similar algorithm to Extendible is Cuckoo hashing. Some information below:
http://en.wikipedia.org/wiki/Cuckoo_hashing
Source code here:
http://lmonson.com/blog/?page_id=99

This file defines a HashTable class. Keys and values in the hash table
are of type Object. Keys cannot be null. The default constructor
creates a table that initially has 64 locations, but a different
initial size can be specified as a parameter to the constructor.
The table increases in size if it becomes more than 3/4 full.
public class HashTable {
static private class ListNode {
// Keys that have the same hash code are placed together
// in a linked list. This private nested class is used
// internally to implement linked lists. A ListNode
// holds a (key,value) pair.
Object key;
Object value;
ListNode next; // Pointer to next node in the list;
// A null marks the end of the list.
}
private ListNode[] table; // The hash table, represented as
// an array of linked lists.
private int count; // The number of (key,value) pairs in the
// hash table.
public HashTable() {
// Create a hash table with an initial size of 64.
table = new ListNode[64];
}
public HashTable(int initialSize) {
// Create a hash table with a specified initial size.
// Precondition: initalSize > 0.
table = new ListNode[initialSize];
}
void dump() {
// This method is NOT part of the usual interface for
// a hash table. It is here only to be used for testing
// purposes, and should be removed before the class is
// released for general use. This lists the (key,value)
// pairs in each location of the table.
System.out.println();
for (int i = 0; i < table.length; i++) {
// Print out the location number and the list of
// key/value pairs in this location.
System.out.print(i + ":");
ListNode list = table[i]; // For traversing linked list number i.
while (list != null) {
System.out.print(" (" + list.key + "," + list.value + ")");
list = list.next;
}
System.out.println();
}
} // end dump()
public void put(Object key, Object value) {
// Associate the specified value with the specified key.
// Precondition: The key is not null.
int bucket = hash(key); // Which location should this key be in?
ListNode list = table[bucket]; // For traversing the linked list
// at the appropriate location.
while (list != null) {
// Search the nodes in the list, to see if the key already exists.
if (list.key.equals(key))
break;
list = list.next;
}
// At this point, either list is null, or list.key.equals(key).
if (list != null) {
// Since list is not null, we have found the key.
// Just change the associated value.
list.value = value;
}
else {
// Since list == null, the key is not already in the list.
// Add a new node at the head of the list to contain the
// new key and its associated value.
if (count >= 0.75*table.length) {
// The table is becoming too full. Increase its size
// before adding the new node.
resize();
}
ListNode newNode = new ListNode();
newNode.key = key;
newNode.value = value;
newNode.next = table[bucket];
table[bucket] = newNode;
count++; // Count the newly added key.
}
}
public Object get(Object key) {
// Retrieve the value associated with the specified key
// in the table, if there is any. If not, the value
// null will be returned.
int bucket = hash(key); // At what location should the key be?
ListNode list = table[bucket]; // For traversing the list.
while (list != null) {
// Check if the specified key is in the node that
// list points to. If so, return the associated value.
if (list.key.equals(key))
return list.value;
list = list.next; // Move on to next node in the list.
}
// If we get to this point, then we have looked at every
// node in the list without finding the key. Return
// the value null to indicate that the key is not in the table.
return null;
}
public void remove(Object key) {
// Remove the key and its associated value from the table,
// if the key occurs in the table. If it does not occur,
// then nothing is done.
int bucket = hash(key); // At what location should the key be?
if (table[bucket] == null) {
// There are no keys in that location, so key does not
// occur in the table. There is nothing to do, so return.
return;
}
if (table[bucket].key.equals(key)) {
// If the key is the first node on the list, then
// table[bucket] must be changed to eliminate the
// first node from the list.
table[bucket] = table[bucket].next;
count--; // Remove new number of items in the table.
return;
}
// We have to remove a node from somewhere in the middle
// of the list, or at the end. Use a pointer to traverse
// the list, looking for a node that contains the
// specified key, and remove it if it is found.
ListNode prev = table[bucket]; // The node that precedes
// curr in the list. Prev.next
// is always equal to curr.
ListNode curr = prev.next; // For traversing the list,
// starting from the second node.
while (curr != null && ! curr.key.equals(key)) {
curr = curr.next;
prev = curr;
}
// If we get to this point, then either curr is null,
// or curr.key is equal to key. In the later case,
// we have to remove curr from the list. Do this
// by making prev.next point to the node after curr,
// instead of to curr. If curr is null, it means that
// the key was not found in the table, so there is nothing
// to do.
if (curr != null) {
prev.next = curr.next;
count--; // Record new number of items in the table.
}
}
public boolean containsKey(Object key) {
// Test whether the specified key has an associated value
// in the table.
int bucket = hash(key); // In what location should key be?
ListNode list = table[bucket]; // For traversing the list.
while (list != null) {
// If we find the key in this node, return true.
if (list.key.equals(key))
return true;
list = list.next;
}
// If we get to this point, we know that the key does
// not exist in the table.
return false;
}
public int size() {
// Return the number of key/value pairs in the table.
return count;
}
private int hash(Object key) {
// Compute a hash code for the key; key cannot be null.
// The hash code depends on the size of the table as
// well as on the value returned by key.hashCode().
return (Math.abs(key.hashCode())) % table.length;
}
private void resize() {
// Double the size of the table, and redistribute the
// key/value pairs to their proper locations in the
// new table.
ListNode[] newtable = new ListNode[table.length*2];
for (int i = 0; i < table.length; i++) {
// Move all the nodes in linked list number i
// into the new table. No new ListNodes are
// created. The existing ListNode for each
// key/value pair is moved to the newtable.
// This is done by changing the "next" pointer
// in the node and by making a pointer in the
// new table point to the node.
ListNode list = table[i]; // For traversing linked list number i.
while (list != null) {
// Move the node pointed to by list to the new table.
ListNode next = list.next; // The is the next node in the list.
// Remember it, before changing
// the value of list!
int hash = (Math.abs(list.key.hashCode())) % newtable.length;
// hash is the hash code of list.key that is
// appropriate for the new table size. The
// next two lines add the node pointed to by list
// onto the head of the linked list in the new table
// at position number hash.
list.next = newtable[hash];
newtable[hash] = list;
list = next; // Move on to the next node in the OLD table.
}
}
table = newtable; // Replace the table with the new table.
} // end resize()
} // end class HashTable
A Program for Testing HashTable:
A little program to test the HashTable class. Note that I start with a really small table so that I can easily test the resize() method.
public class TestHashTable {
public static void main(String[] args){
HashTable table = new HashTable(2);
String key,value;
while (true) {
System.out.println("\nMenu:");
System.out.println(" 1. test put(key,value)");
System.out.println(" 2. test get(key)");
System.out.println(" 3. test containsKey(key)");
System.out.println(" 4. test remove(key)");
System.out.println(" 5. show complete contents of hash table.");
System.out.println(" 6. EXIT");
System.out.print("Enter your command: ");
switch ( TextIO.getlnInt()) {
case 1:
System.out.print("\n Key = ");
key = TextIO.getln();
System.out.print(" Value = ");
value = TextIO.getln();
table.put(key,value);
break;
case 2:
System.out.print("\n Key = ");
key = TextIO.getln();
System.out.println(" Value is " + table.get(key));
break;
case 3:
System.out.print("\n Key = ");
key = TextIO.getln();
System.out.println(" containsKey(" + key + ") is "
+ table.containsKey(key));
break;
case 4:
System.out.print("\n Key = ");
key = TextIO.getln();
table.remove(key);
break;
case 5:
table.dump();
break;
case 6:
return; // End program by returning from main()
default:
System.out.println(" Illegal command.");
break;
}
System.out.println("\nHash table size is " + table.size());
}
}
} // end class TestHashTable

Preface: I do not know what is extendible hashing.
Based on what I understood from this wiki(http://en.wikipedia.org/wiki/Extendible_hashing), that it can search using maximum two lookups, you might want to look at the Java implementation of Bernstein DB (sg-cDB).
http://cr.yp.to/cdb.html

A Java implementation of extendible hashing:
http://en.wikibooks.org/wiki/Data_Structures/Hash_Tables#A_Java_implementation_of_extendible_hashing
As srini.venigalla says... the sg-cDB of Bernstein is good. You can found a ported version of CDB in pure Java made by Michael Alyn Miller in GitHub: https://github.com/malyn/sg-cdb

// Java program to demonstrate implementation of our
// own hash table with chaining for collision detection
import java.util.ArrayList;
// A node of chains
class HashNode<K, V>
{
K key;
V value;
// Reference to next node
HashNode<K, V> next;
// Constructor
public HashNode(K key, V value)
{
this.key = key;
this.value = value;
}
}
// Class to represent entire hash table
class Map<K, V>
{
// bucketArray is used to store array of chains
private ArrayList<HashNode<K, V>> bucketArray;
// Current capacity of array list
private int numBuckets;
// Current size of array list
private int size;
// Constructor (Initializes capacity, size and
// empty chains.
public Map()
{
bucketArray = new ArrayList<>();
numBuckets = 10;
size = 0;
// Create empty chains
for (int i = 0; i < numBuckets; i++)
bucketArray.add(null);
}
public int size() { return size; }
public boolean isEmpty() { return size() == 0; }
// This implements hash function to find index
// for a key
private int getBucketIndex(K key)
{
int hashCode = key.hashCode();
int index = hashCode % numBuckets;
return index;
}
// Method to remove a given key
public V remove(K key)
{
// Apply hash function to find index for given key
int bucketIndex = getBucketIndex(key);
// Get head of chain
HashNode<K, V> head = bucketArray.get(bucketIndex);
// Search for key in its chain
HashNode<K, V> prev = null;
while (head != null)
{
// If Key found
if (head.key.equals(key))
break;
// Else keep moving in chain
prev = head;
head = head.next;
}
// If key was not there
if (head == null)
return null;
// Reduce size
size--;
// Remove key
if (prev != null)
prev.next = head.next;
else
bucketArray.set(bucketIndex, head.next);
return head.value;
}
// Returns value for a key
public V get(K key)
{
// Find head of chain for given key
int bucketIndex = getBucketIndex(key);
HashNode<K, V> head = bucketArray.get(bucketIndex);
// Search key in chain
while (head != null)
{
if (head.key.equals(key))
return head.value;
head = head.next;
}
// If key not found
return null;
}
// Adds a key value pair to hash
public void add(K key, V value)
{
// Find head of chain for given key
int bucketIndex = getBucketIndex(key);
HashNode<K, V> head = bucketArray.get(bucketIndex);
// Check if key is already present
while (head != null)
{
if (head.key.equals(key))
{
head.value = value;
return;
}
head = head.next;
}
// Insert key in chain
size++;
head = bucketArray.get(bucketIndex);
HashNode<K, V> newNode = new HashNode<K, V>(key, value);
newNode.next = head;
bucketArray.set(bucketIndex, newNode);
// If load factor goes beyond threshold, then
// double hash table size
if ((1.0*size)/numBuckets >= 0.7)
{
ArrayList<HashNode<K, V>> temp = bucketArray;
bucketArray = new ArrayList<>();
numBuckets = 2 * numBuckets;
size = 0;
for (int i = 0; i < numBuckets; i++)
bucketArray.add(null);
for (HashNode<K, V> headNode : temp)
{
while (headNode != null)
{
add(headNode.key, headNode.value);
headNode = headNode.next;
}
}
}
}
// Driver method to test Map class
public static void main(String[] args)
{
Map<String, Integer>map = new Map<>();
map.add("this",1 );
map.add("coder",2 );
map.add("this",4 );
map.add("hi",5 );
System.out.println(map.size());
System.out.println(map.remove("this"));
System.out.println(map.remove("this"));
System.out.println(map.size());
System.out.println(map.isEmpty());
}
}

private ITEM[] st; private int N, M; ST(int maxN) { N = 0; M = 4; st = new ITEM[M]; } private void expand() { ITEM[] t = st; N = 0; M = M+M; st = new ITEM[M]; for (int i = 0; i < M/2; i++) if (t[i] != null) insert(t[i]); } void insert(ITEM x) { int i = hash(x.key(), M); while (st[i] != null) i = (i+1) % M; st[i] = x; if (N++ >= M/2) expand(); }

/*
A little program to test the HashTable class. Note that I
start with a really small table so that I can easily test
the resize() method.
*/
public class TestHashTable {
public static void main(String[] args){
HashTable table = new HashTable(2);
String key,value;
while (true) {
System.out.println("\nMenu:");
System.out.println(" 1. test put(key,value)");
System.out.println(" 2. test get(key)");
System.out.println(" 3. test containsKey(key)");
System.out.println(" 4. test remove(key)");
System.out.println(" 5. show complete contents of hash table.");
System.out.println(" 6. EXIT");
System.out.print("Enter your command: ");
switch ( TextIO.getlnInt()) {
case 1:
System.out.print("\n Key = ");
key = TextIO.getln();
System.out.print(" Value = ");
value = TextIO.getln();
table.put(key,value);
break;
case 2:
System.out.print("\n Key = ");
key = TextIO.getln();
System.out.println(" Value is " + table.get(key));
break;
case 3:
System.out.print("\n Key = ");
key = TextIO.getln();
System.out.println(" containsKey(" + key + ") is "
+ table.containsKey(key));
break;
case 4:
System.out.print("\n Key = ");
key = TextIO.getln();
table.remove(key);
break;
case 5:
table.dump();
break;
case 6:
return; // End program by returning from main()
default:
System.out.println(" Illegal command.");
break;
}
System.out.println("\nHash table size is " + table.size());
}
}
} // end class TestHashTable

Hashtable implements a key value pair kind of collection
*/
import java.util.*;
public class HashtableDemo {
static String newLine = System.getProperty("line.separator");
public static void main(String[] args) {
System.out.println(newLine + "Hashtable in Java" + newLine);
System.out.println("-----------------------" + newLine);
System.out.println("Adding items to the Hashtable" + newLine);
//Creating Hashtable object
//dictionary can be created using HashTable object
//as dictionary is an abstract class
Hashtable ht = new Hashtable();
//you add elements to Hashtable using put method
//put(key, value)
ht.put("Java", 1);
ht.put(".NET", 2);
ht.put("Javascript", 3 );
ht.put("HTML", 4);
//You will see that the items will be arranged as per the hash function
System.out.println(newLine + "Items in the Hashtable..." + ht + newLine);
System.out.println("-----------------------" + newLine);
//looping through all the elements in hashtable
String str;
//you can retrieve all the keys in hashtable using .keys() method
Enumeration names = ht.keys();
while(names.hasMoreElements()) {
//next element retrieves the next element in the dictionary
str = (String) names.nextElement();
//.get(key) returns the value of the key stored in the hashtable
System.out.println(str + ": " + ht.get(str) + newLine);
}
}
}

Related

What's wrong with sorted set code? My output should match sample

Can anyone help on what's wrong with my code? Only the first input number shows up when I run it (it doesn't create a sorted list), the delete command isn't working, and the 'true' 'false' in exists command doesn't show up. My output should match the given sample I put at the end.
The areas I had to fill in to make the code work are the areas after the TODOTODOTODO symbols which would be 44-61, 75-83, 97-105. I'm not sure where I went wrong in those areas and why it is not working correctly to give the desired output?
import java.util.Scanner;
// Defines the a Sorted Set collection and implements a driver program in main
public class SortedSet {
// Define a basic element of a linked list
private class LinkedNode {
int x; // Value stored in the node
LinkedNode next; // Reference to the next node in the list
}
LinkedNode front = null; // Reference to the front of the singly linked list
// Adds the integer x to the collection.
// The resulting collection is sorted in increasing order and
// does not contain any duplicate values.
public void add(int x) {
// Initialize a new node to be added to the collection
LinkedNode newNode = new LinkedNode();
LinkedNode cur = front;
newNode.x = x;
// Check if list is empty
if (cur == null) {
front = newNode;
}
// If list is not empty, check if node should be placed in front
else if (front != null) {
if (newNode.x < front.x) {
newNode.next = front;
front = newNode;
}
// If not in front, check for the middle or the end, or duplicate.
else {
// <TODO><TODO><TODO>
LinkedNode temp = cur;
LinkedNode prev = cur;
int middle = x;
while (temp != null) {
if(temp.x > newNode.x) {
middle = 1;
newNode.next = temp;
prev.next = newNode;
}
prev = temp;
temp = temp.next;
}
if (middle == 0) {
prev = newNode;
}
}
}
}
// Deletes the integer x from the sorted set.
// The remaining collection remains sorted and without duplicates.
public void delete(int x){
// Declare a new reference and initialize it to the front of the list
LinkedNode cur = front;
// Check if list is empty
if (front == null) {
System.out.print("There is nothing to delete!");
} else { // Not empty
// Go through list, checking whether node is in the list, and delete if found
// <TODO><TODO><TODO>
LinkedNode prev = new LinkedNode();
while (cur.x != x && cur != null) {
prev = cur;
cur = cur.next;
}
if (cur != null)
prev.next = cur.next;
}
}
// Returns true if the integer x exists in the sorted set and false otherwise.
public void exists(int x) {
// Declare a new reference and initialize it to the front of the list
LinkedNode cur = front;
// Check if list is empty.
if (front == null) {
System.out.println("false");
}
// If not empty, check for the node.
// <TODO><TODO><TODO>
else {
while (cur != null) {
if (cur.x==x)
return;
cur=cur.next;
}
return;
}
}
// Returns a string representing the sorted set as a space separated list.
public String toString() {
String s = "";
LinkedNode cur = front;
while (cur!=null) {
s+= cur.x + " ";
cur = cur.next;
}
return s;
}
// Driver method
public static void main(String[] args) {
// Declare variables
SortedSet sortedSet = new SortedSet();
Scanner scan = new Scanner(System.in);
String[] tokens;
String command;
int num;
// Print header info
System.out.println("Programming Fundamentals\n"
+ "NAME: Andres Reyes\n"
+ "PROGRAMMING ASSIGNMENT 4\n");
// Enter command loop
while (true) {
// Prompt the user for a command
System.out.print("Enter command: ");
String input = scan.nextLine();
// Parse input
if (input.equals("q")) break; // user quits
tokens = input.split("\\s");
if (tokens.length < 2) continue; // invalid input
command = tokens[0];
num = Integer.parseInt(tokens[1]);
// Execute command
if (command.equals("add")){
sortedSet.add(num);
System.out.println(sortedSet);
} else if (command.equals("del")) {
sortedSet.delete(num);
System.out.println(sortedSet);
} else if (command.equals("exists")) {
sortedSet.exists(num);
} else {
System.out.print("Command does not exist");
}
}
System.out.println("\nGood bye!");
}
}
I made following changes in the add-function and they got it working for me:
// If not in front, check for the middle or the end, or duplicate.
else {
// <TODO><TODO><TODO>
LinkedNode temp = cur.next; // start at cur.next as your temp-variable
LinkedNode prev = cur;
int middle = 0; // set middle to 0
while (temp != null) {
if(temp.x > newNode.x) {
middle = 1;
newNode.next = temp;
prev.next = newNode;
}
prev = temp;
temp = temp.next;
}
if (middle == 0) {
// add node to the end
prev.next = newNode;
}
}
}
You have to start at cur.next as your temp-variable.
As far as I can see, you're not yet checking if there are any duplicate values in your list.
Edit: I didn't work on the exists-method, which is not giving you any output at the moment. The problem is simply that you're generating any output while you're checking if a value exists in your list. You could either write a System.out.print which prints "true" in case the value was found or "false" in case if wasn't. Or you change the return type of the exists-function to boolean, return a boolean according to the result and print the return value.
It might also help you to visualize a linked list to unterstand why you have to change the temp-variable to cur.next. I think https://www.tutorialspoint.com/data_structures_algorithms/linked_lists_algorithm.htm gives a good explaination of the insertion process.
I can give you some hints. The main problem I see with this code is that you really need a reference to the start of the LinkedList (the head) would be the only way to print the list and check for duplicates.
The following should be added to your class
LinkedList head = null; //start of the list
Then you have to update your toString() or you will never print the correct elements in your list no matter what you do. Try this:
public String toString(){
StringBuilder output = new StringBuilder();
LinkedNode current = head;
while(current != null){
output.append(current.x).append(" ");
current = current.next;
}
return output.toString();
}
You have to be really careful when you are appending to a String in a loop because Strings are immutable. Everytime you are appending to a list you are creating a new String. Instead, use StringBuilder.
//s+= cur.x + " ";
Your add method should handle the following cases:
Case 1: List is empty: ( don't forget to set ref to head)
Case 2: new element is great than front of list
Case 3: new element is less than current head
Case 4: new element is less than current and greater than head

How to efficiently Get Successor in HashTable?

I want to write a function that gets the data associated with the successor key of a key passed as an argument. I want to write it to perform the operation as simply as possible, and apart from getting some errors so far, I'd like some advice on how to fix my code so it does so. The method I want advice on is GetSuccessor(Comparable key).
class HashTable
{
int size;
int length;
Node nodes[];
// Constants
static final double max_load_factor = 0.7;
static final int initial_size = 5;
public HashTable()
{
size = initial_size;
nodes = new Node[size];
}
// Return the data associated with the given key, or null if the key
// is not present in the hash table.
public Object Search(Comparable key)
{
// Obtain index for the key
int index = key.hashCode() % size;
// Traverse collision list
for (Node node = nodes[index]; node != null; node = node.next)
if (node.key.equals(key))
return node.data;
// Not found
return null;
}
// Insert a pair key-data into the hash table
public void Insert(Comparable key, Object data)
{
// Check if the table must grow
double load_factor = (double) length / size;
if (load_factor > max_load_factor)
Grow();
// Create node
Node node = new Node(key, data);
// Get index for the key
int index = key.hashCode() % size;
// Insert node
node.next = nodes[index];
nodes[index] = node;
//Update length
length++;
}
// Grow the hash table. All node must be repositioned according
// to their new flash indices based on the new table.
void Grow()
{
// Message
System.out.println("Growing hash table");
// Save old nodes and table siz4
int old_size = size;
Node[] old_nodes = nodes;
// Create new table
size = size * 2;
length = 0;
nodes = new Node[size];
// Traverse old nodes
for (int i = 0; i < old_size; i++)
for (Node node = old_nodes[i]; node != null; node = node.next)
Insert(node.key, node.data);
}
// Return the data associated with the successor for the given key,
// or null if the key is the last or it is not present.
public Object GetSuccessor(Comparable key)
{
// Find node
Node node = Search(key);
if (node == null)
return null;
// Get sucessor node
for (Node node = nodes[key.hashCode()]; node != null; node = node.next)
if (node.key.equals(key))
return node.next.data;
// Return associated data
return node == null ? null : node.next.data;
}
}
first observation: when you run this it should produce an error when you initialize node to equal the result from Search(key). If you change it so that node= new Node(key, Search(key)) you'll move on to find an arrayindexoutofbounds exception. if you're in Ubal's 1:30 class check your email

Dijsktra method not working

So trying to make dijsktra algorithm work and apparently im removing the wrong node according to labb assistant (who is no longer available for help)
These are his comments:
"the loop that you are using in the Dijkstra method (lines 158 - 178). For it to be correct, you need to loop through the outer arcs of your minNode (in your case, "n"), and then pick up the node with the minimum cost as your minNode. In your case, you always remove the first node from the tempNode list instead of the minNode. Please resubmit the modified Network.java file."
and these are lines 157-181:
while (! tempNodes.isEmpty()) { // repeat until all nodes become permanent
Node n = tempNodes.get(0);
double min= Double.POSITIVE_INFINITY;
if(n.value < min){
min = n.value;
}
for(Arc a: n.arcList){
if(n.value + a.weight < a.head.value){
a.head.value = n.value + a.weight; // Update the weight of each node that is adjacent to the minimum-
n.prev = a;
}
tempNodes.remove(n);}//Remove the minimum-weight node n from tempNodes
}
Node k;// Represent a tree by assigning 1 to its arcs and 0 to all other arcs.
for (String nodeName: nodeMap.keySet()) {
k = nodeMap.get(nodeName);
if(k.prev != null) {
k.prev.alvalue = 1.0;
}
}
am i correct in assuming the only issue is that im removing n instead of min mayby?
Il just add the rest of the lines:
public class Network {
// Attributes
public String name;
public HashMap<String, Node> nodeMap;
// Constructor
public Network(String name, String inputFileName) {
// Initialize the attributes
this.name = name;
this.nodeMap = new HashMap<String, Node>();
// You MAY need these local variables to store values or objects
// temporarily while constructing a new Network object
String line, arcID, tailName, headName;
Node tail, head;
double weight;
Arc forwardArc, backwardArc;
try {
// Get access to the contents of an ASCII file
File file = new File(inputFileName);
FileReader fReader = new FileReader(file);
BufferedReader bReader = new BufferedReader(fReader);
// Read the first line, and do nothing to skip it
line = bReader.readLine();
// Read the second line, which represents the first
// (undirected) arc stored in the file
line = bReader.readLine();
// Store each element of the network in forward star.
while (line != null) {
// Split each line into an array of 4 Strings
// using ; as separator.
String[] tokens = line.split(";");
arcID = tokens[0];
tailName = tokens[1];
headName = tokens[2];
weight = Double.parseDouble(tokens[3]);
// Check if nodeMap contains a Node whose name is tailName or headName.
if(nodeMap.containsKey(tailName)){
tail = nodeMap.get(tailName);
}
else{
tail = new Node(tailName, Double.POSITIVE_INFINITY, new LinkedList());
nodeMap.put(tailName, tail);
}
if(nodeMap.containsKey(headName)){
head = nodeMap.get(headName);
}
else{
head = new Node(headName, Double.POSITIVE_INFINITY, new LinkedList());
nodeMap.put(headName, tail);
}
// If not, create it, assign it to tail or head, and add it to nodeMap.
// Otherwise, retrieve it from nodeMap.
// Then, create two Arcs:
// one from tail to head, to be added to outArc of tail
// one from head to tail, to be added to outArc of head.
forwardArc = new Arc(arcID+"a",tail,head,weight);
backwardArc = new Arc(arcID+"b",head,tail,weight);
tail.arcList.add(forwardArc);
head.arcList.add(backwardArc);
// Read the next line
line = bReader.readLine(); }
} catch (Exception e) {
e.printStackTrace();
}
}
// Save
public void save(String outputFileName) {
// This object represents an output file, out.txt, located at ./data/.
File file = new File(outputFileName);
// This object represents ASCII data (to be) stored in the file
FileWriter fWriter;
try {
//writing to the output-file
fWriter = new FileWriter(file);
fWriter.write("TLID "+"\t");
fWriter.write("NAME "+"\t");
fWriter.write("ALVALUE"+"\n");
for (Map.Entry<String, Node> entry : nodeMap.entrySet()) {
String key = entry.getKey();
Object value = entry.getValue();
Node values = (Node) value;
for (Arc A: values.arcList) {
String TLID = A.name.substring(0,A.name.length()-1);
fWriter.write(TLID+"\t");
fWriter.write(A.name+"\t");
fWriter.write(A.alvalue+"\n");
}
}
fWriter.close();
} catch (IOException e) {
e.printStackTrace();
}
}
public void printNodes(){
System.out.println("\tNODE NAME\tWEIGHT");
Node node;
for (String nodeName: nodeMap.keySet()) { // loop thru nodeMap
node = nodeMap.get(nodeName);
System.out.print("\t" + node.name); // \t represents tab space
System.out.print("\t\t" + node.value);
System.out.println();
}
}
public void printArcs(){
System.out.print("TLID "+"\t");
System.out.print("NAME "+"\t");
System.out.print("ALVALUE "+"\n");
for (Map.Entry<String, Node> entry : nodeMap.entrySet()) {
String key = entry.getKey();
Object value = entry.getValue();
Node values = (Node) value;
for (Arc A: values.arcList) {
String TLID = A.name.substring(0,A.name.length()-1);
System.out.print(TLID+"\t");
System.out.print(A.name+"\t");
System.out.print(A.alvalue+"\n");
}
}
}
public void dijkstra(Node origin) {
// Set the value (representing shortest path distance) of origin to 0
origin.value = 0;
// Create a set of nodes, called tempNodes, whose shortest path distances are not permanently determined. Initially, this set contains all nodes.
List<Node> tempNodes = new ArrayList<Node>();
for (String nodeName: nodeMap.keySet()) {
tempNodes.add(nodeMap.get(nodeName));
}
// Perform Dijkstra
while (! tempNodes.isEmpty()) { // repeat until all nodes become permanent
Node n = tempNodes.get(0);
double min= Double.POSITIVE_INFINITY;
if(n.value < min){
min = n.value;
}
for(Arc a: n.arcList){
if(n.value + a.weight < a.head.value){
a.head.value = n.value + a.weight; // Update the weight of each node that is adjacent to the minimum-
n.prev = a;
}
tempNodes.remove(n);}//Remove the minimum-weight node n from tempNodes
}
Node k;// Represent a tree by assigning 1 to its arcs and 0 to all other arcs.
for (String nodeName: nodeMap.keySet()) {
k = nodeMap.get(nodeName);
if(k.prev != null) {
k.prev.alvalue = 1.0;
}
}
}
private void clearArcWeight() {
Node n;
for (String nodeName: nodeMap.keySet()) {
n = nodeMap.get(nodeName);
for(Arc a: n.arcList){
a.weight = 0.0;
}
}
}
public void dijkstra(Node origin, Node destination) {
dijkstra(origin); // or this.dijkstra(origin);
clearArcWeight();
trace(destination);
}
// Represent a tree by assigning 1 to its arcs and 0 to all other arcs.
private void trace(Node n){
Arc a = n.prev;
while (a != null) {
a.weight = 1.0;
a = a.tail.prev;
}
}
public HashMap<String, Node> getNodeMap(){
return nodeMap;
}
public void dijkstraByName(String string) {
dijkstra(nodeMap.get(string));
}
}
I might be able to give a little hint here... If you look at these lines:
Node n = tempNodes.get(0);
// [...n is not changed here...]
tempNodes.remove(n);
you will realize that the n passed into remove() will never be anything else than the result of 'tempNodes.get(0)'. That's why the first Node is always removed from the list.

how to remove a object from linked list in java?

i have one problem with my code ,i did a sample program to display the emp details from a linked list,now the problem when i trying to delete a particular entry means it doesn't work,i hope i did some mistake in my code could you suggest how to do that?
import java.util.*;
class EmpDedup {
int record;
String fprint;
int fid;
EmpDedup(int record, String fprint, int fid) {
this.record = record;
this.fprint = fprint;
this.fid = fid;
}
public int getRecord() {
return record;
}
public String getFprint() {
return fprint;
}
public int getFid() {
return fid;
}
public static void main(String[] args) {
int count = 0;
LinkedList<EmpDedup> list = new LinkedList<EmpDedup>();
list.add(new EmpDedup(101, "entry1", 20));
list.add(new EmpDedup(102, "entry2", 30));
list.add(new EmpDedup(103, "entry3", 40));
list.add(new EmpDedup(104, "entry4", 50));
Scanner input = new Scanner(System.in);
System.out.print("Enter record no to display: ");
int rec = input.nextInt();
for (EmpDedup data : list) {
if (data.getRecord() == rec) {
System.out.println(data.getRecord() + "\t" + data.getFprint() + "\t" + data.getFid() + "\t");
count++;
}
}
System.out.println("The size of an linkedlist is: \t" + list.size());
System.out.println("The number of available record is :" + count);
System.out.println("The size of an linkedlist is: \t" + list.size());
Scanner input1 = new Scanner(System.in);
System.out.print("Enter record no to delete: ");// here i try to delete a particular record
int rec1 = input1.nextInt();
for (EmpDedup data : list) {
if (data.getRecord() == rec1) {
// System.out.println(data.getRecord()+"\t"+data.getFprint()+"\t"+data.getFid()+"\t");
list.remove(data); // problem is here
count++;
}
}
}
}
you cannot operate in lists (add, remove... items) while you iterate on them. You have to use an Iterator
for(Iterator<EmpDedup> iter = list.iterator(); iter.hasNext();) {
EmpDedup data = iter.next();
if (data.getRecord() == rec1) {
iter.remove();
}
}
see http://docs.oracle.com/javase/6/docs/api/java/util/Iterator.html
Use an Iterator instead and then use the remove() method on the Iterator
When you do this:
list.remove(101);
you are calling this method, which will not serve your purpose.
You should rather use:
list.remove(data);
You try to delete element with index 101, but you have only 4 items in list. How it can delete element with index 101?
If you want to delete element that is equals to EmpDedup(101,"entry1",20) than you have to pass in remove method this object. And don't foget to redefine equals and hash code of EmpDedup.
The code tries to remove element at index position 101, but there are only four items in the list.
Use the following as a replacement of your code:
for( EmpDedup data : list)
{
if( data.getRecord() == rec1 )
{
list.remove( data );
++count;
}
}
That's where a list object will be deleted.
for( EmpDedup data:list)
{
if(data.getRecord()==rec1)
{
list.remove(data);
count++;
}
}
import java.util.Scanner;
// A complete working Java program to demonstrate deletion in singly
// linked list
class LinkedList
{
Node head; // head of list
/* Linked list Node*/
class Node
{
int data;
Node next;
Node(int d)
{
data = d;
next = null;
}
}
/* Given a key, deletes the first occurrence of key in linked list */
void deleteNode(int key)
{
// Store head node
Node temp = head, prev = null;
// If head node itself holds the key to be deleted
if (temp != null && temp.data == key)
{
head = temp.next; // Changed head
return;
}
// Search for the key to be deleted, keep track of the
// previous node as we need to change temp.next
while (temp != null && temp.data != key)
{
prev = temp;
temp = temp.next;
}
// If key was not present in linked list
if (temp == null) return;
// Unlink the node from linked list
prev.next = temp.next;
}
/* Inserts a new Node at front of the list. */
public void push(int new_data)
{
Node new_node = new Node(new_data);
new_node.next = head;
head = new_node;
}
/* This function prints contents of linked list starting from
the given node */
public void printList()
{
Node tnode = head;
while (tnode != null)
{
System.out.print(tnode.data+" ");
tnode = tnode.next;
}
}
/* Drier program to test above functions. Ideally this function
should be in a separate user class. It is kept here to keep
code compact */
public static void main(String[] args)
{
LinkedList llist = new LinkedList();
llist.push(7);
llist.push(1);
llist.push(3);
llist.push(2);
System.out.println("\nCreated Linked list is:");
llist.printList();
Scanner sc = new Scanner(System.in);
System.out.println("please enter input");
int aa =sc.nextInt();
llist.deleteNode(aa); // Delete node at position 4
System.out.println("\nLinked List after Deletion at position :"+aa);
llist.printList();
System.out.println("adding element to the LinkedList");
System.out.println("enter element");
int dd = sc.nextInt();
llist.push(dd);
llist.printList();
}
}

Duplicate a LinkedList with a pointer to a random node apart from the next node

Q: Every node of the linked list has a random pointer (in addition to the next pointer) which could randomly point to another node or be null. How would you duplicate such a linkedlist?
A: Here's what I have, I just wanted to ratify if this was the optimal way of doing it.
Since there's no space constraints specified, I'm going to use a LinkedHashSet and a LinkedHashMap (I can imagine people nodding their head in disagreement already ;) )
First Iteration: Do the obvious - read each node from the list to be copied and create nodes on the new list. Then, read the random node like so: this.random.data and insert into the LinkedHashSet.
Second Iteration: Iterate through the new list and add each node's data as the first column and the node itself as the second column into the LinkedHashMap (doesn't have to be Linked, but I'm just going with the flow).
Third Iteration: Iterate over the LinkedHashSet (this is the reason why this needs to be Linked - predictable ordering) and the new list simultaneously. For the first node, read the first entry of the LinkedHashSet, look up the corresponding object in the LinkedHashMap and add as the random node to the current node in the new list.
3 iterations does seem a little crazy, but the attempt was to keep the complexity as O(N). Any solution that improves on the O(3N) space requirement and O(3N) runtime complexity would be great. Thanks!
Edit: The entry from the LinkedHashSet can be removed when making an entry into the LinkedHashMap, so this would only take O(2N) space.
As pointed out by MahlerFive, I think you can do this with O(2N) runtime complexity, and O(N) space complexity.
Let's assume you have
public class Node {
private Node next;
private Node random;
private String data;
// getters and setters omitted for the sake of brevity
}
I would do a deep copy of a linked list of Nodes as:
private Node deepCopy(Node original) {
// We use the following map to associate newly created instances
// of Node with the instances of Node in the original list
Map<Node, Node> map = new HashMap<Node, Node>();
// We scan the original list and for each Node x we create a new
// Node y whose data is a copy of x's data, then we store the
// couple (x,y) in map using x as a key. Note that during this
// scan we set y.next and y.random to null: we'll fix them in
// the next scan
Node x = original;
while (x != null) {
Node y = new Node();
y.setData(new String(x.getData()));
y.setNext(null);
y.setRandom(null);
map.put(x, y);
x = x.getNext();
}
// Now for each Node x in the original list we have a copy y
// stored in our map. We scan again the original list and
// we set the pointers buildings the new list
x = original;
while (x != null) {
// we get the node y corresponding to x from the map
Node y = map.get(x);
// let x' = x.next; y' = map.get(x') is the new node
// corresponding to x'; so we can set y.next = y'
y.setNext(map.get(x.getNext()));
// let x'' = x.random; y'' = map.get(x'') is the new
// node corresponding to x''; so we can set y.random = y''
y.setRandom(map.get(x.getRandom()));
x = x.getNext();
}
// finally we return the head of the new list, that is the Node y
// in the map corresponding to the Node original
return map.get(original);
}
Edit: I found that this question is a duplicate of the one asked here: there you find an answer that shows how to solve this problem in O(3N) runtime complexity with no extra space: very ingenious! But it uses a trick with C pointers, and I'm not sure how to do the same in java.
You can do this with 2N steps and a map with N elements.
Walk the old list following the 'next' pointers. For each node you visit, add a node to your new list, connect the previous node in your new list to the new node, store the old node random pointer in the new new node, then store a mapping of the old node pointer to the new node pointer in a map.
Walk the new list, and for each random pointer, look it up in the map to find the associated node in the new list to replace it with.
I too was asked this question in interview recently.
Here is what i proposed.
Create a map of original list nodes where addreess of each node will be key and the offset of random pointer will be the value.
Now create a new linked list with random pointer =null from the original map.
In the end, iterate through the original list , with the help of map get offset of original pointer and use that offset to link random pointer in newly created map.
Interviewer was not happy in the end.May be looking for some better approach or he had the set answer in his mind and unable to grasp new way of solving it.
in O(n) time and with constant space
public class CloneLinkedListWithRandomPointer {
public static void main(String[] args) throws Exception {
SpecialLink link = new SpecialLink(1);
SpecialLink two = new SpecialLink(2);
SpecialLink three = new SpecialLink(3);
SpecialLink four = new SpecialLink(4);
SpecialLink five = new SpecialLink(5);
link.next = two;
two.next = three;
three.next = four;
four.next = five;
link.random = four;
two.random = five;
three.random = null;
four.random = five;
five.random=link;
SpecialLink copy = cloneSpecialLinkedList(link);
System.out.println(link);
System.out.println(copy);
}
public static SpecialLink cloneSpecialLinkedList(SpecialLink link) throws Exception{
SpecialLink temp = link;
while(temp != null){
temp.next = (SpecialLink) temp.clone();
temp = temp.next==null?temp.next:temp.next.next;
}
temp = link;
while(temp != null){
temp.next.random = temp.random!=null?temp.random.next:null;
temp = temp.next==null?temp.next:temp.next.next;
}
SpecialLink copy = link.next;
temp = link;
SpecialLink copyTemp = copy;
while(temp.next!= null && copyTemp.next != null){
temp.next = temp.next.next;
copyTemp.next = copyTemp.next.next;
temp = temp.next;
copyTemp = copyTemp.next;
}
return copy;
}
}
class SpecialLink implements Cloneable{
enum Type{
ORIGINAL,COPY
}
int val;
SpecialLink next;
SpecialLink random;
Type type;
public void setValue(int value){
this.val = value;
}
public SpecialLink addNode(int value){
return next = new SpecialLink(value);
}
public SpecialLink(int value) {
super();
this.val = value;
this.type = Type.ORIGINAL;
}
#Override
public String toString() {
SpecialLink temp = this;
StringBuilder builder = new StringBuilder();
while(temp != null){
builder.append(temp.val).append("--").append(temp.type.toString()).append("->").append(temp.random == null? null:temp.random.val).append("--").append(temp.random == null? null:temp.random.type);
builder.append(", ");
temp = temp.next;
}
return builder.toString();
}
#Override
public Object clone() throws CloneNotSupportedException {
// TODO Auto-generated method stub
SpecialLink clone = (SpecialLink) super.clone();
clone.type = Type.COPY;
return clone;
}
}
Walk the list and use clone()?
I wrote code for #MahlerFive 's solution, which works without mapping.
Here's the code:
private static class Node {
private String item;
private Node next;
private Node random;
}
public static Node cloneLinkedStructure(Node head) {
// make holes after each original node
for (Node p = head; p != null;) {
Node pnext = p.next;
Node hole = new Node();
hole.item = ".";
p.next = hole;
hole.next = pnext;
p = pnext;
}
Node fakeHead = new Node(); // fake new head
Node q = fakeHead;
Node p = head;
while (p != null) {
// build the new linked structure
Node oldq = q;
q = new Node();
q.item = p.item;
oldq.next = q;
q.random = p.random.next; // link to a hole
Node hole = p.next;
hole.random = q; // use link RANDOM as a backward link to new node
p = hole.next;
}
q.next = null;
Node newHead = fakeHead.next; // throw fake head
// build random links for the new linked structure
for (q = newHead; q != null; q = q.next)
q.random = q.random.random;
// delete holes to restore original linked structure
for (p = head; p != null; p = p.next)
p.next = p.next.next;
return newHead;
}
1) Create the copy of node 1 and insert it between node 1 & node 2 in original Linked List, create the copy of 2 and insert it between 2 & 3.. Continue in this fashion, add the copy of N afte the Nth node
2) Now copy the arbitrary link in this fashion
original->next->arbitrary = original->arbitrary->next; /*TRAVERSE TWO NODES*/
This works because original->next is nothing but copy of original and Original->arbitrary-> next is nothing but copy of arbitrary.
3) Now restore the original and copy linked lists in this fashion in a single loop.
original->next = original->next->next;
copy->next = copy->next->next;
4) Make sure that last element of original->next is NULL.
Time Complexity: O(n)
Auxiliary Space: O(1)
source
Here is the Java implementation:
public static <T> RandomLinearNode<T> clone(RandomLinearNode<T> head) {
if (head == null) {
return head;
}
RandomLinearNode<T> itr = head, temp;
// insert copy nodes after each original nodes
while (itr != null) {
temp = new RandomLinearNode<T>(itr.getElement());
temp.next(itr.next());
itr.next(temp);
itr = temp.next();
}
// copy the random pointer
itr = head;
while (itr != null && itr.next() != null) {
if (itr.random() != null) {
itr.next().random(itr.random().next());
}
itr = itr.next().next();
}
// break the list into two
RandomLinearNode<T> newHead = head.next();
itr = head;
while (itr != null && itr.next() != null) {
temp = itr.next();
itr.next(temp.next());
itr = temp.next();
}
return newHead;
}
Here is the unit tests
#Test
public void cloneLinkeListWithRandomPointerTest() {
RandomLinearNode<Integer> one = new RandomLinearNode<Integer>(1, null, null);
RandomLinearNode<Integer> two = new RandomLinearNode<Integer>(2, one, null);
RandomLinearNode<Integer> three = new RandomLinearNode<Integer>(3, two, null);
RandomLinearNode<Integer> four = new RandomLinearNode<Integer>(4, three, null);
RandomLinearNode<Integer> five = new RandomLinearNode<Integer>(5, four, four);
RandomLinearNode<Integer> six = new RandomLinearNode<Integer>(6, five, two);
RandomLinearNode<Integer> seven = new RandomLinearNode<Integer>(7, six, three);
RandomLinearNode<Integer> eight = new RandomLinearNode<Integer>(8, seven, one);
RandomLinearNode<Integer> newHead = LinkedListUtil.clone(eight);
assertThat(eight, not(sameInstance(newHead)));
assertThat(newHead.getElement(), equalTo(eight.getElement()));
assertThat(newHead.random().getElement(), equalTo(eight.random().getElement()));
assertThat(newHead.next().getElement(), equalTo(eight.next().getElement()));
assertThat(newHead.next().random().getElement(), equalTo(eight.next().random().getElement()));
assertThat(newHead.next().next().getElement(), equalTo(eight.next().next().getElement()));
assertThat(newHead.next().next().random().getElement(), equalTo(eight.next().next().random().getElement()));
assertThat(newHead.next().next().next().getElement(), equalTo(eight.next().next().next().getElement()));
assertThat(newHead.next().next().next().random().getElement(), equalTo(eight.next().next().next().random().getElement()));
}

Categories