Question

Goal Design and implement an algorithm to input the data representation of two graphs, determine if the graphs are complements of each other and, if so, outputs a message that the graphs are complements then outputs the degrees of the vertices of each graph, otherwise outputs a message to the contrary and immediately terminates. Details Input to your program consists of the representation of two graphs and will be in the following format: an integer m representing the number of vertices of the first graph Gi . an adjacency matrix for Gi of m rows, each with, m entries. Each entry of the m xm adjacency matrix, will contain a 1 if there is an edge between the corresponding vertices or a 0 if there is no edge an integer n representing the number of vertices of the second graph G .an adjacency matrix for Gi of n rows, each with, n entries. Each entry of the n x n adjacency matrix, will contain a 1 if there is an edge between the corresponding vertices or a 0 if there is no edge There is a space following each number in the adjacency matrix, including the last number in each row. There is NOT a space in the first line indicating the size of the array Output is required to be formatted as in the examples below It is up to you to determine the best algorithm to accomplish this task. You MUST, however, use a 2-D array to represent the adjacency matrix in your program. It is very important that a 2-D array is used as it will be necessary in future labs. In addition, the elements of the 2-D array should be referenced at most once and if the graphs are not complementary the algorithm should terminate as soon as that is determined
media%2F12f%2F12f8cbf5-5e5e-4310-8a0e-c9
media%2F0d9%2F0d91cf11-f8ae-4273-8dee-e9
media%2Fd88%2Fd8871ee5-d609-4db6-b568-30
0 0
Add a comment Improve this question Transcribed image text
Answer #1

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

Add a comment
Know the answer?
Add Answer to:
Goal Design and implement an algorithm to input the data representation of two graphs, determine if...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Give the adjacency matrix representation and the adjacency lists representation for the graph G_1. Assume that...

    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...

    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. (10 points) Let G be an undirected graph with nodes vi,..Vn. The adja.- cency matriz represent...

    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...

    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...

    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)...

    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,...

    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...

    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 t...

    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...

    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...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT