Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

In graph theory, a complete graph on n vertices (typically denoted K,) is a spec

ID: 3283646 • Letter: I

Question

In graph theory, a complete graph on n vertices (typically denoted K,) is a special kind of graph in which every vertex is connected to every other vertex (other than itseln. You can find pictures of K2 through K, here. Write a function that returns the adjacency matrix A for the complete graph K, Hint: You can do this with one short line of code if you use the 'ones' and 'diag' functions. Your Function Reset MATLAB Documentation function A complete graph(n) % computes the adjacency matrix for the complete graph Kn. 4 end

Explanation / Answer

MATLAB code

close all
clear
clc

n = 8; % example
A = complete_graph(n)

function A = complete_graph(n)
% Since every vertex is connected to every other except itself, the
% adjacency matrix will be all ones except the diagonal elements. The
% diagonal elements will be zeros.
A = ones(n)-diag(ones(1,n));
end

Output

A =

0 1 1 1 1 1 1 1
1 0 1 1 1 1 1 1
1 1 0 1 1 1 1 1
1 1 1 0 1 1 1 1
1 1 1 1 0 1 1 1
1 1 1 1 1 0 1 1
1 1 1 1 1 1 0 1
1 1 1 1 1 1 1 0