Question

In java ask the user to enter the order (|V|) and size (|E|) of the graph...

In java ask the user to enter the order (|V|) and size (|E|) of the graph and generate |E| random edges into the adjacency matrix (Adj) to make a random directed graph then print the resulting adjacency matrix.

ex:

enter the order |V|:

6

enter the size |E| :

4

Creating a random adjacency matrix for graph...

1 0 0 0 0 0

0 0 0 1 0 1

0 0 0 0 1 0

0 0 0 0 0 0

0 0 0 0 0 0

0 0 0 0 0 0

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

import java.util.*;
import java.io.*;

public class Graph {
  
   public static void main(String args[]) {
       Scanner scan = new Scanner(System.in);
      
       System.out.print("Enter the order |V|: ");
       int v = scan.nextInt();
      
       System.out.print("Enter the order |E|: ");
       int e = scan.nextInt();
      
       int adj_mat [][] = new int[v][v];
      
       Random random = new Random() ;
       int max = v*v, min = 0;
      
       for(int x=0;x<e;x++)
       {
           int count = 0;
           int randomNumber = random.nextInt(max);
           for(int i=0;i<v;i++)
           {
               for(int j=0;j<v;j++)
               {
                   if(count == randomNumber)
                   {
                       adj_mat[i][j] = 1;
                   }
                   count++;
               }
           }          
       }
       System.out.println("Creating random Adjacenecy Matrix: ");
       for(int i=0;i<v;i++)
       {
           for(int j=0;j<v;j++)
           {
               System.out.print(adj_mat[i][j]+" ");
           }
           System.out.println();
       }      
   }
}

OUTPUT:

Add a comment
Know the answer?
Add Answer to:
In java ask the user to enter the order (|V|) and size (|E|) of the graph...
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
  • I'm having trouble using the syntax using MATLAB and I need help for parts 1-3 in...

    I'm having trouble using the syntax using MATLAB and I need help for parts 1-3 in MATLAB code if anyone can help me thank you very much. Request the user to determine the order (IV]) and size (El) of the graph Generate |El random ones into the adjacency matrix/list (Adj) to make a random undirected graph. (Make sure to have a symmetric matrix) Print the resulting adjacency matrix/list. 1. 2. 3.

  • JAVA Ask the user for integers. If the user did not enter an integer, ask again....

    JAVA Ask the user for integers. If the user did not enter an integer, ask again. (Type Safe Input) Keep a running total of the intergers entered (sum). Once the user enters 0, stop looping. Print the sum of all the numbers. Do not use try-catch. EXAMPLE OUTPUT: Enter an integer, 0 to stop> [fasdfsa] Invalid input. Enter an integer, 0 to stop> [231.342] Invalid input. Enter an integer, 0 to stop> [1] Enter an integer, 0 to stop> [2]...

  • 114points Let G- (V,E) be a directed graph. The in-degree of a vertex v is the...

    114points Let G- (V,E) be a directed graph. The in-degree of a vertex v is the number of edges (a) Design an algorithm (give pseudocode) that, given a vertex v EV, computes the in-degree of v under (b) Design an algorithm (give pseudocode) that, given a vertex v E V, computes the in-degree of v incident into v. the assumption that G is represented by an adjacency list. Give an analysis of your algorithm. under the assumption that G is...

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

  • 1. Given a graph G = (V, E). a) Design an algorithm to print all the...

    1. Given a graph G = (V, E). a) Design an algorithm to print all the shortest path from a starting node s to all other nodes b) Implement your proposed algorithm above in your preferred programming languages. You can choose to represent the edges in such graph by either adjacency matrix or adjacency list.

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

  • 4&5 0 1 2 3 1. Draw the undirected graph that corresponds to this adjacency matrix...

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

  • IN JAVA Given is a weighted undirected graph G = (V, E) with positive weights and...

    IN JAVA Given is a weighted undirected graph G = (V, E) with positive weights and a subset of its edges F E. ⊆ E. An F-containing spanning tree of G is a spanning tree that contains all edges from F (there might be other edges as well). Give an algorithm that finds the cost of the minimum-cost F-containing spanning tree of G and runs in time O(m log n) or O(n2). Input: The first line of the text file...

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