An example of an undirected graph and its corresponding adjacency matrix is show
ID: 1714907 • Letter: A
Question
An example of an undirected graph and its corresponding adjacency matrix is shown. An undirected graph is collection of n vertices numbered 1 to n, some of which may be connected by an edge. The corresponding adjacency matrix is an nxn matrix associated to an undirected graph. The adjacency matrix has a 1 in position (i,j) if there is an edge connecting i to j, and it has a 0 in position (i,j) if there is no edge connecting i to j. Write a function make_adj that takes two inputs, a number n ( the number of vertices in the graph) and a list of the edges in the graph, and returns as output the corresponding nxn adjacency matrix. Include comments describing how the list edges is represented; it is your choice how to represent it.Explanation / Answer
Run the follwing commands.
function adja=adjacent_matrix(n,B) %B is 2*x matrix. 1st raw gives source nodes and 2nd raw gives their respective destination node
adja=zeros(n,n);
l=length(B(1,:))
for i=1:1:l
adja(B(1,i),B(2,i))=1;
end
end
Then give n value and edge list details as follows type the following;
function_definition(5,[1 2 3 4 5;2 3 4 5 1])
Result;
l =
5
ans =
0 1 0 0 0
0 0 1 0 0
0 0 0 1 0
0 0 0 0 1
1 0 0 0 0
Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.