コード例 #1
1
ファイル: ntkm.java プロジェクト: sherlockholms/SPOJ
  public static void main(String[] args) throws IOException {
    in = new Reader();
    out = new PrintWriter(System.out, true);

    int N = in.nextInt();

    PriorityQueue<Long> pq = new PriorityQueue<Long>();
    for (int i = 0; i < N; i++) pq.add(in.nextLong());

    long cost = 0;
    while (pq.size() > 1) {
      long a = pq.poll() + pq.poll();
      cost += a;
      pq.add(a);
    }

    out.println(cost);
  }
コード例 #2
0
  public static void main(String args[]) throws IOException {

    String text = "C:\\Users\\Roshan Rajan\\Desktop\\squaredance.txt";
    String gender = "";
    PriorityQueue<String> men = new PriorityQueue<String>();
    PriorityQueue<String> women = new PriorityQueue<String>();
    BufferedReader input = new BufferedReader(new FileReader(text));
    String line = null;
    while ((line = input.readLine()) != null) {
      gender = line.substring(0, 1);
      if (gender.equals("M")) men.add(line.substring(2));
      else women.add(line.substring(2));
    }
    input.close();

    while (!men.isEmpty() && !women.isEmpty()) {
      System.out.println("The couples are ");
      System.out.println(men.poll() + " is Dancing with " + women.poll());
    }
    if (men.isEmpty()) {
      System.out.println(women.size() + " girls are Waiting to Dance");
      System.out.println(women.peek() + " is the first one waiting");
    }
    if (women.isEmpty()) {
      System.out.println(men.size() + " guys are Waiting to Dance");
      System.out.println(men.peek() + " is the first one waiting");
    }
  }
コード例 #3
0
ファイル: MergeIntervals.java プロジェクト: bsdshell/java
  static void test00() {
    Aron.beg();
    PriorityQueue<Interval> queue = new PriorityQueue<Interval>();
    Stack<Interval> stack = new Stack<Interval>();
    int[] arr1 = {4, 1, 2, 6, 9};
    int[] arr2 = {5, 1, 4, 9, 10};

    for (int i = 0; i < arr1.length; i++) {
      queue.add(new Interval(arr1[i], arr2[i]));
    }
    if (queue.size() > 0) {
      stack.push(queue.remove());
    }
    while (!queue.isEmpty()) {
      Interval top = stack.peek();
      Interval inter = queue.remove();
      if (top.end < inter.begin) stack.push(inter);
      else {
        stack.peek().end = Math.max(stack.peek().end, inter.end);
      }
    }
    while (!stack.empty()) {
      System.out.println("[" + stack.peek().begin + " " + stack.peek().end + "]");
      stack.pop();
    }

    Aron.end();
  }
コード例 #4
0
ファイル: pencil.java プロジェクト: ruhlio/Applied-Algorithms
  private static PencilPosition findShortestRoute(int[][] maze) {
    // all found solutions to the maze
    PriorityQueue<PencilPosition> solutions =
        new PriorityQueue<PencilPosition>(5, new PencilPositionComparator());
    // bread-first search queue
    Queue<PencilPosition> routes = new LinkedList<PencilPosition>();
    // set of already visited positions
    Set<PencilPosition> visitedPositions = new HashSet<PencilPosition>();

    // add the starting positions, which is always (0,0)
    routes.add(new PencilPosition(0, 0, false, null));

    while (!routes.isEmpty()) {
      PencilPosition position = routes.poll();

      // if this is the destinations position then we've found a solution
      if (0 == maze[position.row][position.column]) {
        solutions.add(position);
        continue;
      }

      // if we haven't already visited this position
      if (!visitedPositions.contains(position)) {
        routes.addAll(findPossibleRoutes(position, maze));
        visitedPositions.add(position);
      }
    }

    return solutions.poll();
  }
コード例 #5
0
 /**
  * käy solmun vanhemmat ja pistää ne priorityqueue:n
  *
  * @param käydytsolmut
  * @param vikasolmu
  * @return
  */
 public PriorityQueue<Solmu> käysolmuttakaperin(
     PriorityQueue<Solmu> käydytsolmut, Solmu vikasolmu) {
   if (!vikasolmu.onkoalkusolmu) {
     käydytsolmut.add(vikasolmu);
     käysolmuttakaperin(käydytsolmut, vikasolmu.vanhempi);
   }
   return käydytsolmut;
 }
コード例 #6
0
ファイル: SJF.java プロジェクト: shreeshaprabhu/os-assignment
  @Override
  void solve(Set<? extends Job> jobs) {
    Job[] sortedJobs = jobs.toArray(new Job[jobs.size()]);
    Arrays.sort(sortedJobs, Job::compareArrivalTime);

    processTime = totWT = 0;
    long usefulTime = 0;
    int jobCount = jobs.size();
    PriorityQueue<Job> queue = new PriorityQueue<>(Job::compareBurstTime);
    for (Job job : sortedJobs) {
      if (job == null) {
        jobCount--;
        continue;
      }

      while (!queue.isEmpty() && processTime < job.getArrivalTime()) {
        Job nextJob = queue.poll();
        long arrivalTime = nextJob.getArrivalTime();
        long burstTime = nextJob.getBurstTime();

        if (processTime < nextJob.getArrivalTime()) {
          processList.add(new RunningProcess("Idle", arrivalTime - processTime));
          processTime = arrivalTime;
        }

        processList.add(new RunningProcess("P" + nextJob.getId(), burstTime));
        usefulTime += burstTime;
        totWT += processTime - arrivalTime;
        processTime += burstTime;
      }

      queue.add(job);
    }

    while (!queue.isEmpty()) {
      Job nextJob = queue.poll();
      long arrivalTime = nextJob.getArrivalTime();
      long burstTime = nextJob.getBurstTime();

      if (processTime < nextJob.getArrivalTime()) {
        processList.add(new RunningProcess("Idle", arrivalTime - processTime));
        processTime = arrivalTime;
      }

      processList.add(new RunningProcess("P" + nextJob.getId(), burstTime));
      usefulTime += burstTime;
      totWT += processTime - arrivalTime;
      processTime += burstTime;
    }

    totRT = totWT;
    totTAT = totWT + usefulTime;

    avgRT = avgWT = (double) totWT / (double) jobCount;
    avgTAT = (double) totTAT / (double) jobCount;

    utilization = usefulTime * 100.0 / processTime;
  }
コード例 #7
0
  public static void solveOne() throws Exception {
    int n = nextInt();
    int m = nextInt();
    int k = nextInt();
    int w = nextInt();
    char[][][] levels = new char[k][n][];
    ;
    for (int i = 0; i < k; i++) {
      for (int j = 0; j < n; j++) {
        levels[i][j] = nextString().toCharArray();
      }
    }
    int[][] cost = new int[k][k];
    PriorityQueue<int[]> q =
        new PriorityQueue<>(
            k * k,
            new Comparator<int[]>() {
              @Override
              public int compare(int[] o1, int[] o2) {
                return o1[2] - o2[2];
              }
            });
    for (int i = 0; i < k; i++) {
      for (int j = i + 1; j < k; j++) {
        int[] cur = {i, j, diff(levels[i], levels[j]) * w};
        cost[i][j] = cur[2];
        q.add(cur);
        //              px("edge", cur);
      }
    }
    //      px(q.size());
    //      for (int[] e: cost) px(e);
    DisjointUnionSet djs = new DisjointUnionSet(k);
    boolean[][] al = new boolean[k][k];
    int finalCost = n * m * k;
    for (; q.size() > 0; ) {
      int[] edge = q.poll();
      int p1 = djs.getPartitionId(edge[0]);
      int p2 = djs.getPartitionId(edge[1]);
      //          px(edge, p1 == p2, edge[2]);
      if (edge[2] > n * m) break;
      if (p1 != p2) {

        djs.unionElement(edge[0], edge[1]);
        finalCost -= n * m - edge[2];
        al[edge[1]][edge[0]] = true;
        al[edge[0]][edge[1]] = true;
      }
    }
    //      for (boolean[] e: al) px(e);
    pn(finalCost);
    boolean[] ex = new boolean[k];
    for (int i = 0; i < k; i++) {
      if (!ex[i]) {
        dfs(i, -1, ex, k, al);
      }
    }
  }
コード例 #8
0
ファイル: ShortRoute.java プロジェクト: IT-Kakan/TDA416
  // experimental
  // ====================================================================
  // ====================================================================
  // ====================================================================
  private void readAndDrawBIGGraph(String file) {
    // behövs inte än
    // @TODO
    // hur rita flera linjer mellan 2 noder? (för flera linjer)
    // reading of the graph should be done in the graph itself
    // it should be possible to get an iterator over nodes and one over edges
    // read in all the stops and lines and draw the lmap
    Scanner indata = null;
    // insert into p-queue to get them sorted
    names = new PriorityQueue<String>();
    try {
      // Read stops and put them in the node-table
      // in order to give the user a list of possible stops
      // assume input file is correct
      indata = new Scanner(new File(file + "-stops.txt"), "ISO-8859"); //
      while (indata.hasNext()) {
        String hpl = indata.next().trim();
        int xco = indata.nextInt();
        int yco = indata.nextInt();
        noderna.add(new BusStop(hpl, xco, yco));
        names.add(hpl);
        // Draw
        // this is a fix: fixa att Kålltorp och Torp är samma hållplats
        if (hpl.equals("Torp")) {
          xco += 11;
          hpl = "   / Torp";
        }
        karta.drawString(hpl, xco, yco, DrawGraph.Layer.BASE);
      }
      indata.close();

      //  Read in the lines and add to the graph
      indata = new Scanner(new File(file + "-lines.txt"), "ISO-8859");
      grafen = new DirectedGraph<BusEdge>(noderna.noOfNodes());
      Color color =
          new Color((float) Math.random(), (float) Math.random(), (float) Math.random()); //
      String lineNo = "1"; //
      while (indata.hasNext()) { // assume lines are correct
        int from = noderna.find(indata.next()).getNodeNo();
        int to = noderna.find(indata.next()).getNodeNo();
        grafen.addEdge(new BusEdge(from, to, indata.nextInt(), lineNo));
        indata.nextLine(); // skip rest of line
        // Draw
        BusStop busFrom = noderna.find(from);
        BusStop busTo = noderna.find(to);
        karta.drawLine(
            busFrom.xpos, busFrom.ypos, busTo.xpos, busTo.ypos, color, 2.0f, DrawGraph.Layer.BASE);
      }
      indata.close();
    } catch (FileNotFoundException fnfe) {
      throw new RuntimeException(" Indata till busshållplatserna saknas");
    }
    karta.repaint();
  } // end readAndDrawBIGGraph
コード例 #9
0
  public void getIndexInfo(String indexdir, int freqThreshold) {
    IndexReader reader = null;

    try {
      Directory dir = FSDirectory.open(new File(indexdir));
      System.out.println(dir);
      reader = IndexReader.open(dir);

      System.out.println("document num:" + reader.numDocs());
      System.out.println("======================");

      TermEnum terms = reader.terms();
      sortedTermQueue.clear();
      maxDocNum = reader.maxDoc();
      linkMap.clear();
      termList.clear();
      while (terms.next()) {
        // System.out.print(terms.term() + "\tDocFreq:" +
        TermDocs termDocs = reader.termDocs(terms.term());
        MyTerm temp = new MyTerm(terms.term(), termDocs, maxDocNum);
        if (temp.totalFreq < freqThreshold) {
          continue;
        } /*
           * if(temp.originTrem.text().length()==1){ continue; }
           */
        linkMap.put(temp.originTrem.text(), temp);
        sortedTermQueue.add(temp);
        termList.add(temp);
      }
      System.out.println("total Size:" + sortedTermQueue.size());
      System.out.println("mapsize:" + linkMap.keySet().size());
      // System.exit(0);
      int num = 0;
      this.maxFreq = sortedTermQueue.peek().totalFreq;
      while (!sortedTermQueue.isEmpty()) {
        num++;
        System.out.println(num + ":" + sortedTermQueue.poll());
      }
      System.out.println("read index info done");
    } catch (IOException e) {
      e.printStackTrace();
    } finally {
      try {
        reader.close();

      } catch (IOException e) {
        e.printStackTrace();
      }
    }
  }
コード例 #10
0
  private static int dijkstra(int s, int e, int[] cost, int[][] flow) {
    int[] best = new int[cost.length];
    Arrays.fill(best, -1);
    best[s] = cost[s];
    PriorityQueue<Node> pq = new PriorityQueue<Node>();
    pq.add(new Node(cost[s], s));
    while (pq.size() > 0) {
      Node n = pq.poll();

      if (n.at == e) return n.cost;
      if (n.cost < best[n.at]) continue;
      for (int i = 0; i < cost.length; i++) {
        if (flow[n.at][i] > 0) {
          int c = min(cost[i], n.cost);
          if (c > best[i]) {
            best[i] = c;
            pq.add(new Node(c, i));
          }
        }
      }
    }
    return 0;
  }
コード例 #11
0
ファイル: Word2Vec.java プロジェクト: whqwill/sparktest
  private static List<Double> getTopN(INDArray vec, int N) {
    ArrayComparator comparator = new ArrayComparator();
    PriorityQueue<Double[]> queue = new PriorityQueue(vec.rows(), comparator);

    for (int j = 0; j < vec.length(); j++) {
      final Double[] pair = new Double[] {vec.getDouble(j), (double) j};
      if (queue.size() < N) {
        queue.add(pair);
      } else {
        Double[] head = queue.peek();
        if (comparator.compare(pair, head) > 0) {
          queue.poll();
          queue.add(pair);
        }
      }
    }
    List<Double> lowToHighSimLst = new ArrayList();

    while (!queue.isEmpty()) {
      double ind = queue.poll()[1];
      lowToHighSimLst.add(ind);
    }
    return Lists.reverse(lowToHighSimLst);
  }
コード例 #12
0
ファイル: Trains.java プロジェクト: edwardchen/Trains
  /*
   * Finds the shortest route from a specified start node to a specified end node
   */
  private static void findShortestRoute(String inputString) {
    HashMap<Character, Node> nodeMapTemp = new HashMap<Character, Node>(nodeMap);
    HashMap<Character, Node> nodeMapDijkstra = new HashMap<Character, Node>();
    PriorityQueue<Node> pq = new PriorityQueue<Node>();
    Node current;
    Node currentTo;
    int shortestRoute = INFINITY;
    Set nodeSet;

    first = inputString.charAt(0);
    second = inputString.charAt(2);

    for (char key : nodeMap.keySet()) {
      nodeMap.get(key).setDistance(INFINITY);
    }

    if (first == second) {
      edgeList = nodeMapTemp.get(first).getEdges();
      for (i = 0; i < edgeList.size(); i++) {
        current = nodeMapTemp.get(edgeList.get(i).getEdgeDestination());
        current.setDistance(edgeList.get(i).getEdgeLength());
        pq.add(current);
      }
      nodeMapTemp.remove(first);
      while (pq.size() != 0) {
        current = pq.peek();
        edgeList = current.getEdges();
        if (current.getName() == second) {
          break;
        } else {
          nodeMapDijkstra.put(pq.peek().getName(), pq.peek());
          nodeMapTemp.remove(pq.poll().getName());
        }
        for (i = 0; i < edgeList.size(); i++) {
          if (nodeMapTemp.get(edgeList.get(i).getEdgeDestination()) != null) {
            currentTo = nodeMapTemp.get(edgeList.get(i).getEdgeDestination());
            if (currentTo.getDistance() > edgeList.get(i).getEdgeLength() + current.getDistance()) {
              currentTo.setDistance(edgeList.get(i).getEdgeLength() + current.getDistance());
            }
            pq.add(currentTo);
          } else {
          }
        }
      }
      for (char key : nodeMapDijkstra.keySet()) {
        edgeList = nodeMapDijkstra.get(key).getEdges();
        for (i = 0; i < edgeList.size(); i++) {
          if (edgeList.get(i).getEdgeDestination() == second) {
            if (nodeMapDijkstra.get(key).getDistance() + edgeList.get(i).getEdgeLength()
                < shortestRoute) {
              shortestRoute =
                  nodeMapDijkstra.get(key).getDistance() + edgeList.get(i).getEdgeLength();
            }
          }
        }
      }
    } else {
      edgeList = nodeMapTemp.get(first).getEdges();
      for (i = 0; i < edgeList.size(); i++) {
        current = nodeMapTemp.get(edgeList.get(i).getEdgeDestination());
        current.setDistance(edgeList.get(i).getEdgeLength());
        pq.add(current);
      }
      nodeMapTemp.remove(first);
      while (nodeMapTemp.get(second) != null) {
        current = pq.peek();
        edgeList = current.getEdges();
        if (current.getName() == second) {
          break;
        } else {
          nodeMapTemp.remove(pq.poll().getName());
        }
        for (i = 0; i < edgeList.size(); i++) {
          if (nodeMapTemp.get(edgeList.get(i).getEdgeDestination()) != null) {
            currentTo = nodeMapTemp.get(edgeList.get(i).getEdgeDestination());
            if (currentTo.getDistance() > edgeList.get(i).getEdgeLength() + current.getDistance()) {
              currentTo.setDistance(edgeList.get(i).getEdgeLength() + current.getDistance());
            }
            pq.add(currentTo);
          }
        }
      }
      shortestRoute = nodeMapTemp.get(second).getDistance();
    }
    try {
      bw.write(Integer.toString(shortestRoute));
      bw.newLine();
    } catch (IOException e) {
      System.out.println(e);
    }
  }
コード例 #13
0
  /** ***************************************************************************** */
  void addEntry(String s) {
    // System.err.println("TRACX: " + s);
    if (trace_writer != null) trace_writer.println(s);

    char s0 = s.charAt(0);
    if (s0 == 'T') { // THREAD
      if (thread_entries != null) {
        for (TraceEntry te : thread_entries) pending_entries.add(te);
        thread_entries = null;
      }
      StringTokenizer tok = new StringTokenizer(s);
      tok.nextToken();
      int id = Integer.parseInt(tok.nextToken());
      current_thread = thread_map.get(id);
      if (current_thread == null) {
        int tid = Integer.parseInt(tok.nextToken());
        String tnm = tok.nextToken("\n");
        current_thread = new ThreadData(++thread_counter, tid, tnm);
        thread_map.put(id, current_thread);
      }
      thread_entries = new ArrayList<TraceEntry>();
    } else if (s0 == 'D') { // DONE
      if (trace_writer != null) trace_writer.flush();
      if (thread_entries != null) {
        for (TraceEntry te : thread_entries) pending_entries.add(te);
        thread_entries = null;
      }
      StringTokenizer tok = new StringTokenizer(s);
      tok.nextToken();
      long time = Long.parseLong(tok.nextToken());
      if (next_time != 0) {
        int ct = 0;
        // System.err.println("TRACE: Pending size = " + pending_entries.size());
        while (!pending_entries.isEmpty() && pending_entries.peek().getTime() < next_time) {
          TraceEntry te = pending_entries.remove();
          ++ct;
          outputEntry(te);
        }
        if (ct > 0) {
          for (BdynEventUpdater eu : update_listeners) {
            eu.eventsAdded();
          }
        }
        end_time = Math.max(next_time, end_time);
      }
      next_time = time;
    } else if (s0 == 'S') {
      if (cpu_time == null) {
        StringTokenizer tok = new StringTokenizer(s);
        tok.nextToken();
        cpu_time = Boolean.parseBoolean(tok.nextToken());
      }
    } else if (cpu_time != null) {
      TraceEntry te = new TraceEntry(s, current_thread, cpu_time);
      if (thread_entries == null) pending_entries.add(te);
      else {
        thread_entries.add(te);
        if (te.isExit()) {
          int sz = thread_entries.size();
          int loc = te.getEntryLocation();
          if (sz >= 4) {
            TraceEntry t3 = thread_entries.get(sz - 2);
            if (!t3.isExit() && t3.getEntryLocation() == loc) {
              TraceEntry t2 = thread_entries.get(sz - 3);
              if (t2.isExit() && t2.getEntryLocation() == loc) {
                TraceEntry t1 = thread_entries.get(sz - 4);
                if (!t1.isExit()
                    && t1.getEntryLocation() == loc
                    && (te.getTime() - t1.getTime()) < MERGE_TIME) {
                  t1.merge(t2.getTime(), t3.getTime(), te.getTime());
                  thread_entries.remove(sz - 2);
                  thread_entries.remove(sz - 3);
                }
              }
            }
          }
        }
      }
    }
  }
コード例 #14
0
ファイル: MainOld.java プロジェクト: zchenyu/plusone
  public static void main(String[] args) throws Throwable {
    DATASET = System.getProperty("dataset", "movielens-pos.json");
    TRAINPERCENT = Double.parseDouble(System.getProperty("trainPercent", "0.8"));
    TESTPERCENT = Double.parseDouble(System.getProperty("testPercent", "0.5"));
    RUNS = Integer.parseInt(System.getProperty("runs", "1"));
    PREDICTIONS = Integer.parseInt(System.getProperty("predictions", "1"));
    // rand = new Random( Integer.parseInt( System.getProperty( "seed", "1" ) ) );
    rand = new Random();

    System.out.println("File: " + DATASET);
    System.out.println("Train Percent: " + TRAINPERCENT);
    System.out.println("Test Percent: " + TESTPERCENT);
    System.out.println("Runs: " + RUNS);
    System.out.println("Predictions: " + PREDICTIONS);
    docs = DatasetOld.loadDataset(DATASET);

    for (Algorithm alg : algs) {
      System.out.print(alg.name + "\t");
      double total = 0.0;

      long trainTime = 0;
      long predictTime = 0;

      for (int run = 0; run < RUNS; run++) {
        ArrayList<HashMap<Integer, Double>> traindocs = new ArrayList<HashMap<Integer, Double>>();
        ArrayList<HashMap<Integer, Double>> testdocs = new ArrayList<HashMap<Integer, Double>>();

        for (HashMap<Integer, Double> doc : docs) {
          if (rand.nextDouble() < TRAINPERCENT) traindocs.add(doc);
          else testdocs.add(doc);
        }

        long startTime = System.nanoTime();
        alg.train(traindocs);
        trainTime += System.nanoTime() - startTime;

        int successes = 0;

        for (HashMap<Integer, Double> testdoc : testdocs) {
          HashMap<Integer, Double> givenwords = new HashMap<Integer, Double>();
          HashSet<Integer> testwords = new HashSet<Integer>();

          for (int word : testdoc.keySet()) {
            if (rand.nextDouble() < TESTPERCENT) {
              testwords.add(word);
            } else {
              givenwords.put(word, testdoc.get(word));
            }
          }

          startTime = System.nanoTime();
          double[] scores = alg.predict(givenwords);
          predictTime += System.nanoTime() - startTime;
          // System.out.println(Arrays.toString( scores ));
          PriorityQueue<Pair> pq = new PriorityQueue<Pair>();

          for (int i = 0; i < scores.length; i++) {
            if (givenwords.containsKey(i)) {
              continue;
            }

            if (pq.size() < PREDICTIONS) {
              pq.add(new Pair(i, scores[i]));
            }
            if (scores[i] > pq.peek().score) {
              pq.poll();
              pq.add(new Pair(i, scores[i]));
            }
          }

          while (!pq.isEmpty()) {
            Pair pair = pq.poll();
            // System.out.println( WordIndex.get( pair.word ) + "\t" + pair.score + "\t" +
            // testwords.contains( pair.word ) );
            if (testwords.contains(pair.word)) {
              successes++;
            }
          }
        }

        total += (double) successes / PREDICTIONS / testdocs.size();
      }

      System.out.println(
          total / RUNS
              + "\t"
              + (trainTime / 1000000000.0 / RUNS)
              + "\t"
              + (predictTime / 1000000000.0 / RUNS));
    }
  }
コード例 #15
0
ファイル: InnerJoin.java プロジェクト: rishimittal/Databases
  // Sorts Both the file , individually
  // irrespective of their size.
  public static void initialize(
      String table1, String table1_join_col, String table2, String table2_join_col) {

    // System.out.println(DBSystem.PATH_FOR_DATA);
    // System.out.println(table1);

    tab1 = table1;
    tab2 = table2;

    for (Table t : DBSystem.tableList) {

      if (t.getName().equalsIgnoreCase(table1)) {
        table1JoinColIndex = t.getColumnNum().get(table1_join_col);
        table1JoinColType = t.getColumnData().get(table1_join_col);
        table1Lines = t.getLines();
        t1 = t;
      } else if (t.getName().equalsIgnoreCase(table2)) {
        table2JoinColIndex = t.getColumnNum().get(table2_join_col);
        table2JoinColType = t.getColumnData().get(table2_join_col);
        table2Lines = t.getLines();
        t2 = t;
      }
    }
    // System.out.println(table1 + " - " + table1_join_col + " - " + table1JoinColIndex + " - " +
    // table1JoinColType);
    // System.out.println(table2 + " - " + table2_join_col + " - " + table2JoinColIndex + " - " +
    // table2JoinColType);

    if (!table1JoinColType.equalsIgnoreCase(table2JoinColType)) {
      System.out.println("Both the tables must have similar join types");
      System.exit(1);
    }

    int numberOfcolumnsForJoin = 2; // For Joining Two tables

    // Sort the columns used for join
    for (int k = 0; k < numberOfcolumnsForJoin; k++) {
      String tab = null;
      String tabColType = null;
      int tabColIndex = 0;
      int tLines = 0;

      if (k == 0) {
        // Sorting table 1.
        tab = table1;
        tabColType = table1JoinColType;
        tabColIndex = table1JoinColIndex;
        tLines = table1Lines;
      } else if (k == 1) {
        // Sorting table 2
        tab = table2;
        tabColType = table2JoinColType;
        tabColIndex = table2JoinColIndex;
        tLines = table2Lines;
      }

      Comparator<String> cmp = new SortingComparator(tabColIndex, tabColType);
      PriorityQueue<String> priorityQueue = new PriorityQueue<String>(10, cmp);

      int remainingBufferSize = MAX_MEM_TO_USE;
      int fileCount = 1;
      String line;
      int inOutFlag = 0;

      for (int i = 0; i < tLines; i++) {
        line = DBSystem.getRecord(tab, i);

        // System.out.println(line);
        // String cols [] = line.split(",");

        remainingBufferSize -= line.getBytes().length;
        if (remainingBufferSize >= 0) {
          priorityQueue.add(line);
          inOutFlag = 1;
        } else {
          // write to file
          FileWriter fileWriter = null;
          // System.out.println("Written");
          try {
            // String fileName = setBlockFile(fileCount,table1);
            // System.out.println(fileName);
            fileWriter = new FileWriter(new File(setBlockFile(fileCount, tab)));
            int siz = priorityQueue.size();
            // System.out.println(siz);
            for (int j = 0; j < siz; j++) {
              fileWriter.write(priorityQueue.poll().toString());
              // System.out.println(priorityQueue.poll().toString());
              fileWriter.write("\n");
            }
            fileCount++;
          } catch (IOException e) {
            e.printStackTrace();
          } finally {
            try {
              fileWriter.close();
            } catch (IOException e) {
              e.printStackTrace();
            }
          }
          // set remainingSize back to MAX_MEM_TO_USE
          remainingBufferSize = MAX_MEM_TO_USE;
          // current read line
          priorityQueue.add(line);
          remainingBufferSize -= line.getBytes().length;
          inOutFlag = 0;
        }
      }

      if (inOutFlag == 1) {
        FileWriter fw = null;

        try {
          fw = new FileWriter(new File(setBlockFile(fileCount, tab)));
          int psiz = priorityQueue.size();
          for (int j = 0; j < psiz; j++) {
            fw.write(priorityQueue.poll().toString());
            // System.out.println(priorityQueue.poll().toString());
            fw.write("\n");
          }

        } catch (IOException e) {
          e.printStackTrace();
        } finally {
          try {
            fw.close();
          } catch (IOException e) {
            e.printStackTrace();
          }
        }
      }

      if (k == 0) table1_tempfileCount = fileCount;
      if (k == 1) table2_tempfileCount = fileCount;
    }

    // Begin the merging process for both the files.

    // System.out.println(table1_tempfileCount);
    // System.out.println(table2_tempfileCount);

    // Merging Process
    SortedMergeJoin sortedMergeJoin =
        new SortedMergeJoin(
            table1,
            table1_tempfileCount,
            table1JoinColIndex,
            table1JoinColType,
            table2,
            table2_tempfileCount,
            table2JoinColIndex,
            table2JoinColType);
    sortedMergeJoin.execute();
  }
コード例 #16
0
  public JSONArray Cluster(String wekaFilePath, int clusterNum) throws Exception {
    File inputFile = new File(wekaFilePath);
    ArffLoader arf = new ArffLoader();
    arf.setFile(inputFile);
    Instances originIns = arf.getDataSet();
    Instances insTest = new Instances(originIns);
    insTest.deleteStringAttributes();
    int totalNum = insTest.numInstances();

    // SimpleKMeans sm = new SimpleKMeans();
    EM em = new EM();
    em.setNumClusters(clusterNum);
    MakeDensityBasedClusterer sm = new MakeDensityBasedClusterer();
    sm.setClusterer(em);
    sm.buildClusterer(insTest);

    System.out.println("totalNum:" + insTest.numInstances());
    System.out.println("============================");
    System.out.println(sm.toString());
    Map<Integer, ArrayList<String>> result = new HashMap<Integer, ArrayList<String>>();
    for (int i = 0; i < clusterNum; i++) {
      result.put(i, new ArrayList<String>());
    }

    for (int i = 0; i < totalNum; i++) {
      Instance ins = originIns.instance(i);
      String word = ins.stringValue(0);
      Instance tempIns = new Instance(ins);
      tempIns.deleteAttributeAt(0);
      int cluster = sm.clusterInstance(tempIns);
      result.get(cluster).add(word);
    }

    // print the result
    ArrayList<String> words = new ArrayList<String>();
    JSONArray keyWords = new JSONArray();
    for (int k : result.keySet()) {
      words = result.get(k);
      PriorityQueue<MyTerm> clusterQueue = new PriorityQueue<MyTerm>(1, MyTermCompare);
      for (int i = 0; i < words.size(); i++) {
        String s = words.get(i);
        assert linkMap.containsKey(s);
        int freq = linkMap.get(s).totalFreq;
        clusterQueue.add(linkMap.get(s));
        words.set(i, "(" + s + ":" + freq + ")");
      }

      JSONArray clusterArray = new JSONArray();
      int num = clusterQueue.size() / 10 + 1; // 5%
      int totalFreq = 0;
      int totalLength = 0;
      for (int i = 0; i < num && !clusterQueue.isEmpty(); ) {
        JSONObject mem = new JSONObject();
        MyTerm myTerm = clusterQueue.poll();
        String word = myTerm.originTrem.text();
        if (word.length() == 1) {
          continue;
        }
        mem.put("text", word);
        mem.put("freq", myTerm.totalFreq);
        clusterArray.put(mem);
        i++;
        totalFreq += myTerm.totalFreq;
        totalLength += word.length();
      }

      double averFreq = totalFreq * 1.0 / num;
      double averLength = totalLength * 1.0 / num;
      int count = 0;
      while (!clusterQueue.isEmpty() && count < num) {
        MyTerm myTerm = clusterQueue.poll();
        String word = myTerm.originTrem.text();
        int freq = myTerm.totalFreq;
        int times = (int) (word.length() / averFreq) + 1;
        if (freq > averFreq / times) {
          JSONObject mem = new JSONObject();
          mem.put("text", word);
          mem.put("freq", freq);
          mem.put("extra", true);
          clusterArray.put(mem);
        }
      }

      keyWords.put(clusterArray);
      System.out.println(
          "cluster" + k + ":" + words.size() + ":\t" + (int) (words.size() * 1.0 / totalNum * 100));
      if (result.get(k).size() < 100) {
        System.out.println(result.get(k));
      }
    }
    // System.out.println("errorNum:"+errorNum);
    return keyWords;
  }
コード例 #17
0
ファイル: ShortRoute.java プロジェクト: IT-Kakan/TDA416
  // ====================================================================
  // ====================================================================
  private void readAndDrawGraph() {
    // @TODO
    // hur rita flera linjer mellan 2 noder? (för flera linjer)
    // reading of the graph should be done in the graph itself
    // it should be possible to get an iterator over nodes and one over edges
    // read in all the stops and lines and draw the lmap
    Scanner indata = null;
    // insert into p-queue to get them sorted
    names = new PriorityQueue<String>();
    try {
      // Read stops and put them in the node-table
      // in order to give the user a list of possible stops
      // assume input file is correct
      indata = new Scanner(new File("stops.noBOM.txt"), "UTF-8");
      while (indata.hasNext()) {
        String hpl = indata.next().trim();
        int xco = indata.nextInt();
        int yco = indata.nextInt();
        noderna.add(new BusStop(hpl, xco, yco));
        names.add(hpl);
        // Draw
        /*
        // Denna fix som slår ihop Kålltorp och Torp är förvirrande eftersom de är olika noder i grafen.
        // Tror man att det är samma nod blir resultatet förrvirrande.
        // Till nästa gång: Gör till samma nod med namnet Virginsgatan. Så är det i verkligheten nu.

        				// this is a fix: fixa att Kålltorp och Torp är samma hållplats
        				if ( hpl.equals("Torp") ) {
        					xco += 11;
        					hpl = "   / Torp";
        				}
        */
        karta.drawString(hpl, xco, yco, DrawGraph.Layer.BASE);
      }
      indata.close();

      //  Read in the lines and add to the graph
      indata = new Scanner(new File("lines.noBOM.txt"), "UTF-8");
      grafen = new DirectedGraph<BusEdge>(noderna.noOfNodes());
      while (indata.hasNext()) {
        String lineNo = indata.next();
        int antal = indata.nextInt() - 1;
        int from = noderna.find(indata.next()).getNodeNo();
        // hur rita flera linjer mellan 2 noder?
        // enkel inc fungerar inte
        // färgen kunde vara "äkta" dvs linjefärg
        Color color =
            new Color((float) Math.random(), (float) Math.random(), (float) Math.random());
        for (int i = 0; i < antal; i++) {
          int to = noderna.find(indata.next()).getNodeNo();
          grafen.addEdge(new BusEdge(from, to, indata.nextInt(), lineNo));
          // Draw
          BusStop busFrom = noderna.find(from);
          BusStop busTo = noderna.find(to);
          karta.drawLine(
              busFrom.xpos,
              busFrom.ypos,
              busTo.xpos,
              busTo.ypos,
              color,
              2.0f,
              DrawGraph.Layer.BASE);
          from = to;
        }
      }
      indata.close();
    } catch (FileNotFoundException fnfe) {
      throw new RuntimeException(" Indata till busshållplatserna saknas");
    }
    karta.repaint();
  } // end readAndDrawGraph