Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Store something like following datastructure:- (following is a pseudo code)</p> <pre><code>ValuesSet ={ apple = {"Austrailia","New Zealand"..} sheep = {"Austrailia","New Zealand"..} } for k in ValuesSet for k1 in k.values() for k2 in k.values() if(k1&lt;k2) Set(k1,k2).add(k) </code></pre> <p>time complextiy: O(No of distinct pairs with similar products)</p> <p><strong>Note:</strong> I might be wrong but i donot think u can reduce this time complexity</p> <p>Following is a java implementation for your problem:-</p> <pre><code>public class PairMatching { HashMap Country; ArrayList CountNames; HashMap ProdtoIndex; ArrayList ProdtoCount; ArrayList ProdNames; ArrayList[][] Pairs; int products=0; int countries=0; public void readfile(String filename) { try { BufferedReader br = new BufferedReader(new FileReader(new File(filename))); String line; CountNames = new ArrayList(); Country = new HashMap&lt;String,Integer&gt;(); ProdtoIndex = new HashMap&lt;String,Integer&gt;(); ProdtoCount = new ArrayList&lt;ArrayList&gt;(); ProdNames = new ArrayList(); products = countries = 0; while((line=br.readLine())!=null) { String[] s = line.split(","); s[0] = s[0].trim(); s[1] = s[1].trim(); int k; if(!Country.containsKey(s[0])) { CountNames.add(s[0]); Country.put(s[0],countries); k = countries; countries++; } else { k =(Integer) Country.get(s[0]); } if(!ProdtoIndex.containsKey(s[1])) { ProdNames.add(s[1]); ArrayList n = new ArrayList(); ProdtoIndex.put(s[1],products); n.add(k); ProdtoCount.add(n); products++; } else { int ind =(Integer)ProdtoIndex.get(s[1]); ArrayList c =(ArrayList) ProdtoCount.get(ind); c.add(k); } } System.out.println(CountNames); System.out.println(ProdtoCount); System.out.println(ProdNames); } catch (FileNotFoundException ex) { Logger.getLogger(PairMatching.class.getName()).log(Level.SEVERE, null, ex); } catch (IOException ex) { Logger.getLogger(PairMatching.class.getName()).log(Level.SEVERE, null, ex); } } void FindPairs() { Pairs = new ArrayList[countries][countries]; for(int i=0;i&lt;ProdNames.size();i++) { ArrayList curr = (ArrayList)ProdtoCount.get(i); for(int j=0;j&lt;curr.size();j++) { for(int k=j+1;k&lt;curr.size();k++) { int u =(Integer)curr.get(j); int v = (Integer)curr.get(k); //System.out.println(u+","+v); if(Pairs[u][v]==null) { if(Pairs[v][u]!=null) Pairs[v][u].add(i); else { Pairs[u][v] = new ArrayList(); Pairs[u][v].add(i); } } else Pairs[u][v].add(i); } } } for(int i=0;i&lt;countries;i++) { for(int j=0;j&lt;countries;j++) { if(Pairs[i][j]==null) continue; ArrayList a = Pairs[i][j]; System.out.print("\n{"+CountNames.get(i)+","+CountNames.get(j)+"} : "); for(int k=0;k&lt;a.size();k++) { System.out.print(ProdNames.get((Integer)a.get(k))+" "); } } } } public static void main(String[] args) { PairMatching pm = new PairMatching(); pm.readfile("Input data/BigData.txt"); pm.FindPairs(); } } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload