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??
2 Replies  47166 Views  Last Post: 20 February 2008  08:10 AM
Replies To: how to create a graph with adjacency list
#2
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.
Post your code like this:
Thanks.
Post your code like this:
Thanks.
#3
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:
I am just worried that this won't work, any suggestions??
public class Graph { private ArrayList<Integer> vertices; private LinkedList<Integer>[] edges; private int numVertices = 0; public Graph(int numVertices) { this.numVertices = numVertices; vertices = new ArrayList<Integer>(); edges = (LinkedList<Integer>[]) new LinkedList[numVertices]; for (int i = 0; i < numVertices; i++) { vertices.add(i); edges[i] = new LinkedList<Integer>(); } } public void addEdge(int source, int destination) { int i = vertices.indexOf(source); int j = vertices.indexOf(destination); if (i != 1  j != 1) { edges[i].addFirst(destination); edges[j].addFirst(source); } }
I am just worried that this won't work, any suggestions??
Page 1 of 1
