Skip to content

Commit d1e8c4f

Browse files
committed
Merge branch 'develop'
2 parents 791ce99 + f28fa1a commit d1e8c4f

File tree

1 file changed

+121
-17
lines changed

1 file changed

+121
-17
lines changed

README.md

Lines changed: 121 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,11 @@ var list = Arrays.asList(arr); // array to list
150150
var arr = strList.toArray(String[]::new); // List<String> to String[]
151151
var arr = intList.stream().mapToInt(Integer::intValue).toArray(); // List<Integer> to int[]
152152
var list = Arrays.stream(arr).boxed().sorted().collect(Collectors.toCollection(ArrayList::new)); // array to sorted list
153+
154+
// guava
155+
import com.google.common.collect.*;
156+
List<String> list = Lists.newArrayList();
157+
List<String> list = Lists.asList(boxedArray);
153158
```
154159

155160
**Examples**
@@ -184,10 +189,42 @@ var list = Arrays.stream(arr).boxed().sorted().collect(Collectors.toCollection(A
184189
- Minimum spanning tree algorithm: Kruskal's algorithm, Prim's algorithm
185190
- Maximum flow algorithm: Edmonds-Karp algorithm, Ford-Fulkerson algorithm, Push-relabel algorithm, Maximum bipartite matching
186191

192+
**Python declaration/functions**
193+
194+
```python
195+
# networkx
196+
import networkx
197+
graph = networkx.Graph()
198+
199+
# edges
200+
edges = [(seattle, chicago), (seattle, san_francisco), ...]
201+
graph.add_edges_from(edges)
202+
203+
# weighted edges
204+
weighted_edges = [(seattle, chicago, 1737), (seattle, san_francisco, 678), ...]
205+
graph.add_weighted_edges_from(weighted_edges)
206+
207+
# operations
208+
networkx.bfs_layers(graph, "Boston")
209+
networkx.minimum_spanning_tree(graph, algorithm="kruskal")
210+
networkx.dijkstra_path(graph, "Los Angeles", "Boston")
211+
```
212+
213+
**Java declaration/methods**
214+
215+
```java
216+
// guava graph
217+
import com.google.common.graph.*;
218+
MutableGraph<Integer> graph = GraphBuilder.undirected().build();
219+
MutableValueGraph<City, Distance> roads = ValueGraphBuilder.directed()
220+
.incidentEdgeOrder(ElementOrder.stable())
221+
.build();
222+
```
223+
187224
**Graph algorithms**
188225

189226
- A\* search algorithm: A single-pair shortest path algorithm. This is a variant of Dijkstra's algorithm using heuristics to try to speed up the search.
190-
- Bellman-Ford algorithm, CLRS#24.1: [c++](cpp-algorithm/src/graph), [java](java-algorithm/src/main/java/com/example/algorithm/graph) | A single source shortest path algorithm that can handle negative edge weights. It finds the shortest path from a source vertex to all other vertices in a weighted graph.
227+
- Bellman-Ford algorithm, CLRS#24.1: [c++](cpp-algorithm/src/graph), [java](java-algorithm/src/main/java/com/example/algorithm/graph) | A single source the shortest path algorithm that can handle negative edge weights. It finds the shortest path from a source vertex to all other vertices in a weighted graph.
191228
192229
```txt
193230
algorithm BellmanFord(G, source):
@@ -260,7 +297,7 @@ algorithm DFS-VISIT(G, u):
260297
u.finished = time
261298
```
262299
263-
- Dijkstra's algorithm, CLRS#24.3, CCSP#4.5.1: [c++](cpp-algorithm/src/graph), [python](python-algorithm/algorithm/graph/test)(test), [java](java-algorithm/src/main/java/com/example/algorithm/graph) | A single source the shortest path algorithm that handle non-negative edge weights. It find the shortest path between two vertices in a graph.
300+
- Dijkstra's algorithm, CLRS#24.3, CCSP#4.5.1: [c++](cpp-algorithm/src/graph), [python](python-algorithm/algorithm/graph/test)(test), [java](java-algorithm/src/main/java/com/example/algorithm/graph) | A single source the shortest path algorithm that handle non-negative edge weights. It finds the shortest path between two vertices in a graph.
264301

265302
```txt
266303
algorithm Dijkstra(G, source):
@@ -342,7 +379,7 @@ algorithm Prim(G, root):
342379
```
343380

344381
- Push-relabel algorithm, CLRS#26.4
345-
- Viterbi algorithm: A shortest stochastic path algorithm. It solves with additional probabilistic weights on each node.
382+
- Viterbi algorithm: Shortest stochastic path algorithm. It solves with additional probabilistic weights on each node.
346383

347384
**Examples**
348385

@@ -413,19 +450,62 @@ var set = new HashSet<Integer>();
413450
add(1), remove(1), size(), isEmpty(), contains(1), clear(), iterator()
414451
var arr = set.toArray(Integer[]::new);
415452

416-
// unmodifiable
417-
import java.util.Collections;
418-
Collections.unmodifiableMap(map);
419-
Collections.unmodifiableSet(set);
420-
Collections.unmodifiableSortedMap(map);
421-
Collections.unmodifiableSortedSet(set);
453+
// enum map
454+
import java.util.EnumMap;
455+
Map<City, Integer> map = new EnumMap<>(City.class);
456+
457+
// linked hash map, linked hash set
458+
import java.util.LinkedHashMap;
459+
import java.util.LinkedHashSet;
460+
var map = new LinkedHashMap<String, Integer>();
461+
var set = new LinkedHashSet<Integer>();
422462

423-
// stream
463+
// unboxing
424464
int[] result = map.entrySet().stream()
425465
.sorted(Map.Entry.comparingByValue(Comparator.reverseOrder()))
426466
.map(Map.Entry::getKey)
427467
.mapToInt(Integer::parseInt)
428468
.toArray();
469+
470+
// guava
471+
import com.google.common.collect.*;
472+
Map<String, Integer> map = Maps.newHashMap();
473+
Set<Integer> set = Sets.newHashSet();
474+
EnumMap<City, Country> map = Maps.newEnumMap(City.class);
475+
LinkedHashMap<String, Integer> map = Maps.newLinkedHashMap();
476+
LinkedHashSet<Integer> set = Sets.newLinkedHashSet();
477+
478+
// guava multiset (implements Multiset<E>)
479+
import com.google.common.collect.*;
480+
HashMultiset<String> multiset = HashMultiset.create();
481+
TreeMultiset<String> multiset = TreeMultiset.create();
482+
LinkedHashMultiset<String> multiset = LinkedHashMultiset.create();
483+
ConcurrentHashMultiset<String> multiset = ConcurrentHashMultiset.create();
484+
ImmutableMultiset<String> multiset = ImmutableMultiset.of("a", "b", "c");
485+
486+
// guava multimap (implements Multimap<K, V>)
487+
import com.google.common.collect.*;
488+
ArrayListMultimap<String, Integer> multimap = ArrayListMultimap.create();
489+
HashMultimap<String, Integer> multimap = HashMultimap.create();
490+
LinkedListMultimap<String, Integer> multimap = LinkedListMultimap.create();
491+
LinkedHashMultimap<String, Integer> multimap = LinkedHashMultimap.create();
492+
TreeMultimap<String, Integer> multimap = TreeMultimap.create();
493+
ImmutableListMultimap<String, Integer> multimap = ImmutableListMultimap.of("a", 1, "a", 2, "b", 3);
494+
ImmutableSetMultimap<String, Integer> multimap = ImmutableSetMultimap.of("a", 1, "a", 2, "b", 3);
495+
496+
// guava bimap (implements BiMap<K, V>, Map<K, V>)
497+
import com.google.common.collect.*;
498+
HashBiMap<String, Integer> bimap = HashBiMap.create();
499+
ImmutableBiMap<String, Integer> bimap = ImmutableBiMap.of("a", 1, "b", 2);
500+
EnumBiMap<City, Country> bimap = EnumBiMap.create(City.class, Country.class);
501+
EnumHashBiMap<City, Integer> bimap = EnumHashBiMap.create(City.class);
502+
503+
// guava table (implements Table<R, C, V>)
504+
import com.google.common.collect.*;
505+
Table<Vertex, Vertex, Double> weightedGraph = HashBasedTable.create();
506+
Table<Vertex, Vertex, Double> weightedGraph = TreeBasedTable.create();
507+
Table<Vertex, Vertex, Double> weightedGraph = ArrayTable.create(Arrays.asList(v1, v2), Arrays.asList(v3, v4));
508+
Table<Vertex, Vertex, Double> weightedGraph = ImmutableTable.of(v1, v2, 4.0);
429509
```
430510

431511
**Examples**
@@ -434,11 +514,11 @@ int[] result = map.entrySet().stream()
434514
- Anonymous words constructible: [c++](cpp-algorithm/src/hash_table)(`IsWordConstructibleFromMagazine`) | Check if a letter can be written using the words in a magazine.
435515
- Collatz conjecture, EPI#12.11: [c++](cpp-algorithm/src/hash_table)(`FindNumbersSatisfyingCollatzConjecture`) | Find the numbers satisfying the Collatz conjecture.
436516
- Find anagrams: [c++](cpp-algorithm/src/hash_table)(`FindAnagramMappings`) | Given an array of strings, group anagrams together.
437-
- Find smallest subarray covering all values, EPI#12.6: [c++](cpp-algorithm/src/hash_table)(`FindSmallestSubarrayCoveringSubset`) | Find the smallest subarray that covers all the elements in a set.
438-
- Find smallest subarray sequentially covering all values, EPI#12.7: [c++](cpp-algorithm/src/hash_table)(`FindSmallestSubarraySequentiallyCoveringSubset`) | Find the smallest subarray that sequentially covers all the elements in a set.
439-
- ISBN cache, EPI#12.3: [c++](cpp-algorithm/src/hash_table) | Implement a LRU (Least Recently Used) cache for ISBN lookups.
517+
- Find the smallest subarray covering all values, EPI#12.6: [c++](cpp-algorithm/src/hash_table)(`FindSmallestSubarrayCoveringSubset`) | Find the smallest subarray that covers all the elements in a set.
518+
- Find the smallest subarray sequentially covering all values, EPI#12.7: [c++](cpp-algorithm/src/hash_table)(`FindSmallestSubarraySequentiallyCoveringSubset`) | Find the smallest subarray that sequentially covers all the elements in a set.
519+
- ISBN cache, EPI#12.3: [c++](cpp-algorithm/src/hash_table) | Implement an LRU (Least Recently Used) cache for ISBN lookups.
440520
- Nearest repeated entry, EPI#12.5: [c++](cpp-algorithm/src/hash_table)(`FindNearestRepeatedEntry`) | Find the nearest repeated entry in an array of strings.
441-
- Optimized lowest common ancestor, EPI#12.4: [c++](cpp-algorithm/src/hash_table)(`FindOptimizedLowestCommonAncestor`) | Find the lowest common ancestor of two nodes in a binary tree using a hash table. This traverses together until node1 and node2 meet.
521+
- Optimized the lowest common ancestor, EPI#12.4: [c++](cpp-algorithm/src/hash_table)(`FindOptimizedLowestCommonAncestor`) | Find the lowest common ancestor of two nodes in a binary tree using a hash table. This traverses together until node1 and node2 meet.
442522
- Palindromic permutation, EPI#12.1: [c++](cpp-algorithm/src/hash_table)(`IsPalindromePermutation`) | Given a string, determine if a permutation of the string could form a palindrome.
443523

444524
[:arrow_up_small: back to toc](#table-of-contents)
@@ -472,6 +552,10 @@ var queue = new PriorityQueue<Integer>();
472552
var queue = new PriorityQueue<Integer>(Collections.reverseOrder());
473553
add(1), peek(), poll(), remove(), size(), isEmpty(),
474554
contains(1), clear(), iterator()
555+
556+
// guava
557+
import com.google.common.collect.*;
558+
PriorityQueue<Integer> queue = Queues.newPriorityQueue();
475559
```
476560

477561
**Heap algorithms**
@@ -520,6 +604,11 @@ var list = new ArrayList<Integer>();
520604
add(1), addAll(List.of(2, 3, 4, 5)), remove(0), subList(1, 3),
521605
get(0), size(), isEmpty(), contains(3), containsAll(List.of(3, 4)),
522606
iterator(), listIterator()
607+
608+
// guava
609+
import com.google.common.collect.*;
610+
List<Integer> list = Lists.newLinkedList();
611+
List<Integer> list = Lists.newArrayList();
523612
```
524613

525614
**Examples**
@@ -575,6 +664,10 @@ iterator(), descendingIterator()
575664

576665
var array = deque.toArray(Integer[]::new); // deque to array
577666
var list = new ArrayList<>(deque); // deque to list
667+
668+
// guava
669+
import com.google.common.collect.*;
670+
ArrayDeque<Integer> deque = Queues.newArrayDeque();
578671
```
579672

580673
**Examples**
@@ -669,6 +762,11 @@ var set = new TreeSet<Integer>(List.of(1, 2, 3, 4, 5));
669762
add(1), remove(1), size(), isEmpty(), contains(1), clear(), iterator(), descendingIterator(),
670763
first(), last(), lower(3), higher(3), floor(3), ceiling(3), pollFirst(), pollLast(),
671764
headSet(3), tailSet(3), subSet(2, 4), descendingSet()
765+
766+
// guava
767+
import com.google.common.collect.*;
768+
TreeMap<Integer, Integer> map = Maps.newTreeMap();
769+
TreeSet<Integer> set = Sets.newTreeSet();
672770
```
673771

674772
**Properties of Trees**
@@ -780,7 +878,7 @@ Math.abs(-34.5), Math.ceil(2.17), Math.floor(3.14), Math.max(x, -3), Math.min(x,
780878
- Greatest common divisor (GCD), CLRS#31.2: [python](python-algorithm/algorithm/math), [java](java-algorithm/src/main/java/com/example/algorithm/math) | Find the greatest common divisor of two numbers.
781879
- Integer factorization: [c++](cpp-algorithm/src/math), [java](java-algorithm/src/main/java/com/example/algorithm/math) | Integer factorization is the process of determining which prime numbers divide a given positive integer.
782880
- Least common multiple (LCM): [python](python-algorithm/algorithm/math), [java](java-algorithm/src/main/java/com/example/algorithm/math) | Find the least common multiple of two numbers.
783-
- Miller-Rabin primality test, , CLRS#31.8: [c++](cpp-algorithm/src/math) | Miller-Rabin primality test is a mathematical algorithm that finds whether a given number is prime.
881+
- Miller-Rabin primality test, CLRS#31.8: [c++](cpp-algorithm/src/math) | Miller-Rabin primality test is a mathematical algorithm that finds whether a given number is prime.
784882
- Permutation: [c++](cpp-algorithm/src/math)(`Permutation`) | Find the permutation of a set of items.
785883
- Permutation, EPI#5.10: [c++](cpp-algorithm/src/math)(`ApplyPermutationWithAdditionalSpace`, `ApplyPermutationBySwap`) | Permute the elements of an array
786884
- Permutation: [c++](cpp-algorithm/src/math)(`InversePermutation`)
@@ -832,11 +930,11 @@ int("1000010", 2), int("52", 8), int("2a", 16) # string -> binary/octal/hex
832930
bin(42), oct(42), hex(42) # int -> binary/octal/hex
833931
ascii('a'), chr(97), ord('a') # unicode <-> ascii code
834932

835-
### copy
933+
# copy
836934
copy.deepcopy(number_list) # deep copy
837935
copy.copy(number_list) # shallow copy
838936

839-
### random
937+
# random
840938
random.randrange(28) # [0, 28)
841939
random.randrange(1, 100) # [1, 100)
842940
random.randrange(8, 16) # [8, 16)
@@ -868,6 +966,12 @@ Long.bitCount(42) // number of 1-bits
868966
import java.util.BitSet;
869967
new BitSet(16), set(0), set(0, 8), set(0, 8, true)
870968

969+
// hex digits
970+
import java.util.HexFormat;
971+
HexFormat hex = HexFormat.of();
972+
byte b = 127;
973+
String byteStr = hex.toHexDigits(b);
974+
871975
// random values
872976
import java.util.Random;
873977
var random = new Random();

0 commit comments

Comments
 (0)