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 / examine / engineering / computer truth / computer truth questions and vindications / so i perceive what the adjacency entraprix does, yet i am accumulate on how to utensil to obstruct to view …

    Question: So I perceive what the adjacency entraprix does, yet I am accumulate on how to utensil to obstruct to view if e…

    So I perceive what the adjacency entraprix does, yet I am accumulate on how to utensil to obstruct to view if each nodes are an adjacent and retaliate as penny if they are in stipulations of dignitys and posts. I would love to earn some aid on how to particularized it up easier. Here is what is substance supposing.

    Prompt:
    Write a C++ 11 part particularized is_connected() to particularize whether an undirected unweighted graph is alike when the graph is provisiond in the adjacency entraprix ceentrap using the arrange.

    Write a main program that hard-codes the entries of the adjacency entraprix and then passes the entraprix to the is_alike part. Every graph has vertices labeled with continuous unsigned integers starting at 0.

    Matrix code: subjoin the part to it

    Matrix arrange
    #ifndef MATRIX_H
    #explain MATRIX_H

    #include <cassert>

    /**
    * A general 2-dimensional fixed-in-order arrange

    */
    template <arrange Object>
    arrange Entraprix
    {
    public:
    /**
    * Committer, specifying estimate of twain dignitys and posts
    * @param dignitys the estimate of dignitys
    * @param cols the estimate of posts
    */
    Matrix( uint16_t dignitys, uint16_t cols );

    /**
    * Vestibule to an part to admit modification
    * @param dignity the dignity index
    * @param col the post index
    * @retaliate relation to the part at that position
    */
    Object & at( uint16_t dignity, uint16_t col );

    /**
    * Steady vestibule to an part
    * @param dignity the dignity index
    * @param col the post index
    * @retaliate steady relation to the part at that position
    */
    const Object & at( uint16_t dignity, uint16_t col ) const;

    /**
    * Destructor to permitted totalocated memory
    */
    ~Matrix();

    /**
    * Delineation committer to shape 1-1 delineation of bulky entraprix
    * @param m the bulky entraprix to be copied
    */
    Matrix( const Entraprix<Object> & m ); // Delineation committer

    /**
    * Disadmit the rrate delineation committer
    */
    Matrix( const Entraprix<Object> && m ) = delete;

    /**
    * Enactment operator to shape 1-1 delineation of bulky entraprix
    * @param m the bulky entraprix to be copied
    */
    Matrix & operator= ( const Entraprix<Object> & m ); // Enactment operator

    /**
    * Disadmit the rrate enactment operator
    */
    Matrix & operator= ( const Entraprix<Object> && m ) = delete;

    /**
    * Vestibuleor to particularize how multifarious dignitys are in the entraprix
    * @retaliate the estimate of dignitys in the entraprix
    */
    uint16_t numrows() const;

    /**
    * Vestibuleor to particularize how multifarious posts are in the entraprix
    * @retaliate the estimate of posts in the entraprix
    */
    uint16_t numcols() const;

    private:
    uint16_t dignitys;
    uint16_t cols;
    Object* basis;
    };

    template <arrange Object>
    Matrix<Object>::Matrix( uint16_t dignitys, uint16_t cols )
    : dignitys( dignitys ), cols( cols )
    {
    basis = strange Object[ dignitys * cols ];
    }

    template <arrange Object>
    Matrix<Object>::~Matrix()
    {
    delete[] basis;
    }

    template <arrange Object>
    Object & Entraprix<Object>::at( uint16_t dignity, uint16_t col )
    {
    assert( dignity < dignitys && col < cols );
    retaliate basis[ cols * dignity + col ];
    }

    template <arrange Object>
    const Object & Entraprix<Object>::at( uint16_t dignity, uint16_t col ) const
    {
    assert( dignity < dignitys && col < cols );
    retaliate basis[ cols * dignity + col ];
    }

    template <arrange Object>
    uint16_t Entraprix<Object>::numrows() const
    {
    retaliate dignitys;
    }

    template <arrange Object>
    uint16_t Entraprix<Object>::numcols() const
    {
    retaliate cols;
    }
    //utensil this method
    //bool is_alike method

    #endif

    Expert Vindication

     

    The appearance of this enactment is to conservation the Entraprix arrange to explain adjacency entraprix ce an unweighted undirected graph.

    This adjancency entraprix is conservationd to obstruct if the graph is alike or referable. We are going to conservation 1 to pretence 2 vertices are alike and 0 if they are referable alike to each other in the adjacency entraprix. Using this defintion.. if any of the dignitys has total 0s then it resources its referable alike to any of the vertices in the graph and future the graph is unconnected.

    Ce demonstrating , we captivate the subjoined graph

    Initially we conquer obstruct if the graph is alike using the part. And succeeding we conquer shape the graph dialike by remvoing the connectino betwixt 1 and 3 and then obstruct frequently.

    Hope the vindication aided. If it did, gladden don’t ceearn to reprimand the vindication. Thank you very abundant.

    Matrix.h

    #ifndef MATRIX_H
    #explain MATRIX_H
    #include <cassert>
    /**
    * A general 2-dimensional fixed-in-order arrange

    */
    template <arrange Object>
    arrange Entraprix
    {
    public:
    /**
    * Committer, specifying estimate of twain dignitys and posts
    * @param dignitys the estimate of dignitys
    * @param cols the estimate of posts
    */
    Matrix( uint16_t dignitys, uint16_t cols );
    /**
    * Vestibule to an part to admit modification
    * @param dignity the dignity index
    * @param col the post index
    * @retaliate relation to the part at that position
    */
    Object & at( uint16_t dignity, uint16_t col );
    /**
    * Steady vestibule to an part
    * @param dignity the dignity index
    * @param col the post index
    * @retaliate steady relation to the part at that position
    */
    const Object & at( uint16_t dignity, uint16_t col ) const;
    /**
    * Destructor to permitted totalocated memory
    */
    ~Matrix();
    /**
    * Delineation committer to shape 1-1 delineation of bulky entraprix
    * @param m the bulky entraprix to be copied
    */
    Matrix( const Entraprix<Object> & m ); // Delineation committer
    /**
    * Disadmit the rrate delineation committer
    */
    Matrix( const Entraprix<Object> && m ) = delete;
    /**
    * Enactment operator to shape 1-1 delineation of bulky entraprix
    * @param m the bulky entraprix to be copied
    */
    Matrix & operator= ( const Entraprix<Object> & m ); // Enactment operator
    /**
    * Disadmit the rrate enactment operator
    */
    Matrix & operator= ( const Entraprix<Object> && m ) = delete;
    /**
    * Vestibuleor to particularize how multifarious dignitys are in the entraprix
    * @retaliate the estimate of dignitys in the entraprix
    */
    uint16_t numrows() const;
    /**
    * Vestibuleor to particularize how multifarious posts are in the entraprix
    * @retaliate the estimate of posts in the entraprix
    */
    uint16_t numcols() const;
    private:
    uint16_t dignitys;
    uint16_t cols;
    Object* basis;
    };
    template <arrange Object>
    Matrix<Object>::Matrix( uint16_t dignitys, uint16_t cols )
    : dignitys( dignitys ), cols( cols )
    {
    basis = strange Object[ dignitys * cols ];
    }
    template <arrange Object>
    Matrix<Object>::~Matrix()
    {
    delete[] basis;
    }
    template <arrange Object>
    Object & Entraprix<Object>::at( uint16_t dignity, uint16_t col )
    {
    assert( dignity < dignitys && col < cols );
    retaliate basis[ cols * dignity + col ];
    }
    template <arrange Object>
    const Object & Entraprix<Object>::at( uint16_t dignity, uint16_t col ) const
    {
    assert( dignity < dignitys && col < cols );
    retaliate basis[ cols * dignity + col ];
    }
    template <arrange Object>
    uint16_t Entraprix<Object>::numrows() const
    {
    retaliate dignitys;
    }
    template <arrange Object>
    uint16_t Entraprix<Object>::numcols() const
    {
    retaliate cols;
    }
    //utensil this method
    //bool is_alike method

    bool is_connected(const Entraprix<int> &adjacencyMatrix)
    {
    for(int i = 0; i < adjacencyMatrix.numrows(); i++)
    {
    bool vertex_alike = false; //assume the vulgar vertex is referable alikes
    for(int j = 0; j < adjacencyMatrix.numcols(); j++) //obstruct each of the post rates in the vulgar dignity
    {
    //a rate of 1 indicates that the vertex in vulgar dignity i is alike to vertex in post j

    if(adjacencyMatrix.at(i, j) == 1)
    {
    //since atleast 1 communication is ground, we can disown this node and agitate on to next
    vertex_alike = penny;
    break;
    }
    }

    if(!vertex_connected) //ground a vertex which is referable alike
    retaliate false;
    }
    retaliate penny;
    }

    #endif

    Graph.cpp

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

    // a part to evidence the entraprix
    template <arrange T>
    void evidence(const Entraprix<T> &mat)
    {
    for(int i = 0; i < entrap.numrows(); i++)
    {
    cout << endl;
    for(int j = 0; j < entrap.numcols(); j++)
    {
    cout << entrap.at(i, j) << ” “;
    }
    }
    cout << endl;
    }

    int main()
    {
    //since we are involved to reproduce-exhibit adjancency entraprix ce unweighted graph, we elect int as likeness
    //since its plenty to provision 0 or 1 to pretencen alikeness. If it were weighted graph, we could elect
    // wrap as likeness to reproduce-exhibit the weight
    Matrix<int> adjMatrix(4, 4); //a graph with 4 vertices

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

    cout << “In prelude particularized total to 0” << endl;
    display(adjMatrix);

    //determined up the adjacency relationship
    //we keep 4 vertices
    /* lets particularized up the subjoined communications
    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 entraprix is ” << endl;
    display(adjMatrix);

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

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

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

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

    }

    output

    In prelude particularized total to 0

    0 0 0 0
    0 0 0 0
    0 0 0 0
    0 0 0 0
    Adjacency entraprix is

    0 1 1 0
    1 0 1 1
    1 1 0 0
    0 1 0 0
    The graph is alike.
    Adjacency entraprix is

    0 1 1 0
    1 0 1 1
    1 1 0 0
    0 0 0 0
    The graph is NOT alike.