invalid6363
Programmer
I'm trying to create a graph with 'n' vertices and create edges between all the vertices.
Here is the code for adding vertices and edges:
I get the following results:
I'm able to create edges but i am not sure how to create edges between all of the vertices.
Can someone help me this problem?
Thank you.
Here is the code for adding vertices and edges:
Java:
private static void setVertices(int n)
{
int vertexLength = n;
/* Add vertices */
for(int i = 0; i < vertexLength; i++)
{
undirGraph.addVertex("City" + String.valueOf(i+1) + " - ");
}
}
private static void setEdges() {
ListIterator<Vertex> i = vertices.listIterator();
Object firstCity = i.next();
Object nextCity = i.next();
Object nextCity2 = i.next();
/* Add edges between cities */
while(i.hasNext())
{
nextCity2=i.next();
nextCity = i.next();
undirGraph.addEdge(firstCity, nextCity, (double)(Math.floor(Math.random()*10)+1));
undirGraph.addEdge(nextCity, nextCity2, (double)(Math.floor(Math.random()*10)+2));
}
}
I get the following results:
Code:
Enter number of vertices to be created (>=2):
5
First city to last city: City1 - City5 -
City5 - City1 - Total distance is 0.7778788292870616
Graph with 5 vertices and 2 edges.
Edges exist from the first vertex in each line to the other vertices in the line.
(Edge weights, if any, are given):
City1 - City5 - 2.0
City2 -
City3 -
City4 -
City5 - City4 - 11.0
I'm able to create edges but i am not sure how to create edges between all of the vertices.
Can someone help me this problem?
Thank you.