2 Replies - 48135 Views - Last Post: 20 February 2008 - 08:10 AM Rate Topic: -----

#1 EquinoX  Icon User is offline

  • D.I.C Head

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

how to create a graph with adjacency list

Post icon  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  Icon User is offline

  • Student of Life
  • member icon

Reputation: 418
  • View blog
  • 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.

Post your code like this: :code:

Thanks.
Was This Post Helpful? 0
  • +
  • -

#3 EquinoX  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • 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 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??
Was This Post Helpful? 1

Page 1 of 1