Question

cs 2130 - to be written in Java. Below the question part A I will provide the code that is needed to complete the questions. Thank you so much for your help !

CS 2130 PROJECT #4: Relations, Boolean Matrices, Digraphs, and Trees In this project, you will use the Boolean matrix representation of directed graphs to perform various operations on the digraphs and to examine special properties of the digraphs. A Boolean matrix class file BMat.java will be given to you. This class contains various functions (methods) that operate on Boolean matrices. You will write additional functions and put them into this class file. Part A Add the following functions to the BMat class: 1. BM1.eutdegree (K) - Returns the out-degree for node K in Boolean matrix BM1. Assume hat the nodes are numbered 0,1,2, ....SI2E-1. 2. BM1.meet (M2) -Returns the logical AND of matrices BM1 and BM2. 3. BM1.transpose () - Returns the transpose of matrix BM1. 4. BM1.product (M2) - Returns the Boolean product of matrices BM1 and BM2. ??? elegge ( )-Retums the transitive closure of matrix BMI (use Warshalls algorithm). 6. BM1.eetned) -Returns the root node number (o to SIZE-1) of BMl if a candidate root node exists. Retums -1 if there is no root node. For a root node to exist, there must be exactly one node with in-degree 0. All other nodes must have in-degree 1.

// Boolean Matrix Class

public class BMat

{

// Instance variables

public int M[][];

public int SIZE;

// Boolean matrix constructors

public BMat(int s)

{

SIZE = s;

M = new int[SIZE][SIZE];

// Fill M with zeros

for(int r = 0; r < SIZE; r++){

for(int c = 0; c < SIZE; c++){

M[r][c] = 0;

}

}

}

public BMat(int[][] B)

{

SIZE = B.length;

M = new int[SIZE][SIZE];

// Copy matrix B values into M

for(int r = 0; r < SIZE; r++){

for(int c = 0; c < SIZE; c++){

if(B[r][c] == 0)

M[r][c] = 0;

else

M[r][c] = 1;

}

}

}

// Boolean matrix methods

public void show()

{

// Display matrix

for(int r = 0; r < SIZE; r++){

for(int c = 0; c < SIZE; c++){

System.out.print(" " + M[r][c]);

}

System.out.println();

}

return;

}

public boolean isEqual(BMat M2)

{

// Check if current matrix equals matrix M2

for(int r = 0; r < SIZE; r++){

for(int c = 0; c < SIZE; c++){

if(this.M[r][c] != M2.M[r][c])

return false;

}

}

return true;

}

public int trace()

{

// Sum of main diagonal values

int diag = 0;

for(int r = 0; r < SIZE; r++)

diag = diag + M[r][r];

return diag;

}

public int arrows()

{

// No. of 1's in matrix

int narrows = 0;

for(int r = 0; r < SIZE; r++){

for(int c = 0; c < SIZE; c++){

narrows = narrows + M[r][c];

}

}

return narrows;

}

public int indegree(int K)

{

// Number of arrows INTO node K of digraph

// Nodes are numbered 0,1,2,...,SIZE-1

int colsum = 0;

for(int r = 0; r < SIZE; r++)

colsum = colsum + M[r][K];

return colsum;

}

public BMat complement()

{

// Logical NOT of current matrix

BMat W1 = new BMat(SIZE);

for(int r = 0; r < SIZE; r++){

for(int c = 0; c < SIZE; c++){

if(this.M[r][c] == 0)

W1.M[r][c] = 1;

else

W1.M[r][c] = 0;

}

}

return W1;

}

public BMat join(BMat M2)

{

// Logical OR of current matrix with matrix M2

BMat W1 = new BMat(SIZE);

for(int r = 0; r < SIZE; r++){

for(int c = 0; c < SIZE; c++){

W1.M[r][c] = this.M[r][c] | M2.M[r][c];

}

}

return W1;

}

public BMat power(int N)

{

// Raise current matrix to Boolean Nth power (N >= 1)

BMat W1 = new BMat(this.M);

BMat W2 = new BMat(this.M);

for(int k = 2; k <= N; k++){

W1 = W1.product(W2);

}

return W1;

}

public BMat rclosure()

{

// Reflexive closure of current matrix

BMat W1 = new BMat(this.M);

// Put 1's on main diagonal

for(int r = 0; r < SIZE; r++)

W1.M[r][r] = 1;

return W1;

}

public BMat sclosure()

{

// Symmetric closure of current matrix

BMat W1 = new BMat(this.M);

BMat W2 = W1.transpose();

W1 = W1.join(W2);

return W1;

}

// *********************************

// Project #4 functions to add

// *********************************

public int outdegree(int K)

{

// Number of arrows FROM node K of digraph

// Nodes are numbered 0,1,2,...,SIZE-1

// Put code here...

}

public BMat meet(BMat M2)

{

// Logical AND of current matrix with matrix M2

// Put code here...

}

public BMat transpose()

{

// Transpose of current matrix

// Put code here...

}

public BMat product(BMat M2)

{

// Boolean product of current matrix with matrix M2

// Put code here...

}

public BMat tclosure()

{

// Transitive closure of current matrix (Warshall's algorithm)

// Put code here...

}

public int rootnode()

{

// Root node number (if any) of current matrix

// Nodes are numbered 0,1,2,...,SIZE-1

// Put code here...

}

} // end class

0 0
Add a comment Improve this question Transcribed image text
Answer #1

The added lines are highlighted...

-----------------------------------------------------------------------

Copyable code:

public class BMat {

// Instance variables

    public int M[][];

    public int SIZE;

// Boolean matrix constructors

    public BMat(int s) {

        SIZE = s;

        M = new int[SIZE][SIZE];

// Fill M with zeros

        for (int r = 0; r < SIZE; r++) {

            for (int c = 0; c < SIZE; c++) {

                M[r][c] = 0;

            }

        }

    }

    public BMat(int[][] B) {

        SIZE = B.length;

        M = new int[SIZE][SIZE];

// Copy matrix B values into M

        for (int r = 0; r < SIZE; r++) {

            for (int c = 0; c < SIZE; c++) {

                if (B[r][c] == 0) {

                    M[r][c] = 0;

                } else {

                    M[r][c] = 1;

                }

            }

        }

    }

// Boolean matrix methods

    public void show() {

// Display matrix

        for (int r = 0; r < SIZE; r++) {

            for (int c = 0; c < SIZE; c++) {

                System.out.print(" " + M[r][c]);

            }

            System.out.println();

        }

        return;

    }

    public boolean isEqual(BMat M2) {

// Check if current matrix equals matrix M2

        for (int r = 0; r < SIZE; r++) {

            for (int c = 0; c < SIZE; c++) {

                if (this.M[r][c] != M2.M[r][c]) {

                    return false;

                }

            }

        }

        return true;

    }

    public int trace() {

// Sum of main diagonal values

        int diag = 0;

        for (int r = 0; r < SIZE; r++) {

            diag = diag + M[r][r];

        }

        return diag;

    }

    public int arrows() {

// No. of 1's in matrix

        int narrows = 0;

        for (int r = 0; r < SIZE; r++) {

            for (int c = 0; c < SIZE; c++) {

                narrows = narrows + M[r][c];

            }

        }

        return narrows;

    }

    public int indegree(int K) {

// Number of arrows INTO node K of digraph

// Nodes are numbered 0,1,2,...,SIZE-1

        int colsum = 0;

        for (int r = 0; r < SIZE; r++) {

            colsum = colsum + M[r][K - 1];

        }

        return colsum;

    }

    public BMat complement() {

// Logical NOT of current matrix

        BMat W1 = new BMat(SIZE);

        for (int r = 0; r < SIZE; r++) {

            for (int c = 0; c < SIZE; c++) {

                if (this.M[r][c] == 0) {

                    W1.M[r][c] = 1;

                } else {

                    W1.M[r][c] = 0;

                }

            }

        }

        return W1;

    }

    public BMat join(BMat M2) {

// Logical OR of current matrix with matrix M2

        BMat W1 = new BMat(SIZE);

        for (int r = 0; r < SIZE; r++) {

            for (int c = 0; c < SIZE; c++) {

                W1.M[r][c] = this.M[r][c] | M2.M[r][c];

            }

        }

        return W1;

    }

    public BMat power(int N) {

// Raise current matrix to Boolean Nth power (N >= 1)

        BMat W1 = new BMat(this.M);

        BMat W2 = new BMat(this.M);

        for (int k = 2; k <= N; k++) {

            W1 = W1.product(W2);

        }

        return W1;

    }

    public BMat rclosure() {

// Reflexive closure of current matrix

        BMat W1 = new BMat(this.M);

// Put 1's on main diagonal

        for (int r = 0; r < SIZE; r++) {

            W1.M[r][r] = 1;

        }

        return W1;

    }

    public BMat sclosure() {

// Symmetric closure of current matrix

        BMat W1 = new BMat(this.M);

        BMat W2 = W1.transpose();

        W1 = W1.join(W2);

        return W1;

    }

// *********************************

// Project #4 functions to add

// *********************************

    public int outdegree(int K) {

// Number of arrows FROM node K of digraph

// Nodes are numbered 0,1,2,...,SIZE-1

        int sum = 0;

        for (int r = 0; r < SIZE; r++) {

            sum = sum + M[K - 1][r];

        }

        return sum;

    }

    public BMat meet(BMat M2) {

// Logical AND of current matrix with matrix M2

// Put code here...

        BMat W1 = new BMat(SIZE);

        for (int r = 0; r < SIZE; r++) {

            for (int c = 0; c < SIZE; c++) {

                W1.M[r][c] = this.M[r][c] & M2.M[r][c];

            }

        }

        return W1;

    }

    public BMat transpose() {

// Transpose of current matrix

// Put code here...

        for (int r = 0; r < SIZE; r++) {

            for (int c = 0; c < SIZE; c++) {

                if (r == c) {

                    continue;

                }

                this.M[r][c] = this.M[c][r];

            }

        }

        return this;

    }

    public BMat product(BMat M2) {

// Boolean product of current matrix with matrix M2

// Put code here...

        BMat W1 = new BMat(SIZE);

        for (int i = 0; i < SIZE; i++) {

            for (int j = 0; j < SIZE; j++) {

                for (int k = 0; k < SIZE; k++) {

                    W1.M[i][j] = W1.M[i][j] + this.M[i][k] * M2.M[k][j];

                }

            }

        }

        return W1;

    }

    public BMat tclosure() {

// Transitive closure of current matrix (Warshall's algorithm)

// Put code here...

        for (int i = 0; i < SIZE; i++) {

            for (int j = 0; j < SIZE; j++) {

                if (this.M[j][i] == 1) {

                    for (int k = 0; k < SIZE; k++) {

                        if (this.M[j][i] == 1 && this.M[i][k] == 1) {

                            this.M[j][k] = 1;

                        } else {

                            this.M[j][k] = 0;

                        }

                   }

                }

            }

        }

        return this;

    }

    public int rootnode() {

// Root node number (if any) of current matrix

// Nodes are numbered 0,1,2,...,SIZE-1

// Put code here...

        int noRootNode = 0;

        //Storing the root node

        int node = -1;

        //for loop to check all Node

        for (int i = 0; i < SIZE; i++)

        {

            int count = 0;          

            //for loop to check how many inedge is there for ith Node

            for (int j = 0; j < SIZE; j++)

            {

                if (this.M[j][i] == 0) {

                    count = count + 1;

                }

            }

            //Condition to check there is no InEdge

            if (count == SIZE)

            {

                noRootNode = noRootNode + 1;

                node = i;

            }

        }

        //Condition to check no of root node in graph is exactly one

        if (noRootNode == 1)

        {

            //return Node number

            return node + 1;

        }

        return -1;

    }

} // end class

Add a comment
Know the answer?
Add Answer to:
cs 2130 - to be written in Java. Below the question part A I will provide...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • please make a pretty JAVA GUI for this code this is RED BLACK TREE and i...

    please make a pretty JAVA GUI for this code this is RED BLACK TREE and i Finished code already jus need a JAVA GUI for this code ... if poosible make it pretty to look thanks and please make own GUI code base on my code thanks ex: (GUI only have to show RBTree) ---------------------------------------- RBTree.java import java.util.Stack; public class RBTree{    private Node current;    private Node parent;    private Node grandparent;    private Node header;    private Node...

  • Java - I need help creating a method that removes a node at the specific index...

    Java - I need help creating a method that removes a node at the specific index position. The * first node is index 0. public boolean delAt(int index) { src code 2 different classes ******************************************** public class Node { private String data; private Node next; public Node(String data, Node next) { this.data = data; this.next = next; } public Node() { } public String getData() { return data; } public void setData(String data) { this.data = data; } public void...

  • Java help! Please help complete the min method below in bold. import java.util.Arrays; import java.util.ArrayList; import...

    Java help! Please help complete the min method below in bold. import java.util.Arrays; import java.util.ArrayList; import java.util.Collections; import java.util.Iterator; import java.util.List; /** * Provides an implementation of a binary search tree * with no balance constraints, implemented with linked nodes. * * * */ public class Bst<T extends Comparable<T>> implements Iterable<T> { ////////////////////////////////////////////////////////////////// // I M P L E M E N T T H E M I N M E T H O D B E L O W...

  • JAVA QUESTION: *******THE QUESTION:******** /** numberOfNodesAtDepth    *    * Returns the number of nodes with...

    JAVA QUESTION: *******THE QUESTION:******** /** numberOfNodesAtDepth    *    * Returns the number of nodes with depth == d    * Precondition: none    *    * param: d the depth to search for    *    * hint: use a recursive helper function    *        * ToDo 4    */    public int numNodesAtDepth(int d) {        return -1;    } **********USEFUL CODE FROM THE ASSIGNMENT:*********** public class simpleBST<Key extends Comparable<Key>, Value> {    private Node root;            ...

  • Please help with the codes for the implementation of java.util.List, specifically for the 3 below overridden...

    Please help with the codes for the implementation of java.util.List, specifically for the 3 below overridden methods @Override        public int lastIndexOf(Object arg0) { required code }        @Override        public ListIterator<R> listIterator() { required code }        @Override        public ListIterator<R> listIterator(int arg0) { required code } PLEASE NOTE: Below are some of other overridden methods that are already implemented, they are meant to be examples of what the answers to the above questions for the 3 methods should...

  • Hi! Can someone can convert this java code to c++. ASAP thanks I will thumbs up Here's the code: ...

    Hi! Can someone can convert this java code to c++. ASAP thanks I will thumbs up Here's the code: package lists; import bookdata.*; public class DoublyLinkedList { int size; //Variable que define el tamano de la lista. Node head, tail; //Nodos que definen el Head y Tail en la lista. //Constructor public DoublyLinkedList(){ this.head = null; this.tail = null; this.size = 0; } //Insert a new book in alphabetic order. public void insert(Book nb){ //Creamos uno nuevo nodo con el...

  • PROMPT: Consider a graph G. A connected component is a maximal subset of nodes that induces a connected sub graph. It’s maximal in the sense that you cannot add a node with the resulting induced sub g...

    PROMPT: Consider a graph G. A connected component is a maximal subset of nodes that induces a connected sub graph. It’s maximal in the sense that you cannot add a node with the resulting induced sub graph remaining connected.The following function numComponents returns the number of connected components in an undirected graph. QUESTION: What is the time complexity for this function? The time complexity should be a function of the number of nodes |V| and the number of edges |E|....

  • package hw3; import java.util.LinkedList; /* *********************************************************************** * A simple BST with int keys and no values...

    package hw3; import java.util.LinkedList; /* *********************************************************************** * A simple BST with int keys and no values * * Complete each function below. * Write each function as a separate recursive definition (do not use more than one helper per function). * Depth of root==0. * Height of leaf==0. * Size of empty tree==0. * Height of empty tree=-1. * * TODO: complete the functions in this file. * DO NOT change the Node class. * DO NOT change the name...

  • Does not pass the testcase testDTreeAdvancedReRootchangesParent Java I'm trying to extend the implementation of a general...

    Does not pass the testcase testDTreeAdvancedReRootchangesParent Java I'm trying to extend the implementation of a general tree with new operations that change the structure of the tree. I've created 5 classes: Node.java, SimpleNode.java, Tree.java, RerootableTree.java and SimpleTree.java(which is the main java class that I need to change). The code does not pass ONE TESTCASE : testDTreeAdvancedReRootchangesParent The code passes all the other testcases except theone mentioned above. This is because in the SimpleTree.java the method "reRoot(Node newRoot)" is most likely...

  • Instructions Write a program in Java that implements the A* algorithm to find a path from...

    Instructions Write a program in Java that implements the A* algorithm to find a path from any two given nodes. Problem Overview & Algorithm Description In a fully-observable environment where there are both pathable and blocked nodes, an agent must find a good path from their starting node to the goal node. The agent must use the A* algorithm to determine its path. For this program, you must use the Manhattan method for calculating the heuristic. Remember: your heuristic function...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT