how to create a graph with adjacency list

Page 1 of 1

2 Replies - 51263 Views - Last Post: 20 February 2008 - 08:10 AMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=43783&amp;s=1c41d1ebdfc278a1e648e15e2007fe67&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

#1 EquinoX

Reputation: 1
• Posts: 63
• Joined: 19-January 07

how to create a graph with adjacency list

Posted 19 February 2008 - 09:25 PM

I am going to create a graph representation using a adjacency list. How do I represent the vertices and the connected vertices??? I am thinking of using a set of hash map to represent all the vertices and linked list to represent the adjacency vertices. But my problem comes to how do I connect from the vertices in the hash map into the linked list?? Say that in the hash map I have the vertices number from 0 up to n as the key, and what is the value here??

Is This A Good Question/Topic? 0

Replies To: how to create a graph with adjacency list

#2 Jayman

• Student of Life

Reputation: 421
• Posts: 9,532
• Joined: 26-December 05

Re: how to create a graph with adjacency list

Posted 19 February 2008 - 10:31 PM

Dream.In.Code has a policy by which we prefer to see a good faith effort on your part before providing source code for homework assignments. Please post the code you have written in an effort to resolve the problem, and our members would be happy to provide some guidance. Be sure to include a description of any errors you are encountering as well.

Thanks.

#3 EquinoX

Reputation: 1
• Posts: 63
• Joined: 19-January 07

Re: how to create a graph with adjacency list

Posted 20 February 2008 - 08:10 AM

here's the code that I have so far:

```public class Graph {

private ArrayList<Integer> vertices;
private int numVertices = 0;

public Graph(int numVertices) {
this.numVertices = numVertices;
vertices = new ArrayList<Integer>();
for (int i = 0; i < numVertices; i++) {
}
}
public void addEdge(int source, int destination) {
int i = vertices.indexOf(source);
int j = vertices.indexOf(destination);
if (i != -1 || j != -1) {