Write a program that specifies a simple undirected graph by its “adjacency matrix”. Recall that that the adjacency matrix A is such that A(i, j) = 1 if nodes i and j are adjacent and A(i, j) = 0 otherwise.
Let αk(i ,j) be the number of paths of length k between nodes i and j. For instance, the number of paths of length-1 between nodes i and j in a simple undirected graph is 1 if they are adjacent and zero otherwise.
Show by numerical experiments that the matrix Ak allows you to calculate αk(i ,j) for all i and j. You should consider small graphs for which you can verify the result by hand. You should submit the code and your numerical verification.
I have written page number on the page so you can sequence them easily
//Code that verifies the above proven statements and demonstrates the results
You have to understand Matrix exponentiation to understand the code because for greater values of k, time complexity would be exponential by using brute force approaches.
#include <bits/stdc++.h>
using namespace std;
#define N 3
// Function to multiply two matrices
void multiply(int a[][N], int b[][N], int AK[][N])
{
int mul[N][N];
for (int i = 0; i < N; i++)
{
for (int j = 0; j < N;
j++)
{
mul[i][j] =
0;
for (int k = 0;
k < N; k++)
mul[i][j] += a[i][k] * b[k][j];
}
}
// Storing the multiplication result in res[][]
for (int i = 0; i < N; i++)
for (int j = 0; j < N;
j++)
AK[i][j] =
mul[i][j];
}
// Function to compute G raised to the power n
void power(int A[N][N], int AK[N][N], int n)
{
// Base condition
if (n == 1) {
for (int i = 0; i < N;
i++)
for (int j = 0;
j < N; j++)
AK[i][j] = A[i][j];
return;
}
// Recursion call for first half
power(A, AK, n / 2);
// Multiply two halves
multiply(A, A, AK);
// If n is odd
if (n % 2 != 0)
multiply(AK, A, AK);
}
int main()
{
int A[N][N] = { { 1, 1, 1 },
{ 0, 0, 1 },
{ 0, 1, 0 } };
int k, AK[N][N];
cout << "Enter the value of k" << endl;
cin>>k;
power(A, AK, k);
for (int i = 0; i < N; i++)
{
for (int j = 0; j < N;
j++)
cout <<
AK[i][j] << " ";
cout << "\n";
}
return 0;
}
Write a program that specifies a simple undirected graph by its “adjacency matrix”. Recall that that...
Solve all parts please 5. In the following problems, recall that the adjacency matrix (or incidence matrix) for a simple graph with n vertices is an n x n matrix with entries that are all 0 or 1. The entries on the diagonal are all 0, and the entry in the ih row and jth column is 1 if there is an edge between vertex i and vertex j and is 0 if there is not an edge between vertex...
4&5 0 1 2 3 1. Draw the undirected graph that corresponds to this adjacency matrix 0 0 1 1 0 1 1 1 1 0 1 1 1 2 1 1 1 0 1 3 1 0 1 1 0 1 2. Given the following directed graph, how would you represent it with an adjacency list? 3. We've seen two ways to store graphs - adjacency matrices, and adjacency lists. For a directed graph like the one shown above,...
Consider an unweighted, undirected graph G = 〈V, E). The neighbourhood of a node u E V in the graph is the set of all nodes that are adjacent (or directly connected) to v. Subsequently, we can define the neighbourhood degree of the node v as the sum of the degrees of all its neighbours (those nodes that are directly connects to v) (a) Design an algorithm that returns a list containing the neighbourhood degree for each node v V,...
This question needs to be done using pseudocode (not any particular programming language). Thanks Consider an unweighted, undirected graph G = 〈V, E). The neighbourhood of a node u E V in the graph is the set of all nodes that are adjacent (or directly connected) to v. Subsequently, we can define the neighbourhood degree of the node v as the sum of the degrees of all its neighbours (those nodes that are directly connects to v) (a) Design an...
hello there ,, can anyone give the solution of this Assuming a graph is represented as an adjacency matrix, write the pseudocode for an algorithm that can determine if any path exists between two vertices. The algorithm would accept as input: The nxn adjacency matrix for an undirected, unweighted graph A source vertex A destination vertex Returning as output: A boolean value indicating whether there is a path between the source and destination. You can use anything for variable/function names...
Please show work clearly. Thanks 3. (10 points) Let G be an undirected graph with nodes vi,..Vn. The adja.- cency matriz representation for G is the nx n matrix M given by: Mij-1 if there is an edge from v, to ty. and M,',-0 otherwise. A triangle is a set fvi, vjof 3 distinct vertices so that there is an edge from v, to vj, another from v to k and a third from vk to v. Give and analyze...
JAVA LAB 1 2 5 7 6 9 3 8 . Write code to implement an adjacency matrix (2d matrix) which represents the graph. Your code should contain a function called addEdgelint i, int j). Use this function to add the appropriate edges in your matrix. Write code to implement Depth-First-Search (DFS) and Breadth-First-Search (BFS) of your graph. Let 0 be the source Node . Traverse the graph using DFS, print the Nodes as they are visited. . Traverse the...
Design and implement Dijkstra’s algorithm to compute all-pair shortest paths in any given graph using An adjacency matrix using a one-dimensional array for storing only the elements of the lower triangle in the adjacency matrix.[Program in C language] The input to program must be connected, undirected, and weighted graphs. The programs must be able to find shortest paths on two types of connected, undirected, and weighted graphs: complete graph (a graph with a link between every pair of nodes) and...
3. (6) The directed graph below with node 1, 2, 3 in orange color and directed edges in blue color. (Note, there is an edge from node 3 to itself.) The adjacency matrix of this graph is the 1 if there is an edge from node i to node j, otherwise ay 0. Find 3 x 3 matrix A with ay the number of directed paths from node 3 to node 1 of length 10. (Note: each edge is considered...
Write a program to process a weighted undirected graph as follows. (a) Read in the number of vertices V and the number of edges E of the graph followed by its E edges, each in the form u, v, w where 1 <= u, v <= V & w > 0 representing an edge uv with weight w. (b) Set up and print the adjacency matrix representation of the Graph. (c) Determine whether the graph is connected. ...