Smart questions
Smart answers
Smart people
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Member Login

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips now!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

Join Tek-Tips
*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

LINK TO THIS FORUM!

Add Stickiness To Your Site By Linking To This Professionally Managed Technical Forum.
Just copy and paste the
code below into your site.

Partner With Us!

"Best Of Breed" Forums Add Stickiness To Your Site
Partner Button
(Download This Button Today!)

Feedback

"...love the site and am constantly recommending it to (selected !) clients here in ireland..."

Geography

Where in the world do Tek-Tips members come from?

Need help creating edges between all vertices

invalid6363 (Programmer)
26 Mar 09 22:43
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:

CODE --> 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 --> results

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.
 
Diancecht (Programmer)
27 Mar 09 3:18
The pseeucode would be something like

CODE

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

Cheers,
Dian

invalid6363 (Programmer)
28 Mar 09 1:37
Ok. Thanks.

new code to add edges between vertices:

CODE --> 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 --> results

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.
 
 
Diancecht (Programmer)
30 Mar 09 6:25
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,
Dian

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Back To Forum

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close