Skip to content
Snippets Groups Projects
Commit 610e625a authored by Christopher Schankula's avatar Christopher Schankula :earth_africa:
Browse files

fix merge conflicts

parents 89db0c92 b9cba827
No related branches found
No related tags found
No related merge requests found
......@@ -2,15 +2,19 @@ package search;
import java.util.Iterator;
public class Fish<Integer> implements Iterable<Integer> {
public class Bag<Integer> implements Iterable<Integer> {
private Node first;
private Node first; //pointer to head of linked list
private class Node{
Integer item;
Node next;
}
/**
* Adds new node to linked list
* @param item the item to be added to the linked list
*/
public void add(Integer item){
Node oldfirst = first;
first = new Node();
......
......@@ -9,15 +9,25 @@ public class CC {
private int count;
public static void main(String[] args) {
Graph g = new Graph(5);
Graph g = new Graph(7);
g.addEdge(4, 3);
g.addEdge(2, 3);
g.addEdge(1, 2);
g.addEdge(0, 2);
g.addEdge(5, 6);
CC component = new CC(g);
System.out.println(component.connected(0, 2));
System.out.println(component.connected(2, 0)); //true
System.out.println(component.connected(0,1)); //true
System.out.println(component.connected(1, 5)); //false
//System.out.println(component.connected(1,6)); //throws exception (supposed to)
System.out.println(component.id(0));
System.out.println(component.id(5));
System.out.println(component.id(6));
}
/**
* Constructor for Connected Component
* @param G Graph object consisting of a completed graph
*/
public CC(Graph G){
marked = new boolean[G.V()];
id = new int[G.V()];
......@@ -28,7 +38,11 @@ public class CC {
}
}
}
/**
* Method to recursively do depth-first search
* @param G Graph object consisting of a completed graph
* @param v Value of node to be searched
*/
private void dfs(Graph G, int v){
marked[v] = true;
id[v] = count;
......@@ -37,11 +51,21 @@ public class CC {
dfs(G, w);
}
}
/**
* Boolean method to checked if two components are connected
* @param v First value to be checked
* @param w Second value to be checked
* @return True if connected, else False
*/
public boolean connected(int v, int w){
return id[v] == id[w];
}
/**
* accesses the value of component id
* @param v indexed value
* @return value at index v
*/
public int id(int v){
return id[v];
}
......
......@@ -4,35 +4,52 @@ public class Graph {
private final int V; // Number of nodes
private int E; // Number of edges
private Fish<Integer>[] adj; // Adjacency list for a node
private Bag<Integer>[] adj; // Adjacency list for a node
public static void main(String[] args) {
}
/**
* Constructor used to build a graph of a specified size
* @param V The size of graph, or, the number of nodes in graph
*/
public Graph(int V){
this.V = V;
this.E = 0;
adj = (Fish<Integer>[]) new Fish[V];
adj = (Bag<Integer>[]) new Bag[V];
for(int v = 0; v < V; v++)
adj[v] = new Fish<Integer>();
adj[v] = new Bag<Integer>();
}
/**
* Accesses the number of vertices
* @return The number of vertices
*/
public int V(){
return V;
}
/**
* Accesses the number of edges
* @return The number of edges
*/
public int E(){
return E;
}
/**
* Method to connect two vertices with an edge
* @param v First vertex to be connected
* @param w Second vertex to be connected
*/
public void addEdge(int v, int w){
adj[v].add(w);
adj[w].add(v);
E++;
}
/**
* Method to access value in adjacency list
* @param V Vertex to be accessed
* @return Value of vertex V
*/
public Iterable<Integer> adj(int V){
return adj[V];
}
......
This diff is collapsed.
package sort;
public class MergeSort{
/*// Main Function for Testing Purposes Only
public static void main(String[] args) {
GeneralCompare<Integer> b1;
b1 = (a1, a2) -> (Integer) a1 - (Integer) a2;
Integer[] test = {3, 4, 2, 1, 5, 7, 9, 10, 11};
//Integer[] test = {2, 1};
sort(test, 0, test.length - 1, b1);
for (int i = 0 ; i < (test.length) ; i++) {
System.out.println(test[i]);
}
}*/
/**
* Wrapper function for the MergeSort implementation
* @param x Array of comparable items to be sorted
* @param lo Lower bound of a sub-array to be sorted
* @param hi Upper bound of a sub-array to be sorted
* @param gc A lambda function that compares two comparable items
*/
public static <T> void sort(Comparable<T>[] x, int lo, int hi, GeneralCompare<T> gc) {
Comparable<T>[] aux;
aux = (Comparable<T>[]) new Comparable[x.length];
sortWrapped(x, lo, hi, gc, aux);
}
/**
* Recursively sort each half of a sub-array
* @param lo
* @param hi
* @param gc
* @param aux Auxiliary array to accommodate temporary memory use in the algorithm
*/
private static <T> void sortWrapped(Comparable<T>[] x, int lo, int hi, GeneralCompare<T> gc, Comparable<T>[] aux) {
int n = hi - lo;
if(n < 1)
return;
// Recursively sort each half of the array
int mid = lo + (n/2);
sortWrapped(x, lo, mid, gc, aux);
sortWrapped(x, mid+1, hi, gc, aux);
merge(x, lo, hi, gc, aux);
}
/**
* Merges two sorted sub-arrays into a single sorted array
* @param x
* @param lo
* @param hi
* @param gc
* @param aux
*/
private static <T> void merge(Comparable<T>[] x, int lo, int hi, GeneralCompare<T> gc, Comparable<T>[] aux){
int n = hi - lo;
int mid = lo + (n/2);
for(int k = lo; k <= hi; k++){
aux[k] = x[k];
}
int i = lo;
int j = mid + 1;
for (int k = lo; k <= hi ; k++) {
if (i > mid)
x[k] = aux[j++]; //All elems in first half already added to x
else if (j > hi)
x[k] = aux[i++]; //All elems in second half already added to x
else if (gc.compare(aux[i], aux[j]) > 0)
x[k] = aux[j++];
else
x[k] = aux[i++];
}
}
}
package sort;
public class MergeSort{
/*// Main Function for Testing Purposes Only
public static void main(String[] args) {
GeneralCompare<Integer> b1;
b1 = (a1, a2) -> (Integer) a1 - (Integer) a2;
Integer[] test = {3, 4, 2, 1, 5, 7, 9, 10, 11};
//Integer[] test = {2, 1};
sort(test, 0, test.length - 1, b1);
for (int i = 0 ; i < (test.length) ; i++) {
System.out.println(test[i]);
}
}*/
/**
* Wrapper function for the MergeSort implementation
* @param x Array of comparable items to be sorted
* @param lo Lower bound of a sub-array to be sorted
* @param hi Upper bound of a sub-array to be sorted
* @param gc A lambda function that compares two comparable items
*/
public static <T> void sort(Comparable<T>[] x, int lo, int hi, GeneralCompare<T> gc) {
Comparable<T>[] aux;
aux = (Comparable<T>[]) new Comparable[x.length];
sortWrapped(x, lo, hi, gc, aux);
}
/**
* Recursively sort each half of a sub-array
* @param lo Lower bound of a sub-array to be sorted
* @param hi Upper bound of a sub-array to be sorted
* @param gc A lambda function that compares two comparable items
* @param aux Auxiliary array to accommodate temporary memory use in the algorithm
*/
private static <T> void sortWrapped(Comparable<T>[] x, int lo, int hi, GeneralCompare<T> gc, Comparable<T>[] aux) {
int n = hi - lo;
if(n < 1)
return;
// Recursively sort each half of the array
int mid = lo + (n/2);
sortWrapped(x, lo, mid, gc, aux);
sortWrapped(x, mid+1, hi, gc, aux);
merge(x, lo, hi, gc, aux);
}
/**
* Merges two sorted sub-arrays into a single sorted array
* @param x Array of comparable items to be sorted
* @param lo Lower bound of a sub-array to be sorted
* @param hi Upper bound of a sub-array to be sorted
* @param gc A lambda function that compares two comparable items
* @param aux Auxiliary array to accommodate temporary memory use in the algorithm
*/
private static <T> void merge(Comparable<T>[] x, int lo, int hi, GeneralCompare<T> gc, Comparable<T>[] aux){
int n = hi - lo;
int mid = lo + (n/2);
for(int k = lo; k <= hi; k++){
aux[k] = x[k];
}
int i = lo;
int j = mid + 1;
for (int k = lo; k <= hi ; k++) {
if (i > mid)
x[k] = aux[j++]; //All elems in first half already added to x
else if (j > hi)
x[k] = aux[i++]; //All elems in second half already added to x
else if (gc.compare(aux[i], aux[j]) > 0)
x[k] = aux[j++];
else
x[k] = aux[i++];
}
}
}
......@@ -36,7 +36,6 @@ public class QuickSelect {
*/
public static <T> void median(Comparable<T>[] a, int lo, int hi, GeneralCompare<T> gc) {
sort(a, lo, hi, (hi + lo) / 2, gc);
}
/**
* Partially sorts a comparable array such that elements smaller than the kth largest element
......
package test;
import search.Graph;
import static org.junit.Assert.*;
import org.junit.*;
public class TestGraph {
@Before
public void setUp() throws Exception {
System.out.println("Setting up....");
}
@Test
public void testV(){
Graph g = new Graph(8);
assertEquals(g.V(), 8);
assertNotEquals(g.V(), 7);
assertNotEquals(g.V(), 9);
}
@Test
public void testAddEdgeAndE(){
Graph g = new Graph(8);
g.addEdge(4, 3);
g.addEdge(2, 3);
g.addEdge(1, 2);
g.addEdge(0, 2);
assertNotEquals(g.E(), 3);
assertEquals(g.E(), 4);
g.addEdge(7, 2);
assertNotEquals(g.E(), 4);
assertEquals(g.E(), 5);
}
@Test
public void adj(){
Graph g = new Graph(8);
g.addEdge(4, 3);
g.addEdge(2, 3);
g.addEdge(1, 2);
g.addEdge(0, 2);
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment