Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

In java generate a random graph using the following process: Given a graph with n nodes, and a number p chosen between 0 and 1,

In java generate a random graph using the following process: Given a graph with n nodes, and a number p chosen between 0 and 1, a link between two nodes is going to be generated if a uniform random draw between 0 and 1 is less than the number p.

choose you graph implementation allowing to build such graphs(you should be able to generate graphs up to 1000 nodes).

Implement a method on your graph using breadth first search to check if all nodes are reachable from each other. Then implement the following experiment for n being 100, 200, 300, 400, 500, and for each value of n, for p equal to .25, .5, .75:

Generate 1000 random graphs for n and p, and countre connected using the above.

This is what I have so far.

import java.util.ArrayList; import java.util.LinkedList; import java.util.List; import java.util.Queue; import java.util.Random; public class Graph2 { private static int numberOfNodes; private boolean[][] adjacencyMatrix; private static int connectedCount = 0; private static int connected; //constructor Graph2(int n0) { Graph2.numberOfNodes = n0; this.adjacencyMatrix = new boolean[numberOfNodes][numberOfNodes]; } List outEdges(int node1) { List edges = new ArrayList(); for (int node2 = 0; node2 < numberOfNodes; node2++) if (adjacencyMatrix[node1][node2]){ edges.add(node2); } return edges; } //method to generate random graphs public static Graph2 createRandomGraph(int nodeCount, float p, int graphIndex) { Graph2 graph = new Graph2(graphIndex+1); Random randomInt = new Random(); int randomNode = randomInt.nextInt(graphIndex+1); int randomNode1 = randomInt.nextInt(graphIndex+1); Random random = new Random(); float randomDraw = random.nextFloat();//uniform random draw between 0 and 1 //System.out.println("Random Draw " + randomDraw + "\tIndex " + graphIndex); if(randomDraw < p){//if random draw is less than p, a link between two nodes is generated graph.adjacencyMatrix[randomNode1][randomNode] = true; graph.adjacencyMatrix[randomNode][randomNode1] = true; connected++; connectedCount++; } graph.adjacencyMatrix[graphIndex][graphIndex]=false; return graph ; }//end method generate graph public void breadthFirst(){ } boolean breadthFirstSearch(Graph2 graph, int graphIndex) {//checks connectivity boolean[] visited = new boolean[graph.adjacencyMatrix.length]; Queue queue = new LinkedList(); queue.add(graphIndex);//adds the current index to the queue visited[graphIndex] = true;//marks the current index as visited while (!queue.isEmpty()) { int removedFromQueue = queue.remove();//removes the current index from queue after visiting it for (Object j : graph.outEdges(removedFromQueue)) {//sends current index to outEdges method if (!visited[(int) j]) {//if j is not visited add it to queue //System.out.println(j); queue.add((Integer) j); visited[(int) j] = true;//mark j as visited add to visited array return true; // System.out.println("visited" + visited); } } } return false; } public static void main(String[] args){ //generate 1000 random graphs for n and p, and count how many are connected // using above method. //Experiment for n = 100, 200, 300, 400, 500, and for each //value of n, for p equal to .25, .5 and .75: Graph2 graph = new Graph2(numberOfNodes); for (int nodeCount = 100; nodeCount <= 500; nodeCount += 100) { for (float p = (float) .25; p <=.75; p += .25) { System.out.println(" \t FOR n = " + nodeCount + " AND p = "+ p); connected = 0; for (int graphIndex = 0; graphIndex < 1000; ++graphIndex) { graph = createRandomGraph(nodeCount, p, graphIndex); if (graph.breadthFirstSearch(graph, graphIndex)) { //..... } System.out.println(connectedCount + " graphs are connected."); } } System.out.println("\tNUMBER OF TOTAL CONNECTED THIS GRAPH IS: " + connected); } } System.out.println("\tNUMBER OF TOTAL CONNECTED IS: " + connectedCount); }//end main }//end class Graph

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Data Access Patterns Database Interactions In Object Oriented Applications

Authors: Clifton Nock

1st Edition

0321555627, 978-0321555625

More Books

Students also viewed these Databases questions

Question

How do Excel Pivot Tables handle data from non OLAP databases?

Answered: 1 week ago