Matricies and Graphs program I need to focus on matrices and directed graphs rep
ID: 3821637 • Letter: M
Question
Matricies and Graphs program
I need to focus on matrices and directed graphs represented as adjacency matrices. C++ or MATLAB is needed to be used to perform the tasks. Thanks in advance if anyone figures this one out.
1. Roots and Leaves of a Graph (50 points Taking an adjacency matrix as input (matrix in MATLAB, 2d array in C++), return the set of roots and leaves in the graph. For example, in the graph below, the graph has roots 1,2 and leaf 7. It is possible for a graph not to have roots and/or leaves 2. Determinant (50 points) Write a function that takes as input a 2x2 matrix and a 3x3 matrix and outputs if the matrix is regular or singular. Implement two test cases for singular and regular.Explanation / Answer
1>
% If indegree of a node is zero then it is a root node.
% If outdegree of a node is zero then it is a leaf node.
prompt = 'Input the Graph:';
graph = input(prompt);
% This will return a logical array(1 if there is atleast one non zero entry
% in a column)
root = any(graph);
% Rows becomes columns and columns becomes rows in a transpose matrix
leaf = any(graph.');
disp('root nodes are : ')
for i=1:size(root,2)
if(~ root(i))
disp(i)
end
end
disp('leaf nodes are : ')
for i=1:size(leaf,2)
if(~ leaf(i))
disp(i)
end
end
Sample Run:
Input the graph: [[0,0,1,0,0,0,0,0];[0,0,1,0,0,0,0,0];[0,0,0,1,1,0,0,0];[0,0,0,0,0,1,1,1];[0,0,0,0,0,1,1,1];[0,0,0,0,0,0,1,0];[0,0,0,0,0,0,0,0];[0,0,0,0,0,0,1,0]]
root nodes are:
1
2
leaf nodes are:
7
2>
function determinant(A)
if(det(A)==0)
disp('Matrix is Singular');
else
disp('Matrix is Regular');
end
end
Sample run:
>> determinant([[1,0];[0,1]])
Matrix is Regular
>> determinant([[1,0];[0,0]])
Matrix is Singular