import java.util.Scanner;
public class lab1 {
public static void main(String[] args) {
int m,n,flag;
Scanner sc = new Scanner(System.in);
System.out.println("Input (Adjacency Matrix Representation)");
m = sc.nextInt();
int[][] adj_mat1 = new int[m][m];
int[] deg1 = new int[m];
for(int i=0;i<m;i++)
{
for(int j=0;j<m;j++)
{
adj_mat1[i][j] = sc.nextInt();
System.out.print(" ");
}
System.out.println();
}
n = sc.nextInt();
int[][] adj_mat2 = new int[n][n];
int[] deg2 = new int[n];
for(int i=0;i<n;i++)
{
for(int j=0;j<n;j++)
{
adj_mat2[i][j] = sc.nextInt();
System.out.print(" ");
}
System.out.println();
}
if(m!=n)
{
System.out.println("Output: ");
System.out.println();
System.out.println("The graphs are not complementary.");
System.out.println("The graphs have different number of vertices.");
return;
}
else
{
for(int i=0;i<m;i++)
{
for(int j=0;j<m;j++)
{
if(adj_mat1[i][j]==adj_mat2[i][j])
{
System.out.println();
System.out.println("Output: ");
System.out.println("The graphs are not complementary.");
System.out.println("The graphs have the same number of vertices but don't have complementary edges");
return;
}
}
}
}
System.out.println();
System.out.println("Output: ");
System.out.println("The graphs are complementary.");
System.out.println("");
for(int i=0;i<m;i++)
{
for(int j=0;j<m;j++)
{
int count=0;
if(adj_mat1[i][j]==1)
{
count++;
deg1[i] = count;
}
}
}
for(int i=0;i<n;i++)
{
for(int j=0;j<n;j++)
{
int count=0;
if(adj_mat2[i][j]==1)
{
count++;
deg2[i] = count;
}
}
}
System.out.println("Vertex G1 Degree G2 Degree");
for(int i=0;i<m;i++)
{
System.out.println(i+" "+deg1[i]+" "+deg2[i]);
}
}
}
OUTPUT:
Input (Adjacency Matrix Representation)
3
0 1 1
0 1 0
1 1 0
3
1 0 0
1 0 1
0 0 1
Output:
The graphs are complementary.
Vertex G1 Degree G2 Degree
0 2 1
1 1 2
2 2 1
Goal Design and implement an algorithm to input the data representation of two graphs, determine if...
Give the adjacency matrix representation and the adjacency lists representation for the graph G_1. Assume that vertices (e.g., in adjacency lists) are ordered alphabetically. For the following problems, assume that vertices are ordered alphabetically in the adjacency lists (thus you will visit adjacent vertices in alphabetical order). Execute a Breadth-First Search on the graph G_1, starting on vertex a. Specifiy the visit times for each node of the graph. Execute a Depth-First Search on the graph G_1 starting on vertex...
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...
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...
Task 3: Grid Graphs and Mazes Part A - Generating Grid Graphs In the lecture we said that we can use Prim's algorithm to create mazes by starting from a regular "grid graph and then finding a spanning tree. Implement a function grid graph (m, n) that takes as input two positive integers, and returns the adjacency matrix of a m by n grid graphie, a graph with ses vertices that, when drawn on a regular m by n grid,...
Lab 11
Adjacency Matrix Graph
Objective:
Create a class which constructs an adjacency matrix
representation of a graph and performs a few graph operations.
Write an Adjacency Matrix Graph class which has the
following:
Two constructors:
Default which makes the matrix of a pre-defined size
Parameterized which takes in a non-negative or 0 size and
creates an empty matrix
addEdge: this method returns nothing and takes in two string
parameters and a weight. The two integer parameters correspond to
the...
3. The indegree of a vertex u is the number of incoming edges into u, .e, edges of the form (v,u) for some vertex v Consider the following algorithm that takes the adjacency list Alvi, v2, n] of a directed graph G as input and outputs an array containing all indegrees. An adjacency list Alvi, v.. /n] is an array indexed by the vertices in the graph. Each entry Alv, contains the list of neighbors of v) procedure Indegree(Alvi, v2,......
Dijkstra’s Algorithm: You have to implement the Dijkstra’s
algorithm and apply it on the graph provided below.
You have to take the input from the user as an adjacency matrix
representing the graph, the source, the destination. Then you have
to apply the Dijkstra’s algorithm to find the shortest path from
the source and the destination, and find the shortest
route between the source and the destination.
For the input you have to read it from a file. It will...
PYTHON ONLY Implement the Dijkstra’s Shortest path algorithm in Python. A graph with 10 nodes (Node 0 to node 9) must be implemented. You are supposed to denote the distance of the edges via an adjacency matrix (You can assume the edge weights are either 0 or a positive value). The adjacency matrix is supposed to be a 2-D array and it is to be inputted to the graph. Remember that the adjacency list denotes the edge values for the...
in C++ Design a format for storing graphs in files. This will store your graph into a file with the following requirements: The first line will contain the number of Vertices. The second line will have a ‘U’ or a ‘D’ to tell the system if it is Undirected or Directed. The rest of the lines will be here for each of the edges. Each edge will contain three pieces of information: An integer containing the first Vertex An integer...
We saw during our discussion of the implementation of Graphs that there are two broad implementation strategies that we can choose if we're implementing a graph: adjacency lists or an adjacency matrix. The difference turns out not to be academic, nor is it a matter of convenience or familiarity; the distinction is a critical one that we need to get right. For each of the scenarios described below, choose one of the implementation strategies or the other — either adjacency...