Answered step by step
Verified Expert Solution
Question
1 Approved Answer
please write the whole code for Matlab... the code should be simple as possible... please don't give the adjacency matrix I need a valid code
please write the whole code for Matlab...
the code should be simple as possible...
please don't give the adjacency matrix
I need a valid code for matlab in (generic form) the code should be valid if we change the matric (not designed for one problem)
2 Part 2-Network Application: The Adjacency Matrix of a Graph 2.1 Introduction Definition 1 (Graph). A graph is a finite set of objects called nodes, together with some paths between some of the nodes, as illustrated below. A path of length one is a path that directly connects one node to another. A path of length k is a path made up of k consecutive paths of length one. Definition 2 (Adjacency Matrix). When the nodes have been numbered from 1 ton, the adjacency matrix A of the graph is defined by letting aij = 1 if there is a path of length one between vertices i and j and aj = 0 otherwise. For example, the graph in Figure 1 has the matrix A shown as its adjacency matrix. 5 A= 01000 direct path from node 1 to node 2 101001 direct path from node 2 to 1:2 to 3: 2 to 6 010100 001011 000101 01011 od direct path from node 6 to 2: 6 to 4; 6 to 5 Figure 1: A graph representation of a network along with its associated adjacency matrix. Theorem 1 (Interpretation of the powers of an adjacency matrix). If A is the adjacency matrix of a graph, then the (ij) entry of Ak is a nonnegative integer which is the number of paths of length k from node i to node ; in the graph. 2 To understand what the theorem says for the example above, let's carefully examine the (6.3) entry of AP of the graph in Figure 1. The (6.3) entry of A2 is obtained by inner product between the 6th row and the 3rd column of A. That is, (6,3) entry of AP = 061013 +062023 +063033 +264043 +265453 +266063 = (0) (0) + (1)(1) + (0) (0) + (1)(1) + (1)(0) + (0) (0) = 2. (1) Explain what each term in the sum above tells about paths of length 2 from node 6 to node 3. It is clear that there are two paths of length two that link node 6 to node 3 (ie., the first path is 6 - 2 - 3 and the second path is 6 - 4-3), which is equal to (6,3) entry of A2. In particular, there are two terms in the expression in (1) that contribute to obtain the value 2. These two terms are (62023 = and 264043 = Each one of these two terms represents one path. For example, by 062023 = (1)(1) = 1; this says that the length one paths 6 +2 and 2 + 3 appear in the graph, and together they give one path from node 6 to node 3, of length 2 (i.e., 6 + 2 + 3). Similarly, 264043 = (1)(1) = 1; this says that the length one paths 6 + 4 and 4 3 appear in the graph, and together they give one path from node 6 to node 3, of length 2 (i.e. 6+4+3). Te following Matlab code define the adjacency matrix A of Figure 1 and computes B = A and C = A3: 1 % Define the adjacency matrix 2 A = 1010000; 10100 1: 01010 0: 0 0 1 0 1 1: 0 0 0 1 0 1: 01011 0: 3 4 6 % Compute the powers of A 10 B = A 2 % computes A A 11 C = A3 % computes A+A A = B A = A*B The obtained B and C matrices are as follows: 10 1 0 0 1 0 3 0 2 1 0 1 0 2 0 1 2 B2 = 0 2 0 3 1 1 011121 1 0 2 1 1 3 B3 = 0 3 0 2 1 0 3 0 5 1 26 0 5 0 5 2 1 2 1 5 2 4 6 1 2 2 4 2 4 0 6 1 6 4 2 It is worth to reemphasize that each non-zero term in the expression that calculates the i. ;) entry in Ak is corresponding to one path of length k that connects node i to node j. Below are some examples that helps further understanding the interpretation of the power of the adjacency matrix: The (1, 2) entry of B = A2 is zero, so there are no paths of length two from node 1 to node 2. Verify this by studying the graph. 3 The (3, 2) entry of C = A' is 5, so there are 5 paths of length 3 from node 3 to node 2. These five paths are: Path 1: 3 + 2 + 1 + 2 Path 2: 3 2 3 2 Path 3: 3 4 32 Path 4: 3 + 2 + 6 + 2 Path 5: 3462 To illustrate how we obtain these 5 paths. Again, the (3, 2) entry of C = A is obtained by multiplying the 3rd row of B by the 2nd column of A. This yields the following expression: C32 = 631012 + b33a32 +636062 = (as2021212 + (a32023 + 234043a32 + (a36062 + 2340 46)262 (2) = 232021012 +03223232 +234043232 +236962062 +234046062 Path 1 Path 2 Path 3 Path 4 Path 5 What are the paths of length two from node 2 to itself? This is equivalent to the (2,2) entry of B. That is b22 = 3. What are the paths of length three from node 3 to node 4? This is equivalent to the (3, 4) entry of C. That is C34 = 5. Definition 3 (Contact level). When we have a graph, we will say that there is a contact level k between node i and node ; if there is a path of length less than or equal to k from node i to node . Suppose A is the adjacency matrix of a graph. To decide which nodes have contact level k with each other requires calculating the sum A + A + ... + Ak. 2.2 Task Exercise 1. In a manufacturer, there are 8 workers, denoted by Wi...,Wg. It is known that if an worker becomes infected with COVID-19, he/she could spread this through contact with workers up to k contact levels. In particular, let us assume that the infection can spread up to 2 levels. That is, if W1 got infected and he/she is in direct contact with W2. Then, all workers who are in direct contact with W, are in high risk to be infected. Consider the following graph in Figure 2 that shows the level one contacts between the workers. Write a matlab code that is composed of the "main" script and one function called "EmployeeCon- tact Level" as described below: (a) Main script: 4 Defines the adjacency matrix A. Asks the user to enter the required contact level (i.e., k). call the function "EmployeeContact Level" Print the returned output from the function "Employee Contact Level" in a proper format. (b) Employee Contact Level function Input: Adjacency matrix (i.e., A). Infection spread contact level (i.e., k). Output: . The number and indices of the workers who have a contact level k with each worker. The index of the most dangerous worker (i.e., the one who will spread the disease among others if he/she got infected more than any other employee) W5 W4 W2 W3 W8 W7 W6 W1 Figure 2: A graph representation of level one contacts between the 8 workers. Make sure to present the output within a proper message. For example, assume that you are asked to find the rank of a matrix. Do not just print the number that represents its rank. You need to show the output in a clear/meaningful statement. Below are some examples for such proper/acceptable formats to show the output: The rank of matrix A = 4" "The rank of matrix A equals to 4" "rank(A) = 4Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started