This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package graph; | |
import java.util.ArrayList; | |
import java.util.Scanner; | |
public class ConnectedComponents { | |
private static boolean[] v; | |
private static ArrayList<Integer>[] g; | |
private static int counter = 0; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package graph; | |
import java.util.PriorityQueue; | |
public class GraphSearch { | |
// Priority queue for getting the shortest connected edge | |
PriorityQueue<Node> Q = new PriorityQueue<Node>((n1, n2) -> { | |
if (n1.distance < n2.distance) | |
return -1; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package graph; | |
import java.util.ArrayList; | |
import java.util.Scanner; | |
import java.util.Stack; | |
public class Reachability { | |
private static int[] pre; | |
private static int[] post; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.masary.util; | |
import java.util.Collection; | |
import java.util.Map; | |
import java.util.Set; | |
import java.util.Timer; | |
import java.util.TimerTask; | |
import java.util.concurrent.ConcurrentHashMap; | |
import java.util.stream.Collectors; | |
/** |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(def combinations | |
"creates combinations of items for example [1 2 3] | |
will generate ((1 2 3) (1 2) (1 3) (1) (2 3) (2) (3) [])" | |
(memoize (fn[items] | |
(if (empty? items) [[]] | |
(let [els (combinations (rest items))] | |
(concat (map #(cons (first items) %)els) els)))))) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(defn node [g] | |
(set (flatten g))) | |
(defn adjList [g] | |
(into {} (vec ((fn [m] into {} (for [[k v]m] {k (node v)}))(group-by first g))))) | |
(defn connected? [g a b] | |
(loop [n (a g) | |
v #{}] | |
(let [diff (clojure.set/difference n v)] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(defn longest [xs ys] (if (> (count xs) (count ys)) xs ys)) | |
(def lcs | |
(memoize | |
(fn [[x & xs] [y & ys]] | |
(cond | |
(or (= x nil) (= y nil) ) nil | |
(= x y) (cons x (lcs xs ys)) | |
:else (longest (lcs (cons x xs) ys) (lcs xs (cons y ys))))))) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(def items [[1 2] [2 4] [3 6] [10 5]]) | |
(defn values [] (into [] ((fn [items] (for [i items] {:weight (first i) :value (last i)}))items))) | |
(defn knapsack [weight values] | |
(if | |
(empty? values) 0 ;; base case | |
(if (> (:weight (first values)) weight) | |
(knapsack weight (rest values)) ;; continue |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.algorithms; | |
import java.util.ArrayList; | |
import java.util.Arrays; | |
import java.util.List; | |
import java.util.Scanner; | |
import static java.lang.Math.*; | |
import static java.util.Arrays.*; | |
import static java.util.stream.IntStream.*; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.algorithms; | |
/** | |
* Created by Hadi on 10/21/2016. | |
*/ | |
public class CoinChange { | |
public static void main(String[] args){ | |
int arr[] = {1, 3, 5}; |
OlderNewer