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;            ...

  • Currently working on a Java Assignment. I have written most codes for swap, reverse and insert....

    Currently working on a Java Assignment. I have written most codes for swap, reverse and insert. Just need a. itemCount receives a value and returns a count of the number of times this item is found in the list. c. sublist receives two indexes and returns an ArrayList of node values from the first index to the second index, provided the indexes are valid. d. select receives a variable number of indexes, and returns an ArrayList of node values corresponding...

  • Complete LinkedListSet.java in java programming language. package Homework3; public class LinkedListSet extends LinkedListCollection { LinkedListSet() {...

    Complete LinkedListSet.java in java programming language. package Homework3; public class LinkedListSet extends LinkedListCollection { LinkedListSet() { } public boolean add(T element) { // Code here return true; } } Below is the LinkedListCollection.java code. Use to complete LinkedListSet.java. public class LinkedListCollection <T> { protected Node<T> head = null; public LinkedListCollection() { } public boolean isEmpty() { return head == null; } public int size() { int counter = 0; Node<T> cursor = head; while (cursor != null) { cursor =...

  • 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|....

  • Represent the control-flow graph as a set of nodes and a map between nodes and their...

    Represent the control-flow graph as a set of nodes and a map between nodes and their neighbors. The following code snippet declares two classes—ControlFlowGraph and its static inner class Node—to enable the desired representation: a. Implement the addNode method and the two addEdge methods. b. Implement the reachable method. Note: "..." is where code is to be inserted below the post condition import java.util.*; public class ControlFlowGraph { // invariant : set " nodes " contains all nodes in the...

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