i want to create a graph using random number. Edges will be taken from random number. But when i created this, duplicate edges like 4-1 and 1-4 comes. As it's undirected graph it'll create problem for giving weights.
my code:
public class Edge
{
public static void main(String[] args)
{
Random random = new Random();
int v,e;
Scanner sc=new Scanner(System.in);
System.out.println("input vertex");
v=sc.nextInt();
System.out.println("input edge");
e=sc.nextInt();
for(int i = 1; i <= e; i++) {
String y= Integer.toString(random.nextInt(v));
int y1 = Integer.parseInt(y)+1;
String x= Integer.toString(random.nextInt(v));
int x1 = Integer.parseInt(x)+1;
if(x1==y1){
i--;
}
else
System.out.println(" "+y1+" "+x1); } }}
output:
node 4 edge 6
2 4
2 3
4 1
4 3
1 2
3 2
as 2-3 and 3-2 came twice how to give condition here
Aucun commentaire:
Enregistrer un commentaire