Skip to content

Commit c786435

Browse files
committed
Merge branch 'gaussianElimination' of https://github.com/jiegillet/algorithm-archive
2 parents 42ab99e + f957b3c commit c786435

File tree

26 files changed

+416
-91
lines changed

26 files changed

+416
-91
lines changed

CONTRIBUTORS.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,11 @@ Gathros
55
Jeremie Gillet (- Jie -)
66
Salim Khatib
77
Hitesh C
8+
Shaurya
89
Maxime Dherbécourt
910
Jess 3Jane
1011
Pen Pal
1112
Chinmaya Mahesh
1213
Unlambder
1314
Kjetil Johannessen
14-
CDsigma
15+
CDsigma

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22
The Arcane Algorithm Archive is a collaborative effort to create a guide for all important algorithms in all languages.
33
This goal is obviously too ambitious for a book of any size, but it is a great project to learn from and work on and will hopefully become an incredible resource for programmers in the future.
44
The book can be found here: https://www.algorithm-archive.org/.
5-
The github repository can be found here: https://github.com/algorithm-archivists/algorithm-archive.
6-
Most algorithms have been covered on the youtube channel LeiosOS: https://www.youtube.com/user/LeiosOS
7-
and livecoded on Twitch: https://www.twitch.tv/simuleios.
5+
The GitHub repository can be found here: https://github.com/algorithm-archivists/algorithm-archive.
6+
Most algorithms have been covered on the YouTube channel LeiosOS: https://www.youtube.com/user/LeiosOS
7+
and live coded on Twitch: https://www.twitch.tv/simuleios.
88
If you would like to communicate more directly, please feel free to go to our discord: https://discord.gg/Pr2E9S6.
99

1010

chapters/QI/QI.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,11 @@ There are many places to start an introduction to quantum information theory, so
1818
3. **Quantum computers in the wild:** Current experimental techniques to create a quantum computer and what makes them ill-suited as real quantum computers
1919
4. **A survey of current quantum algorithms:** There are a number of algorithms that promise fantastic advantages when performed on quantum computers and should really shake up the industry when they are finally experimentally realized.
2020

21-
As a note, item 3 might seem out of place for a book on algorithms, and I would tend to agree; however, at this point there is a phenominal amount of research being done to realize the first truly quantum computer and there are a number of potential systems that could work for this purpose.
21+
As a note, item 3 might seem out of place for a book on algorithms, and I would tend to agree; however, at this point there is a phenomenal amount of research being done to realize the first truly quantum computer and there are a number of potential systems that could work for this purpose.
2222
These systems will change how we think about and interface with quantum computation in the future and it is important to discuss where the field might be heading and when we can expect quantum computers at home.
2323

2424
Now, there are not too many languages that can compile quantum code.
25-
A while ago, we tried to make a quantum circuit compiler, which was modelled after the SPICE circuit simulator, but this was far from a computer language.
25+
A while ago, we tried to make a quantum circuit compiler, which was modeled after the SPICE circuit simulator, but this was far from a computer language.
2626
At this point in time, it is impossible to tell what quantum computing languages will look like when we finally have a truly quantum machine, so for the time being, we will not ask for community code for the chapters related to quantum information.
2727

2828
basically, it's hard to imagine how to would adequately implement Shor's algorithm in C.

chapters/convolutions/convolutions.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,9 +61,9 @@ Now, let me tell you about a bit of black computational magic:
6161

6262
That is crazy!
6363
It's also incredibly hard to explain, so let me do my best.
64-
As described in the chapter on [Fourier Transforms](chapters/FFT/cooley_tukey.md), Fourier Tranforms allow programmers to move from real space to frequency space.
64+
As described in the chapter on [Fourier Transforms](chapters/FFT/cooley_tukey.md), Fourier Transforms allow programmers to move from real space to frequency space.
6565
When we transform a wave into frequency space, we see a single peak in frequency space related to the frequency of that wave.
66-
No matter what function we send into a Fourier Transform, the frequency-space image can be interpreted as a seires of different waves with a specified frequency.
66+
No matter what function we send into a Fourier Transform, the frequency-space image can be interpreted as a series of different waves with a specified frequency.
6767

6868
So here's the idea: if we take two functions $$f(x)$$ and $$g(x)$$ and move them to frequency space to be $$\hat f(\xi)$$ and $$\hat g(\xi)$$, we can then multiply those two functions and transform them back into a third function to blend the signals together.
6969
In this way, we will have a third function that relates the frequency-space images of the two input functions.
Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
import java.util.*;
2+
3+
class TreeNode {
4+
String letter = "";
5+
int frequency = 0;
6+
TreeNode left = null, right = null;
7+
8+
public TreeNode(String letter, int frequency) {
9+
this.letter = letter;
10+
this.frequency = frequency;
11+
}
12+
13+
public TreeNode(int frequency, TreeNode left, TreeNode right) {
14+
this.frequency = frequency;
15+
this.left = left;
16+
this.right = right;
17+
}
18+
}
19+
20+
class HuffmanTree {
21+
private Map<String, Integer> frequencyMap = new HashMap<>();
22+
private Map<String, String> codeBook = new HashMap<>(), reverseCodeBook = new HashMap<>();
23+
private TreeNode root;
24+
private String stringToEncode;
25+
26+
public HuffmanTree(String stringToEncode) {
27+
this.stringToEncode = stringToEncode;
28+
}
29+
30+
public void createTree() {
31+
for (int i = 0; i < stringToEncode.length(); i++) {
32+
String key = Character.toString(stringToEncode.charAt(i));
33+
if (!frequencyMap.containsKey(key)) {
34+
frequencyMap.put(key, 1);
35+
} else {
36+
int frequency = frequencyMap.get(key) + 1;
37+
frequencyMap.replace(key, frequency);
38+
}
39+
}
40+
Queue<TreeNode> priorityQueue = new PriorityQueue<>(Comparator.comparingInt(o -> o.frequency));
41+
for (Map.Entry<String, Integer> m : frequencyMap.entrySet()) {
42+
priorityQueue.add(new TreeNode(m.getKey(), m.getValue()));
43+
}
44+
while (priorityQueue.size() > 1) {
45+
TreeNode left = priorityQueue.remove();
46+
TreeNode right = priorityQueue.remove();
47+
priorityQueue.add(new TreeNode(left.frequency + right.frequency, left, right));
48+
}
49+
root = priorityQueue.remove();
50+
}
51+
52+
private void traverse(TreeNode node, StringBuilder code) {
53+
if (node.left == null && node.right == null) {
54+
codeBook.put(node.letter, code.toString());
55+
}
56+
if (node.left != null) {
57+
traverse(node.left, code.append(0));
58+
code.deleteCharAt(code.length() - 1);
59+
}
60+
if (node.right != null) {
61+
traverse(node.right, code.append(1));
62+
code.deleteCharAt(code.length() - 1);
63+
}
64+
}
65+
66+
public void printCodeBook() {
67+
System.out.println("Code Book");
68+
for (Map.Entry<String, String> m : codeBook.entrySet()) {
69+
System.out.println(m.getKey() + "\t" + m.getValue());
70+
}
71+
System.out.println();
72+
}
73+
74+
private void CodeBookReverse() {
75+
for (Map.Entry<String, String> m : codeBook.entrySet()) {
76+
reverseCodeBook.put(m.getValue(), m.getKey());
77+
}
78+
}
79+
80+
public String encode() {
81+
traverse(root, new StringBuilder());
82+
StringBuilder encode = new StringBuilder();
83+
for (int i = 0; i < stringToEncode.length(); i++) {
84+
String k = Character.toString(stringToEncode.charAt(i));
85+
encode.append(codeBook.get(k));
86+
}
87+
printCodeBook();
88+
return encode.toString();
89+
}
90+
91+
public String decode(String encoded) {
92+
StringBuilder decoded = new StringBuilder(), key = new StringBuilder();
93+
CodeBookReverse();
94+
for (int i = 0; i < encoded.length(); i++) {
95+
key = key.append(encoded.charAt(i));
96+
if (reverseCodeBook.containsKey(key.toString())) {
97+
decoded.append(reverseCodeBook.get(key.toString()));
98+
key = new StringBuilder();
99+
}
100+
}
101+
return decoded.toString();
102+
}
103+
}
104+
105+
class Huffman {
106+
public static void main(String[] args) {
107+
HuffmanTree huffmanTree = new HuffmanTree("bibbity_bobbity");
108+
huffmanTree.createTree();
109+
String encoded = huffmanTree.encode();
110+
System.out.println("Encoded String: " + encoded);
111+
System.out.println("Decoded String: " + huffmanTree.decode(encoded));
112+
}
113+
}

chapters/data_compression/huffman/huffman.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,9 +118,11 @@ Program.cs
118118
{% sample lang="js" %}
119119
### JavaScript
120120
[import, lang:"javascript"](code/javascript/huffman.js)
121+
{% sample lang="java" %}
122+
### Java
123+
[import, lang:"java"](code/java/huffman.java)
121124
{% endmethod %}
122125

123-
124126
<script>
125127
MathJax.Hub.Queue(["Typeset",MathJax.Hub]);
126128
</script>
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
clc;clear;close all
2+
3+
%==========================================================================
4+
% Define Function
5+
f =@(x) -3*x;
6+
7+
% Define Initial and Final time
8+
tInit=0;
9+
tLast=5;
10+
11+
% Define number of points
12+
N=1e2;
13+
14+
% Set Initial Conditions
15+
yInit=1;
16+
%==========================================================================
17+
18+
dt=(tLast-tInit)/(N-1); % Calculate dt
19+
t=[tInit:dt:tLast]; % Preallocate time array
20+
y=zeros(1,length(t)); % Preallocate solution array
21+
y(1)=yInit; % Impose Initial Conditions
22+
23+
% Loop over time
24+
for i=1:length(t)-1
25+
26+
t(i+1) = t(i) + dt; % Calculate next time
27+
y(i+1) = y(i) + f( y(i) )*dt; % Update solution
28+
29+
end
30+
31+
% Plot numerical solution
32+
plot(t,y)
33+
34+
% Create analytical solution
35+
g=@(x) exp(-3*x);
36+
z=g(t);
37+
38+
% Plot analytical solution on the same graph
39+
hold on
40+
plot(t,z,'--')
41+
42+
% Set axis, title and legend
43+
xlabel('t');ylabel('y(t)');
44+
title('Analytical VS Numerical Solution')
45+
grid
46+
legend('Numerical','Analytical')
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
// submitted by lolatomroflsinnlos, modified by xam4lor
2+
public class EuclideanAlgo {
3+
public static int euclidSub(int a, int b) {
4+
a = Math.abs(a);
5+
b = Math.abs(b);
6+
7+
while (a != b) {
8+
if (a > b) {
9+
a -= b;
10+
} else {
11+
b -= a;
12+
}
13+
}
14+
15+
return a;
16+
}
17+
18+
public static int euclidMod(int a, int b) {
19+
while (b != 0) {
20+
int tmp = b;
21+
b = a % b;
22+
a = tmp;
23+
}
24+
25+
return a;
26+
}
27+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
//submitted by lolatomroflsinnlos, modified by xam4lor
2+
public class MainClass {
3+
public static void main(String[] args) {
4+
System.out.println("Euclidean Algorithm :");
5+
System.out.println(EuclideanAlgo.euclidMod(64 * 67, 64 * 81));
6+
System.out.println(EuclideanAlgo.euclidSub(128 * 12, 128 * 77) + "\n");
7+
}
8+
}

chapters/euclidean_algorithm/code/java/euclidean_example.java

Lines changed: 0 additions & 36 deletions
This file was deleted.

chapters/euclidean_algorithm/euclidean.md

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ The algorithm is a simple way to find the *greatest common divisor* (GCD) of two
1313
[import:2-8, lang="clojure"](code/clojure/euclidean_example.clj)
1414
{% sample lang="cpp" %}
1515
[import:20-33, lang="c_cpp"](code/c++/euclidean.cpp)
16+
{% sample lang="java" %}
17+
[import:3-16, lang="java"](code/java/EuclideanAlgo.java)
1618
{% sample lang="js" %}
1719
[import:15-29, lang="javascript"](code/javascript/euclidean_example.js)
1820
{% sample lang="py" %}
@@ -23,8 +25,6 @@ The algorithm is a simple way to find the *greatest common divisor* (GCD) of two
2325
[import:3-15, lang="rust"](code/rust/euclidean_example.rs)
2426
{% sample lang="ml" %}
2527
[import:9-17, lang="ocaml"](code/ocaml/euclidean_example.ml)
26-
{% sample lang="java" %}
27-
[import:9-22, lang="java"](code/java/euclidean_example.java)
2828
{% sample lang="go" %}
2929
[import:25-38, lang="golang"](code/go/euclidean.go)
3030
{% endmethod %}
@@ -46,6 +46,8 @@ Modern implementations, though, often use the modulus operator (%) like so
4646
[import:9-13, lang="clojure"](code/clojure/euclidean_example.clj)
4747
{% sample lang="cpp" %}
4848
[import:7-17, lang="c_cpp"](code/c++/euclidean.cpp)
49+
{% sample lang="java" %}
50+
[import:18-26, lang="java"](code/java/EuclideanAlgo.java)
4951
{% sample lang="js" %}
5052
[import:1-13, lang="javascript"](code/javascript/euclidean_example.js)
5153
{% sample lang="py" %}
@@ -56,8 +58,6 @@ Modern implementations, though, often use the modulus operator (%) like so
5658
[import:17-27, lang="rust"](code/rust/euclidean_example.rs)
5759
{% sample lang="ml" %}
5860
[import:3-7, lang="ocaml"](code/ocaml/euclidean_example.ml)
59-
{% sample lang="java" %}
60-
[import:24-35, lang="java"](code/java/euclidean_example.java)
6161
{% sample lang="go" %}
6262
[import:14-23, lang="golang"](code/go/euclidean.go)
6363
{% endmethod %}
@@ -88,6 +88,12 @@ Program.cs
8888
{% sample lang="cpp" %}
8989
### Cpp
9090
[import, lang="c_cpp"](code/c++/euclidean.cpp)
91+
{% sample lang="java" %}
92+
### Java
93+
EuclideanAlgo.java
94+
[import, lang="java"](code/java/EuclideanAlgo.java)
95+
MainClass.java
96+
[import, lang="java"](code/java/MainClass.java)
9197
{% sample lang="js" %}
9298
### JavaScript
9399
[import, lang="javascript"](code/javascript/euclidean_example.js)
@@ -103,10 +109,6 @@ Program.cs
103109
{% sample lang="ml" %}
104110
### Ocaml
105111
[import, lang="ocaml"](code/ocaml/euclidean_example.ml)
106-
{% sample lang="java" %}
107-
### Java
108-
[import, lang="java"](code/java/euclidean_example.java)
109-
{% sample lang="go" %}
110112
### Go
111113
[import, lang="golang"](code/go/euclidean.go)
112114
{% endmethod %}

chapters/getting_started.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# My Introduction to Hobby Programming
22

3-
Here's the thing. I love programming. I know it's fun, interactive, and addictive, but it's hard to see that when you are looking through a pair of spectacles that sees the world ever-so-slightly differently than mine. For that reason, I feel I should start with my own introduction to the world of computer science, with the hope of inspiring you to share your own story. Full disclosure: this story is a decade old and involved linux. In fact, the open source community was one of the main reasons I ever became interested in hobby programming to begin with. Regardless, let's start at the start.
3+
Here's the thing. I love programming. I know it's fun, interactive, and addictive, but it's hard to see that when you are looking through a pair of spectacles that sees the world ever-so-slightly differently than mine. For that reason, I feel I should start with my own introduction to the world of computer science, with the hope of inspiring you to share your own story. Full disclosure: this story is a decade old and involved Linux. In fact, the open source community was one of the main reasons I ever became interested in hobby programming to begin with. Regardless, let's start at the start.
44

55
I don't know if you guys had a similar childhood to mine or not, but I found that no matter who I spoke to, there was one central theme in all of our lives -- something so fundamental to the way we communicated with each other that it was nearly impossible to have a discussion without bringing it up: **Video Games**.
66

@@ -12,11 +12,11 @@ Being the curious kid I was, I asked what I should do to avoid Vista and they sa
1212

1313
My friends could have just said, "It's the thing that operates your hardware. It brings up the UI elements and stuff you work with." Sure, it's not quite the whole story, but as a computer novice, it would have been just fine for me. That said, they took it a step further and said, "Nowadays, there are many operating systems to choose from, including Windows, Mac, and Linux." I may have been a complete novice, but I at least knew about Windows and Mac. Linux, though? What the heck was that?
1414

15-
Of course I asked about it, but this was early high school. My friends may have been smarter than I was, but there was a limit to their knowledge. Apparently, the only thing they knew about linux was that it was supposedly faster and couldn't catch viruses. That caught me.
15+
Of course I asked about it, but this was early high school. My friends may have been smarter than I was, but there was a limit to their knowledge. Apparently, the only thing they knew about Linux was that it was supposedly faster and couldn't catch viruses. That caught me.
1616

1717
Remember how I said I couldn't get the virus software to work on my computer? Yeah. At this point, I was thinking, *What? A computer that couldn't catch viruses? No way! That's gotta be **leagues** easier to use than Windows!*
1818

19-
*Oh ho* I was... well... I was downright naive. I had no idea what I was doing. That night, I ran home, super excited to learn about linux and operating systems, and spent the entire night browsing the internet, trying to find whatever information was out there. At the time, my google-fu was weak and my internet was slow. The first thing I learned was that there were apparently different types of linuxes called "distributions." At that point, I probably searched "What is the best linux?" or something to that effect. Somehow, I managed to find [distrowatch](https://distrowatch.com/), which had a list of all the most popular distros on the side.
19+
*Oh ho* I was... well... I was downright naive. I had no idea what I was doing. That night, I ran home, super excited to learn about Linux and operating systems, and spent the entire night browsing the internet, trying to find whatever information was out there. At the time, my google-fu was weak and my internet was slow. The first thing I learned was that there were apparently different types of linuxes called "distributions." At that point, I probably searched "What is the best Linux?" or something to that effect. Somehow, I managed to find [distrowatch](https://distrowatch.com/), which had a list of all the most popular distros on the side.
2020

2121
I don't remember the exact order, but I knew the key players were there: Ubuntu, Mint, Fedora, Debian, and Arch. Now, here's where my years of gaming experience came in. I personified each distribution as a class in a game world. Ubuntu was the easy to use axe-wielding warrior that would get the job done. Fedora was the Archer in the back with a feather in his cap and a quick quip for everything. Debian was the grandmotherly spellcaster just trying to keep everyone alive. Then there was Arch, the one who rushed into combat without any armor and uses only the environment as a weapon.
2222

@@ -36,7 +36,7 @@ In my head, I knew what this meant. I needed to create my username, right? So I
3636

3737
**root**
3838

39-
Now, at the time, I didn't realize what I was doing. I didn't know that root was the god of my computer -- the user who decides all other user disputes. At the time, I thought it was an odd, quircky word chosen by the Arch developers in the same way they were talking about cheese graters before. I suppose that knowledge would come in time. At that moment, I was celebrating what might have been the most difficult thing I had done to that point in my life: typing in a 4 letter word.
39+
Now, at the time, I didn't realize what I was doing. I didn't know that root was the god of my computer -- the user who decides all other user disputes. At the time, I thought it was an odd, quirky word chosen by the Arch developers in the same way they were talking about cheese graters before. I suppose that knowledge would come in time. At that moment, I was celebrating what might have been the most difficult thing I had done to that point in my life: typing in a 4 letter word.
4040

4141
#### Step 3: The blue screen of death
4242

0 commit comments

Comments
 (0)