Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

public class ArticulationPointsAdjacencyList {

private int n, id, rootNodeOutcomingEdgeCount;
private int n, id, rootNodeOutgoingEdgeCount;
private boolean solved;
private int[] low, ids;
private boolean[] visited, isArticulationPoint;
Expand All @@ -40,9 +40,9 @@ public boolean[] findArticulationPoints() {

for (int i = 0; i < n; i++) {
if (!visited[i]) {
rootNodeOutcomingEdgeCount = 0;
rootNodeOutgoingEdgeCount = 0;
dfs(i, i, -1);
isArticulationPoint[i] = (rootNodeOutcomingEdgeCount > 1);
isArticulationPoint[i] = (rootNodeOutgoingEdgeCount > 1);
}
}

Expand All @@ -52,7 +52,7 @@ public boolean[] findArticulationPoints() {

private void dfs(int root, int at, int parent) {

if (parent == root) rootNodeOutcomingEdgeCount++;
if (parent == root) rootNodeOutgoingEdgeCount++;

visited[at] = true;
low[at] = ids[at] = id++;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,6 @@
*
* <p>Space Complexity: O(n*log2(n))
*
* <p>To run script:
*
* <p>./gradlew run -Palgorithm=graphtheory.treealgorithms.LowestCommonAncestorEulerTour
*
* @author William Fiset
*/
package com.williamfiset.algorithms.graphtheory.treealgorithms;
Expand All @@ -20,70 +16,6 @@

public class LowestCommonAncestorEulerTour {

public static void main(String[] args) {
TreeNode root = createFirstTreeFromSlides();
LowestCommonAncestorEulerTour solver = new LowestCommonAncestorEulerTour(root);

// LCA of 13 and 14 = 2
TreeNode lca = solver.lca(13, 14);
System.out.printf("LCA of 13 and 14 = %s\n", lca);
if (lca.index() != 2) {
System.out.println("Error, expected lca to be 2");
}

// LCA of 9 and 11 = 0
lca = solver.lca(9, 11);
System.out.printf("LCA of 9 and 11 = %s\n", lca);
if (lca.index() != 0) {
System.out.println("Error, expected lca to be 0");
}

// LCA of 12 and 12 = 12
lca = solver.lca(12, 12);
System.out.printf("LCA of 12 and 12 = %s\n", lca);
if (lca.index() != 12) {
System.out.println("Error, expected lca to be 12");
}
}

private static TreeNode createFirstTreeFromSlides() {
int n = 17;
List<List<Integer>> tree = createEmptyGraph(n);

addUndirectedEdge(tree, 0, 1);
addUndirectedEdge(tree, 0, 2);
addUndirectedEdge(tree, 1, 3);
addUndirectedEdge(tree, 1, 4);
addUndirectedEdge(tree, 2, 5);
addUndirectedEdge(tree, 2, 6);
addUndirectedEdge(tree, 2, 7);
addUndirectedEdge(tree, 3, 8);
addUndirectedEdge(tree, 3, 9);
addUndirectedEdge(tree, 5, 10);
addUndirectedEdge(tree, 5, 11);
addUndirectedEdge(tree, 7, 12);
addUndirectedEdge(tree, 7, 13);
addUndirectedEdge(tree, 11, 14);
addUndirectedEdge(tree, 11, 15);
addUndirectedEdge(tree, 11, 16);

return TreeNode.rootTree(tree, 0);
}

/* Graph/Tree creation helper methods. */

// Create a graph as a adjacency list with 'n' nodes.
public static List<List<Integer>> createEmptyGraph(int n) {
List<List<Integer>> graph = new ArrayList<>(n);
for (int i = 0; i < n; i++) graph.add(new LinkedList<>());
return graph;
}

public static void addUndirectedEdge(List<List<Integer>> graph, int from, int to) {
graph.get(from).add(to);
graph.get(to).add(from);
}

public static class TreeNode {
// Number of nodes in the subtree. Computed when tree is built.
private int n;
Expand All @@ -100,7 +32,7 @@ public TreeNode(int index) {
public TreeNode(int index, TreeNode parent) {
this.index = index;
this.parent = parent;
children = new LinkedList<>();
children = new ArrayList<>();
}

public void addChildren(TreeNode... nodes) {
Expand Down Expand Up @@ -134,8 +66,8 @@ public static TreeNode rootTree(List<List<Integer>> graph, int rootId) {
TreeNode root = new TreeNode(rootId);
TreeNode rootedTree = buildTree(graph, root);
if (rootedTree.size() < graph.size()) {
System.out.println(
"WARNING: Input graph malformed. Did you forget to include all n-1 edges?");
throw new IllegalArgumentException(
"Input graph malformed. Did you forget to include all n-1 edges?");
}
return rootedTree;
}
Expand Down Expand Up @@ -227,6 +159,20 @@ public TreeNode lca(int index1, int index2) {
return nodeOrder[i];
}

/* Graph/Tree creation helper methods. */

// Create a graph as an adjacency list with 'n' nodes.
public static List<List<Integer>> createEmptyGraph(int n) {
List<List<Integer>> graph = new ArrayList<>(n);
for (int i = 0; i < n; i++) graph.add(new ArrayList<>());
return graph;
}

public static void addUndirectedEdge(List<List<Integer>> graph, int from, int to) {
graph.get(from).add(to);
graph.get(to).add(from);
}

// Sparse table for efficient minimum range queries in O(1) with O(nlogn) space
private static class MinSparseTable {

Expand Down Expand Up @@ -295,4 +241,45 @@ public int queryIndex(int l, int r) {
}
}
}

public static void main(String[] args) {
TreeNode root = createSampleTree();
LowestCommonAncestorEulerTour solver = new LowestCommonAncestorEulerTour(root);

// LCA of 13 and 14 = 2
TreeNode lca = solver.lca(13, 14);
System.out.printf("LCA of 13 and 14 = %s\n", lca);

// LCA of 9 and 11 = 0
lca = solver.lca(9, 11);
System.out.printf("LCA of 9 and 11 = %s\n", lca);

// LCA of 12 and 12 = 12
lca = solver.lca(12, 12);
System.out.printf("LCA of 12 and 12 = %s\n", lca);
}

private static TreeNode createSampleTree() {
int n = 17;
List<List<Integer>> tree = createEmptyGraph(n);

addUndirectedEdge(tree, 0, 1);
addUndirectedEdge(tree, 0, 2);
addUndirectedEdge(tree, 1, 3);
addUndirectedEdge(tree, 1, 4);
addUndirectedEdge(tree, 2, 5);
addUndirectedEdge(tree, 2, 6);
addUndirectedEdge(tree, 2, 7);
addUndirectedEdge(tree, 3, 8);
addUndirectedEdge(tree, 3, 9);
addUndirectedEdge(tree, 5, 10);
addUndirectedEdge(tree, 5, 11);
addUndirectedEdge(tree, 7, 12);
addUndirectedEdge(tree, 7, 13);
addUndirectedEdge(tree, 11, 14);
addUndirectedEdge(tree, 11, 15);
addUndirectedEdge(tree, 11, 16);

return TreeNode.rootTree(tree, 0);
}
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
/**
* Bubble sort implementation
*
* <p>Run with:
*
* <p>$ ./gradlew run -Palgorithm=sorting.BubbleSort
*
* @author William Fiset, william.alexandre.fiset@gmail.com
*/
package com.williamfiset.algorithms.sorting;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
/**
* Bucket sort implementation
*
* <p>Run with:
*
* <p>$ ./gradlew run -Palgorithm=sorting.BucketSort
*
* @author William Fiset, william.alexandre.fiset@gmail.com
*/
package com.williamfiset.algorithms.sorting;
Expand Down Expand Up @@ -37,7 +33,7 @@ private static void bucketSort(int[] ar, int minValue, int maxValue) {

// Place each element in a bucket
for (int i = 0; i < N; i++) {
int bi = (ar[i] - minValue) / M;
int bi = (ar[i] - minValue) / N;
List<Integer> bucket = buckets.get(bi);
bucket.add(ar[i]);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
/**
* An implementation of counting sort!
*
* <p>Run with:
*
* <p>$ ./gradlew run -Palgorithm=sorting.CountingSort
*
* @author William Fiset, william.alexandre.fiset@gmail.com
*/
package com.williamfiset.algorithms.sorting;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
/**
* Implementation of heapsort
*
* <p>Run with:
*
* <p>$ ./gradlew run -Palgorithm=sorting.Heapsort
*
* @author William Fiset, william.alexandre.fiset@gmail.com
*/
package com.williamfiset.algorithms.sorting;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
/**
* Insertion sort implementation
*
* <p>Run with:
*
* <p>$ ./gradlew run -Palgorithm=sorting.InsertionSort
*
* @author William Fiset, william.alexandre.fiset@gmail.com
*/
package com.williamfiset.algorithms.sorting;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
/**
* Mergesort implementation
*
* <p>Run with:
*
* <p>$ ./gradlew run -Palgorithm=sorting.Mergesort
*
* @author William Fiset, william.alexandre.fiset@gmail.com
*/
package com.williamfiset.algorithms.sorting;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
/**
* Quicksort implementation using Hoare partitioning
*
* <p>Run with:
*
* <p>$ ./gradlew run -Palgorithm=sorting.QuickSort
*
* @author William Fiset, william.alexandre.fiset@gmail.com
*/
package com.williamfiset.algorithms.sorting;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,6 @@
* improved partitioning algorithm. QuickSort is quite slow in the case where very few unique
* elements exist in the array so the QuickSort3 algorithm is used at that time.
*
* <p>Run with:
*
* <p>$ ./gradlew run -Palgorithm=sorting.QuickSort3
*
* @author Atharva Thorve, aaathorve@gmail.com
*/
package com.williamfiset.algorithms.sorting;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,6 @@
*
* <p>Time Complexity: O(nw)
*
* <p>Run with:
*
* <p>$ ./gradlew run -Palgorithm=sorting.RadixSort
*
* @author EAlexa
*/
package com.williamfiset.algorithms.sorting;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
/**
* Selection sort implementation
*
* <p>Run with:
*
* <p>$ ./gradlew run -Palgorithm=sorting.SelectionSort
*
* @author William Fiset, william.alexandre.fiset@gmail.com
*/
package com.williamfiset.algorithms.sorting;
Expand Down
Loading
Loading