im trying to create a B+ tree from a previous B tree implementation I create, but Im really lost here... the only difference from B to B+ im trying to implement, is storing the keys on the leaves instead of removing them.
Example:
Final B Tree
3 6 false
1 2 true
4 5 true
7 8 9 10 true
Final B+ Tree
3 6 false
1 2 true
3 4 5 true
6 7 8 9 10 true
This is what I have for a B Tree (I really didnt want to post the whole code, but explaining all of it will be harder and confusing). I would appreciate at least some ideas...
MAIN
public class BTreeTest{
public static void main(String[] args) {
Random generator = new Random();
BTree T = new BTree(3);
final int INSERTS = 50; // how many elements are inserted
final int VALUE_LIMIT = 1000; // generated integers up to VALUE_LIMIT
int[] values = new int[INSERTS]; // array can be used to print insert order
// or to test other methods
for (int i=0;i<INSERTS;i++){
int val = generator.nextInt(VALUE_LIMIT);
values[i] = val;
T.insert(val);
}
T.printNodes();
}}
B tree NODE
public class BTreeNode{
public int[] key;
public BTreeNode[] c;
boolean isLeaf;
public int n;
private int T; //Each node has at least T-1 and at most 2T-1 keys
public BTreeNode(int t){
T = t;
isLeaf = true;
key = new int[2*T-1];
c = new BTreeNode[2*T];
n=0;
}
public boolean isFull(){
return n==(2*T-1);
}
public void insert(int newKey){
// Insert new key to current node
// We make sure that the current node is not full by checking and
// splitting if necessary before descending to node
//System.out.println("inserting " + newKey); // Debugging code
int i=n-1;
if (isLeaf){
while ((i>=0)&& (newKey<key[i])) {
key[i+1] = key[i];
i--;
}
n++;
key[i+1]=newKey;
}
else{
while ((i>=0)&& (newKey<key[i])) {
i--;
}
int insertChild = i+1; // Subtree where new key must be inserted
if (c[insertChild].isFull()){
// The root of the subtree where new key will be inserted has to be split
// We promote the mediand of that root to the current node and
// update keys and references accordingly
//System.out.println("This is the full node we're going to break ");
// Debugging code
//c[insertChild].printNodes();
//System.out.println("going to promote " + c[insertChild].key[T-1]);
n++;
c[n]=c[n-1];
for(int j = n-1;j>insertChild;j--){
c[j] =c[j-1];
key[j] = key[j-1];
}
key[insertChild]= c[insertChild].key[T-1];
c[insertChild].n = T-1;
BTreeNode newNode = new BTreeNode(T);
for(int k=0;k<T-1;k++){
newNode.c[k] = c[insertChild].c[k+T];
newNode.key[k] = c[insertChild].key[k+T];
}
newNode.c[T-1] = c[insertChild].c[2*T-1];
newNode.n=T-1;
newNode.isLeaf = c[insertChild].isLeaf;
c[insertChild+1]=newNode;
//System.out.println("This is the left side ");
//c[insertChild].printNodes();
//System.out.println("This is the right side ");
//c[insertChild+1].printNodes();
//c[insertChild+1].printNodes();
if (newKey <key[insertChild]){
c[insertChild].insert(newKey); }
else{
c[insertChild+1].insert(newKey); }
}
else
c[insertChild].insert(newKey);
}
}
public void print(){
//Prints all keys in the tree in ascending order
if (isLeaf){
for(int i =0; i<n;i++)
System.out.print(key[i]+" ");
System.out.println();
}
else{
for(int i =0; i<n;i++){
c[i].print();
System.out.print(key[i]+" ");
}
c[n].print();
}
}
public void printNodes(){
//Prints all keys in the tree, node by node, using preorder
//It also prints the indicator of whether a node is a leaf
//Used mostly for debugging purposes
printNode();
if (!isLeaf){
for(int i =0; i<=n;i++){
c[i].printNodes();
}
}
}
public void printNode(){
//Prints all keys in node
for(int i =0; i<n;i++)
System.out.print(key[i]+" ");
System.out.println(isLeaf);
}
}
B Tree
public class BTree{
private BTreeNode root;
private int T; //2T is the maximum number of childen a node can have
private int height;
public BTree(int t){
root = new BTreeNode(t);
T = t;
height = 0;
}
public void printHeight(){
System.out.println("Tree height is "+height);
}
public void insert(int newKey){
if (root.isFull()){//Split root;
split();
height++;
}
root.insert(newKey);
}
public void print(){
// Wrapper for node print method
root.print();
}
public void printNodes(){
// Wrapper for node print method
root.printNodes();
}
public void split(){
// Splits the root into three nodes.
// The median element becomes the only element in the root
// The left subtree contains the elements that are less than the median
// The right subtree contains the elements that are larger than the median
// The height of the tree is increased by one
//System.out.println("Before splitting root");
//root.printNodes(); // Code used for debugging
BTreeNode leftChild = new BTreeNode(T);
BTreeNode rightChild = new BTreeNode(T);
leftChild.isLeaf = root.isLeaf;
rightChild.isLeaf = root.isLeaf;
leftChild.n = T-1;
rightChild.n = T-1;
int median = T-1;
for (int i = 0;i<T-1;i++){
leftChild.c[i] = root.c[i];
leftChild.key[i] = root.key[i];
}
leftChild.c[median]= root.c[median];
for (int i = median+1;i<root.n;i++){
rightChild.c[i-median-1] = root.c[i];
rightChild.key[i-median-1] = root.key[i];
}
rightChild.c[median]=root.c[root.n];
root.key[0]=root.key[median];
root.n = 1;
root.c[0]=leftChild;
root.c[1]=rightChild;
root.isLeaf = false;
//System.out.println("After splitting root");
//root.printNodes();
}}
Related
Can you please help me to put an error tarpping in my code. I have been doing it since yesterday but I think my braincell is already exhausted. Please send help po. And also can we print a graph in blueJ? how to print the graph? kinds of error trapping (i, i) = (0,0) same source destination, not allowed (i, j) = (i, j) existing edge, not allowed (i, j) = (j, i) existing edge, not allowed n (level of vertex) > n = not allowed
*
import java.util.Scanner;
import java.util.ArrayList;
import java.util.List;
import java.util.Stack;
public class AdjMatrixGraph{
List<List<Integer>> graph;
Scanner input = new Scanner(System.in);
ArrayList<Connection> Links = new ArrayList<Connection>();
boolean visited[];
int nodes;
int vertices;
int matrix[][];
class Node {//stores node information
String name;
boolean visited;
ArrayList<Node> neighbors = new ArrayList<Node>();
Node(String name) {
this.name = name;
visited = false;
}
}
class Connection {//stores edges information
double fare;
Node source, destination;
Connection(Node source, Node destination) {
this.source = source;
this.destination = destination;
}
}
public static void main(String[] args){
Scanner s = new Scanner(System.in);
System.out.println("Enter the number of vertices");
int V = s.nextInt();
AdjMatrixGraph amg = new AdjMatrixGraph(V);
System.out.println("Enter the number of edges");
int E = s.nextInt();
System.out.print("\n");
for(int i=0;i<E;i++){
System.out.println("Enter the endpoints");
int mark = s.nextInt();
int mac = s.nextInt();
if (mark == mac) { //checks if newly input edge is a loop
System.out.println("\nERROR TRAPPED!!\nNo Loops Allowed Here!\nAdd New Edge!");
i--;
continue;
}
amg.addEdge(mark,mac);
amg.addEdge1(mark,mac);
}
amg.printGraph();
System.out.print("\nSize of the graph: " + E);
System.out.print("\nOrder of the graph: " + V);
System.out.println(amg.ifGraphConnected());
}
AdjMatrixGraph(int vertices){
graph = new ArrayList<>();
visited = new boolean[vertices];
this.vertices=vertices;
matrix=new int[vertices][vertices];
for (int i = 0; i < vertices; i++){
graph.add(i, new ArrayList<>());
}
}
public void addEdge(int source,int destination){
matrix[source][destination]= 1;
matrix[destination][source]= 1;
}
public void addEdge1(int a, int b) {
graph.get(a).add(b);
graph.get(b).add(a);
}
public boolean ifGraphConnected() {
int startIndex = 0;
dfs(startIndex);
for(int i = 0; i < visited.length; i++) {
if(!visited[i]) {
System.out.println("\n");
System.out.println("The Graph is not connected!: ");
System.out.print("\nCreate another graph? \n1=Yes or 2=No\n");
String menu = input.nextLine();
if(menu.equals("1")){//goes back to the first menu and resets the list
input.close();
main(null);
}else if(menu.equals("2")){//terminates the program
System.exit(0);
}else{
System.out.println("\nERROR!");
}
return false;
}
}
System.out.println("\n");
System.out.println("The Graph is connected!");
System.out.print("\nCreate another graph? \n1=Yes or 2=No\n");
String menu = input.nextLine();
if(menu.equals("1")){//goes back to the first menu and resets the list
input.close();
main(null);
}else if(menu.equals("2")){//terminates the program
System.exit(0);
}else{
System.out.println("\nERROR!");
}
return true;
}//print true if it's connected
public void dfs(int start){ //the DFS traversal method for determining whether it is connected or not.
Stack<Integer> stack = new Stack<>(); //implement a stack that stores integers and traverse the graph
stack.push(start);
visited[start] = true; //places the vertex's start or address and sets it to true
while(!stack.isEmpty()) { //While the stack is not empty, the arguments in this block will be executed.
Integer vertices = stack.pop(); //the element is inserted into the stack
List<Integer> neighboursList = graph.get(vertices); //This will create a linked list of the graph's vertices
for(Integer neighbour: neighboursList) { //This will scan over all of the vertices that are related to the initial index.
if(!visited[neighbour]) {
stack.push(neighbour); //All the vertices that is related to vertex will be pushed.
visited[neighbour] = true; //Makes the vertex that is related to it true. It will then check to see if the stack is still full. If the loop is not empty, it will pop the visited vertex and proceed.
}
}
}
}
void printGraph(){ //printing Graph
System.out.print("\n");
System.out.println("The Adjacency Matrix is:");
for(int i=0;i<vertices;i++){ //using for loop method
for(int j = 0;j<vertices;j++){
System.out.print(matrix[i][j]+" ");
}
System.out.println();
}
}
}```
I have to create an array that will hold a user input that is 3 letter code followed by a ticket number. ex) Ama-34. How do I do this?
I know long is not correct, I just am modeling it off of another project.
I also have to allow for user input and manipulation which I am having a hard time.
This is what I have so far...
class QueueOrder{
//Global Variables
static Scanner orderScan = new Scanner(System.in);
//Variables
public int MaxSize;
//How to make an array hold both names and numbers??
public long[] BodaciousArray;
public int Front; //Track the front pointer
public int Rear; //track the last pointer
public int NumberOfOrders; //track the number of orders in the system
//Constructor
public QueueOrder(int size){
MaxSize = size;
BodaciousArray = new long[MaxSize];
Front = 0;
Rear = -1;
NumberOfOrders = 0;
}
//Enqueue - add to the rear of the queue
//Allow the server to add one to the array
public void Enqueue(){
long j = 0;
//Add a wrap around
if(Rear == MaxSize - 1){
Rear = -1;
}
//Increment the rear and insert a new item
BodaciousArray[++Rear] = j;
NumberOfOrders++;
}
//Dequeue - remove one from the array
//Allow the server to remove what is next in line
public long Dequeue(){
//Get the first value and incrament the front
long temp = BodaciousArray[Front++];
//Add a wrap around
if(Front == MaxSize){
Front = 0;
}
//Remove one item
NumberOfOrders--;
return temp;
}
//Peek at the front of the queue
//Allow the server to see what order is next
public long peekFront(){
return BodaciousArray[Front];
}
//Check to is the queue is empty
public boolean isEmpty(){
return(NumberOfOrders == 0);
}
//Check to see if the queue is full
public boolean isFull(){
return(NumberOfOrders == MaxSize);
}
//Check how many items are in the queue
public int size(){
return NumberOfOrders;
}
public void DisplayQueueOrder(){
int i;
if(Front == Rear){
System.out.println("There are no orders to fill");
}else{
for(i = Front; i < Rear; i++){
System.out.print("The current orders are: "
+ BodaciousArray[i] + ", ");
}
}
}
If you want to keep the two seperate, and the 3 letter code is unique HashMap is the way to go:
HashMap<String, Integer> queueArray = new HashMap<String, Integer>();
queueArray.put("Ama", 34);
System.out.println(arr.get("Ama"));
outputs:
34
Otherwise, why not just do something like this:
String[] tickets = {"Ama-34", "Abc-60", "Xyz-76"};
public String getTicketCode(int index) {
return tickets[index].split("-")[0];
}
public int getTicketNumber(int index) {
return Integer.parseInt(tickets[index].split("-")[1]);
}
used like:
System.out.println(getTicketCode(0));
System.out.println(getTicketNumber(0));
prints:
Ama
76
I have created the following method so as to create unique random numbers . (This unique values belong to the nodes of a tree):
static Random rand = new Random();
public static ArrayList<Node> go(int n) {
ArrayList<Node> list = new ArrayList<Node>();
ArrayList<Integer> numList = new ArrayList<Integer>();
// TODO Auto-generated method stub
for(int i = 1; i<=5; i++)
{
int number = rand.nextInt(10)+1;
if(list.size()>0 && !check(list,number))
{
i--;
continue;
}
numList.add(number);
Node node = new Node();
node.data = number;
list.add(node);
}
int w = 0;
for (Node d : list) {
System.out.println(w+": "+d.data);
w++;
}
return list;
}
private static boolean check(ArrayList<Node> list, int num) {
// TODO Auto-generated method stub
boolean b = false;
/*if(list.size()==0)
return true;
*/
for (Node node : list) {
if(node.data == num)
b = false;
else
b = true;
}
return b;
}
But it doesn’t create unique numbers and there are still duplicates in my list. Like :
0: 10
1: 1
2: 10
3: 5
4: 6
The problem is that you don't stop the for loop inside the check function if it finds a duplicated number. The loop continues and b can change back to true.
What you should do is for example:
private static boolean check(ArrayList<Node> list, int num) {
for (Node node : list) {
if(node.data == num)
return false;
}
return true;
}
Jón Trausti Arason has your answer, but...
Since you have a finite number of allowed values (integers), and since you don't want the same one picked more than once, perhaps it would be easier to just shuffle an array of the allowed values. Then you could just pick off the next value from the array and not worry about checking every time whether it's a repeat.
In your example selecting five values between one and ten, you could start with an array {1,2,3,4,5,6,7,8,9,10} and run it through a shuffle to rearrange it to something else like {3,4,7,1,10,9,5,8,2,6}. Take the first five values out of that resulting array with no worries about repeats.
In your check method, this looks a bit dodgy:
if (node.data == num)
b = false;
else
b = true
Surely once you've found a match (e.g. b = false) you want to return? Otherwise the next time around the loop b might be set to true. To simplify a bit, if you want to check whether an item is in a collection you can do list.contains(element)
You "forget" to use the numList that you've prepared.
This code should work fine:
static Random rand = new Random();
public static ArrayList<Node> go(int n) {
ArrayList<Node> list = new ArrayList<Node>();
ArrayList<Integer> numList = new ArrayList<Integer>();
for (int i = 1; i <= 5; i++) {
int number = rand.nextInt(10) + 1;
if (numList.contains(number)) {
i--;
continue;
}
numList.add(number);
Node node = new Node();
node.data = number;
list.add(node);
}
int w = 0;
for (Node d : list) {
System.out.println(w + ": " + d.data);
w++;
}
return list;
}
To illustrate on #eaj's point.
public static List<Node> go(int n) {
List<Integer> numbers = new ArrayList<Integer>();
for (int i = 1; i <= 10; i++) numbers.add(i);
Collections.shuffle(numbers);
List<Node> nodes = new ArrayList<Node>();
for (Integer data : numbers.subList(0, 5))
nodes.add(new Node(data)); // use a constructor for Node.
for (int w = 0; w < nodes.size(); w++)
System.out.println(w + ": " + nodes.get(w).data);
return nodes;
}
Your check function is wrong. Currently, it simply returns whether the last element matches num. You want to declare true (e.g. with return true;) once you find a match.
In fact, you can do everything without b. And I'm sure you can use list's contain method instead.
You should change your check method to something like:
private static boolean check(ArrayList<Node> list, int num)
{
for (Node node : list)
if (node.data == num)
return false;
return true;
}
In this way you go over the list and return false as soon as you find an equal element. If you are able to finish the loop without returning then no duplicates are found and you can return true.
This is my solution:
import java.util.ArrayList;
import java.util.Collections;
public class comboGenerator {
public static void main(String[] args) {
ArrayList<Integer> $combo = new ArrayList<Integer>(); // init. array list combo for randomization
while ($combo.size() < 6) {
int rand = (int) (Math.random()*49+1); // make new random number 1-49
if (!$combo.contains(rand)){ // check if we have that number in array list,{
$combo.add(rand); // if there is no such number then add it to array list
Collections.sort($combo); // sort the array list small >> large
}
}
System.out.println("Random combination " + $combo);
}
}
And you CAN'T get same numbers!
I'm back with another similar question. I am currently working on a Java program that will check if a graph is 2-colorable, i.e. if it contains no odd cycles (cycles of odd number length). The entire algorithm is supposed to run in O(V+E) time (V being all vertices and E being all edges in the graph). My current algorithm does a Depth First Search, recording all vertices in the path it takes, then looks for a back edge, and then records between which vertices the edge is between. Next it traces a path from one end of the back edge until it hits the other vertex on the other end of the edge, thus retracing the cycle that the back edge completes.
I was under the impression that this kind of traversing could be done in O(V+E) time for all cycles that exist in my graph, but I must be missing something, because my algorithm is running for a ridiculously long time for very large graphs (10k nodes, no idea how many edges).
Is my algorithm completely wrong? And if so, can anyone point me in the right direction for a better way to record these cycles or possibly tell if they have odd numbers of vertices? Thanks for any and all help you guys can give. Code is below if you need it.
Addition: Sorry I forgot, if the graph is not 2-colorable, I need to provide an odd cycle that proves that it is not.
package algorithms311;
import java.util.*;
import java.io.*;
public class CS311 {
public static LinkedList[] DFSIter(Vertex[] v) {
LinkedList[] VOandBE = new LinkedList[2];
VOandBE[0] = new LinkedList();
VOandBE[1] = new LinkedList();
Stack stack = new Stack();
stack.push(v[0]);
v[0].setColor("gray");
while(!stack.empty()) {
Vertex u = (Vertex) stack.peek();
LinkedList adjList = u.getAdjList();
VOandBE[0].add(u.getId());
boolean allVisited = true;
for(int i = 0; i < adjList.size(); i++) {
if(v[(Integer)adjList.get(i)].getColor().equals("white")) {
allVisited = false;
break;
}
else if(v[(Integer)adjList.get(i)].getColor().equals("gray") && u.getPrev() != (Integer)adjList.get(i)) {
int[] edge = new int[2]; //pair of vertices
edge[0] = u.getId(); //from u
edge[1] = (Integer)adjList.get(i); //to v
VOandBE[1].add(edge);
}
}
if(allVisited) {
u.setColor("black");
stack.pop();
}
else {
for(int i = 0; i < adjList.size(); i++) {
if(v[(Integer)adjList.get(i)].getColor().equals("white")) {
stack.push(v[(Integer)adjList.get(i)]);
v[(Integer)adjList.get(i)].setColor("gray");
v[(Integer)adjList.get(i)].setPrev(u.getId());
break;
}
}
}
}
return VOandBE;
}
public static void checkForTwoColor(String g) { //input is a graph formatted as assigned
String graph = g;
try {
// --Read First Line of Input File
// --Find Number of Vertices
FileReader file1 = new FileReader("W:\\Documents\\NetBeansProjects\\algorithms311\\src\\algorithms311\\" + graph);
BufferedReader bReaderNumEdges = new BufferedReader(file1);
String numVertS = bReaderNumEdges.readLine();
int numVert = Integer.parseInt(numVertS);
System.out.println(numVert + " vertices");
// --Make Vertices
Vertex vertex[] = new Vertex[numVert];
for(int k = 0; k <= numVert - 1; k++) {
vertex[k] = new Vertex(k);
}
// --Adj Lists
FileReader file2 = new FileReader("W:\\Documents\\NetBeansProjects\\algorithms311\\src\\algorithms311\\" + graph);
BufferedReader bReaderEdges = new BufferedReader(file2);
bReaderEdges.readLine(); //skip first line, that's how many vertices there are
String edge;
while((edge = bReaderEdges.readLine()) != null) {
StringTokenizer ST = new StringTokenizer(edge);
int vArr[] = new int[2];
for(int j = 0; ST.hasMoreTokens(); j++) {
vArr[j] = Integer.parseInt(ST.nextToken());
}
vertex[vArr[0]-1].addAdj(vArr[1]-1);
vertex[vArr[1]-1].addAdj(vArr[0]-1);
}
LinkedList[] l = new LinkedList[2];
l = DFSIter(vertex);//DFS(vertex);
System.out.println(l[0]);
for(int i = 0; i < l[1].size(); i++) {
int[] j = (int[])l[1].get(i);
System.out.print(" [" + j[0] + ", " + j[1] + "] ");
}
LinkedList oddCycle = new LinkedList();
boolean is2Colorable = true;
//System.out.println("iterate through list of back edges");
for(int i = 0; i < l[1].size(); i++) { //iterate through the list of back edges
//System.out.println(i);
int[] q = (int[])(l[1].get(i)); // q = pair of vertices that make up a back edge
int u = q[0]; // edge (u,v)
int v = q[1];
LinkedList cycle = new LinkedList();
if(l[0].indexOf(u) < l[0].indexOf(v)) { //check if u is before v
for(int z = l[0].indexOf(u); z <= l[0].indexOf(v); z++) { //if it is, look for u first; from u to v
cycle.add(l[0].get(z));
}
}
else if(l[0].indexOf(v) < l[0].indexOf(u)) {
for(int z = l[0].indexOf(v); z <= l[0].indexOf(u); z++) { //if it is, look for u first; from u to v
cycle.add(l[0].get(z));
}
}
if((cycle.size() & 1) != 0) { //if it has an odd cycle, print out the cyclic nodes or write them to a file
is2Colorable = false;
oddCycle = cycle;
break;
}
}
if(!is2Colorable) {
System.out.println("Graph is not 2-colorable, odd cycle exists");
if(oddCycle.size() <= 50) {
System.out.println(oddCycle);
}
else {
try {
BufferedWriter outFile = new BufferedWriter(new FileWriter("W:\\Documents\\NetBeansProjects\\algorithms311\\src\\algorithms311\\" + graph + "OddCycle.txt"));
String cyc = oddCycle.toString();
outFile.write(cyc);
outFile.close();
}
catch (IOException e) {
System.out.println("Could not write file");
}
}
}
}
catch (IOException e) {
System.out.println("Could not open file");
}
System.out.println("Done!");
}
public static void main(String[] args) {
//checkForTwoColor("smallgraph1");
//checkForTwoColor("smallgraph2");
//checkForTwoColor("smallgraph3");
//checkForTwoColor("smallgraph4");
checkForTwoColor("smallgraph5");
//checkForTwoColor("largegraph1");
}
}
Vertex class
package algorithms311;
import java.util.*;
public class Vertex implements Comparable {
public int id;
public LinkedList adjVert = new LinkedList();
public String color = "white";
public int dTime;
public int fTime;
public int prev;
public boolean visited = false;
public Vertex(int idnum) {
id = idnum;
}
public int getId() {
return id;
}
public int compareTo(Object obj) {
Vertex vert = (Vertex) obj;
return id-vert.getId();
}
#Override public String toString(){
return "Vertex # " + id;
}
public void setColor(String newColor) {
color = newColor;
}
public String getColor() {
return color;
}
public void setDTime(int d) {
dTime = d;
}
public void setFTime(int f) {
fTime = f;
}
public int getDTime() {
return dTime;
}
public int getFTime() {
return fTime;
}
public void setPrev(int v) {
prev = v;
}
public int getPrev() {
return prev;
}
public LinkedList getAdjList() {
return adjVert;
}
public void addAdj(int a) { //adds a vertex id to this vertex's adj list
adjVert.add(a);
}
public void visited() {
visited = true;
}
public boolean wasVisited() {
return visited;
}
}
I was under the impression that this kind of traversing could be done in O(V+E) time for all cycles that exist in my graph
There may be much more cycles than O(V+E) in a graph. If you iterate all of them, you will run long.
Back to your original idea, you could just try to implement a straightforward algorithm to color graph in two colors (mark an arbitrary node as black, all neighbors in white, all their neighbors in black, etc; that would be a breadth-first search). That is indeed done in O(V+E) time. If you succeed, then graph is 2-colorable. If you fail, it's not.
Edit: If you need a cycle that proves graph is not 2-colorable, just record for each node the vertex you traversed into it from. When you happen to traverse from black vertex A to black vertex B (thus needing to color black B into white and proving your graph is not 2-colorable), you get the cycle by looking back to parents:
X -> Y -> Z -> U -> V -> P -> Q -> A
\-> D -> E -> B
Then, A-B-E-D-V-P-Q (the paths up to their common ancestor) is the cycle you needed.
Note that in this version you don't have to check all cycles, you just output a first cycle, where back-edge in the tree has both vertexes colored in the same color.
you are describing a bipartite graph. a bipartite graph is 2 colorable and it contains no odd length cycles. You can use BFS to prove that a graph is bipartite or not. Hope this helps.
I'm writing a Java program that searches for and outputs cycles in a graph. I am using an adjacency list for storing my graph, with the lists stored as LinkedLists. My program takes an input formatted with the first line as the number of nodes in the graph and each subsequent line 2 nodes that form an edge e.g.:
3
1 2
2 3
3 1
My problem is that when the inputs get very large (the large graph I am using has 10k nodes and I don't know how many edges, the file is 23mb of just edges) I am getting a java.lang.StackOverflowError, but I don't get any errors with small inputs. I'm wondering if it would be better to use another data structure to form my adjacency lists or if there is some method I could use to avoid this error, as I'd rather not just have to change a setting on my local installation of Java (because I have to be sure this will run on other computers that I can't control the settings on as much). Below is my code, the Vertex class and then my main class. Thanks for any help you can give!
Vertex.java:
package algorithms311;
import java.util.*;
public class Vertex implements Comparable {
public int id;
public LinkedList adjVert = new LinkedList();
public String color = "white";
public int dTime;
public int fTime;
public int prev;
public Vertex(int idnum) {
id = idnum;
}
public int getId() {
return id;
}
public int compareTo(Object obj) {
Vertex vert = (Vertex) obj;
return id-vert.getId();
}
#Override public String toString(){
return "Vertex # " + id;
}
public void setColor(String newColor) {
color = newColor;
}
public String getColor() {
return color;
}
public void setDTime(int d) {
dTime = d;
}
public void setFTime(int f) {
fTime = f;
}
public int getDTime() {
return dTime;
}
public int getFTime() {
return fTime;
}
public void setPrev(int v) {
prev = v;
}
public int getPrev() {
return prev;
}
public LinkedList getAdjList() {
return adjVert;
}
public void addAdj(int a) { //adds a vertex id to this vertex's adj list
adjVert.add(a);
}
}
CS311.java:
package algorithms311;
import java.util.*;
import java.io.*;
public class CS311 {
public static final String GRAPH= "largegraph1";
public static int time = 0;
public static LinkedList[] DFS(Vertex[] v) {
LinkedList[] l = new LinkedList[2];
l[0] = new LinkedList();
l[1] = new LinkedList(); //initialize the array with blank lists, otherwise we get a nullpointerexception
for(int i = 0; i < v.length; i++) {
v[i].setColor("white");
v[i].setPrev(-1);
}
time = 0;
for(int i = 0; i < v.length; i++) {
if(v[i].getColor().equals("white")) {
l = DFSVisit(v, i, l);
}
}
return l;
}
public static LinkedList[] DFSVisit(Vertex[] v, int i, LinkedList[] l) { //params are a vertex of nodes and the node id you want to DFS from
LinkedList[] VOandBE = new LinkedList[2]; //two lists: visit orders and back edges
VOandBE[0] = l[0]; // l[0] is visit Order, a linked list of ints
VOandBE[1] = l[1]; // l[1] is back Edges, a linked list of arrays[2] of ints
VOandBE[0].add(v[i].getId());
v[i].setColor("gray"); //color[vertex i] <- GRAY
time++; //time <- time+1
v[i].setDTime(time); //d[vertex i] <- time
LinkedList adjList = v[i].getAdjList(); // adjList for the current vertex
for(int j = 0; j < adjList.size(); j++) { //for each v in adj[vertex i]
if(v[(Integer)adjList.get(j)].getColor().equals("gray") && v[i].getPrev() != v[(Integer)adjList.get(j)].getId()) { // if color[v] = gray and Predecessor[u] != v do
int[] edge = new int[2]; //pair of vertices
edge[0] = i; //from u
edge[1] = (Integer)adjList.get(j); //to v
VOandBE[1].add(edge);
}
if(v[(Integer)adjList.get(j)].getColor().equals("white")) { //do if color[v] = WHITE
v[(Integer)adjList.get(j)].setPrev(i); //then "pi"[v] <- vertex i
DFSVisit(v, (Integer)adjList.get(j), VOandBE); //DFS-Visit(v)
}
}
VOandBE[0].add(v[i].getId());
v[i].setColor("black");
time++;
v[i].setFTime(time);
return VOandBE;
}
public static void main(String[] args) {
try {
// --Read First Line of Input File
// --Find Number of Vertices
FileReader file1 = new FileReader("W:\\Documents\\NetBeansProjects\\algorithms311\\src\\algorithms311\\" + GRAPH);
BufferedReader bReaderNumEdges = new BufferedReader(file1);
String numVertS = bReaderNumEdges.readLine();
int numVert = Integer.parseInt(numVertS);
System.out.println(numVert + " vertices");
// --Make Vertices
Vertex vertex[] = new Vertex[numVert];
for(int k = 0; k <= numVert - 1; k++) {
vertex[k] = new Vertex(k);
}
// --Adj Lists
FileReader file2 = new FileReader("W:\\Documents\\NetBeansProjects\\algorithms311\\src\\algorithms311\\" + GRAPH);
BufferedReader bReaderEdges = new BufferedReader(file2);
bReaderEdges.readLine(); //skip first line, that's how many vertices there are
String edge;
while((edge = bReaderEdges.readLine()) != null) {
StringTokenizer ST = new StringTokenizer(edge);
int vArr[] = new int[2];
for(int j = 0; ST.hasMoreTokens(); j++) {
vArr[j] = Integer.parseInt(ST.nextToken());
}
vertex[vArr[0]-1].addAdj(vArr[1]-1);
vertex[vArr[1]-1].addAdj(vArr[0]-1);
}
for(int i = 0; i < vertex.length; i++) {
System.out.println(vertex[i] + ", adj nodes: " + vertex[i].getAdjList());
}
LinkedList[] l = new LinkedList[2];
l = DFS(vertex);
System.out.println("");
System.out.println("Visited Nodes: " + l[0]);
System.out.println("");
System.out.print("Back Edges: ");
for(int i = 0; i < l[1].size(); i++) {
int[] q = (int[])(l[1].get(i));
System.out.println("[" + q[0] + "," + q[1] + "] ");
}
for(int i = 0; i < l[1].size(); i++) { //iterate through the list of back edges
int[] q = (int[])(l[1].get(i)); // q = pair of vertices that make up a back edge
int u = q[0]; // edge (u,v)
int v = q[1];
LinkedList cycle = new LinkedList();
if(l[0].indexOf(u) < l[0].indexOf(v)) { //check if u is before v
for(int z = l[0].indexOf(u); z <= l[0].indexOf(v); z++) { //if it is, look for u first; from u to v
cycle.add(l[0].get(z));
}
}
else if(l[0].indexOf(v) < l[0].indexOf(u)) {
for(int z = l[0].indexOf(v); z <= l[0].indexOf(u); z++) { //if it is, look for u first; from u to v
cycle.add(l[0].get(z));
}
}
System.out.println("");
System.out.println("Cycle detected! : " + cycle);
if((cycle.size() & 1) != 0) {
System.out.println("Cycle is odd, graph is not 2-colorable!");
}
else {
System.out.println("Cycle is even, we're okay!");
}
}
}
catch (IOException e) {
System.out.println("AHHHH");
e.printStackTrace();
}
}
}
The issue is most likely the recursive calls in DFSVisit. If you don't want to go with the 'easy' answer of increasing Java's stack size when you call the JVM, you may want to consider rewriting DFSVisit to use an iterative algorithm instead of recursive. While Depth First Search is more easily defined in a recursive manner, there are iterative approaches to the algorithm that can be used.
For example: this blog post
The stack is a region in memory that is used for storing execution context and passing parameters. Every time your code invokes a method, a little bit of stack is used, and the stack pointer is increased to point to the next available location. When the method returns, the stack pointer is decreased and the portion of the stack is freed up.
If an application uses recursion heavily, the stack quickly becomes a bottleneck, because if there is no limit to the recursion depth, there is no limit to the amount of stack needed. So you have two options: increase the Java stack (-Xss JVM parameter, and this will only help until you hit the new limit) or change your algorithm so that the recursion depth is not as deep.
I am not sure if you were looking for a generic answer, but from a brief glance at your code it appears that your problem is recursion.
If you're sure your algorithm is correct and the depth of recursive calls you're making isn't accidental, then solutions without changing your algorithm are:
add to the JVM command line e.g. -Xss128m to set a 128 MB stack size (not a good solution in multi-threaded programs as it sets the default stack size for every thread not just the particular thread running your task);
run your task in its own thread, which you can initialise with a stack size specific to just that thread (and set the stack size within the program itself)-- see my example in the discussion of fixing StackOverflowError, but essentially the stack size is a parameter to the Thread() constructor;
don't use recursive calls at all-- instead, mimic the recursive calls using an explicit Stack or Queue object (this arguably gives you a bit more control).