have to construct a connected graph ( some of the vertices have to connect two or more edges) from a list of pseudorandom numbers. I have tried this but got errors. Any help would be highly appreciated. import java.util.ArrayList; import java.util.List; import java.util.Random; public class MuddyTown { int weight; int m = 5; int d = 1; int k = 32; int s = 1; ArrayList randomList = new ArrayList<>(); //ArrayList randomList = new ArrayList<>(); //static ArrayList randomList; ArrayList connectedGraph = new ArrayList<>(); // Maximum number of vertices int V; //ArrayList[] adj; List> adjList; public MuddyTown() { V = randomList.size(); adjList = new ArrayList<>(V); for (int i = 0; i< V; i++) { adjList.add(new ArrayList<>()); } // for (int i = 0; i(V)); // } } ArrayList pseudoRandom() { for (int i = 0; i< 30; i++) { s = ( m * s + d) % k; randomList.add(s); } return randomList; } void addEdge(int x, int y) { adjList.get(x).add(y); } void createRandomGraph() { Random random = new Random(); // Add each vertex with its previous vertex for (int i = 0; i < randomList.size() -1; i++) { int currentVertex = randomList.get(i); addEdge(i, randomList.get(i + 1)); int index = random.nextInt(randomList.size()); weight = randomList.get(index); String line = weight + " " + currentVertex + " " + randomList.get(i + 1); connectedGraph.add(line); } // join 20 edges randomly for (int a = 0; a<20; a++) { int j = random.nextInt(randomList.size()); int k = random.nextInt(randomList.size()); addEdge(j, randomList.get(k)); int b = random.nextInt(randomList.size()); weight = randomList.get(b); String value = weight + "," + randomList.get(j) + "," + randomList.get(k); connectedGraph.add(value); } } public static void main(String[] args) { MuddyTown g = new MuddyTown(); g.pseudoRandom(); System.out.println("Size of random list: " + g.randomList.size()); for (int a: g.randomList ) { System.out.println("Random list: " + a); } g.createRandomGraph(); for (int i = 0; i< g.connectedGraph.size(); i++) { System.out.println(g.connectedGraph.get(i)); } } }
I have to construct a connected graph ( some of the vertices have to connect two or more edges) from a list of pseudorandom numbers. I have tried this but got errors.
Any help would be highly appreciated.
import java.util.ArrayList; import java.util.List; import java.util.Random; public class MuddyTown { int weight; int m = 5; int d = 1; int k = 32; int s = 1; ArrayList<Integer> randomList = new ArrayList<>(); //ArrayList<Integer> randomList = new ArrayList<>(); //static ArrayList<Integer> randomList; ArrayList<String> connectedGraph = new ArrayList<>(); // Maximum number of vertices int V; //ArrayList<Integer>[] adj; List<List<Integer>> adjList; public MuddyTown() { V = randomList.size(); adjList = new ArrayList<>(V); for (int i = 0; i< V; i++) { adjList.add(new ArrayList<>()); } // for (int i = 0; i<V; i++) { // adjList.add(new ArrayList<>(V)); // } } ArrayList<Integer> pseudoRandom() { for (int i = 0; i< 30; i++) { s = ( m * s + d) % k; randomList.add(s); } return randomList; } void addEdge(int x, int y) { adjList.get(x).add(y); } void createRandomGraph() { Random random = new Random(); // Add each vertex with its previous vertex for (int i = 0; i < randomList.size() -1; i++) { int currentVertex = randomList.get(i); addEdge(i, randomList.get(i + 1)); int index = random.nextInt(randomList.size()); weight = randomList.get(index); String line = weight + " " + currentVertex + " " + randomList.get(i + 1); connectedGraph.add(line); } // join 20 edges randomly for (int a = 0; a<20; a++) { int j = random.nextInt(randomList.size()); int k = random.nextInt(randomList.size()); addEdge(j, randomList.get(k)); int b = random.nextInt(randomList.size()); weight = randomList.get(b); String value = weight + "," + randomList.get(j) + "," + randomList.get(k); connectedGraph.add(value); } } public static void main(String[] args) { MuddyTown g = new MuddyTown(); g.pseudoRandom(); System.out.println("Size of random list: " + g.randomList.size()); for (int a: g.randomList ) { System.out.println("Random list: " + a); } g.createRandomGraph(); for (int i = 0; i< g.connectedGraph.size(); i++) { System.out.println(g.connectedGraph.get(i)); } } }
Step by step
Solved in 2 steps