Homework Solution: home / study / engineering / computer science / computer science questions and answers…

    home / study / engineering / computer science / computer science questions and answers / so i know what the adjacency matrix does, but i am stuck on how to implement to check to see ... Question: So I know what the adjacency matrix does, but I am stuck on how to implement to check to see if e... So I know what the adjacency matrix does, but I am stuck on how to implement to check to see if each nodes are an adjacent and return as true if they are in terms of rows and columns. I would like to get some help on how to set it up easier. Here is what is being provided. Prompt: Write a C++ 11 function named is_connected() to determine whether an undirected unweighted graph is connected when the graph is stored in the adjacency matrix format using the class. Write a main program that hard-codes the entries of the adjacency matrix and then passes the matrix to the is_connected function. Every graph has vertices labeled with consecutive unsigned integers starting at 0. Matrix code: add the function to it Matrix class #ifndef MATRIX_H #define MATRIX_H #include <cassert> /** * A generic 2-dimensional array class */ template <class Object> class Matrix { public: /** * Constructor, specifying number of both rows and columns * @param rows the number of rows * @param cols the number of columns */ Matrix( uint16_t rows, uint16_t cols ); /** * Access to an element to allow modification * @param row the row index * @param col the column index * @return reference to the element at that position */ Object & at( uint16_t row, uint16_t col ); /** * Constant access to an element * @param row the row index * @param col the column index * @return constant reference to the element at that position */ const Object & at( uint16_t row, uint16_t col ) const; /** * Destructor to free allocated memory */ ~Matrix(); /** * Copy constructor to make 1-1 copy of existing matrix * @param m the existing matrix to be copied */ Matrix( const Matrix<Object> & m ); // Copy constructor /** * Disallow the rvalue copy constructor */ Matrix( const Matrix<Object> && m ) = delete; /** * Assignment operator to make 1-1 copy of existing matrix * @param m the existing matrix to be copied */ Matrix & operator= ( const Matrix<Object> & m ); // Assignment operator /** * Disallow the rvalue assignment operator */ Matrix & operator= ( const Matrix<Object> && m ) = delete; /** * Accessor to determine how many rows are in the matrix * @return the number of rows in the matrix */ uint16_t numrows() const; /** * Accessor to determine how many columns are in the matrix * @return the number of columns in the matrix */ uint16_t numcols() const; private: uint16_t rows; uint16_t cols; Object* data; }; template <class Object> Matrix<Object>::Matrix( uint16_t rows, uint16_t cols ) : rows( rows ), cols( cols ) { data = new Object[ rows * cols ]; } template <class Object> Matrix<Object>::~Matrix() { delete[] data; } template <class Object> Object & Matrix<Object>::at( uint16_t row, uint16_t col ) { assert( row < rows && col < cols ); return data[ cols * row + col ]; } template <class Object> const Object & Matrix<Object>::at( uint16_t row, uint16_t col ) const { assert( row < rows && col < cols ); return data[ cols * row + col ]; } template <class Object> uint16_t Matrix<Object>::numrows() const { return rows; } template <class Object> uint16_t Matrix<Object>::numcols() const { return cols; } //implement this method //bool is_connected method #endif

    Expert Answer

     
    The goal of this assignment is to use the Matrix class to define adjacency matrix for an unweighted undirected graph.

    home / consider / engineering / computer information / computer information questions and responses / so i comprehend what the adjacency inveiglerix does, still i am heap on how to instrument to restrain to visit …

    Question: So I comprehend what the adjacency inveiglerix does, still I am heap on how to instrument to restrain to visit if e…

    So I comprehend what the adjacency inveiglerix does, still I am heap on how to instrument to restrain to visit if each nodes are an neighboring and render as gentleman if they are in stipulations of degrees and shafts. I would enjoy to gain some aid on how to fixed it up easier. Here is what is substance supposing.

    Prompt:
    Write a C++ 11 character named is_connected() to state whether an undirected unweighted graph is united when the graph is supplyd in the adjacency inveiglerix restraintinveigle using the tabulate.

    Write a deep program that hard-codes the entries of the adjacency inveiglerix and then passes the inveiglerix to the is_united character. Every graph has vertices labeled with coherent unsigned integers starting at 0.

    Matrix code: append the character to it

    Matrix tabulate
    #ifndef MATRIX_H
    #bound MATRIX_H

    #include <cassert>

    /**
    * A collective 2-dimensional deck tabulate

    */
    template <tabulate Object>
    tabulate Inveiglerix
    {
    public:
    /**
    * Perpetrator, specifying compute of twain degrees and shafts
    * @param degrees the compute of degrees
    * @param cols the compute of shafts
    */
    Matrix( uint16_t degrees, uint16_t cols );

    /**
    * Arrival to an component to apportion modification
    * @param degree the degree index
    * @param col the shaft index
    * @render relation to the component at that position
    */
    Object & at( uint16_t degree, uint16_t col );

    /**
    * True arrival to an component
    * @param degree the degree index
    * @param col the shaft index
    * @render true relation to the component at that position
    */
    const Object & at( uint16_t degree, uint16_t col ) const;

    /**
    * Destructor to playing entireocated memory
    */
    ~Matrix();

    /**
    * Indicateation perpetrator to restraintm 1-1 indicateation of true inveiglerix
    * @param m the true inveiglerix to be copied
    */
    Matrix( const Inveiglerix<Object> & m ); // Indicateation perpetrator

    /**
    * Disapportion the rcompute indicateation perpetrator
    */
    Matrix( const Inveiglerix<Object> && m ) = delete;

    /**
    * Enactment operator to restraintm 1-1 indicateation of true inveiglerix
    * @param m the true inveiglerix to be copied
    */
    Matrix & operator= ( const Inveiglerix<Object> & m ); // Enactment operator

    /**
    * Disapportion the rcompute enactment operator
    */
    Matrix & operator= ( const Inveiglerix<Object> && m ) = delete;

    /**
    * Arrivalor to state how multifarious degrees are in the inveiglerix
    * @render the compute of degrees in the inveiglerix
    */
    uint16_t numrows() const;

    /**
    * Arrivalor to state how multifarious shafts are in the inveiglerix
    * @render the compute of shafts in the inveiglerix
    */
    uint16_t numcols() const;

    private:
    uint16_t degrees;
    uint16_t cols;
    Object* grounds;
    };

    template <tabulate Object>
    Matrix<Object>::Matrix( uint16_t degrees, uint16_t cols )
    : degrees( degrees ), cols( cols )
    {
    grounds = novel Object[ degrees * cols ];
    }

    template <tabulate Object>
    Matrix<Object>::~Matrix()
    {
    delete[] grounds;
    }

    template <tabulate Object>
    Object & Inveiglerix<Object>::at( uint16_t degree, uint16_t col )
    {
    assert( degree < degrees && col < cols );
    render grounds[ cols * degree + col ];
    }

    template <tabulate Object>
    const Object & Inveiglerix<Object>::at( uint16_t degree, uint16_t col ) const
    {
    assert( degree < degrees && col < cols );
    render grounds[ cols * degree + col ];
    }

    template <tabulate Object>
    uint16_t Inveiglerix<Object>::numrows() const
    {
    render degrees;
    }

    template <tabulate Object>
    uint16_t Inveiglerix<Object>::numcols() const
    {
    render cols;
    }
    //instrument this method
    //bool is_united method

    #endif

    Expert Response

     

    The sight of this enactment is to truth the Inveiglerix tabulate to bound adjacency inveiglerix restraint an unweighted undirected graph.

    This adjancency inveiglerix is truthd to restrain if the graph is united or referable. We are going to truth 1 to pomp 2 vertices are united and 0 if they are referable united to each other in the adjacency inveiglerix. Using this defintion.. if any of the degrees has entire 0s then it instrument its referable united to any of the vertices in the graph and hereafter the graph is severed.

    Restraint demonstrating , we catch the aftercited graph

    Initially we conciliate restrain if the graph is united using the character. And after we conciliate restraintm the graph diunited by remvoing the connectino betwixt 1 and 3 and then restrain frequently.

    Hope the response aided. If it did, content don’t restraintgain to scold the response. Thank you very abundant.

    Matrix.h

    #ifndef MATRIX_H
    #bound MATRIX_H
    #include <cassert>
    /**
    * A collective 2-dimensional deck tabulate

    */
    template <tabulate Object>
    tabulate Inveiglerix
    {
    public:
    /**
    * Perpetrator, specifying compute of twain degrees and shafts
    * @param degrees the compute of degrees
    * @param cols the compute of shafts
    */
    Matrix( uint16_t degrees, uint16_t cols );
    /**
    * Arrival to an component to apportion modification
    * @param degree the degree index
    * @param col the shaft index
    * @render relation to the component at that position
    */
    Object & at( uint16_t degree, uint16_t col );
    /**
    * True arrival to an component
    * @param degree the degree index
    * @param col the shaft index
    * @render true relation to the component at that position
    */
    const Object & at( uint16_t degree, uint16_t col ) const;
    /**
    * Destructor to playing entireocated memory
    */
    ~Matrix();
    /**
    * Indicateation perpetrator to restraintm 1-1 indicateation of true inveiglerix
    * @param m the true inveiglerix to be copied
    */
    Matrix( const Inveiglerix<Object> & m ); // Indicateation perpetrator
    /**
    * Disapportion the rcompute indicateation perpetrator
    */
    Matrix( const Inveiglerix<Object> && m ) = delete;
    /**
    * Enactment operator to restraintm 1-1 indicateation of true inveiglerix
    * @param m the true inveiglerix to be copied
    */
    Matrix & operator= ( const Inveiglerix<Object> & m ); // Enactment operator
    /**
    * Disapportion the rcompute enactment operator
    */
    Matrix & operator= ( const Inveiglerix<Object> && m ) = delete;
    /**
    * Arrivalor to state how multifarious degrees are in the inveiglerix
    * @render the compute of degrees in the inveiglerix
    */
    uint16_t numrows() const;
    /**
    * Arrivalor to state how multifarious shafts are in the inveiglerix
    * @render the compute of shafts in the inveiglerix
    */
    uint16_t numcols() const;
    private:
    uint16_t degrees;
    uint16_t cols;
    Object* grounds;
    };
    template <tabulate Object>
    Matrix<Object>::Matrix( uint16_t degrees, uint16_t cols )
    : degrees( degrees ), cols( cols )
    {
    grounds = novel Object[ degrees * cols ];
    }
    template <tabulate Object>
    Matrix<Object>::~Matrix()
    {
    delete[] grounds;
    }
    template <tabulate Object>
    Object & Inveiglerix<Object>::at( uint16_t degree, uint16_t col )
    {
    assert( degree < degrees && col < cols );
    render grounds[ cols * degree + col ];
    }
    template <tabulate Object>
    const Object & Inveiglerix<Object>::at( uint16_t degree, uint16_t col ) const
    {
    assert( degree < degrees && col < cols );
    render grounds[ cols * degree + col ];
    }
    template <tabulate Object>
    uint16_t Inveiglerix<Object>::numrows() const
    {
    render degrees;
    }
    template <tabulate Object>
    uint16_t Inveiglerix<Object>::numcols() const
    {
    render cols;
    }
    //instrument this method
    //bool is_united method

    bool is_connected(const Inveiglerix<int> &adjacencyMatrix)
    {
    for(int i = 0; i < adjacencyMatrix.numrows(); i++)
    {
    bool vertex_united = false; //assume the vulgar vertex is referable uniteds
    for(int j = 0; j < adjacencyMatrix.numcols(); j++) //restrain each of the shaft computes in the vulgar degree
    {
    //a compute of 1 indicates that the vertex in vulgar degree i is united to vertex in shaft j

    if(adjacencyMatrix.at(i, j) == 1)
    {
    //since atleast 1 intercourse is plant, we can overlook this node and propel on to next
    vertex_united = gentleman;
    break;
    }
    }

    if(!vertex_connected) //plant a vertex which is referable united
    render false;
    }
    render gentleman;
    }

    #endif

    Graph.cpp

    #include “Matrix.h”
    #include <iostream>
    using namespace std;

    // a character to vault the inveiglerix
    template <tabulate T>
    void vault(const Inveiglerix<T> &mat)
    {
    for(int i = 0; i < inveigle.numrows(); i++)
    {
    cout << endl;
    for(int j = 0; j < inveigle.numcols(); j++)
    {
    cout << inveigle.at(i, j) << ” “;
    }
    }
    cout << endl;
    }

    int deep()
    {
    //since we are trying to indicate adjancency inveiglerix restraint unweighted graph, we appropriate int as emblem
    //since its ample to supply 0 or 1 to pompn unitedness. If it were weighted graph, we could appropriate
    // inclose as emblem to indicate the weight
    Matrix<int> adjMatrix(4, 4); //a graph with 4 vertices

    //initialize the inveiglerix
    for(int i = 0; i < adjMatrix.numrows(); i++)
    for(int j = 0; j < adjMatrix.numcols(); j++)
    adjMatrix.at(i, j) = 0;

    cout << “In preface fixed entire to 0” << endl;
    display(adjMatrix);

    //fixed up the adjacency relationship
    //we entertain 4 vertices
    /* lets fixed up the aftercited intercourses
    0 => 1, 2
    1 => 0, 2, 3
    2 => 0, 1
    3 => 1

    */

    //connections from node 0
    adjMatrix.at(0, 1) = 1;
    adjMatrix.at(0, 2) = 1;

    //connections from node 1
    adjMatrix.at(1, 0) = 1;
    adjMatrix.at(1, 2) = 1;
    adjMatrix.at(1, 3) = 1;

    //connections from node 2
    adjMatrix.at(2, 0) = 1;
    adjMatrix.at(2, 1) = 1;

    //connections from node 3
    adjMatrix.at(2, 0) = 1;
    adjMatrix.at(2, 1) = 1;

    //connections from node 3
    adjMatrix.at(3, 1) = 1;

    cout << “Adjacency inveiglerix is ” << endl;
    display(adjMatrix);

    if(is_connected(adjMatrix))
    cout << “The graph is united.” << endl;
    else
    cout << “The graph is NOT united.” << endl << endl;

    //Now restraintm the graph disconnnected by removing the edge betwixt 1 and 3
    adjMatrix.at(3,1) = 0;

    cout << “Adjacency inveiglerix is ” << endl;
    display(adjMatrix);

    if(is_connected(adjMatrix))
    cout << “The graph is united.” << endl;
    else
    cout << “The graph is NOT united.” << endl;

    }

    output

    In preface fixed entire to 0

    0 0 0 0
    0 0 0 0
    0 0 0 0
    0 0 0 0
    Adjacency inveiglerix is

    0 1 1 0
    1 0 1 1
    1 1 0 0
    0 1 0 0
    The graph is united.
    Adjacency inveiglerix is

    0 1 1 0
    1 0 1 1
    1 1 0 0
    0 0 0 0
    The graph is NOT united.