I'm trying to find the number of disjoint sets for given N sets and M relations. For example given a relation "i j" ,I have to merge the sets containing these two elements. M and N can be as large as 100000.
I tried using ArrayList of Hashsets. But couldn't implement it efficiently. This was my code:
import java.io.InputStreamReader;
import java.io.BufferedReader;
import java.util.*;
import java.lang.Object;
class fire
{
public static void main(String[] args)throws Exception
{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int n,m;
int t=Integer.parseInt(br.readLine());
String st[];
while(t-->0)
{
st=br.readLine().split(" ");
n=Integer.parseInt(st[0]);
m=Integer.parseInt(st[1]);
ArrayList<HashSet<Integer>> list = new ArrayList<HashSet<Integer>>(n+1);
for(int i=0;i<n+1;i++)
{
list.add(i, new HashSet<Integer>());
list.get(i).add(i);
}
int a,b;
while(m-->0)
{
st=br.readLine().split(" ");
a=Integer.parseInt(st[0]);
b=Integer.parseInt(st[1]);
if(list.get(a).contains(a))
{
if(list.get(b).contains(b))
{
Iterator<Integer> it = list.get(b).iterator();
while(it.hasNext())
{
list.get(a).add(new Integer((int)it.next()));
}
list.get(b).clear();
}
else
{
for(int i=1;i<n+1;i++)
if(list.get(i).contains(b))
{
if(i!=a)
{
Iterator<Integer> it = list.get(i).iterator();
while(it.hasNext())
list.get(a).add(new Integer((int)it.next()));
list.get(i).clear();
}
break;
}
}
}
else
{
for(int i=1;i<n+1;i++)
if(list.get(i).contains(a))
{
if(list.get(b).contains(b))
{
Iterator<Integer> it = list.get(b).iterator();
while(it.hasNext())
list.get(a).add(new Integer((int)it.next()));
list.get(b).clear();
}
else
{
for(int j=1;j<n+1;j++)
if(list.get(j).contains(b))
{
if(i!=j)
{
Iterator<Integer> it = list.get(j).iterator();
while(it.hasNext())
list.get(a).add(new Integer((int)it.next()));
list.get(j).clear();
}
break;
}
}
break;
}
}
}
int size=0,prod=1;
int num=0;
for(int i=1;i<n+1;i++)
{
num=list.get(i).size();
if(num!=0)
{
prod*=num;
size++;
}
}
System.out.println(size+" "+prod);
}
}
};
This is a problem from Codechef. The solution is right but I'm getting TimeLimitExceeded for this problem. Should I work on improving this code or would I have to use a different data structure? Any ideas would be really grateful :). Thank you.
You should use disjoint set forest data structure for this problem. Very easy to implement and extremely effiecient.
Related
I'm struggling with a really simple problem in java. I've implemented quicksort in java that works on arraylists and can take any value. The problem is that it works only for an arraylist lower than about 8000 size.
Can anyone tell me what's wrong with my program? I think it might be connected with recursion depth limit but i'm not sure (because sometimes it works for larger sizes and sometimes not). How can I improve my quicksort implementation so it will work for much larger size of Arraylist like 100000?
import java.util.ArrayList;
import java.util.Random;
public class QuickSort {
Random gener;
int temporary,genertype,NInts,flag;
ArrayList<Integer> mylist;
public QuickSort(int type,int ilosc){
gener = new Random();
mylist= new ArrayList<>();
this.genertype=type;
this.NInts=ilosc;
}
void generate(){
if(genertype==0){
for(int i=0;i<NInts;i++){
mylist.add(gener.nextInt(100000));
}
}else {
for(int i=0;i<NInts;i++){
mylist.add(NInts-i);
}
}
}
int count1(ArrayList<Integer> list,int counter1,int counter2){
while(list.get(0)<list.get(counter1)){
if(counter1==counter2){
flag=1;
return counter1;
}
counter1++;
}
flag=0;
return counter1;
}
int count2(ArrayList<Integer> list,int counter1,int counter2){
while(list.get(0)>list.get(counter2)){
if(counter1==counter2){
flag=1;
return counter2;
}
counter2--;
}
flag=0;
return counter2;
}
public ArrayList<Integer> sorting(ArrayList<Integer> list) {
ArrayList<Integer> left = new ArrayList<Integer>();
ArrayList<Integer> right = new ArrayList<Integer>();
int counter1,counter2;
if (list.size() == 1) {
return list;
}else {
counter1=1;
counter2=list.size()-1;
while(counter1!=counter2) {
counter1=count1(list,counter1,counter2);
if(flag==1)
break;
counter2=count2(list,counter1,counter2);
if(flag==1)
break;
temporary = list.get(counter1);
list.set(counter1, list.get(counter2));
list.set(counter2, temporary);
}
for (int i = 0; i < counter1; i++) {
left.add(list.get(i));
}
for (int i = counter1; i < list.size(); i++) {
right.add(list.get(i));
}
left = sorting(left);
right = sorting(right);
list=merge(left, right);
}
return list;
}
ArrayList<Integer> merge(ArrayList<Integer> left, ArrayList<Integer> right) {
if(left.get(0)>right.get(right.size()-1)){
right.addAll(left);
return right;
}
else{
left.addAll(right);
return left;
}
}
void printing(){
for(int k=0;k<NInts;k++){
System.out.print(" "+mylist.get(k));
}
}
public static void main(String[] args){
QuickSort instance = new QuickSort(1,1000);
instance.generate();
instance.mylist=instance.sorting(instance.mylist);
instance.printing();
}
}
Ps.If you see anything wrong in my code, let me know so I can improve it :)
There could be many reasons why your code could not run for large number of inputs. Mostly it could be because the Heap Size capacity specified for your application is overflown. This can be resolved by increasing Heap Size of your application (check out this stackoverflow link on how to increase the heap size of your application)
I have a working word occurrence program that took me a while to code (still new at Java) and I was wondering if I could get a little assistance. Here is my code that I have so far:
import java.io.*;
import java.util.ArrayList;
import java.util.List;
public class TestWordOccurenceProgram {
public static void main(String[] args) {
String thisLine = null;
try {
FileReader fr = new FileReader("myTextDocument.txt");
BufferedReader br = new BufferedReader(fr);
//List<String> wordList = new ArrayList<>();
List<String> words = new ArrayList<>();
// make ArrayList of integers
List<Integer> counts = new ArrayList<>();
String word = "";
while ((thisLine = br.readLine()) != null ) {
word = word.concat(thisLine);
word = word.concat(" ");
}
String[] wordList = word.split("\\s");
for (int i = 0; i < wordList.length; i++) {
String temp = wordList[i];
if(words.contains(temp)) {
int x = words.indexOf(temp);
int value = counts.get(x);
value++;
counts.set(x, value);
}
else {
words.add(temp);
counts.add(1);
}
}
for (int i = 0; i < words.size(); i++) {
System.out.println(words.get(i) + ": " + counts.get(i));
}
br.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
System.exit(1);
} catch (IOException e) {
e.printStackTrace();
System.exit(1);
}
}
}
Here is what "myTextDocument.txt" has:
i am a rabbit
a happy rabbit am
yay i am a rabbit
a rabbit i am yay
Here is my output:
i: 3
am: 4
a: 4
rabbit: 4
happy: 1
yay: 2
Does anyone know if I could arrange these items from the highest number of word occurrences to the lowest number of word occurrences? Any help would be great!
You can use Map instead of List. and use compare method to sort map via its value.
refer this code :
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.Comparator;
import java.util.HashMap;
import java.util.Map;
import java.util.TreeMap;
public class PQ {
public static void main(String[] args) {
String thisLine = null;
try {
FileReader fr = new FileReader("D:\\test.txt");
BufferedReader br = new BufferedReader(fr);
HashMap<String,Integer> map = new HashMap<String,Integer>();
ValueComparator comparator = new ValueComparator(map);
TreeMap<String, Integer> treemap = new TreeMap<String, Integer>(comparator);
while((thisLine = br.readLine()) != null){
String[] str = thisLine.split("\\s+");
for(String s:str){
if(map.containsKey(s)){
Integer i = map.get(s);
i++;
map.put(s,i);
}else{
map.put(s, 1);
}
}
}
treemap.putAll(map);
System.out.println(treemap);
br.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
System.exit(1);
} catch (IOException e) {
e.printStackTrace();
System.exit(1);
}
}
}
class ValueComparator implements Comparator<String>{
Map<String, Integer> base;
public ValueComparator(Map<String, Integer> base) {
this.base = base;
}
public int compare(String a, String b) {
if (base.get(a) >= base.get(b)) {
return -1;
} else {
return 1;
}
}
}
Rather than using two separate lists (one with words, one with counts), why not create a WordAndCount object that has something like getWord and getCount methods? This WordAndCount class can implement Comparable, where you do comparisons based on count. Then, you can store a single List<WordAndCount>, and just sort the single list using Collections.sort.
Roughly, the outline could look like this:
public class WordAndCount implements Comparable<WordAndCount> {
private String word;
private int count;
public WordAndCount(String word) {...}
public void incrementCount() {...}
public int compareTo(WordAndCount other) {...}
}
Wrapping up the combination into a single class makes this much easier to solve, as it provides the easy link between word and its count.
I would recommend using Collections in Java for this, but instead you can use temp variables.
So the idea is to sort by counts. Pseudo-code before outputting:
int tempCount;
String tempWord;
for (int i = 1; i < counts.size(); i++) {
if (counts.get(i) < counts.get(i-1)) {
tempCount = counts.get(i-1);
tempWord = words.get(i-1);
counts.set(i-1, i);
counts.set(i, tempCount);
words.set(i-1, i);
words.set(i, tempWord);
}
You'd need an extra loop around that to correctly order them but hopefully gives you the right idea.
i think that the problem is root being null. WOuld someone one teach me how to do it the proper way ? Because i don't know why it is null ... I think the remove function is well implemented, but due to root being null it doesn't continue executing. Help please.
Any recommendation are welcome too :D
public static void main(String[] args) {
Tree t = new Tree("");
String msg;
String[] inputs;
Scanner sc = new Scanner(System.in);
ArrayList <String> palavras = new ArrayList <String>();
int i = 0;
while (true) {
msg = sc.nextLine();
if (msg.equals("")) {
break;
}
inputs = msg.split(" ");
i = 0;
while (i < inputs.length) {
palavras.add(inputs[i]);
i++;
}
}
i = 0;
while (i < palavras.size()) {
if (palavras.get(i).equals("REMOVE")) {
t.remove(palavras.get(i+1));
palavras.remove(i+1);
i+=1;
} else {
t.insert(palavras.get(i).toLowerCase());
i++;
}
}
t.postorder();
t.preorder();
t.inorder();
}
**********************************************************
public void remove( String word)
{
Node father=root;
Node actual=root;
boolean leftnode=true;
if(root!=null){
while(!word.equals(actual.str))
{
father=actual;
if(word.compareTo(actual.str)<0)
{
leftnode=true;
actual=actual.left;
}
else{
leftnode=false;
actual=actual.right;
}
if(actual==null){
return ;
}
}
actual.occ = 0;
}
}
I was implementing Splay tree which works on the same concept as red black tree and at a point I was stuck like this. I will suggest you to draw the flow diagram of your code and then code it again. I hope your problem will be solved as mine.
you can refer this and this articles.
i have one arrayList
List value = new ArrayList();
this arraylist are value is = {a,b,c,d}
i have required combination to string using this arraylist
Required Output: abcd,bcd,acd,abd,abc,cd,bd,bc,ad,ac,ab,a,b,c,d,null
If it's possible? then please send me code....
It's my code but not perfectly work
import java.util.ArrayList;
import java.util.List;
public class PossibleCombination {
public static void main(String[] args) {
List segList = new ArrayList();
for(int i=65;i<70;i++){
segList.add((char)i);
}
int segSize = segList.size();
int[][] a = new int[segSize][2];
int i;
for(i=0; i<= segSize-1; i++)
{
a[i][0] = 0;
a[i][1] = 1;
}
boolean b1 = true;
int t =0;
while(b1)
{
StringBuffer stb = new StringBuffer();
for(i=0;i<segSize; i++)
{
if(a[i][0]==0)
stb.append(segList.get(i));
}
System.out.println(stb);
if(t>=a.length){
t=0;
}
int Pos=t;
while(a[Pos][0]>=a[Pos][1])
{
if(Pos<segSize-1)
Pos++;
else
break;
}
a[Pos][0]++;
Pos--;
while(Pos>=0)
{
if(a[Pos][0]>0)
{
a[Pos][0]--;
break;
}
Pos--;
}
t++;
if(a[segSize-1][0]> a[segSize-1][1]){
b1 = false;
}
}
}
}
Yes, It is definitely possible. However, it will of course require exponential time.
I will leave the actual implementation to you, but here are some hints.
The easiest way to do this is recursively, but that will take a lot of stack space very quickly.
Another way to do this is via some kind of breath-first expanding of the resulting list. This can be done iteratively with a FIFO queue.
List<String[]> value = new ArrayList<String[]>();
String[] item = {"a","b","c"};
value.add(item);
I've implement Tarjan's algorithm following the Wikipedia's article however I'm experiencing a problem. What I'm trying to do is to find all strongly connected components with size greater than 1.
With smaller sized inputs everything works fine however, when tested with input.txt the algorithm starts to behave strangely - on changing the initial value of n (for example 1, -10, 3) the program gives different results. And that's what I cannot understand. The algorithm should not depend on the initial value since n is used only as time stamp. If have any idea please share it it would help me so much, thank you.
Here's the code:
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Stack;
public class GraphAlgorithms {
HashMap<String, HashSet<String>> graph;
int n;
public GraphAlgorithms(String fileName) {
graph = new HashMap<String, HashSet<String>>();
try (BufferedReader reader = new BufferedReader(new FileReader(fileName))) {
String line;
while ((line = reader.readLine()) != null) {
String[] tokens = line.split(" +");
HashSet<String> to = new HashSet<String>();
for (int i = 1; i < tokens.length; i++)
to.add(tokens[i]);
graph.put(tokens[0], to);
}
} catch (IOException e) {
e.printStackTrace();
}
}
public GraphAlgorithms(HashMap<String, HashSet<String>> graph) {
this.graph = graph;
}
private void strongConnect(String v, HashMap<String, Integer> index,
HashMap<String, Integer> lowlink, Stack<String> stack,
HashSet<String> inStack, ArrayList<HashSet<String>> ans)
{
index.put(v, n);
lowlink.put(v, n);
++n;
stack.push(v);
inStack.add(v);
HashSet<String> successors = graph.get(v);
for(String w : successors) {
if (!index.containsKey(w)) {
strongConnect(w, index, lowlink, stack, inStack, ans);
if (lowlink.get(w) < lowlink.get(v))
lowlink.put(v, lowlink.get(w));
}
else if (inStack.contains(w)) {
if (index.get(w) < lowlink.get(v))
lowlink.put(v, index.get(w));
}
}
if (index.get(v) == lowlink.get(v)) {
HashSet<String> connectedComponent = new HashSet<String>();
String w;
do {
w = stack.pop();
inStack.remove(w);
connectedComponent.add(w);
}
while (!w.equals(v));
ans.add(connectedComponent);
}
}
public ArrayList<HashSet<String>> getStronglyConnectedComponents() {
HashMap<String, Integer> index = new HashMap<String, Integer>();
HashMap<String, Integer> lowlink = new HashMap<String, Integer>();
Stack <String> stack = new Stack<String>();
HashSet<String> inStack = new HashSet<String>();
ArrayList<HashSet<String>> ans = new ArrayList<HashSet<String>>();
n = 0;
for (String v : graph.keySet()) {
if (!index.containsKey(v))
strongConnect(v, index, lowlink, stack, inStack, ans);
}
return ans;
}
public void printComponentsLargerThanOne() {
for (HashSet<String> component : getStronglyConnectedComponents()) {
if (component.size() > 1) {
for (String element : component) {
System.out.print(element + " ");
}
System.out.println();
}
}
}
}
And the main class
public class Main {
public static void main(String[] args) {
GraphAlgorithms ga = new GraphAlgorithms("input.txt");
ga.printComponentsLargerThanOne();
}
}
Since Java apparently doesn't always intern objects of type java.lang.Integer, the line
if (index.get(v) == lowlink.get(v)) {
should be
if (index.get(v).equals(lowlink.get(v))) {