Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Need help creating edges between all vertices

Status
Not open for further replies.

invalid6363

Programmer
Mar 21, 2008
24
US
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:
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.
 
The pseeucode would be something like

Code:
for i=1 to n
 for j=1 to i
  addEdge(element-j, element-i)

Cheers,
Dian
 
Ok. Thanks.

new code to add edges between vertices:
Java:
private static void setVertices(int n) 
	{
		int vertexLength = n;
		
		/* Create vertices */
		for(int i = 0; i < vertexLength; i++)
		{
			undirGraph.addVertex("City" + String.valueOf(i+1) + " - ");
		}
	}

	private static void setEdges() 
	{
		Object firstCity;
		Object nextCity;

		for (int g = 0; g < num;g++)
		{			
			ListIterator<Vertex> i = vertices.listIterator(g);
			firstCity = i.next();
			
			for(int h = 0;h < g;h++)
			{
				ListIterator<Vertex> j = vertices.listIterator(h);				
				nextCity = j.next();
				
				/* Create an edge between firstCity and nextCity + create a random weight */
				undirGraph.addEdge(firstCity, nextCity, (double)(Math.floor(Math.random()*10)+1));
				undirGraph.addEdge(nextCity, firstCity, (double)(Math.floor(Math.random()*10)+1));
			}
		}
		
	}

Code:
Enter number of vertices to be created (>=2): 
5

First city to last city: City1 - City5 - 
City1 -  City2 -  City3 -  City4 -  City5 -  Total distance is 2.432884011676882


Graph with 5 vertices and 20 edges.


Edges exist from the first vertex in each line to the other vertices in the line.
(Edge weights, if any, are given):

City1 -  City2 - 10.0 City3 - 6.0 City4 - 1.0 City5 - 5.0 
City2 -  City1 - 4.0 City3 - 10.0 City4 - 3.0 City5 - 10.0 
City3 -  City1 - 8.0 City2 - 7.0 City4 - 8.0 City5 - 7.0 
City4 -  City1 - 6.0 City2 - 5.0 City3 - 3.0 City5 - 7.0 
City5 -  City1 - 2.0 City2 - 6.0 City3 - 9.0 City4 - 4.0

There is still one pair of vertices that do not have an edge between them. In a graph with 5 vertices, that would be between city2 and city5, but other than that it works great.

 
I'd not mix iterators and indexes.

[/code]
for (int i=0;i<vertices.length;i++)
for (int j=0;j<i;j++)
undirGraph.addEdge(vertices.get(i),vertices.get(j), (double)(Math.floor(Math.random()*10)+1));


Code:
Cheers,
[url=http://ferdiad.blogspot.com]Dian[/url]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top