Question

Write a program in Java; provide an example of outputProblem Given the vertex pairs associated to the edges of a graph, construct an adjacency matrix for the graph. (Produce a ve

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Java code:

import java.util.Scanner;

public class Main

{

private final int vertex;

private int[][] matrix;

public Main(int v)

{

vertex=v;

matrix = new int[vertex+1][vertex+1];

}

public void createEdge(int t,int f,int e)

{

try

{

matrix[t][f]=e;

}

catch (ArrayIndexOutOfBoundsException index)

{

System.out.println("The vertices doesn't exists");

}

}

public int getEdge(int t,int f)

{

try

{

return matrix[t][f];

}

catch (ArrayIndexOutOfBoundsException index)

{

System.out.println("The vertices does not exists");

}

return -1;

}

public static void main(String args[])

{

int v,e,c=1,t=0,f=0;

Scanner sc= new Scanner(System.in);

Main g;

System.out.println("Enter number of vertices: ");

v = sc.nextInt();

System.out.println("Enter the number of edges: ");

e = sc.nextInt();

g=new Main(v);

System.out.println("Enter the edges: <to> <from>");

while(c<=e)

{

t= sc.nextInt();

f=sc.nextInt();

g.createEdge(t,f,1);

c++;

}

System.out.println("The adjacency matrix for graph is: ");

System.out.print(" ");

for(int i=1;i<=v;i++)

System.out.print(i + " ");

System.out.println();

for (int i=1;i<=v;i++)

{

System.out.print(i + " ");

for (int j = 1; j <= v; j++)

System.out.print(g.getEdge(i, j) + " ");

System.out.println();

}

}

}

java version 1.8.0 31 Java (TM) SE Runtime Environment (build 1.8.0 31-b13) Java HotSpot (TM) 64-Bit Server VM (build 25.31-

if you like the answer please provide a thumbs up.

Add a comment
Know the answer?
Add Answer to:
Write a program in Java; provide an example of output Problem Given the vertex pairs associated...
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
  • How would I traverse through this graph? Provide example code, please! class Edge {    int src,...

    How would I traverse through this graph? Provide example code, please! class Edge {    int src, dest;    Edge(int src, int dest)    {        this.src = src;        this.dest = dest;    } }; // class to represent a graph object class Graph {    // A list of lists to represent adjacency list    List<List<Integer>> adj = new ArrayList<>();    // Constructor to construct graph    public Graph(List<Edge> edges)    {        // allocate memory for adjacency list        for (int i = 0; i < edges.size(); i++) {            adj.add(i,...

  • Help. I need to write a small program that executes the following graph algorithms in any language: 1. All-Pairs Shortest Path (Floyd-Warshall). It must ask for the vertices and edges for the user to...

    Help. I need to write a small program that executes the following graph algorithms in any language: 1. All-Pairs Shortest Path (Floyd-Warshall). It must ask for the vertices and edges for the user to enter them. As an output, deploy the resulting matrix. This will be done only for directed graphs. 2. Kruskal or Prim algorithm whatever you want to do. It must ask for a graph and present it at the end. The minimum coating tree that results from...

  • write a program that will find the shortest path from a given vertex to a given...

    write a program that will find the shortest path from a given vertex to a given vertex. Your program must allow the user to enter any start vertex and any destination vertex and output the shortest path. A B F E D Using the attached graph, write a program that will find the shortest path from a given vertex to a given vertex. Your program must allow the user to enter any start vertex and any destination vertex and output...

  • IN C LANGUAGE Write a program that counts the leaves of a given binary tree. Hint: You will make a small (and...

    IN C LANGUAGE Write a program that counts the leaves of a given binary tree. Hint: You will make a small (and smart) update to the add function we discussed in class. a) 25 points Create the following binary trees by creating the nodes (malloc) and setting the related pointers (leftChild, right Child). Make content random. 50 40 60 100 70 45 30 120 47 b) 25 points Display the trees on screen the way we did in slide 9...

  • Help with Java Program Please Create a simple graph class. The graph class should have the...

    Help with Java Program Please Create a simple graph class. The graph class should have the following items: an adjacency list or matrix to hold the graph data variables to hold the current size and max size of the graph default constructor create empty adjacency list/matrix max size = 10 overloaded constructor create empty adjacency list/matrix max size = int parameter isEmpty check if current size is zero createGraph read a formatted file and fill adjacency list/matrix The first line...

  • In C++ Problem 3 (25 points) Write a program to find minimum weight cycle in an...

    In C++ Problem 3 (25 points) Write a program to find minimum weight cycle in an undirected weighted graph. The input is the Adjacency Matrix A of the graph. If there is an edge between vertex i to vertex j, and weight of this edge is u, then ?3] Aj, i-w. If there is no edge between i and J, A 11.j-Aj, i--1.

  • Given an unweighted directed graph G, write a program that counts and prints all simple paths...

    Given an unweighted directed graph G, write a program that counts and prints all simple paths from a given ‘s’ to a given ‘d’. Assume the graph G is represented using adjacent matrix. Using Java Language

  • Battle Ship Game Write a Java program from scratch. Simple geometrical reasoning is at the core...

    Battle Ship Game Write a Java program from scratch. Simple geometrical reasoning is at the core of Battleships. One important notion is adjacency, with two sub-types: edge adjacency and corner adjacency. For example, the square (4,3) is edge-adjacent to the square (3,3), it is corner-adjacent to the square (3,2), and it is not adjacent to the square (6,7). Write a program that reads a square from the user, and prints out three lists(each with a seperate method): 1) a list...

  • Given a graph and a starting vertex, count the number of nodes a specified distance away....

    Given a graph and a starting vertex, count the number of nodes a specified distance away. Requirements: Create a graph of int's. Given a starting node with value key and a distance dist , return the number of nodes that have a path from id with exactly dist edges. If there are multiple paths, only consider the shortest one. int countNodesWithDist(int id, int dist); // example declaration Examples: For the graph below, countNodesWithDist(2, 1) should return 2 since there are...

  • Write a program in Java that stores an array nxm  in less space than nxm  given...

    Write a program in Java that stores an array nxm  in less space than nxm  given that the numbers in the area are repeated a lot     - Input:      Array n x m    - Output:         - Area is stored in less than n x m space         - When called the program is return the original array          Example:   Given 3x6 array A           A: [2,3,5,5,5,5                   1,3,5,6,5,6                   0,5,5,5,6,3] Program stored it  in less that   When...

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